0

I found dijkstra implementation that do exactly what I want and work great in Python version but there is problem with C# version that I need (https://www.geeksforgeeks.org/printing-paths-dijkstras-shortest-path-algorithm/?ref=rp)

When array have many zeros then error occur System.IndexOutOfRangeException: „Index was outside the bounds of the array. in added[nearestVertex] = true;

using System;
 
public class DijkstrasAlgorithm
{


private static readonly int NO_PARENT = -1;

private static void dijkstra(int[,] adjacencyMatrix,
                                    int startVertex)
{
    int nVertices = adjacencyMatrix.GetLength(0);

    int[] shortestDistances = new int[nVertices];


    bool[] added = new bool[nVertices];

    for (int vertexIndex = 0; vertexIndex < nVertices;
                                        vertexIndex++)
    {
        shortestDistances[vertexIndex] = int.MaxValue;
        added[vertexIndex] = false;
    }
     
    shortestDistances[startVertex] = 0;

    int[] parents = new int[nVertices];

    parents[startVertex] = NO_PARENT;

    for (int i = 1; i < nVertices; i++)
    {

        int nearestVertex = -1;
        int shortestDistance = int.MaxValue;
        for (int vertexIndex = 0;
                vertexIndex < nVertices;
                vertexIndex++)
        {
            if (!added[vertexIndex] &&
                shortestDistances[vertexIndex] <
                shortestDistance)
            {
                nearestVertex = vertexIndex;
                shortestDistance = shortestDistances[vertexIndex];
            }
        }

        added[nearestVertex] = true;

        for (int vertexIndex = 0;
                vertexIndex < nVertices;
                vertexIndex++)
        {
            int edgeDistance = adjacencyMatrix[nearestVertex,vertexIndex];
             
            if (edgeDistance > 0
                && ((shortestDistance + edgeDistance) <
                    shortestDistances[vertexIndex]))
            {
                parents[vertexIndex] = nearestVertex;
                shortestDistances[vertexIndex] = shortestDistance +
                                                edgeDistance;
            }
        }
    }

    printSolution(startVertex, shortestDistances, parents);
}

private static void printSolution(int startVertex,
                                int[] distances,
                                int[] parents)
{
    int nVertices = distances.Length;
    Console.Write("Vertex\t Distance\tPath");
     
    for (int vertexIndex = 0;
            vertexIndex < nVertices;
            vertexIndex++)
    {
        if (vertexIndex != startVertex)
        {
            Console.Write("\n" + startVertex + " -> ");
            Console.Write(vertexIndex + " \t\t ");
            Console.Write(distances[vertexIndex] + "\t\t");
            printPath(vertexIndex, parents);
        }
    }
}

private static void printPath(int currentVertex,
                            int[] parents)
{
     
    if (currentVertex == NO_PARENT)
    {
        return;
    }
    printPath(parents[currentVertex], parents);
    Console.Write(currentVertex + " ");
}

public static void Main(String[] args)
{
   int[,] adjacencyMatrix =   { { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                                { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
    dijkstra(adjacencyMatrix, 0);
}

}

  • I'm guessing that if you pass the same array you are using here to python solution you should get the same result. The solution is not suited for cases where there are no "nearest vertexes", i.e. `nearestVertex` stays at `-1`. If you used sample `adjacencyMatrix` as in the example it will provide a result. – Guru Stron Mar 21 '23 at 12:19
  • In Python version this work but C# version have problem with that. In python if there is no nearest neighbor then return only parent id and distance as sys.maxsize – I'mStuckOnLine911 Mar 21 '23 at 12:31
  • It seems that there is a bug which is a feature in this case in python version. Just check that `nearestVertex` is >=0 before setting `added` and updating the path. I.e. `if(nearestVertex>=0){added[nearestVertex] = true; for (...){....}` – Guru Stron Mar 21 '23 at 12:41
  • After added if in `nearestVertex` and `edgeDistance`. In index 0 I got always max value, in other indexes I have stack overflow error – I'mStuckOnLine911 Mar 21 '23 at 13:11
  • I don't know what you are doing but this worked for me on the sample data and data in question. – Guru Stron Mar 21 '23 at 13:21

0 Answers0