Given an undirected, connected and weighted graph, find Minimum Spanning Tree (MST) of the graph using Prim’s algorithm.
Input : Adjacency List representation
of above graph
Output : Edges in MST
0 - 1
1 - 2
2 - 3
3 - 4
2 - 5
5 - 6
6 - 7
2 - 8
Note: There are two possible MSTs, the other
MST includes edge 0-7 in place of 1-2.
We have discussed below Prim’s MST implementations.
The second implementation is time complexity wise better, but is really complex as we have implemented our own priority queue. STL provides priority_queue, but the provided priority queue doesn’t support decrease key operation. And in Prim’s algorithm, we need a priority queue and below operations on priority queue :
- ExtractMin : from all those vertices which have not yet been included in MST, we need to get vertex with minimum key value.
- DecreaseKey : After extracting vertex we need to update keys of its adjacent vertices, and if new key is smaller, then update that in data structure.
The algorithm discussed here can be modified so that decrease key is never required. The idea is, not to insert all vertices in priority queue, but only those which are not MST and have been visited through a vertex that has included in MST. We keep track of vertices included in MST in a separate boolean array inMST[].
1) Initialize keys of all vertices as infinite and
parent of every vertex as -1.
2) Create an empty priority_queue pq. Every item
of pq is a pair (weight, vertex). Weight (or
key) is used as first item of pair
as first item is by default used to compare
two pairs.
3) Initialize all vertices as not part of MST yet.
We use boolean array inMST[] for this purpose.
This array is required to make sure that an already
considered vertex is not included in pq again. This
is where Prim's implementation differs from Dijkstra.
In Dijkstra's algorithm, we didn't need this array as
distances always increase. We require this array here
because key value of a processed vertex may decrease
if not checked.
4) Insert source vertex into pq and make its key as 0.
5) While either pq doesn't become empty
a) Extract minimum key vertex from pq.
Let the extracted vertex be u.
b) Include u in MST using inMST[u] = true.
c) Loop through all adjacent of u and do
following for every vertex v.
// If weight of edge (u,v) is smaller than
// key of v and v is not already in MST
If inMST[v] = false && key[v] > weight(u, v)
(i) Update key of v, i.e., do
key[v] = weight(u, v)
(ii) Insert v into the pq
(iv) parent[v] = u
6) Print MST edges using parent array.
Below is C++ implementation of above idea.
C++
#include<bits/stdc++.h>
using namespace std;
# define INF 0x3f3f3f3f
typedef pair< int , int > iPair;
class Graph
{
int V;
list< pair< int , int > > *adj;
public :
Graph( int V);
void addEdge( int u, int v, int w);
void primMST();
};
Graph::Graph( int V)
{
this ->V = V;
adj = new list<iPair> [V];
}
void Graph::addEdge( int u, int v, int w)
{
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
void Graph::primMST()
{
priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
int src = 0;
vector< int > key(V, INF);
vector< int > parent(V, -1);
vector< bool > inMST(V, false );
pq.push(make_pair(0, src));
key[src] = 0;
while (!pq.empty())
{
int u = pq.top().second;
pq.pop();
if (inMST[u] == true ){
continue ;
}
inMST[u] = true ;
list< pair< int , int > >::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
{
int v = (*i).first;
int weight = (*i).second;
if (inMST[v] == false && key[v] > weight)
{
key[v] = weight;
pq.push(make_pair(key[v], v));
parent[v] = u;
}
}
}
for ( int i = 1; i < V; ++i)
printf ( "%d - %d\n" , parent[i], i);
}
int main()
{
int V = 9;
Graph g(V);
g.addEdge(0, 1, 4);
g.addEdge(0, 7, 8);
g.addEdge(1, 2, 8);
g.addEdge(1, 7, 11);
g.addEdge(2, 3, 7);
g.addEdge(2, 8, 2);
g.addEdge(2, 5, 4);
g.addEdge(3, 4, 9);
g.addEdge(3, 5, 14);
g.addEdge(4, 5, 10);
g.addEdge(5, 6, 2);
g.addEdge(6, 7, 1);
g.addEdge(6, 8, 6);
g.addEdge(7, 8, 7);
g.primMST();
return 0;
}
|
Java
import java.util.*;
class iPair {
int first, second;
public iPair( int first, int second)
{
this .first = first;
this .second = second;
}
}
class Graph {
int V;
List<List<iPair> > adj;
public Graph( int V)
{
this .V = V;
adj = new ArrayList<>();
for ( int i = 0 ; i < V; i++) {
adj.add( new ArrayList<>());
}
}
void addEdge( int u, int v, int w)
{
adj.get(u).add( new iPair(v, w));
adj.get(v).add( new iPair(u, w));
}
void primMST()
{
PriorityQueue<iPair> pq = new PriorityQueue<>(
V, new Comparator<iPair>() {
public int compare(iPair a, iPair b)
{
return a.second - b.second;
}
});
int src = 0 ;
int INF = Integer.MAX_VALUE;
int [] key = new int [V];
Arrays.fill(key, INF);
int [] parent = new int [V];
Arrays.fill(parent, - 1 );
boolean [] inMST = new boolean [V];
pq.offer( new iPair( 0 , src));
key[src] = 0 ;
while (!pq.isEmpty()) {
int u = pq.peek().second;
pq.poll();
if (inMST[u]) {
continue ;
}
inMST[u] = true ;
for (iPair i : adj.get(u)) {
int v = i.first;
int weight = i.second;
if (!inMST[v] && key[v] > weight) {
key[v] = weight;
pq.offer( new iPair(key[v], v));
parent[v] = u;
}
}
}
for ( int i = 1 ; i < V; i++) {
System.out.println(parent[i] + " - " + i);
}
}
}
public class Main {
public static void main(String[] args)
{
int V = 9 ;
Graph graph = new Graph(V);
graph.addEdge( 0 , 1 , 4 );
graph.addEdge( 0 , 7 , 8 );
graph.addEdge( 1 , 2 , 8 );
graph.addEdge( 1 , 7 , 11 );
graph.addEdge( 2 , 3 , 7 );
graph.addEdge( 2 , 8 , 2 );
graph.addEdge( 2 , 5 , 4 );
graph.addEdge( 3 , 4 , 9 );
graph.addEdge( 3 , 5 , 14 );
graph.addEdge( 4 , 5 , 10 );
graph.addEdge( 5 , 6 , 2 );
graph.addEdge( 6 , 7 , 1 );
graph.addEdge( 6 , 8 , 6 );
graph.addEdge( 7 , 8 , 7 );
graph.primMST();
}
}
|
Output
0 - 1
1 - 2
2 - 3
3 - 4
2 - 5
5 - 6
6 - 7
2 - 8
Time complexity : O(E Log V))
Auxiliary Space :O(V)
A Quicker Implementation using array of vectors representation of a weighted graph :
C++
#include<bits/stdc++.h>
using namespace std;
# define INF 0x3f3f3f3f
typedef pair< int , int > iPair;
void addEdge(vector <pair< int , int > > adj[], int u,
int v, int wt)
{
adj[u].push_back(make_pair(v, wt));
adj[v].push_back(make_pair(u, wt));
}
void primMST(vector<pair< int , int > > adj[], int V)
{
priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
int src = 0;
vector< int > key(V, INF);
vector< int > parent(V, -1);
vector< bool > inMST(V, false );
pq.push(make_pair(0, src));
key[src] = 0;
while (!pq.empty())
{
int u = pq.top().second;
pq.pop();
if (inMST[u] == true ){
continue ;
}
inMST[u] = true ;
for ( auto x : adj[u])
{
int v = x.first;
int weight = x.second;
if (inMST[v] == false && key[v] > weight)
{
key[v] = weight;
pq.push(make_pair(key[v], v));
parent[v] = u;
}
}
}
for ( int i = 1; i < V; ++i)
printf ( "%d - %d\n" , parent[i], i);
}
int main()
{
int V = 9;
vector<iPair > adj[V];
addEdge(adj, 0, 1, 4);
addEdge(adj, 0, 7, 8);
addEdge(adj, 1, 2, 8);
addEdge(adj, 1, 7, 11);
addEdge(adj, 2, 3, 7);
addEdge(adj, 2, 8, 2);
addEdge(adj, 2, 5, 4);
addEdge(adj, 3, 4, 9);
addEdge(adj, 3, 5, 14);
addEdge(adj, 4, 5, 10);
addEdge(adj, 5, 6, 2);
addEdge(adj, 6, 7, 1);
addEdge(adj, 6, 8, 6);
addEdge(adj, 7, 8, 7);
primMST(adj, V);
return 0;
}
|
Output
0 - 1
1 - 2
2 - 3
3 - 4
2 - 5
5 - 6
6 - 7
2 - 8
Note: Like Dijkstra’s priority_queue implementation, we may have multiple entries for same vertex as we do not (and we can not) make isMST[v] = true in if condition.
C++
if (inMST[v] == false && key[v] > weight)
{
key[v] = weight;
pq.push(make_pair(key[v], v));
parent[v] = u;
}
|
Java
if (inMST[v] == false && key[v] > weight)
{
key[v] = weight;
pq.add( new Pair<Integer, Integer>(key[v], v));
parent[v] = u;
}
|
Python3
if (inMST[v] = = False and key[v] > weight) :
key[v] = weight
pq.append([key[v], v])
parent[v] = u
|
C#
if (inMST[v] == false && key[v] > weight)
{
key[v] = weight;
pq.Add( new Tuple< int , int >(key[v], v));
parent[v] = u;
}
|
Javascript
<script>
if (inMST[v] == false && key[v] > weight)
{
key[v] = weight;
value = [key[v], v];
pq.push(value);
parent[v] = u;
}
</script>
|
But as explained in Dijkstra’s algorithm, time complexity remains O(E Log V) as there will be at most O(E) vertices in priority queue and O(Log E) is same as O(Log V).
Unlike Dijkstra’s implementation, a boolean array inMST[] is mandatory here because the key values of newly inserted items can be less than the key values of extracted items. So we must not consider extracted items.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Feb, 2023
Like Article
Save Article