实现 Brovuka 算法以查找 MST 时遇到麻烦

trouble implementing Brovuka's Algorithm for finding a MST

本文关键字:遇到 麻烦 MST 查找 Brovuka 算法 实现      更新时间:2023-10-16

我不知道为什么在运行代码时,第一个循环中的find函数中存在访问冲突,我有点迷路了,这是我的代码,如果你们有任何现有的brovuka的c++或java实现的话?

#include <iostream>
using namespace std;
int numVertices,numEdges;
int *parent,*weight,numTrees;
int *bestEdgeNum;
struct edge {
    int tail,head,weight;
};
typedef struct edge edgeType;
edgeType *edgeTab;
int find(int x)
{
    int i,j,root;
    for (i=x;parent[i]!=i; i=parent[i]);
        root=i;
    // path compression 
    for (i=x; parent[i]!=i;j=parent[i],parent[i]=root,i=j);
    return root;
}
void makeEquivalent(int i,int j)
{
    if (weight[i]>weight[j])
    {
        parent[j]=i;
        weight[i]+=weight[j];
    }
    else
    {
        parent[i]=j;
        weight[j]+=weight[i];
    }
    numTrees--;
}
int main()
{
    int i,MSTweight=0;
    int root1,root2;
    int usefulEdges;
    cout << "Enter the number of Verticesn";
    cin >> numVertices;
    cout << "Enter the number of Edgesn";
    cin >> numEdges;
    edgeTab = new edgeType[numEdges];
    parent = new int[numVertices];
    weight = new int[numVertices];
    bestEdgeNum = new int[numVertices];
    if (!edgeTab || !parent || !weight || !bestEdgeNum)
    {
        cout << "errorn";
    }
    cout << "Enter the undirected edge weights for the graph in the format u v wn";
    for (i=0;i<numEdges;i++)
    {
        cin >> edgeTab[i].tail >> edgeTab[i].head  >> edgeTab[i].weight;
    }
    for (i=0;i<numVertices;i++)
    {
        parent[i]=i;
        weight[i]=1;
    }
    numTrees=numVertices;  // Each vertex is initially in its own subtree
    usefulEdges=numEdges;  // An edge is useful if the two vertices are separate
    while (numTrees>1 && usefulEdges>0)
    {
        for (i=0;i<numVertices;i++)
            bestEdgeNum[i]=(-1);
        usefulEdges=0;
        for (i=0;i<numEdges;i++)
        {
            root1=find(edgeTab[i].tail);
                    root2=find(edgeTab[i].head);
                    if (root1==root2)
                        cout << edgeTab[i].tail <<" , " <<  edgeTab[i].head << " : "
                             << edgeTab[i].weight << " is uselessn";
                    else
                    {
                        usefulEdges++;
                        if (bestEdgeNum[root1] == -1||edgeTab[bestEdgeNum[root1]].weight>edgeTab[i].weight)
                            bestEdgeNum[root1]=i;  // Have a new best edge from this component
            if (bestEdgeNum[root2]==(-1)|| edgeTab[bestEdgeNum[root2]].weight>edgeTab[i].weight)
                bestEdgeNum[root2]=i;  // Have a new best edge from this component
        }
    }
    for (i=0;i<numVertices;i++)
        if (bestEdgeNum[i]!=(-1))
        {
            root1=find(edgeTab[bestEdgeNum[i]].tail);
            root2=find(edgeTab[bestEdgeNum[i]].head);
            if (root1==root2)
                continue;  // This round has already connected these components.
            MSTweight+=edgeTab[bestEdgeNum[i]].weight;
            cout << edgeTab[bestEdgeNum[i]].tail << " " << edgeTab[bestEdgeNum[i]].head << " " << edgeTab[bestEdgeNum[i]].weight << "included in MSTn";    
            makeEquivalent(root1,root2);
                    }
                    cout << "numTrees is " << numTrees << endl;
    }
    if (numTrees!=1)
        cout << "MST does not existn";
    cout << "Sum of weights of spanning edges is " << MSTweight << endl;
    return 0;
}

您的问题是以基于1的格式输入顶点,但所有数组(如父数组)都是基于0的格式。当你试图得到父母时,坏事就会发生。

最简单的解决方法可能是在输入头和尾时减去1,然后在打印结果时再加1。