图算法-最小路径

1、最小路径算法之Dijkstra算法


void ShortestPath_Dijkstra(int startVertexNum, int[] shortestPath, int[] shortestDistance)//根据Dijkstra法计算距离指定顶点的最短路径
        {
            bool[] isDone=new bool[vertexNums];
            //初始化基本信息,包括最短路径信息,最短距离信息,是否找到最短距离信息
            for (int i = 0; i < vertexNums; i++)
            {
                shortestDistance[i] = edges[i, startVertexNum].weight;
                if (shortestDistance[i] < int.MaxValue)
                    shortestPath[i] = startVertexNum;//将最短路径初始化为原点号
                isDone[i] = false;
            }
            shortestDistance[startVertexNum] = 0;
            isDone[startVertexNum] = true;

            //开始主循环,每次求得起始点到各点的最短路径
            for (int i = 1; i < vertexNums; i++)
            {
                int min = int.MaxValue;
                int minIndex = 0;

                for (int j = 0; j < vertexNums; j++)
                {
                    if (!isDone[j] && shortestDistance[j] < min)
                    {
                        min = shortestDistance[j];
                        minIndex = j;
                    }
                }
                isDone[minIndex] = true;

                //以当前找到最短路径的顶点为中间点,修改相关点权值
                for (int j = 0; j < vertexNums; j++)
                {
                    if (!isDone[j] && min + edges[minIndex, j].weight < shortestDistance[j])
                    {
                        shortestDistance[j] = min + edges[minIndex, j].weight;
                        shortestPath[j] = minIndex;
                    }
                }
            }
        }


你可能感兴趣的:(算法,C#,图,dijkstra,最小路径)