Content deleted Content added
m Should be using wikicode for pseudocode |
wikicode; finish proof |
||
Line 1:
The '''Bellman-Ford algorithm''' computes single-source shortest paths in a [[weighted graph]] (where some of the [[edge (graph theory)|edge]] weights may be negative). [[Dijkstra's algorithm]] accomplishes the same problem with a lower running time, but requires edges to be non-negative. Thus, Bellman-Ford is usually used only when there are negative edge weights.
Bellman Ford runs in O(VE) time, where V and E are the number of vertices and edges.
{{wikicode}}
''// Define datatypes for a graph''
'''record''' vertex {
''list'' edges
''real'' distance
for each vertex v in G other than s, ▼
''vertex'' predecessor
}
'''record''' edge {
for each edge (u,v) in G do▼
''node'' source
''node'' dest
''real'' weight
for each edge (u,r) in G do▼
}
'''function''' BellmanFord(''list'' vertices, ''list'' edges, ''vertex'' source)
''// This implementation takes in a graph, represented as lists of vertices''
''// and edges, and modifies the vertices so that their ''distance'' and''
''//'' predecessor ''attributes store the shortest paths.''
''// Step 1: Initialize graph''
'''if''' v '''is''' source '''then''' v.distance = 0
'''else''' v.distance = '''infinity'''
vertex.predecessor = '''null'''
''// Step 2: relax edges repeatedly
'''for''' i '''from''' 1 '''to''' size(vertices):
u := uv.source
v := uv.destination ''// uv is the edge from u to v''
'''if''' v.distance > u.distance + uv.weight
v.distance := u.distance + uv.weight
v.predecessor := u
''// Step 3: check for negative-weight cycles''
u := uv.source
v := uv.destination
'''if''' v.distance > u.distance + uv.weight
'''error''' "Graph contains a negative-weight cycle"
== Proof of correctness ==
Line 30 ⟶ 52:
* If there is a path from ''s'' to ''u'' with at most ''i'' edges, then Distance(u) is at most the length of the shortest path from ''s'' to ''u'' with at most ''i'' edges.
'''Proof'''. For the base case of induction, consider
For the inductive case, we first prove the first part. Consider a moment when
<code>v.distance := u.distance + uv.weight</code>. By inductive assumption, <code>u.distance</code> is the length of some path from ''source'' to ''u''. Then <code>u.distance + uv.weight</code> is the length of the path from ''source'' to ''v'' that follows the path from ''source'' to ''u'' and then goes to ''v''.
For the second part, consider the shortest path from ''
When ''i'' equals the number of vertices in the graph, each path will be the shortest path overall, unless there are negative-weight cycles. If a negative-weight cycle exists and is accessible from the source, then given any path, a shorter one exists, so there is no shortest path. Otherwise, the shortest path will not include any cycles (because going around a cycle would not make the path shorter), so each shortest path visits each vertex at most once, and its number of edges is less than the number of vertices in the graph. Therefore, each path stored in the graph by this point is the shortest path.
'''End of proof.'''▼
▲'''End of proof.'''
==Implementations==
|