We have discussed Prim’s algorithm and its implementation for adjacency matrix representation of graphs.

As discussed in the previous post, in Prim’s algorithm, two sets are maintained, one set contains list of vertices already included in MST, other set contains vertices not yet included. In every iteration, we consider the minimum weight edge among the edges that connect the two sets.

The implementation discussed in previous post uses two arrays to find minimum weight edge that connects the two sets. Here we use one inMST[V]. The value of MST[i] is going to be true if vertex i is included in the MST. In every pass, we consider only those edges such that one vertex of the edge is included in MST and other is not. After we pick an edge, we mark both vertices as included in MST.

`// A simple C++ implementation to find minimum ` `// spanning tree for adjacency representation. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define V 5 ` ` ` `// Returns true if edge u-v is a valid edge to be ` `// include in MST. An edge is valid if one end is ` `// already included in MST and other is not in MST. ` `bool` `isValidEdge(` `int` `u, ` `int` `v, vector<` `bool` `> inMST) ` `{ ` ` ` `if` `(u == v) ` ` ` `return` `false` `; ` ` ` `if` `(inMST[u] == ` `false` `&& inMST[v] == ` `false` `) ` ` ` `return` `false` `; ` ` ` `else` `if` `(inMST[u] == ` `true` `&& inMST[v] == ` `true` `) ` ` ` `return` `false` `; ` ` ` `return` `true` `; ` `} ` ` ` `void` `primMST(` `int` `cost[][V]) ` `{ ` ` ` `vector<` `bool` `> inMST(V, ` `false` `); ` ` ` ` ` `// Include first vertex in MST ` ` ` `inMST[0] = ` `true` `; ` ` ` ` ` `// Keep adding edges while number of included ` ` ` `// edges does not become V-1. ` ` ` `int` `edge_count = 0, mincost = 0; ` ` ` `while` `(edge_count < V - 1) { ` ` ` ` ` `// Find minimum weight valid edge. ` ` ` `int` `min = INT_MAX, a = -1, b = -1; ` ` ` `for` `(` `int` `i = 0; i < V; i++) { ` ` ` `for` `(` `int` `j = 0; j < V; j++) { ` ` ` `if` `(cost[i][j] < min) { ` ` ` `if` `(isValidEdge(i, j, inMST)) { ` ` ` `min = cost[i][j]; ` ` ` `a = i; ` ` ` `b = j; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `if` `(a != -1 && b != -1) { ` ` ` `printf` `(` `"Edge %d:(%d, %d) cost: %d \n"` `, ` ` ` `edge_count++, a, b, min); ` ` ` `mincost = mincost + min; ` ` ` `inMST[b] = inMST[a] = ` `true` `; ` ` ` `} ` ` ` `} ` ` ` `printf` `(` `"\n Minimum cost= %d \n"` `, mincost); ` `} ` ` ` `// driver program to test above function ` `int` `main() ` `{ ` ` ` `/* Let us create the following graph ` ` ` `2 3 ` ` ` `(0)--(1)--(2) ` ` ` `| / \ | ` ` ` `6| 8/ \5 |7 ` ` ` `| / \ | ` ` ` `(3)-------(4) ` ` ` `9 */` ` ` `int` `cost[][V] = { ` ` ` `{ INT_MAX, 2, INT_MAX, 6, INT_MAX }, ` ` ` `{ 2, INT_MAX, 3, 8, 5 }, ` ` ` `{ INT_MAX, 3, INT_MAX, INT_MAX, 7 }, ` ` ` `{ 6, 8, INT_MAX, INT_MAX, 9 }, ` ` ` `{ INT_MAX, 5, 7, 9, INT_MAX }, ` ` ` `}; ` ` ` ` ` `// Print the solution ` ` ` `primMST(cost); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Edge 0:(0, 1) cost: 2 Edge 1:(1, 2) cost: 3 Edge 2:(1, 4) cost: 5 Edge 3:(0, 3) cost: 6 Minimum cost= 16

Time Complexity : O(V^{3})

Note that time complexity of previous approach that uses adjacency matrix is O(V^{2}) and time complexity of the adjacency list representation implementation is O((E+V)LogV).

## Recommended Posts:

- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- Push Relabel Algorithm | Set 2 (Implementation)
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Karger's algorithm for Minimum Cut | Set 1 (Introduction and Implementation)
- Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Freivald’s Algorithm to check if a matrix is product of two
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
- Josephus problem | Set 2 (A Simple Solution when k = 2)
- C/C++ program to make a simple calculator
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Program to check diagonal matrix and scalar matrix

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.