Prims and Kruskal
Prims and Kruskal
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning
tree from a graph. Prim's algorithm finds the subset of edges that includes every
vertex of the graph such that the sum of the weights of the edges can be
minimized.
Prim's algorithm starts with the single node and explores all the adjacent nodes with
all the connecting edges at every step. The edges with the minimal weights causing
no cycles in the graph got selected.
Step 2 - Now, we have to choose and add the shortest edge from vertex B. There
are two edges from vertex B that are B to C with weight 10 and edge B to D with
weight 4. Among the edges, the edge BD has the minimum weight. So, add it to the
MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the
other edges. In this case, the edges DE and CD are such edges. Add them to MST
and explore the adjacent of C, i.e., E and A. So, select the edge DE and add it to the
MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would
create a cycle to the graph. So, choose the edge CA and add it to the MST.
So, the graph produced in step 5 is the minimum spanning tree of the given graph.
The cost of the MST is given below -
Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has mini
mum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT
o Time Complexity
Data structure used for the minimum edge Time Complexity
weight
The time complexity of the prim's algorithm is O(E logV) or O(V logV), where E is the
no. of edges, and V is the no. of vertices.
Kruskal's Algorithm
Spanning tree - A spanning tree is the subgraph of an undirected
connected graph.
Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted
graph. The main target of the algorithm is to find the subset of edges by using which we can
traverse every vertex of the graph. It follows the greedy approach that finds an optimum solution
at every stage instead of focusing on a global optimum.
How does Kruskal's algorithm work?
In Kruskal's algorithm, we start from edges with the lowest weight and keep adding the edges
until the goal is reached. The steps to implement Kruskal's algorithm are listed as follows -
The weight of the edges of the above graph is given in the below table -
Edge A A A A B C D
B C D E C D E
Weig 1 7 10 5 3 4 2
ht
Now, sort the edges given above in the ascending order of their weights.
Edge A D B C A A A
B E C D E C D
Weig 1 2 3 4 5 7 10
ht
Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the cycle.
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is
not forming the cycle.
Step 5 - After that, pick the edge AE with weight 5. Including this
edge will create the cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will
create the cycle, so discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will
also create the cycle, so discard it.
So, the final minimum spanning tree obtained from the given
weighted graph by using Kruskal's algorithm is -
Algorithm
1. Step 1: Create a forest F in such a way that every vertex of the grap
h is a separate tree.
2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spa
nning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, t
hen add it to the forest F
6. (for combining two trees into one tree).
7. ELSE
8. Discard the edge
9. Step 6: END
o Time Complexity
The time complexity of Kruskal's algorithm is O(E logE) or O(V
logV), where E is the no. of edges, and V is the no. of vertices.