Home > Algos > Implement Dijkstra’s Algorithm in C#

Implement Dijkstra’s Algorithm in C#

This was a part of my project work done, when I was in semester 6. The project was “Routing System” was an online graphical solution, which displayed the shortest possible path between any two destinations. Well, I am not going to mention the various features which I had included in the system, rather I am giving an idea on implementation of dijkstra’s algorithm which was the central most attraction and requirement of the system.

Please refer and understand dijkstra’s algorithm before you implement it.

The implementation (source code) provided below can be used to find the smallest possible path from any node to any node:-

int startnode=-1,endnode=-1;
int count = 0;
int[,] A = new int[num_node, num_node];//main matrix
//num_node represents the number of nodes in the system.
//Assuming that the matrix A[num_node][num_node], which stores the distances between the pairs of node, is provided.
int[] C = new int[num_node];//to check if the node is visited or not.
int[] D = new int[num_node];//stores total distances between startnode and endnode through all the possible path.
string[] traverse = new string[num_node];//stores all the possible path between start node and end node.

//Dijkstra’s algorithm starts here————————————————————-
//startnode and endnode should be provided by user of the system through any input method in C#.

int minNode = startnode;
for (int i = 0; i < num_node; i++)
{
C[i] = i;
D[i] = A[minNode, i];
if (A[minNode, i] > 0)
{
traverse[i] = Convert.ToString(minNode) + “,” + Convert.ToString(i);//stores the path having direct connection
}
}

C[startnode] = -1;//marking the startnode as read by assigning it value -1

for (int k = 0; k < num_node; k++)//loop to find the next minimum node
{
int minValue = Int32.MaxValue;
for (int l = 0; l < num_node; l++)
{
if (C[l] >= 0)//check if the node is traversed before
{
if ((D[l] > 0) && (D[l] < minValue))
{
minValue = D[l];//stores the minimum value
minNode = l;//mimimum node is stored
}
}
}

C[minNode] = -1;//marking the minimum node as read by assigning its value -1
for (int m = 0; m < num_node; m++)
{
if (C[m] >= 0)//check if the node is traversed before
{
if (A[minNode, m] > 0)//checks if there is a direct connection
{
if (D[m] < 0)
{
D[m] = A[minNode, m] + minValue;
traverse[m] = traverse[minNode] + “,” + Convert.ToString(m);//stores the path
}
else if ((A[minNode, m] + minValue) < D[m])//checks if the distance is minimum
{
D[m] = A[minNode, m] + minValue;
traverse[m] = traverse[minNode] + “,” + Convert.ToString(m);//stores the path
}
}
}
}
}

D[endnode] ” and ” traverse[endnode] “ gives the smallest distance and path between start node and end node, which can be used as per the requirement

hope this helps many….. :)

  1. bob
    October 6, 2010 at 8:14 am | #1

    Unfortunately this doesn’t work. Haven’t had time to debug yet

    • October 6, 2010 at 2:11 pm | #2

      hi,
      It does work…but you will have to change it according to your use. Please read out the comments against the code. let me know if you still have any problem. You can email me at shobhitsharda@gmail.com

  2. Erik
    March 29, 2011 at 7:23 pm | #3

    Thank you very much. This is very helpfull. I’m using it in a project for a mine detection robot that has to scan a grid.
    I only changed the output string to an array of integers. I know my code is nog good, but i’ll share it anyway:

    string [] split = traverse[endnode].Split(new Char [] {‘,’});
    int n = 0;
    int routeArray[];
    foreach (string s in split)
    {
    if (s.Trim() != “”)
    {
    try
    {
    this.routeArray[n] = Int16.Parse(s);
    n++;
    }
    catch (NullReferenceException) { }
    }
    }

    • March 29, 2011 at 11:10 pm | #4

      Hi Erik,
      Your most welcome and thanks for sharing the code. :)

    • Erik
      March 30, 2011 at 9:31 am | #5

      Correction to the code I posted:

      string[] split = traverse[endnode].Split(new Char[] { ‘,’ });
      int n = 0;
      foreach (string s in split)
      {
      if (s.Trim() != “”)
      {
      n++;
      }
      }
      int[] routeArray = new int[n];
      for (int j = 0; j < n; j++)
      {
      routeArray[j] = Int32.Parse(split[j]);
      }

  3. Rohit Aggarwal
    July 29, 2011 at 8:11 am | #6

    can you tell me where u have store the data initially that is distance between two nodes???
    and where u have passed it to Dijkstra function.
    Thank You

    • October 5, 2011 at 10:16 am | #7

      Hey Rohit,
      Sorry for late reply. I am using a 2D matrix named A[num_node][num_node] to store the distances between the two node. Please read the above code and the commented lines once again to understand clearly.
      regards,

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.