// 建構子
 public Floyd(int[,] Weight_Path, int number) : base(Weight_Path, number)
 {
     cost    = new int[number][];
     capcity = Graph_Matrix.GetLength(0);
     for (int i = 0; i < capcity; i++)
     {
         cost[i] = new int[number];
     }
 }
 // 所有頂點兩兩之間的最短距離
 public void ShortestPath()
 {
     for (int i = 1; i < Graph_Matrix.GetLength(0); i++)
     {
         for (int j = i; j < Graph_Matrix.GetLength(0); j++)
         {
             cost[i][j] = cost[j][i] = Graph_Matrix[i, j];
         }
     }
     for (int k = 1; k < Graph_Matrix.GetLength(0); k++)
     {
         for (int i = 1; i < Graph_Matrix.GetLength(0); i++)
         {
             for (int j = 1; j < Graph_Matrix.GetLength(0); j++)
             {
                 if (cost[i][k] + cost[k][j] < cost[i][j])
                 {
                     cost[i][j] = cost[i][k] + cost[k][j];
                 }
             }
         }
     }
     Write("頂點 vex1 vex2 vex3 vex4 vex5 vex6\n");
     for (int i = 1; i < Graph_Matrix.GetLength(0); i++)
     {
         Write("vex" + i + " ");
         for (int j = 1; j < Graph_Matrix.GetLength(0); j++)
         {
             // 調整顯示的位置, 顯示距離陣列
             if (cost[i][j] < 10)
             {
                 Write(" ");
             }
             if (cost[i][j] < 100)
             {
                 Write(" ");
             }
             Write(" " + cost[i][j] + " ");
         }
         WriteLine();
     }
 }
Exemple #3
0
        // 單點對全部頂點最短距離
        public void ShortestPath(int source)
        {
            int shortest_distance;
            int shortest_vertex = 1;
            int i, j;

            for (i = 1; i < Graph_Matrix.GetLength(0); i++)
            {
                cost[i] = Graph_Matrix[source, i];
            }
            selected[source] = 1;
            cost[source]     = 0;
            for (i = 1; i < Graph_Matrix.GetLength(0) - 1; i++)
            {
                shortest_distance = INFINITE;
                for (j = 1; j < Graph_Matrix.GetLength(0); j++)
                {
                    if (shortest_distance > cost[j] && selected[j] == 0)
                    {
                        shortest_vertex   = j;
                        shortest_distance = cost[j];
                    }
                }
                selected[shortest_vertex] = 1;
                for (j = 1; j < Graph_Matrix.GetLength(0); j++)
                {
                    if (selected[j] == 0 &&
                        cost[shortest_vertex] + Graph_Matrix[shortest_vertex, j] < cost[j])
                    {
                        cost[j] = cost[shortest_vertex] + Graph_Matrix[shortest_vertex, j];
                    }
                }
            }
            WriteLine("==================================");
            WriteLine("頂點1到各頂點最短距離的最終結果");
            WriteLine("==================================");
            for (j = 1; j < Graph_Matrix.GetLength(0); j++)
            {
                WriteLine("頂點1到頂點" + j + "的最短距離= " + cost[j]);
            }
        }