Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. A single graph can have many different spanning trees. A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected and undirected graph is a spanning tree with weight less than or equal to the weight of every other spanning tree. The weight of a spanning tree is the sum of weights given to each edge of the spanning tree.
Below are the steps for finding MST using Kruskal’s algorithm
- Sort all the edges in non-decreasing order of their weight.
- Pick the smallest edge. Check if it forms a cycle with the spanning-tree formed so far. If the cycle is not formed, include this edge. Else, discard it.
- Repeat step#2 until there are (V-1) edges in the spanning tree.
Like Kruskal’s algorithm, Prim’s algorithm is also a Greedy algorithm. It starts with an empty spanning tree. The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, the other set contains the vertices not yet included. At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST.
Below are the steps for finding MST using Prim’s algorithm
- Create a set mstSet that keeps track of vertices already included in MST.
- Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE. Assign key value as 0 for the first vertex so that it is picked first.
- While mstSet doesn’t include all vertices
- Pick a vertex u which is not there in mstSet and has minimum key value.
- Include u to mstSet.
- Update the key value of all adjacent vertices of u. To update the key values, iterate through all adjacent vertices. For every adjacent vertex v, if the weight of edge u-v is less than the previous key value of v, update the key value as the weight of u-v
Both Prim’s and Kruskal’s algorithm finds the Minimum Spanning Tree and follow the Greedy approach of problem-solving, but there are few major differences between them.
|Prim’s Algorithm||Kruskal’s Algorithm|
|It starts to build the Minimum Spanning Tree from any vertex in the graph.||It starts to build the Minimum Spanning Tree from the vertex carrying minimum weight in the graph.|
|It traverses one node more than one time to get the minimum distance.||It traverses one node only once.|
|Prim’s algorithm has a time complexity of O(V^2), V being the number of vertices.||Kruskal’s algorithm’s time complexity is O(logV), V being the number of vertices.|
|Prim’s algorithm gives connected component as well as it works only on connected graph.||Kruskal’s algorithm can generate forest(disconnected components) at any instant as well as it can work on disconnected components|
|Prim’s algorithm runs faster in dense graphs.||Kruskal’s algorithm runs faster in sparse graphs.|
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Difference between RSA algorithm and DSA
- Difference Between Algorithm and Flowchart
- Difference between Algorithm, Pseudocode and Program
- Difference between FCFS and C-LOOK disk scheduling algorithm
- Difference between SSTF and LOOK disk scheduling algorithm
- Difference between C-SCAN and SSTF Disk Scheduling Algorithm
- Difference between FCFS and SSTF Disk Scheduling Algorithm
- Difference between First Come First Served (FCFS) and Round Robin (RR) Scheduling Algorithm
- Difference Between Flood-fill and Boundary-fill Algorithm
- Prim's algorithm using priority_queue in STL
- Graph Coloring | Set 2 (Greedy Algorithm)
- Optimal Page Replacement Algorithm
- Boruvka's algorithm | Greedy Algo-9
- Program for Next Fit algorithm in Memory Management
- Program for First Fit algorithm in Memory Management
- Greedy Algorithm for Egyptian Fraction
- Program for Best Fit algorithm in Memory Management
- Welsh Powell Graph colouring Algorithm
- Comparions between DDA and Bresenham Line Drawing algorithm
- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.