简体   繁体   English

C ++ STL中Prim的算法实现错误?

[英]Prim's Algorithm implementation in C++ STL error?

I am trying to implement Prim's MST algorithm in C++ using STL. 我正在尝试使用STL在C ++中实现Prim的MST算法。

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 ; Prim的MST算法的伪代码;

在此处输入图片说明

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. 想想u是什么。 In the first go around the while loop u == s due to pq.push(ii(0,s)); 首先,由于pq.push(ii(0,s));绕过while循环u == s 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)); 然而,在下一个以及所有后续循环中,由于REP(i,1,V) pq.push(ii(i,INF));而导致u == INF 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). 尝试访问AdjList[INF]是“错误的”,并导致未定义的行为(在您的情况下崩溃)。

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. 大概您了解该算法及其应经历的状态,因此请观察所有变量以确保它们是应有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM