-1

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

#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汽车:

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
4

2 回答 2

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

First of all, please use braces to enclose the while statement. Anyone adding another line to it would likely cause undesired behavior.

Your problem is likely that parent[i] assigns a value to i that is outside of the bounds of the parent array.

Try this to see what it's assigning to i:

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

Since the parent array has a size of 9, if i is ever set to 9 or greater (or less than 0 somehow), you may get an access violation when using parent[i].

Unrelated: It's good to be explicit about what condition you're checking in the while. Before I saw that parent was an int[], I didn't know if it might be an array of pointers, or booleans, I didn't know what the while condition was checking for.

If you want to be safe, bounds check your parent array:

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];
}

You likely need to increase the parentSize to something larger. If you want something that is more dynamic you might considering using std::vector instead of an array, it can be resized at runtime if you run into a case where the container isn't large enough.

于 2015-06-22T18:12:53.563 回答
0

您已将“父”数组的大小定义为 9(假设您最多有 9 个顶点,因此最大父数为 9)。六个顶点将起作用,因为它小于 9。使用 13 个顶点,您可能正在访问通过父数组大小的元素;因此,您应该尝试根据顶点数定义数组大小。

PS 一般来说,您不想在代码中使用幻数。

于 2015-06-22T19:14:42.873 回答