Javatpoint Logo
Javatpoint Logo

Kruskal algorithm in C

The greedy Kruskal's method searches for the quickest route through a connected weighted network. In this algorithm, we start with an empty set of edges and add edges to the set one at a time until we obtain a spanning tree.

Kruskal's approach is a well-known algorithm for determining the shortest spanning tree in a linked, weighted network. A minimum spanning tree is a tree that joins every node in a graph using the fewest possible total edge weights.

The algorithm considers all edges of the graph in ascending weight order and adds them to the minimum spanning tree if they do not form a cycle with the edges already in the tree. Kruskal's approach employs the disjoint-set data structure to prevent the formation of cycles.


  • If an edge does not form a cycle with edges already in the tree, the method evaluates all edges of the graph in ascending weight order and adds them to the minimal spanning tree.
  • The algorithm maintains a set of disjoint subsets of vertices, with each subset representing a connected component of the graph. As edges are added to the selected set, the subsets are merged together, until all vertices belong to the same subset.


Kruskal's algorithm can be used in a variety of applications, such as network design, clustering, and image segmentation. It is frequently used in engineering, computer science, and other disciplines that call for graph analysis.

The Kruskal algorithm's steps are listed below:

  • Sort all the edges according to weight in a non-decreasing order.
  • Initialize an empty set for the minimum spanning tree and an empty disjoint-set data structure.
  • Add the edge to the tree and combine the two sets that the edge's endpoints belong to in the disjoint-set data structure for each edge in the sorted order if doing so does not result in a cycle.
  • Until all edges have been taken into account or the smallest spanning tree has n-1 edges-where n is the number of nodes in the graph-repeat step 3 as necessary.
  • At the end of the algorithm, the minimum spanning tree will be the set of edges added to the tree in step 3. The temporal complexity of Kruskal's algorithm, where E is the number of edges and V is the number of vertices in the graph, is either O(ElogE) or O(ElogV).

Here is the implementation of Kruskal's algorithm in C:

C Program:

This code reads in the number of vertices and edges of a graph, as well as the edges and their weights, from the user. It then runs Kruskal's algorithm on the graph and prints out the minimum spanning tree.

Input and output for the Kruskal's algorithm implementation in C:



Minimum Spanning Tree:
(0, 1) -> 2
(1, 2) -> 3
(1, 4) -> 5
(0, 3) -> 6

In this example, the input specifies a graph with 5 vertices and 7 edges. The edge weights are then read in from the user. Kruskal's algorithm's output displays the edges that make up the minimal spanning tree.


  • Kruskal's algorithm is simple to implement and understand.
  • It finds the minimum spanning tree of a graph in a reasonable amount of time, making it suitable for large graphs.
  • The algorithm is suitable for a wide range of applications.


  • The algorithm may not find the unique minimum spanning tree of a graph, since there may be multiple minimum spanning trees with the same total weight.
  • The algorithm requires sorting the edges of the graph by weight, which can be time-consuming for large graphs.
  • The algorithm requires extra space to store the disjoint subsets of vertices.


The Kruskal algorithm is a straightforward and effective method for determining a graph's least spanning tree. E is the number of edges in the graph, and its temporal complexity is O(E log E). It is hence appropriate for huge graphs.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA