We have discussed Kruskal’s algorithm for Minimum 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.
A group of edges that connects two set of vertices in a graph is called cut in graph theory. So, at every step of Prim’s algorithm, we find a cut (of two sets, one contains the vertices already included in MST and other contains rest of the verices), pick the minimum weight edge from the cut and include this vertex to MST Set (the set that contains already included vertices).

How does Prim’s Algorithm Work?

The idea behind Prim’s algorithm is simple, a spanning tree means all vertices must be connected. So the two disjoint subsets (discussed above) of vertices must be connected to make a Spanning Tree. And they must be connected with the minimum weight edge to make it a Minimum Spanning Tree.

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

a) Pick a vertex u which is not there in mstSet and has minimum key value.

b) Include u to mstSet.

c) Update key value of all adjacent vertices of u. To update the key values, iterate through all adjacent vertices. For every adjacent vertex v, if weight of edge u-v is less than the previous key value of v, update the key value as weight of u-v

The idea of using key values is to pick the minimum weight edge from cut. The key values are used only for vertices which are not yet included in MST, the key value for these vertices indicate the minimum weight edges connecting them to the set of vertices included in MST.

Let us understand with the following  example

Prim’s Minimum Spanning Tree (MST)

The set mstSet is initially empty and keys assigned to vertices are {0, INF, INF, INF, INF, INF, INF, INF} where INF indicates infinite. Now pick the vertex with minimum key value. The vertex 0 is picked, include it in mstSet. So mstSet becomes {0}. After including to mstSet, update key values of adjacent vertices. Adjacent vertices of 0 are 1 and 7. The key values of 1 and 7 are updated as 4 and 8. Following subgraph shows vertices and their key values, only the vertices with finite key values are shown. The vertices included in MST are shown in green color.

Prim’s Minimum Spanning Tree (MST)

 

Pick the vertex with minimum key value and not already included in MST (not in mstSET). The vertex 1 is picked and added to mstSet. So mstSet now becomes {0, 1}. Update the key values of adjacent vertices of 1. The key value of vertex 2 becomes 8.

Prim’s Minimum Spanning Tree (MST)

Pick the vertex with minimum key value and not already included in MST (not in mstSET). We can either pick vertex 7 or vertex 2, let vertex 7 is picked. So mstSet now becomes {0, 1, 7}. Update the key values of adjacent vertices of 7. The key value of vertex 6 and 8 becomes finite (7 and 1 respectively).
Prim’s Minimum Spanning Tree (MST)Pick the vertex with minimum key value and not already included in MST (not in mstSET). Vertex 6 is picked. So mstSet now becomes {0, 1, 7, 6}. Update the key values of adjacent vertices of 6. The key value of vertex 5 and 8 are updated.

We repeat the above steps until mstSet includes all vertices of given graph. Finally, we get the following graph.

Prim’s Minimum Spanning Tree (MST)

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

 [ad type=”banner”]

How to implement the above algorithm?
We use a boolean array mstSet[] to represent the set of vertices included in MST. If a value mstSet[v] is true, then vertex v is included in MST, otherwise not. Array key[] is used to store key values of all vertices. Another array parent[] to store indexes of parent nodes in MST. The parent array is the output array which is used to show the constructed MST.

C

[pastacode lang=”c” manual=”%2F%2F%20A%20C%20%2F%20C%2B%2B%20program%20for%20Prim’s%20Minimum%20Spanning%20Tree%20(MST)%20algorithm.%20%0A%2F%2F%20The%20program%20is%20for%20adjacency%20matrix%20representation%20of%20the%20graph%0A%20%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Climits.h%3E%0A%20%0A%2F%2F%20Number%20of%20vertices%20in%20the%20graph%0A%23define%20V%205%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20the%20vertex%20with%20minimum%20key%20value%2C%20from%0A%2F%2F%20the%20set%20of%20vertices%20not%20yet%20included%20in%20MST%0Aint%20minKey(int%20key%5B%5D%2C%20bool%20mstSet%5B%5D)%0A%7B%0A%20%20%20%2F%2F%20Initialize%20min%20value%0A%20%20%20int%20min%20%3D%20INT_MAX%2C%20min_index%3B%0A%20%0A%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20v%2B%2B)%0A%20%20%20%20%20if%20(mstSet%5Bv%5D%20%3D%3D%20false%20%26%26%20key%5Bv%5D%20%3C%20min)%0A%20%20%20%20%20%20%20%20%20min%20%3D%20key%5Bv%5D%2C%20min_index%20%3D%20v%3B%0A%20%0A%20%20%20return%20min_index%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20the%20constructed%20MST%20stored%20in%20parent%5B%5D%0Aint%20printMST(int%20parent%5B%5D%2C%20int%20n%2C%20int%20graph%5BV%5D%5BV%5D)%0A%7B%0A%20%20%20printf(%22Edge%20%20%20Weight%5Cn%22)%3B%0A%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20printf(%22%25d%20-%20%25d%20%20%20%20%25d%20%5Cn%22%2C%20parent%5Bi%5D%2C%20i%2C%20graph%5Bi%5D%5Bparent%5Bi%5D%5D)%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20construct%20and%20print%20MST%20for%20a%20graph%20represented%20using%20adjacency%0A%2F%2F%20matrix%20representation%0Avoid%20primMST(int%20graph%5BV%5D%5BV%5D)%0A%7B%0A%20%20%20%20%20int%20parent%5BV%5D%3B%20%2F%2F%20Array%20to%20store%20constructed%20MST%0A%20%20%20%20%20int%20key%5BV%5D%3B%20%20%20%2F%2F%20Key%20values%20used%20to%20pick%20minimum%20weight%20edge%20in%20cut%0A%20%20%20%20%20bool%20mstSet%5BV%5D%3B%20%20%2F%2F%20To%20represent%20set%20of%20vertices%20not%20yet%20included%20in%20MST%0A%20%0A%20%20%20%20%20%2F%2F%20Initialize%20all%20keys%20as%20INFINITE%0A%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20key%5Bi%5D%20%3D%20INT_MAX%2C%20mstSet%5Bi%5D%20%3D%20false%3B%0A%20%0A%20%20%20%20%20%2F%2F%20Always%20include%20first%201st%20vertex%20in%20MST.%0A%20%20%20%20%20key%5B0%5D%20%3D%200%3B%20%20%20%20%20%2F%2F%20Make%20key%200%20so%20that%20this%20vertex%20is%20picked%20as%20first%20vertex%0A%20%20%20%20%20parent%5B0%5D%20%3D%20-1%3B%20%2F%2F%20First%20node%20is%20always%20root%20of%20MST%20%0A%20%0A%20%20%20%20%20%2F%2F%20The%20MST%20will%20have%20V%20vertices%0A%20%20%20%20%20for%20(int%20count%20%3D%200%3B%20count%20%3C%20V-1%3B%20count%2B%2B)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Pick%20the%20minimum%20key%20vertex%20from%20the%20set%20of%20vertices%0A%20%20%20%20%20%20%20%20%2F%2F%20not%20yet%20included%20in%20MST%0A%20%20%20%20%20%20%20%20int%20u%20%3D%20minKey(key%2C%20mstSet)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Add%20the%20picked%20vertex%20to%20the%20MST%20Set%0A%20%20%20%20%20%20%20%20mstSet%5Bu%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Update%20key%20value%20and%20parent%20index%20of%20the%20adjacent%20vertices%20of%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20picked%20vertex.%20Consider%20only%20those%20vertices%20which%20are%20not%20yet%0A%20%20%20%20%20%20%20%20%2F%2F%20included%20in%20MST%0A%20%20%20%20%20%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20v%2B%2B)%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20graph%5Bu%5D%5Bv%5D%20is%20non%20zero%20only%20for%20adjacent%20vertices%20of%20m%0A%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20mstSet%5Bv%5D%20is%20false%20for%20vertices%20not%20yet%20included%20in%20MST%0A%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Update%20the%20key%20only%20if%20graph%5Bu%5D%5Bv%5D%20is%20smaller%20than%20key%5Bv%5D%0A%20%20%20%20%20%20%20%20%20%20if%20(graph%5Bu%5D%5Bv%5D%20%26%26%20mstSet%5Bv%5D%20%3D%3D%20false%20%26%26%20graph%5Bu%5D%5Bv%5D%20%3C%20%20key%5Bv%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20parent%5Bv%5D%20%20%3D%20u%2C%20key%5Bv%5D%20%3D%20graph%5Bu%5D%5Bv%5D%3B%0A%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%2F%2F%20print%20the%20constructed%20MST%0A%20%20%20%20%20printMST(parent%2C%20V%2C%20graph)%3B%0A%7D%0A%20%0A%20%0A%2F%2F%20driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%2F*%20Let%20us%20create%20the%20following%20graph%0A%20%20%20%20%20%20%20%20%20%202%20%20%20%203%0A%20%20%20%20%20%20(0)–(1)–(2)%0A%20%20%20%20%20%20%20%7C%20%20%20%2F%20%5C%20%20%20%7C%0A%20%20%20%20%20%206%7C%208%2F%20%20%20%5C5%20%7C7%0A%20%20%20%20%20%20%20%7C%20%2F%20%20%20%20%20%5C%20%7C%0A%20%20%20%20%20%20(3)——-(4)%0A%20%20%20%20%20%20%20%20%20%20%20%209%20%20%20%20%20%20%20%20%20%20*%2F%0A%20%20%20int%20graph%5BV%5D%5BV%5D%20%3D%20%7B%7B0%2C%202%2C%200%2C%206%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B2%2C%200%2C%203%2C%208%2C%205%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%203%2C%200%2C%200%2C%207%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B6%2C%208%2C%200%2C%200%2C%209%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%205%2C%207%2C%209%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20%2F%2F%20Print%20the%20solution%0A%20%20%20%20primMST(graph)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A%0A” message=”c” highlight=”” provider=”manual”/]

[ad type=”banner”]

JAVA

[pastacode lang=”java” manual=”%2F%2F%20A%20Java%20program%20for%20Prim’s%20Minimum%20Spanning%20Tree%20(MST)%20algorithm.%0A%2F%2F%20The%20program%20is%20for%20adjacency%20matrix%20representation%20of%20the%20graph%0A%20%0Aimport%20java.util.*%3B%0Aimport%20java.lang.*%3B%0Aimport%20java.io.*%3B%0A%20%0Aclass%20MST%0A%7B%0A%20%20%20%20%2F%2F%20Number%20of%20vertices%20in%20the%20graph%0A%20%20%20%20private%20static%20final%20int%20V%3D5%3B%0A%20%0A%20%20%20%20%2F%2F%20A%20utility%20function%20to%20find%20the%20vertex%20with%20minimum%20key%0A%20%20%20%20%2F%2F%20value%2C%20from%20the%20set%20of%20vertices%20not%20yet%20included%20in%20MST%0A%20%20%20%20int%20minKey(int%20key%5B%5D%2C%20Boolean%20mstSet%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Initialize%20min%20value%0A%20%20%20%20%20%20%20%20int%20min%20%3D%20Integer.MAX_VALUE%2C%20min_index%3D-1%3B%0A%20%0A%20%20%20%20%20%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20v%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(mstSet%5Bv%5D%20%3D%3D%20false%20%26%26%20key%5Bv%5D%20%3C%20min)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min%20%3D%20key%5Bv%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min_index%20%3D%20v%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20return%20min_index%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20A%20utility%20function%20to%20print%20the%20constructed%20MST%20stored%20in%0A%20%20%20%20%2F%2F%20parent%5B%5D%0A%20%20%20%20void%20printMST(int%20parent%5B%5D%2C%20int%20n%2C%20int%20graph%5B%5D%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20System.out.println(%22Edge%20%20%20Weight%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(parent%5Bi%5D%2B%22%20-%20%22%2B%20i%2B%22%20%20%20%20%22%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20graph%5Bi%5D%5Bparent%5Bi%5D%5D)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Function%20to%20construct%20and%20print%20MST%20for%20a%20graph%20represented%0A%20%20%20%20%2F%2F%20%20using%20adjacency%20matrix%20representation%0A%20%20%20%20void%20primMST(int%20graph%5B%5D%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Array%20to%20store%20constructed%20MST%0A%20%20%20%20%20%20%20%20int%20parent%5B%5D%20%3D%20new%20int%5BV%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Key%20values%20used%20to%20pick%20minimum%20weight%20edge%20in%20cut%0A%20%20%20%20%20%20%20%20int%20key%5B%5D%20%3D%20new%20int%20%5BV%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20To%20represent%20set%20of%20vertices%20not%20yet%20included%20in%20MST%0A%20%20%20%20%20%20%20%20Boolean%20mstSet%5B%5D%20%3D%20new%20Boolean%5BV%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Initialize%20all%20keys%20as%20INFINITE%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20key%5Bi%5D%20%3D%20Integer.MAX_VALUE%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20mstSet%5Bi%5D%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Always%20include%20first%201st%20vertex%20in%20MST.%0A%20%20%20%20%20%20%20%20key%5B0%5D%20%3D%200%3B%20%20%20%20%20%2F%2F%20Make%20key%200%20so%20that%20this%20vertex%20is%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20picked%20as%20first%20vertex%0A%20%20%20%20%20%20%20%20parent%5B0%5D%20%3D%20-1%3B%20%2F%2F%20First%20node%20is%20always%20root%20of%20MST%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20The%20MST%20will%20have%20V%20vertices%0A%20%20%20%20%20%20%20%20for%20(int%20count%20%3D%200%3B%20count%20%3C%20V-1%3B%20count%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Pick%20thd%20minimum%20key%20vertex%20from%20the%20set%20of%20vertices%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20not%20yet%20included%20in%20MST%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20u%20%3D%20minKey(key%2C%20mstSet)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Add%20the%20picked%20vertex%20to%20the%20MST%20Set%0A%20%20%20%20%20%20%20%20%20%20%20%20mstSet%5Bu%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Update%20key%20value%20and%20parent%20index%20of%20the%20adjacent%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20vertices%20of%20the%20picked%20vertex.%20Consider%20only%20those%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20vertices%20which%20are%20not%20yet%20included%20in%20MST%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20v%20%3D%200%3B%20v%20%3C%20V%3B%20v%2B%2B)%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20graph%5Bu%5D%5Bv%5D%20is%20non%20zero%20only%20for%20adjacent%20vertices%20of%20m%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20mstSet%5Bv%5D%20is%20false%20for%20vertices%20not%20yet%20included%20in%20MST%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Update%20the%20key%20only%20if%20graph%5Bu%5D%5Bv%5D%20is%20smaller%20than%20key%5Bv%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(graph%5Bu%5D%5Bv%5D!%3D0%20%26%26%20mstSet%5Bv%5D%20%3D%3D%20false%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20graph%5Bu%5D%5Bv%5D%20%3C%20%20key%5Bv%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20parent%5Bv%5D%20%20%3D%20u%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20key%5Bv%5D%20%3D%20graph%5Bu%5D%5Bv%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20print%20the%20constructed%20MST%0A%20%20%20%20%20%20%20%20printMST(parent%2C%20V%2C%20graph)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20static%20void%20main%20(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20Let%20us%20create%20the%20following%20graph%0A%20%20%20%20%20%20%20%20%20%20%202%20%20%20%203%0A%20%20%20%20%20%20%20%20(0)–(1)–(2)%0A%20%20%20%20%20%20%20%20%7C%20%20%20%20%2F%20%5C%20%20%20%7C%0A%20%20%20%20%20%20%20%206%7C%208%2F%20%20%20%5C5%20%7C7%0A%20%20%20%20%20%20%20%20%7C%20%2F%20%20%20%20%20%20%5C%20%7C%0A%20%20%20%20%20%20%20%20(3)——-(4)%0A%20%20%20%20%20%20%20%20%20%20%20%20%209%20%20%20%20%20%20%20%20%20%20*%2F%0A%20%20%20%20%20%20%20%20MST%20t%20%3D%20new%20MST()%3B%0A%20%20%20%20%20%20%20%20int%20graph%5B%5D%5B%5D%20%3D%20new%20int%5B%5D%5B%5D%20%7B%7B0%2C%202%2C%200%2C%206%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B2%2C%200%2C%203%2C%208%2C%205%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%203%2C%200%2C%200%2C%207%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B6%2C%208%2C%200%2C%200%2C%209%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B0%2C%205%2C%207%2C%209%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Print%20the%20solution%0A%20%20%20%20%20%20%20%20t.primMST(graph)%3B%0A%20%20%20%20%7D%0A%7D%0A%2F%2F%20This%20code%20is%20contributed%20by%20Aakash%20Hasija%0A” message=”java” highlight=”” provider=”manual”/]

Output:

Edge   Weight
0 - 1    2
1 - 2    3
0 - 3    6
1 - 4    5

Time Complexity of the above program is O(V^2). If the input graph is represented using adjacency list, then the time complexity of Prim’s algorithm can be reduced to O(E log V) with the help of binary heap. Please see Prim’s MST for Adjacency List Representation for more details.

[ad type=”banner”]

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,