I was trying to apply the prims algorithm using a priority queue. when I try to push the element in priority queue the priority of the element changes to some unexpected value.
When I run the code 2 times the priority of the same element on same input is changed
while(!Q.empty()){
int u = Q.top().first;
Q.pop();
temp = Q;
PQ[u] = false;
for(int i = 0; i < adj[u].size(); i++){
if(cost[adj[u][i].first] > adj[u][i].second && PQ[adj[u][i].first] == true){
cost[adj[u][i].first] = adj[u][i].second;
parent[adj[u][i].first] = u;
Q.push(make_pair(adj[u][i].first,cost[adj[u][i].second]));
}
}
temp = Q;
while(!temp.empty()){
cout << temp.top().first << " = " << temp.top().second << endl;
temp.pop();
}
cout << "\n\n";
}
output for first run
7 = 6.95326e-310
8 = 6.95326e-310
5 = 6.95336e-309
6 = 6.95336e-309
1 = 1.41421
1 = 32767
3 = 32767
8 = 32767
5 = 32767
3 = 32767
2 = 32767
7 = 32767
6 = 32767
4 = 32767
4 = 32767
2 = 32767
output for 2nd run
5 = 0
6 = 0
1 = 1.41421
8 = 12.53
7 = 12.53
1 = 32767
3 = 32767
8 = 32767
5 = 32767
3 = 32767
2 = 32767
7 = 32767
6 = 32767
4 = 32767
4 = 32767
2 = 32767
Aucun commentaire:
Enregistrer un commentaire