无向图求最短路径 迪杰斯特拉(dijkstra)算法实现

Dijkstra算法说明  http://ibupu.link/?id=29
namespace ConsoleApp14
{
    class Program
    {
        public static  int M = -1;
        static void Main(string[] args)
        {

            //源点
            int start = 0;
            int sVertet= start;
            int vCount = 6;
	    //图的邻接矩阵
            int[,] map = new int[6,6] {
                {0,7,9,M,M,14 }, //0点到其它点距离
                {7,0,10,15,M,M}, 
                {9,10,0,11,M,2 },
                {M,15,11,0,6,M },
                {M,M,M,6,0,9 },
                {14,M,2,M,9,0 }
            };
            //上一次最短距离 
            int[] lastMinEdge = new int[vCount];
            //当前最短距离
            int[] currentMinEdge= new int[vCount];
            //已经求得的最短距离
            int[] tColle = new int[vCount];

            //置为M ,M标记未记录的结点
            for (int i = 0; i < tColle.Length; i++)
            {
                tColle[i] = M;
            }
            //置为M ,M代表未使用
            for (int i = 0; i < lastMinEdge.Length; i++)
            {
                lastMinEdge[i] = M;
            }

            //将源点加入已记录的节点
            tColle[sVertet] = 0;
          
            while (true)
            {
                for (int i = 0; i < 6; i++)
                {
                    currentMinEdge[i] = M;
                    //未处理的顶点,
                    if (tColle[sVertet] != M && i != sVertet && tColle[i]==M)
                    {
                        //领接边距离
                        if (map[sVertet, i] != M)
                        {
                            currentMinEdge[i] = tColle[sVertet] + map[sVertet, i];
                        }
                        else

                        {
                            //非领接边 距离为M
                            currentMinEdge[i] = M;
                        }
                    }
                }
          
                //与上一轮比较
                for (int i = 0; i < 6; i++)
                {
                    if (lastMinEdge[i]!=M && tColle[i]==M)
                    {
                        if (currentMinEdge[i]!=M)
                        {
                            currentMinEdge[i] = lastMinEdge[i] < currentMinEdge[i] ? lastMinEdge[i] : currentMinEdge[i];
                        }
                        else
                        {
                            currentMinEdge[i] = lastMinEdge[i];
                        }
                    }
                  
                }


                //取得新结点最小路径
                sVertet = FindMinVertetIndex(currentMinEdge);
                tColle[sVertet] = currentMinEdge[sVertet];
                //保存当前记录
                for (int i = 0; i < currentMinEdge.Length; i++)
                {
                    if (tColle[i]==M)
                    {
                        lastMinEdge[i] = currentMinEdge[i];
                    }
                    else
                    {
                        lastMinEdge[i] = M;
                    }
                }

                Console.WriteLine("{0}到{1}的最短路径:{2}", start+1, sVertet + 1,tColle[sVertet]);


                if (tColle.Count(temp => temp == M) == 0)
                {
                    break;
                }
            }

            Console.ReadKey();
            Console.WriteLine();

        }


        private static int FindMinVertetIndex(int[] a)
        {

            int min = a.Where(temp=>temp!=M && temp>0).Min();
            int index = 0;

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i]==min)
                {
                    index = i;
                }
            }
            return index;
        }
    }
}

你可能感兴趣的:(无向图求最短路径 迪杰斯特拉(dijkstra)算法实现)