- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this post, we will understand the differences between Prim's and Kruskal's algorithms.

- When a connected and undirected graph is given, a spanning tree of such a graph is the subgraph which is a tree that connects all of the vertices.
- A single graph can have multiple spanning trees.
- A minimum spanning tree (MST) (also known as minimum weight spanning tree) for a weighted, connected and undirected graph is a spanning tree that weighs less than or equal to the weight of every other spanning tree.
- The weight of a spanning tree is determined by adding the weights associated with every edge of the spanning tree.
- The Minimum Spanning Tree can be built from the vertex that has the minimum weight in the graph.
- One node is traversed only once.
- It runs quickly in sparse graphs.
- The time complexity is O(E log V), where V is the number of vertices.
- It can work with disconnected components too.

- Sort the edges in ascending order of their associated weight.
- Select the smallest edge.
- Check to see if it forms a cycle with the spanning-tree that has been formed until that point in time.
- If the cycle hasn't been formed, this edge has to be included.
- Otherwise, it can be discarded.
- Steps 2,3,4 are repeated until the spanning tree contains V-1 edges.

- This is similar to Kruskal's algorithm, i.e it is a greedy algorithm.
- It begins with an empty spanning tree. Two sets of vertices are maintained.
- The first set would contain the vertices which are already included in the MST, whereas the other set contains the vertices that haven't been included yet.
- At every step, the algorithm considers all the edges that would connect the two sets. It then picks the minimum weight edge among these edges.
- After this step, the algorithm moves to the other endpoint of the edge of the set that contains MST.
- The Minimum Spanning Tree can be built from any vertex in the graph.
- One node is travelled multiple times to get the minimum distance value.
- It has a time complexity of O(V2), where V is the number of vertices. This time complexity can be enhanced to O(E + log V) using Fibonacci heaps.
- It runs quickly in dense graphs.
- It gives the connected components, and works with connected graph only.

- An mstSet is created, that keeps track of vertices that are already included in MST.
- A key value is assigned to all vertices of the input graph.
- The key values are initially assigned as 'INFINITE'.
- The key value 0 is assigned to the first vertex so that it can be picked first.
- When mstSet doesn't include all of the vertices, the below steps are followed.
- A vertex 'u' is chosen that is not present in the mstSet and also has minimum key value.
- This 'u' is now included in mstSet.
- The key value of all the adjacent vertices of 'u' are updated.
- This can be done by iterating through all the adjacent vertices.
- For every adjacent vertex 'v', if the weight of edge 'u'-'v' is less than that of the previous key value of 'v', the key value is updated as the weight of 'u-v'.

- Related Questions & Answers
- Kruskal’s Minimum Spanning Tree Algorithm
- Prim’s Minimum Spanning Tree Algorithm
- Kruskal’s (Minimum Spanning Tree) MST Algorithm
- Prim’s (Minimum Spanning Tree) MST Algorithm
- Fleury’s Algorithm
- kasai’s Algorithm
- Manacher’s Algorithm
- Prim’s Algorithm (Simple Implementation for Adjacency Matrix Representation) in C++
- Prim’s MST for Adjacency List Representation
- Dijkstra’s Shortest Path Algorithm
- Find yesterday’s, today’s and tomorrow’s date in Python
- Prim's algorithm in Javascript
- Kruskal's algorithm in Javascript
- Berkeley’s Algorithm in C/C++
- Sort an arrays of 0’s, 1’s and 2’s using C++

Advertisements