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