SoFunction
Updated on 2025-03-07

Shortest path analysis implemented by C#


using System;
 using ;
 using ;
 using ;

 namespace ConsoleApplication1
 {
     class Program
     {
         static int length = 6;
         static string[] shortedPath = new string[length];
         static int noPath = 2000;
         static int MaxSize = 1000;
         static int[,] G =
         {
             { noPath, noPath, 10, noPath, 30, 100 },
             { noPath, noPath, 5, noPath, noPath, noPath },
             { noPath, noPath, noPath, 50, noPath, noPath },
             { noPath, noPath, noPath, noPath, noPath, 10 },
             { noPath, noPath, noPath, 20, noPath, 60 },
             { noPath, noPath, noPath, noPath, noPath, noPath }
         };
         static string[] PathResult = new string[length];

         static int[] path1 = new int[length];
         static int[,] path2 = new int[length, length];
         static int[] distance2 = new int[length];

         static void Main(string[] args)
         {
             int dist1 = getShortedPath(G, 0, 1, path1);
("Point 0 to point 5 path:");
             for (int i = 0; i < ; i++)
                 (path1[i].ToString() + " "); 
("Length:" + dist1);

 
             ("\r\n-----------------------------------------\r\n");

             int[] pathdist = getShortedPath(G, 0, path2);
("The path from point 0 to any point:");
             for (int j = 0; j < ; j++)
             {
(Path from "point 0 to " + j + ":");
                 for (int i = 0; i < length; i++)
                     (path2[j, i].ToString() + " ");
("Length:" + pathdist[j]);
             }
             ();

         }

 
//From a certain source point, find the shortest path to a certain node
         static int getShortedPath(int[,]G, int start, int end,int [] path)
         {
bool[] s = new bool[length]; // indicates that the shortest path between the starting node and the current node is found
int min; //Minimum distance temporary variable
int curNode=0; //Temporary node, record the currently calculated node
             int[] dist = new int[length];
             int[] prev = new int[length];

//Initial node information
             for (int v = 0; v < length; v++)
             {
                 s[v] = false;
                 dist[v] = G[start, v];
                 if (dist[v] > MaxSize)
                     prev[v] = 0;
                 else
                     prev[v] = start;
             }
             path[0] = end;
             dist[start] = 0;
             s[start] = true;
//Main loop
             for (int i = 1; i < length; i++)
             {
                 min = MaxSize;
                 for (int w = 0; w < length; w++)
                 {
                     if (!s[w] && dist[w] < min)
                     {
                         curNode = w;
                         min = dist[w];
                     }
                 }

                 s[curNode] = true;
                 for (int j = 0; j < length; j++)
                     if (!s[j] && min + G[curNode, j] < dist[j])
                     {
                         dist[j] = min + G[curNode, j];
                         prev[j] = curNode;
                     }

             }
//Output path node
             int e = end, step = 0;
             while (e != start)
             {
                 step++;
                 path[step] = prev[e];
                 e = prev[e];
             }
             for (int i = step; i > step/2; i--)
             {
                 int temp = path[step - i];
                 path[step - i] = path[i];
                 path[i] = temp;
             }
             return dist[end];
         }

 

 

 
//From a certain source point, find the shortest path to all nodes
         static int[] getShortedPath(int[,] G, int start, int[,] path)
         {
int[] PathID = new int[length];//Path (denoted by number)
bool[] s = new bool[length]; // indicates that the shortest path between the starting node and the current node is found
int min; //Minimum distance temporary variable
int curNode = 0; //Temporary node, record the currently calculated node
             int[] dist = new int[length];
             int[] prev = new int[length];
//Initial node information
             for (int v = 0; v < length; v++)
             {
                 s[v] = false;
                 dist[v] = G[start, v];
                 if (dist[v] > MaxSize)
                     prev[v] = 0;
                 else
                     prev[v] = start;
                 path[v,0] = v;
             }

             dist[start] = 0;
             s[start] = true;
//Main loop
             for (int i = 1; i < length; i++)
             {
                 min = MaxSize;
                 for (int w = 0; w < length; w++)
                 {
                     if (!s[w] && dist[w] < min)
                     {
                         curNode = w;
                         min = dist[w];
                     }
                 }

                 s[curNode] = true;

                 for (int j = 0; j < length; j++)
                     if (!s[j] && min + G[curNode, j] < dist[j])
                     {
                         dist[j] = min + G[curNode, j];
                         prev[j] = curNode;
                     }

 
             }
//Output path node
             for (int k = 0; k < length; k++)
             {
                 int e = k, step = 0;
                 while (e != start)
                 {
                     step++;
                     path[k, step] = prev[e];
                     e = prev[e];
                 }
                 for (int i = step; i > step / 2; i--)
                 {
                     int temp = path[k, step - i];
                     path[k, step - i] = path[k, i];
                     path[k, i] = temp;
                 }
             }
             return dist;

         }

 
     }
 }