Javatpoint Logo
Javatpoint Logo

Kruskal's Algorithm in C

You will discover the workings of Kruskal's algorithm in this lesson. Additionally, you may discover functioning Kruskal's Algorithm examples in Python, Java, C, and C++.

When given a graph as input, Kruskal's algorithm, a minimum spanning tree algorithm, determines the subset of its edges that are

  • Create a tree from the graph that has the least amount of weights
  • Added to each vertex among all possible trees.

How the Kruskal's algorithm functions?

It belongs to a category of algorithms known as greedy algorithms, which locate the local optimum with the intention of locating a global optimum.

The edges with the lowest weight are where we start, and we keep adding edges until we reach our destination.

The following are the stages for applying Kruskal's algorithm:

  • Order every edge from light to heavy.
  • Add the edge to the spanning tree that has the lowest weight. Reject this edge if adding it resulted in a loop.
  • Up until we reach all vertices, keep adding edges

Pseudo-Code Kruskal Algorithm

Any minimal spanning tree algorithm centers on determining whether or not adding an edge results in the creation of a loop. The Union Find algorithm is the most used method for discovering this information. With the help of the Union-Find method, we can group the vertices into clusters, determine if two vertices are members of the same cluster, and determine whether or not adding an edge results in the creation of a cycle.

Let's look at the example of kruskal's algorithm in C:


2 - 1 : 2
5 - 2 : 2
3 - 2 : 3
4 - 3 : 3
1 - 0 : 4
Spanning tree cost: 14
Process exited after 0.02407 seconds with return value 0
Press any key to continue . . .

Prime's Algorithm Vs Kruskal's Algorithm

Another well-liked minimal spanning tree approach that determines a graph's MST employs a different logic: Prim's algorithm. Prim's approach begins at a vertex rather than an edge and continues to add lowest-weight edges that aren't part of the tree until all vertices are covered.

Next TopicFCFS Program in C

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA