Geeks面试题:Floyd Warshall Algorithm 所有顶点之间的最短路径问题

Floyd Warshall Algorithm

The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem. The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph.


Example:


Input:
       graph[][] = { {0,   5,  INF, 10},
                    {INF,  0,  3,  INF},
                    {INF, INF, 0,   1},
                    {INF, INF, INF, 0} }
which represents the following graph
             10
       (0)------->(3)
        |         /|\
      5 |          |
        |          | 1
       \|/         |
       (1)------->(2)
            3       
Note that the value of graph[i][j] is 0 if i is equal to j 
And graph[i][j] is INF (infinite) if there is no edge from vertex i to j.


Output:
Shortest distance matrix
      0      5      8      9
    INF      0      3      4
    INF    INF      0      1
    INF    INF    INF      0 
Floyd Warshall Algorithm
We initialize the solution matrix same as the input graph matrix as a first step. Then we update the solution matrix by considering all vertices as an intermediate vertex. The idea is to one by one pick all vertices and update all shortest paths which include the picked vertex as an intermediate vertex in the shortest path. When we pick vertex number k as an intermediate vertex, we already have considered vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i, j) of source and destination vertices respectively, there are two possible cases.
1) k is not an intermediate vertex in shortest path from i to j. We keep the value of dist[i][j] as it is.
2) k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as dist[i][k] + dist[k][j].


就是严慧敏的数据结构书上的图论介绍的所有顶点之间的最短路径问题。

不过书上没有说是使用动态规划法做这道题的,也就说的并不透切。

记得当年钻研这本书的时候,觉得本题十分难理解,半天没搞明白,然后后来反复记了本题的程序好几遍,始终没记住,终归没有彻底理解本题的思想精髓,那么是很难写出程序的。

利用动态规划法的思想做这道题,难度还不是特别大的。主要想清楚什么是从地往上(buttom-up)的方法,如下述:

For every pair (i, j) of source and destination vertices respectively, there are two possible cases.
1) k is not an intermediate vertex in shortest path from i to j. We keep the value of dist[i][j] as it is.
2) k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as dist[i][k] + dist[k][j].

为什么这样考虑是正确的呢?比如:
举例: 假设一个图1->9->20为最短路径?其最短路径进一步展开为1->15->9和9->13->20为最短路径的时候,那么其实根据本算法考虑的顺序并不是按照1->9->20来考虑的,而是考虑了1->15->20这条路径就考虑了1->9->20这条路径了,比如先考虑1->15路径; 15->13最短路径经过9,那么就会更新了15->9->13的最短路径,保存到15->13;15->20也会更新最短路径为15->13->20,保存到15->20,所以其实已经考虑了1->15->9->13->20这条路径的了。

这样去证明当然不是很正规,所以我十分佩服那些数学家,能做出如此严密的证明。

但是我觉得写程序,能想出正确的算法,并且以自己的思维验证正确就很不错了。

思想难,但是程序是很简单的:

vector<vector<int> > floydWarshell (vector<vector<int> > graph)
{
	vector<vector<int> > sol = graph;

	for (int i = 0; i < graph.size(); i++)
	{
		for (int j = 0; j < graph.size(); j++)
		{
			for (int k = 0; k < graph.size(); k++)
			{
				sol[j][k] = min(sol[j][k], sol[j][i]+sol[i][k]);
			}
		}
	}
	return sol;
}





Geeks面试题:Floyd Warshall Algorithm 所有顶点之间的最短路径问题

上一篇:Linux/Mac scp命令笔记


下一篇:大数据传统企业实施理性篇---请放慢你的步伐