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

Updated on: 02-Mar-2021

394 Views 