最短路徑之Floyd演算法

何畢之發表於2018-06-11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 最短路徑之Floyd演算法
{
    class Program
    {
        public static readonly int M = 999999;//表示不可到達距離

        static int[,] map = new int[,] {
                { 0, 6, 3, M,M,M },
                { 6, 0, 2, 5,M,M },
                { 3, 2, 0, 3, 4, M },
                { M, 5, 3, 0, 2, 3 },
                { M,M, 4, 2, 0, 5 },
                { M,M,M, 3, 5, 0 }
            };//路徑圖

        static int places = (int)Math.Sqrt(map.Length);//獲取地點數;
        static int[,] path = new int[places, places];//路徑
        static void Main(string[] args)
        {
            Console.WriteLine("原路徑圖:");
            DisplayMap();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("計算後的路徑圖:");
            floyd_alg();
            DisplayMap();
            Console.WriteLine();
            Console.WriteLine();

            displayResult();

            Console.ReadKey();
        }

        /// <summary>
        /// 表示A-E六個地點
        /// </summary>
        public enum Place
        {
            A = 0,
            B = 1,
            C = 2,
            D = 3,
            E = 4,
            F = 5
        }

        /// <summary>
        /// 將原矩陣顯示
        /// </summary>
        public static void DisplayMap()
        {
            Console.Write("    ");
            for (int i = 0; i < places; i++)
            {
                Console.Write((Place)i + "   ");
            }
            Console.Write("\r\n");
            for (int i = 0; i < places; i++)
            {
                Console.Write((Place)i + "   ");
                for (int j = 0; j < places; j++)
                {
                    if (map[i, j] == M)
                        Console.Write("M   ");
                    else if (map[i, j] < 0)
                        Console.Write(map[i, j] + "  ");
                    else
                        Console.Write(map[i, j] + "   ");
                }
                Console.Write("\r\n");
            }
        }

        /// <summary>
        /// Flody演算法
        /// </summary>
        public static void floyd_alg()
        {
            //初始化路徑
            for (int i = 0; i < places; i++)
                for (int j = 0; j < places; j++)
                    path[i, j] = -1;

            for (int k = 0; k < places; k++)
                for (int i = 0; i < places; i++)
                    for (int j = 0; j < places; j++)
                        if (map[i, j] > (map[i, k] + map[k, j]))
                        {
                            map[i, j] = map[i, k] + map[k, j];
                            path[i, j] = k;
                        }
        }

        /// <summary>
        /// 輸出路徑和距離
        /// </summary>
        public static void displayResult()
        {

            for (int i = 0; i < places; i++)
            {
                string str = ((Place)i).ToString();
                int k = path[0, i];
                while (k != -1)
                {
                    str = (Place)k + "-" + str;
                    k = path[i, k];
                }
                Console.WriteLine((Place)0 + "-" + str + ";最短距離為:" + map[0, i]);
            }

        }
    }
}


相關文章