0

My code runs Dijkstra's algorithm on an adjacency matrix which currently outputs the distance from the given source node to every other node in the graph. However, I am trying to now make it output the route that the algorithm has taken for each result but I'm not sure how I would go about this?

I may also change the code to instead only output the desired route as well instead of all the combinations to make the code a bit more efficient.

using System;

namespace Dijkstra
{
    class Program
    {
        static void Main(string[] args)
        {    
            int[,] graph = {
            //A  B  C  D  E  F  G  H  I  J  K
            { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0  }, //A
            { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0  }, //B
            { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0  }, //C
            { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0  }, //D
            { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0  }, //E
            { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1  }, //F
            { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0  }, //G
            { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0  }, //H
            { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0  }, //I
            { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0  }, //J
            { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0  } //K
};

            Dijkstra(graph, 0, 11);
        }

        private static int MinimumDistance(int[] distance, bool[] shortestPathTreeSet, int verticesCount)
        {
            int min = int.MaxValue;
            int minIndex = 0;

            for (int v = 0; v < verticesCount; ++v)
            {
                if (shortestPathTreeSet[v] == false && distance[v] <= min)
                {
                    min = distance[v];
                    minIndex = v;
                }
            }

            return minIndex;
        }

        private static void Print(int[] distance, int verticesCount)
        {
            Console.WriteLine("Vertex    Distance from source");

            for (int i = 0; i < verticesCount; ++i)
                Console.WriteLine("{0}\t  {1}", i, distance[i]);
        }

        public static void Dijkstra(int[,] graph, int source, int verticesCount)
        {
            int[] distance = new int[verticesCount]; // Integer array, size of number of verticies
            bool[] shortestPathTreeSet = new bool[verticesCount]; // boolean array size of number of verticies

            for (int i = 0; i < verticesCount; ++i)     // For every verticy, set the verticy distance to infinite (max int value)
            {
                distance[i] = int.MaxValue;
                shortestPathTreeSet[i] = false;
            }

            distance[source] = 0;   // Distance of source verticie is 0

            for (int count = 0; count < verticesCount - 1; ++count)
            {
                int u = MinimumDistance(distance, shortestPathTreeSet, verticesCount);
                shortestPathTreeSet[u] = true;

                for (int v = 0; v < verticesCount; ++v)
                    if (!shortestPathTreeSet[v] && Convert.ToBoolean(graph[u, v]) && distance[u] != int.MaxValue && distance[u] + graph[u, v] < distance[v])
                        distance[v] = distance[u] + graph[u, v];
            }

            Print(distance, verticesCount);
        }

    }
}
JamBur05
  • 1
  • 1
  • 1
    Not sure if exact dupe or not, since the answer is pretty much the same as [what to do for BFS](https://stackoverflow.com/q/9590299/572670). – amit Aug 02 '22 at 14:41

1 Answers1

1

As you update the distance for a vertex, you can also store it's prev vertex - the vertex that led you to the current vertex. Then, once you are done, you can easily trace your steps back to the starting vertex, recursively and store them in a list.

Edit: Do you mean to say you want the path taken to EVERY vertex in the graph?

Berthur
  • 4,300
  • 2
  • 14
  • 28