Error: Index was outside the bounds of the array in C#

1.1k Views Asked by At

I am writing BellmanFord algorithm in C#. However during run I am getting error:

Index was outside the bounds of the array

I don't know why it gives this error while array[6,6] = array that I initialized when reading the matrix. This is my code:

internal class BellmanFords { private int[] distances; private int numberofvertices; public static readonly int MAX_VALUE = 999; private int[] parent;

        public BellmanFords(int numberofvertices)
        {
            this.numberofvertices = numberofvertices;
            distances = new int[numberofvertices + 1];
            parent = new int[numberofvertices + 1];
        }

        public void BellmanFordEvaluation(int[,] adjacencymatrix, int source)
        {
            for (int node = 0; node < numberofvertices; node++)
            {
                distances[node] = MAX_VALUE;
            }
            Array.Fill(parent, -1);
            distances[source] = 0;
            for (int node = 0; node < numberofvertices - 1; node++)
            {
                for (int sourcenode = 0; sourcenode < numberofvertices; sourcenode++)
                {
                    for (int destinationnode = 0; destinationnode < numberofvertices; destinationnode++)
                    {
                        if (adjacencymatrix[sourcenode, destinationnode] != MAX_VALUE)
                        {
                            if (distances[destinationnode] > distances[sourcenode]
                                    + adjacencymatrix[sourcenode, destinationnode])
                            {
                                distances[destinationnode] = distances[sourcenode]
                                    + adjacencymatrix[sourcenode, destinationnode];
                                parent[destinationnode] = sourcenode;
                            }
                        }
                    }
                }
            }

            for (int sourcenode = 0; sourcenode < numberofvertices; sourcenode++)
            {
                for (int destinationnode = 0; destinationnode < numberofvertices; destinationnode++)
                {
                    if (adjacencymatrix[sourcenode,destinationnode] != MAX_VALUE)
                    {
                        if (distances[destinationnode] > distances[sourcenode]
                               + adjacencymatrix[sourcenode, destinationnode])
                        {
                            Console.Write("graph with negative circuit");
                            return;
                        }
                    }
                }
            }

            for (int vertex = 0; vertex < numberofvertices; vertex++)
            {
                if (vertex != source)
                {
                    if (distances[vertex] == MAX_VALUE)
                    {
                        Console.Write("Trackless");
                    }
                    else
                    {
                        List<int> path = new List<int>();
                        GetPath(parent, vertex, path);
                        Console.Write("distance of source  " + source + " to "
                                  + vertex + " is " + distances[vertex] + ". Its path is: " + path);
                    }
                }
            }
        }

        public static void GetPath(int[] parent, int vertex, List<int> path)
        {
            if (vertex < 0)
            {
                return;
            }
            GetPath(parent, parent[vertex], path);
            path.Add(vertex);
        }
    }

And this is the input file:

6
0 6 0 7 0 0
0 0 5 8 -4 0
0 -2 0 0 0 0
0 0 -3 0 9 0
2 0 7 0 0 0
0 0 3 0 0 0

Please help me review it. Thanks

0

There are 0 best solutions below