C# 实现Dijkstra算法

public void Dijkstra()
        {
            int [,] A = new int[6,6]
                {
                     {0,20,30,28,2048,2048 },
                     {2048,0,20,2048,30,2048},
                     {2048,2048,0,10,25,2048},
                     {2048,2048,2048,0,2048,32},
                     {2048,2048,2048,2048,0,15},
                     {2048,2048,2048,2048,2048,0}
                };
            int i = 0;
            int []TT = new int[6];
            int min = 0;
            string[] path = new string[6] {"","","","","",""};
            string thisPath = "";
            string allPath = "1";
            int z = 0;
            Console.Write("\n");
            Console.Write("          Dijkstra算法" + "\n \n");
            Console.Write("===========分析过程===========" + "\n \n");
            Console.Write("第1步:"+"\n");
            Console.Write("初始TT数组为:");
            for (int c = 0;c<6;c++)
            Console.Write(TT[c]+" ");
            Console.Write("\n");
            Console.Write("初始最小值为:" + min + "\n");
            Console.Write("初始查找路径为:" + "1" + "\n \n");
            while (z < 5)
            {
                if (i == 0)
                {
                    //查找第一列,初始化数组TT
                    for (int j = 0; j < 6; j++)
                    {
                        if (A[i, j] != 0 && A[i, j] != 2048)
                        {
                            TT[j] =A[i, j];
                            path[j] = (i + 1).ToString();
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < 6; j++)
                    {
                        if (A[i, j] != 0 && A[i, j] != 2048)
                        {
                            bool isAdd = false;
                            int w = i-1;
                            while(w >=0)
                            {
                                if (A[w, j] != 0 && A[w, j] != 2048)
                                {
                                    int tmp = A[i, j] + min;
                                    if (tmp < TT[j])
                                    {
                                        TT[j] = tmp;
                                        path[j] =(i + 1).ToString();
                                    }
                                    isAdd = true;
                                    break;
                                }
                                w--;
                            }
                            if (isAdd == false)
                            {
                                TT[j] = A[i, j] + min;
                                path[j] = (i + 1).ToString();
                            }
                        }
                    }
                }
                //查找TT数组中的最小值
                int temp = 0;
                Console.Write("第{0}步:" + "\n",z+2);
                Console.Write("当前TT数组为:");
                for (int t = 0; t < 6; t++)
                {
                    Console.Write(TT[t] + " ");
                    if (TT[t] != 0 && temp == 0)
                        temp = TT[t];
                    else if (TT[t] != 0 && TT[t] < temp)
                    {
                        temp = TT[t];
                    }
                    min = temp;
                }
                for (int t = 0; t < 6; t++)
                {
                    if (TT[t] == min)
                    {
                        thisPath = path[t];
                        TT[t] = 0;
                        i = t;
                    }
                }
                Console.Write("\n");
                Console.Write("当前TT数组中的非零最小值为:" + min + "\n");
                Console.Write("当前查找路径为:" + thisPath + "\n \n");
                allPath += "," + thisPath;
                z++;
            }
            Console.Write("===========分析结果===========" + "\n \n");
            Console.Write("最小路径值为:" + min+"\n");
            Console.Write("最终路径为:" + allPath + "\n");
        }

结果如下图:

C# 实现Dijkstra算法_第1张图片

你可能感兴趣的:(C#,Dijkstra算法,算法,dijkstra)