【发布时间】:2011-02-10 23:20:52
【问题描述】:
我正在尝试使用此逻辑来了解 adjacency matrix 发生了什么,但我很困惑它所说的关于 a b c d 的间隔.....
谁能解释这里发生了什么?
谢谢 (标记为 java 作为它向我们演示的语言,所以如果有人发布任何代码示例,他们可以看到它是用该语言编写的)
http://compprog.wordpress.com/2007/11/15/all-sources-shortest-path-the-floyd-warshall-algorithm/
代码如下:
for (k = 0; k < n; ++k) {
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
/* If i and j are different nodes and if
the paths between i and k and between
k and j exist, do */
if ((dist[i][k] * dist[k][j] != 0) && (i != j))
/* See if you can't get a shorter path
between i and j by interspacing
k somewhere along the current
path */
if ((dist[i][k] + dist[k][j] < dist[i][j]) ||
(dist[i][j] == 0))
dist[i][j] = dist[i][k] + dist[k][j];
【问题讨论】:
-
@stan:Floyd-Warshall 是典型的“DP 算法”之一,还有 Levenhstein 的编辑距离和“0-1 背包”。要理解它,您需要了解“动态编程”是什么(大多数没有 CS 学位的程序员对 DP 一无所知)。关于这个主题的维基百科条目很好:en.wikipedia.org/wiki/Dynamic_programming,否则你可以尝试参加一些在线比赛(如 TopCoder),通常很多问题都需要 DP 解决方案。
标签: java algorithm revision floyd-warshall