最短路径算法

松弛技术:

RELAX(u,v,w)
if(d[v]>d[u]+w(u,v))
{
d[v]=d[u]+w(u,v);
p[v]=u;
}

松弛是改变最短路径和前趋的唯一方式。算法之间的区别在于对每条边进行松弛的次数,以及对边执行松弛操作的次序有所不同。

Bellman–Ford is in its basic structure very similar to Dijkstra's algorithm, but instead of greedily selecting the minimum-weight node not yet processed to relax, it simply relaxes all the edges, and does this |V | − 1 times, where |V | is the number of vertices in the graph. The repetitions allow minimum distances to accurately propagate throughout the graph, since, in the absence of negative cycles, the shortest path can only visit each node at most once. Unlike the greedy approach, which depends on certain structural assumptions derived from positive weights, this straightforward approach extends to the general case.

Bellman–Ford runs in O(|V|·|E|) time, where |V| and |E| are the number of vertices and edges respectively.

BELLMAN-FORD(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 for i ← 1 to |V[G]| - 1
3 do for each edge (u, v) ∈ E[G]
4 do RELAX(u, v, w)
5 for each edge (u, v) ∈ E[G]
6 do if d[v] > d[u] + w(u, v)
7 then return FALSE
8 return TRUE

 

有向无回路图中的单源最短路径DAG-SHORTEST-PATHS

DAG-SHORTEST-PATHS(G, w, s)
1 topologically sort the vertices of G
2 INITIALIZE-SINGLE-SOURCE(G, s)
3 for each vertex u, taken in topologically sorted order
4 do for each vertex v ∈ Adj[u]
5 do RELAX(u, v, w)

 

DIJKSTRA(G, w, s)
1 INITIALIZE-SINGLE-SOURCE(G, s)
2 S ← Ø
3 Q ← V[G]
4 while Q ≠ Ø
5 do u ← EXTRACT-MIN(Q)
6 S ← S ∪{u}
7 for each vertex v ∈ Adj[u]
8 do RELAX(u, v, w)

这个算法解决的问题是单源的,有向有权图,且权值不为负值。

由于总是从距离源点s最近的点开始向外扩展,所以后扩展的节点到s距离一定比先前的节点大,所以后来的节点的距离值的更新不会影响到先前的节点的最短距离值。



posted @ 2012-02-17 14:18  ITfresh  阅读(249)  评论(0编辑  收藏  举报