- 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

# Difference Between Prim’s and Kruskal’s Algorithm

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

## Kruskal's algorithm for Mininum Spanning Tree (MST)

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

## Steps to find MST using the Kruskal's algorithm:

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

## Prim's algorithm Mininum Spanning Tree (MST)

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

## Steps to find MST using the Prim's algorithm:

- 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
- Prim's algorithm in Javascript
- Kruskal's algorithm in Javascript
- 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
- Kruskal's Minimum Spanning Tree Algorithm-Greedy algorithm in C++
- Prim’s Algorithm (Simple Implementation for Adjacency Matrix Representation) in C++
- Difference between char s[] and char *s in C
- Difference Between PGP and S/MIME
- Program to find out an MST using Prim's algorithm in Python
- Prim’s MST for Adjacency List Representation
- kasai’s Algorithm
- Manacher’s Algorithm
- Fleury’s Algorithm

Advertisements