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. |

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Why Prim’s and Kruskal's MST algorithm fails for Directed Graph?
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- Travelling Salesman Problem | Set 2 (Approximate using MST)
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Difference between Algorithm, Pseudocode and Program
- Difference Between Algorithm and Flowchart
- Difference Between Flood-fill and Boundary-fill Algorithm
- Difference between First Come First Served (FCFS) and Round Robin (RR) Scheduling Algorithm
- Difference between RSA algorithm and DSA
- Difference between FCFS and SSTF Disk Scheduling Algorithm
- Difference between SSTF and LOOK disk scheduling algorithm
- Difference between FCFS and C-LOOK disk scheduling algorithm
- Difference between C-SCAN and SSTF Disk Scheduling Algorithm
- Difference between C-LOOK and C-SCAN Disk Scheduling Algorithm
- Difference between SSTF and C-LOOK disk scheduling algorithm
- Difference between FCFS and C-SCAN disk scheduling algorithm
- Comparions between DDA and Bresenham Line Drawing algorithm
- Difference and Similarities between PHP and C
- Difference between Stop and Wait protocol and Sliding Window protocol

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.