C ++ Kruskal算法在运行时发出unhandeled异常

时间:2015-06-22 17:57:44

标签: c++ visual-studio-2013 kruskals-algorithm

以下代码应该从邻接矩阵中找到最小生成树:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
#include <vector>
#include <string>

using namespace std;

int i, j, k, a, b, u, v, n, ne = 1;
int min, mincost = 0, cost[9][9], parent[9];
int find(int);
int uni(int, int);

int find(int i)
{
    while (parent[i])  // Error occurs at this line
        i = parent[i];
    return i;
}

int uni(int i, int j)
{
    if (i != j)
    {
        parent[j] = i;
        return 1;
    }
    return 0;
}

int main()
{
    cout << "MST Kruskal:\n=================================\n";
    cout << "\nNo. of vertices: ";
    cin >> n;
    cout << "\nAdjacency matrix:\n\n";

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            cin >> cost[i][j];
            if (cost[i][j] == 0)
                cost[i][j] = 999;
        }
    }

    cout << "\nMST Edge:\n\n";

    while (ne < n)
    {
        for (i = 1, min = 999; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                if (cost[i][j] < min)
                {
                    min = cost[i][j];
                    a = u = i;
                    b = v = j;
                }
            }
        }

        u = find(u);
        v = find(v);

        if (uni(u, v))
        {
            cout << ne++ << "th" << " edge " << "(" << a << "," << b << ")" << " = " << min << endl;
            mincost += min;
        }
        cost[a][b] = cost[b][a] = 999;
    }

    cout << "\nMinimum cost = " << mincost << "\n" << endl;

    system("PAUSE");

    return 0;
}

它适用于6个顶点和以下矩阵:

0 3 1 6 0 0
3 0 5 0 3 0
1 5 0 5 6 4
6 0 5 0 0 2
0 3 6 0 0 6
0 0 4 2 6 0

但对于13个顶点并使用以下矩阵:

0 1 0 0 0 2 6 0 0 0 0 0 0
1 0 1 2 4 0 0 0 0 0 0 0 0
0 1 0 0 4 0 0 0 0 0 0 0 0
0 2 0 0 2 1 0 0 0 0 0 0 0
0 4 4 2 0 2 1 0 0 0 0 4 0
2 0 0 1 2 0 0 0 0 0 0 2 0
6 0 0 0 1 0 0 3 0 1 0 5 0
0 0 0 0 0 0 3 0 2 0 0 0 0
0 0 0 0 0 0 0 2 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0 1 3 2
0 0 0 0 0 0 0 0 1 1 0 0 0
0 0 0 0 4 2 5 0 0 3 0 0 1
0 0 0 0 0 0 0 0 0 2 0 1 0

发生此错误:

Unhandled exception at 0x00ED5811 in KruskalMST.exe: 0xC0000005: Access violation reading location 0x00F67A1C.

错误发生在第17行:while (parent[i])

VS Autos:

Name    Value                           Type

i       138596                                                  int
parent  0x00ee048c {2, 999, 999, 999, 999, 999, 999, 999, 2}    int[9]
[0] 2                                                           int
[1] 999                                                         int
[2] 999                                                         int
[3] 999                                                         int
[4] 999                                                         int
[5] 999                                                         int
[6] 999                                                         int
[7] 999                                                         int
[8] 2                                                           int

2 个答案:

答案 0 :(得分:0)

while (parent[i])
{
   i = parent[i];
}

首先,请使用大括号括起while语句。任何向其添加另一行的人都可能会导致意外行为。

您的问题可能是parent[i]i数组的范围之外的parent分配了一个值。

尝试此操作以查看它分配给i的内容:

while (parent[i] != 0)
{
   cout << "parent[i] is " << parent[i];
   i = parent[i];
}

由于父数组的大小为9,如果i设置为9或更高(或某种程度上小于0),则在使用parent[i]时可能会出现访问冲突。

无关:明确你在while检查的条件是很好的。在我看到parent是一个int []之前,我不知道它是否是一个指针或布尔数组,我不知道while条件正在检查什么。< / p>

如果您想要安全,请检查您的parent数组:

static const int parentSize = 9;
int parent[parentSize];

while (parent[i] != 0 && i > 0 && i < parentSize)
{
   cout << "parent[i] is " << parent[i];
   i = parent[i];
}

您可能需要将parentSize增加到更大的值。如果你想要更动态的东西,你可能会考虑使用std :: vector而不是数组,如果遇到容器不够大的情况,可以在运行时重新调整它。

答案 1 :(得分:0)

您已将'父'数组定义为大小为9(假设您最多有9个顶点,因此父项的最大数目为9)。六个顶点将起作用,因为它小于9.使用十三个顶点,您可以访问传递父数组大小的元素;因此,您应该尝试根据顶点数量定义数组大小。

P.S一般情况下,您不希望代码中包含幻数。