C#实现的最短路径分析,转载的
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Program 9 { 10 static int length = 6; 11 static string[] shortedPath = new string[length]; 12 static int noPath = 2000; 13 static int MaxSize = 1000; 14 static int[,] G = 15 { 16 { noPath, noPath, 10, noPath, 30, 100 }, 17 { noPath, noPath, 5, noPath, noPath, noPath }, 18 { noPath, noPath, noPath, 50, noPath, noPath }, 19 { noPath, noPath, noPath, noPath, noPath, 10 }, 20 { noPath, noPath, noPath, 20, noPath, 60 }, 21 { noPath, noPath, noPath, noPath, noPath, noPath } 22 }; 23 static string[] PathResult = new string[length]; 24 25 static int[] path1 = new int[length]; 26 static int[,] path2 = new int[length, length]; 27 static int[] distance2 = new int[length]; 28 29 static void Main(string[] args) 30 { 31 int dist1 = getShortedPath(G, 0, 1, path1); 32 Console.WriteLine("点0到点5路径:"); 33 for (int i = 0; i < path1.Length; i++) 34 Console.Write(path1[i].ToString() + " "); 35 Console.WriteLine("长度:" + dist1); 36 37 38 Console.WriteLine("\r\n-----------------------------------------\r\n"); 39 40 int[] pathdist = getShortedPath(G, 0, path2); 41 Console.WriteLine("点0到任意点的路径:"); 42 for (int j = 0; j < pathdist.Length; j++) 43 { 44 Console.WriteLine("点0到" + j + "的路径:"); 45 for (int i = 0; i < length; i++) 46 Console.Write(path2[j, i].ToString() + " "); 47 Console.WriteLine("长度:" + pathdist[j]); 48 } 49 Console.ReadKey(); 50 51 } 52 53 54 //从某一源点出发,找到到某一结点的最短路径 55 static int getShortedPath(int[,]G, int start, int end,int [] path) 56 { 57 bool[] s = new bool[length]; //表示找到起始结点与当前结点间的最短路径 58 int min; //最小距离临时变量 59 int curNode=0; //临时结点,记录当前正计算结点 60 int[] dist = new int[length]; 61 int[] prev = new int[length]; 62 63 //初始结点信息 64 for (int v = 0; v < length; v++) 65 { 66 s[v] = false; 67 dist[v] = G[start, v]; 68 if (dist[v] > MaxSize) 69 prev[v] = 0; 70 else 71 prev[v] = start; 72 } 73 path[0] = end; 74 dist[start] = 0; 75 s[start] = true; 76 //主循环 77 for (int i = 1; i < length; i++) 78 { 79 min = MaxSize; 80 for (int w = 0; w < length; w++) 81 { 82 if (!s[w] && dist[w] < min) 83 { 84 curNode = w; 85 min = dist[w]; 86 } 87 } 88 89 s[curNode] = true; 90 for (int j = 0; j < length; j++) 91 if (!s[j] && min + G[curNode, j] < dist[j]) 92 { 93 dist[j] = min + G[curNode, j]; 94 prev[j] = curNode; 95 } 96 97 } 98 //输出路径结点 99 int e = end, step = 0; 100 while (e != start) 101 { 102 step++; 103 path[step] = prev[e]; 104 e = prev[e]; 105 } 106 for (int i = step; i > step/2; i--) 107 { 108 int temp = path[step - i]; 109 path[step - i] = path[i]; 110 path[i] = temp; 111 } 112 return dist[end]; 113 } 114 115 116 117 118 119 120 //从某一源点出发,找到到所有结点的最短路径 121 static int[] getShortedPath(int[,] G, int start, int[,] path) 122 { 123 int[] PathID = new int[length];//路径(用编号表示) 124 bool[] s = new bool[length]; //表示找到起始结点与当前结点间的最短路径 125 int min; //最小距离临时变量 126 int curNode = 0; //临时结点,记录当前正计算结点 127 int[] dist = new int[length]; 128 int[] prev = new int[length]; 129 //初始结点信息 130 for (int v = 0; v < length; v++) 131 { 132 s[v] = false; 133 dist[v] = G[start, v]; 134 if (dist[v] > MaxSize) 135 prev[v] = 0; 136 else 137 prev[v] = start; 138 path[v,0] = v; 139 } 140 141 dist[start] = 0; 142 s[start] = true; 143 //主循环 144 for (int i = 1; i < length; i++) 145 { 146 min = MaxSize; 147 for (int w = 0; w < length; w++) 148 { 149 if (!s[w] && dist[w] < min) 150 { 151 curNode = w; 152 min = dist[w]; 153 } 154 } 155 156 s[curNode] = true; 157 158 for (int j = 0; j < length; j++) 159 if (!s[j] && min + G[curNode, j] < dist[j]) 160 { 161 dist[j] = min + G[curNode, j]; 162 prev[j] = curNode; 163 } 164 165 166 } 167 //输出路径结点 168 for (int k = 0; k < length; k++) 169 { 170 int e = k, step = 0; 171 while (e != start) 172 { 173 step++; 174 path[k, step] = prev[e]; 175 e = prev[e]; 176 } 177 for (int i = step; i > step / 2; i--) 178 { 179 int temp = path[k, step - i]; 180 path[k, step - i] = path[k, i]; 181 path[k, i] = temp; 182 } 183 } 184 return dist; 185 186 } 187 188 189 } 190 }