I am trying to implement Prim's MST algorithm in C++ using STL.
But for the following program it seems to enter in an infinite loop. And then exits with an error.
Pseudo-code for Prim's MST Algorithm ;
My Code :
#include<algorithm>
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
#define REP(i,a,b) for(int i=int(a);i<b;i++)
#define TRvii(c,it) for(vii::iterator it=(c).begin();it!=(c).end();it++)
#define INF 2000000000
void Prims(int V, int s, vector<vii> &AdjList)
{
vector<int> dist(V,INF);
dist[s] = 0;
priority_queue<ii,vector<ii>,greater<ii> > pq;
pq.push(ii(0,s));
REP(i,1,V) pq.push(ii(i,INF));
bool inPriorityQueue[V];
REP(i,0,V) inPriorityQueue[i] = true;
while(!pq.empty())
{
ii top = pq.top(); pq.pop();
int d = top.first,u = top.second;
inPriorityQueue[u] = false;
TRvii(AdjList[u],it)
{
int v = it->first, weight_u_v = it->second;
if(inPriorityQueue[v] && weight_u_v<dist[v])
{
dist[v] = weight_u_v;
}
}
}
cout << "The shortest distance from " << s << " to all the nodes is" << endl;
REP(i,0,V)
{
cout << i << " : " << dist[i] << endl;
}
}
int main()
{
int v,s,edges;
printf("Enter number of vertices : ");
scanf("%d",&v);
vector<vii> adjList(v+1);
printf("\nEnter source vertex : ");
scanf("%d",&s);
adjList[0].push_back(make_pair(1,4));
adjList[0].push_back(make_pair(7,8));
adjList[1].push_back(make_pair(0,4));
adjList[1].push_back(make_pair(2,8));
adjList[1].push_back(make_pair(7,11));
adjList[7].push_back(make_pair(0,8));
adjList[7].push_back(make_pair(1,11));
adjList[7].push_back(make_pair(8,7));
adjList[7].push_back(make_pair(6,1));
adjList[2].push_back(make_pair(1,8));
adjList[2].push_back(make_pair(3,7));
adjList[2].push_back(make_pair(8,2));
adjList[2].push_back(make_pair(5,4));
adjList[8].push_back(make_pair(2,2));
adjList[8].push_back(make_pair(7,7));
adjList[8].push_back(make_pair(6,6));
adjList[6].push_back(make_pair(7,1));
adjList[6].push_back(make_pair(5,2));
adjList[6].push_back(make_pair(8,2));
adjList[5].push_back(make_pair(6,2));
adjList[5].push_back(make_pair(2,4));
adjList[5].push_back(make_pair(3,14));
adjList[5].push_back(make_pair(4,10));
adjList[4].push_back(make_pair(3,9));
adjList[4].push_back(make_pair(5,10));
adjList[3].push_back(make_pair(2,7));
adjList[3].push_back(make_pair(5,14));
adjList[3].push_back(make_pair(4,9));
Prims(v, s, adjList);
return 0;
}
Graph on which this algorithm is implemented :
If you had tried debugging it you would have very quickly found the problem lies with the line:
TRvii(AdjList[u],it)
Think about what u
is. In the first go around the while
loop u == s
due to pq.push(ii(0,s));
. In the next, and all subsequent loops however, u == INF
due to REP(i,1,V) pq.push(ii(i,INF));
.
Trying to access AdjList[INF]
is "bad" and results in undefined behaviour (a crash in your case).
I would suggest debugging your algorithm further, possibly with a simpler test case. Step through it and watch all variables. Assumably you understand the algorithm and what states it should go through so watch all the variables to make sure they are what they should be.