Find the shortest path with the least number of edges

前端 未结 5 456
渐次进展
渐次进展 2021-01-18 07:48

I need to modify Dijkstra\'s algorithm so that if there are several shortest paths I need to find the one with minimum number of edges on the path.

I\'ve been stuck

5条回答
  •  悲&欢浪女
    2021-01-18 08:22

    inspired by BlueRaja

    public void updateAdjacencyMatrix()
    {
            epsilon = 0.001
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                if (adjacencyMatrix[i, j] != 0)
                    adjacencyMatrix[i, j] = adjacencyMatrix[i, j] + epsilon;
            }
        }
    }
    

    then call updateAdjacencyMatrix() in your normal Dijkstra's implementation

提交回复
热议问题