实现 Brovuka 算法以查找 MST 时遇到麻烦
trouble implementing Brovuka's Algorithm for finding a MST
我不知道为什么在运行代码时,第一个循环中的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。
相关文章:
- 查找奇数平方和时遇到麻烦
- CSUF EPP - 你有什么想法(在添加到链接列表末尾时遇到麻烦)
- MySQL 从注入过渡到参数化时遇到麻烦
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 以分类方式插入节点时遇到麻烦
- 在此任务上遇到麻烦,我们需要使用指针和数组来找到文本文件中某些数字的平均值,中值和模式
- 我在DFS搜索(树)代码中遇到麻烦.我如何打破递归
- 我在处理字符时遇到麻烦
- 在我的代码覆盖本身时遇到麻烦,我不确定为什么
- C++处理文件,在制作程序时遇到麻烦
- 在客户端/服务器计算器C 方面遇到麻烦
- 从一个字符串中获取 2-5 个连续的单词短语,我得到了 2 个工作,但在做 3 个时遇到麻烦
- boost :: phoenix ::功能遇到麻烦
- 在具有模板成员功能的模板类方面遇到麻烦
- 通过模板参数遇到麻烦
- 快速排序在C++,遇到麻烦
- 计算字符时遇到麻烦
- 在C++中摸索多个排他性组合时遇到麻烦
- 编译phash程序时遇到麻烦
- 在 Linux 上为 OpenGL 4.2 设置开发环境(查找 gl.h 时遇到麻烦)