C++ Kruskal Algorithm gives out unhandeled exception at runtime

190 views Asked by At

The following code is supposed to find the minimum spanning tree from a adjacency matrix:

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

It works for 6 number of vertices and the following matrix:

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

however for 13 vertices and with the following matrix:

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

this error occurs:

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

The error occurs at line 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

There are 2 answers

3
Nic Foster On
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.

0
corecase On

You've defined your 'parent' array to have a size of 9 (assuming you have a maximum of 9 vertices, so max number of parents is 9). Six vertices will work because it's less than 9. With thirteen vertices you MAY be accessing elements passed your parent array size; thus, you should try and define your array size depending on the number of vertices.

P.S In general you don't want to have magic numbers in your code.