BellmanFord Algorithm CSCI 385 Data Structures Analysis of
Bellman-Ford Algorithm CSCI 385 Data Structures & Analysis of Algorithms Lecture note Sajedul Talukder
Single-Source Shortest Path Problem • Weights on edges represent costs. • A weighted directed graph: • The cost of a path is the sum of the weights along that path. s 3 32 1 • A shortest path from s to t is a directed path from s to t with the smallest cost. • The single-source shortest path problem is to find the shortest path from s to v for all v in the graph. u 5 1 a This is a path from s to t of cost 22. 16 b v 13 21 2 t This is a path from s to t of cost 10. It is the shortest path from s to t.
Last time • Dijkstra’s algorithm! • Solves single-source shortest path in weighted graphs. s u 3 1 1 5 b a 21 16 t v 13 2
One drawback to Dijkstra • Might not work with negative edge weights s u 3 -1 5 Why would we ever have negative weights? -1 b a 13 21 16 v t -2 • Negative costs might mean benefits. • eg, it costs me -$2 when I get $2.
Toda y • Bellman-Ford algorithm • Another single-source shortest path algorithm • This is an example of dynamic programming • We’ll see what that means • Slower (but arguably simpler) than Dijkstra’s algorithm. • Works with negative edge weights.
Bellman-ford Algorithm • Applies as long as there are no nonpositive (negative) weight cycles • If there are cycles of weight 0 or less, then the shortest paths are not well defined E. g. , 4 s v 11 8 -7 -7 What is the shortest path from s to v? • The problem is due to the presence of a cycle C, reachable by the source, whose total weight is negative C is called a negative-weight cycle
Algorithm description • Every node v maintains a shortest path weight estimate, d(v) • These estimates are initialized to infinity, for all vertices except source, s, d(s)=0 • Every node repeatedly updates its shortest path estimate as follows w(e 1)=1 0 8 8 − 1 w(e 2)=− 3 w(e 4)=2 v 2 w(e 5)=4 u 8 s 8 d(s) = 0 w(e 3)=1 1
Bellman-Ford algorithm - Example 5 A C 2 1 1 E 2 -3 B Vertex -2 2 D A B C D E Shortest distance from A Previous vertex
Bellman-Ford algorithm - Example Iterate |V|− 1 times 5 A Vertex C 2 1 -2 2 D A E 2 -3 B 1 B C D E Shortest distance from A Previous vertex
Bellman-Ford algorithm - Example 5 A C 2 1 E 2 -3 B 1 -2 2 D Vertex Shortest distance from A Previous vertex A 0 B 2 C C 5 A D 0 B E 2 D
Bellman-Ford Algorithm Bellman-Ford (G, s) Input: A digraph G, weights c: E(G) → R+ and a vertex s V(G). Output: Shortest paths from s to all v V(G) and their lengths. 1. 2. 3. 4. 5. 6. 7. 8. // G = (V, E) is a weighted directed graph; edge weights may be negative Initialize-Single-Source ( G, s ) for i ← 1 to |V | − 1 for each edge (u, v) in E Relax(u, v) // Check for negative length cycles: for each edge (u, v) in E if d(u) + w(u, v) < d(v) // there is an edge that could still be relaxed then d(u) + w(u, v) = d(v) previous(v) = u
Bellman-Ford Algorithm Complexity Bellman-Ford (G, s) Input: A digraph G, weights c: E(G) → R+ and a vertex s V(G). Output: Shortest paths from s to all v V(G) and their lengths. 1. 2. 3. 4. 5. 6. 7. 8. // G = (V, E) is a weighted directed graph; edge weights may be negative Initialize-Single-Source ( G, s ) for i ← 1 to |V | − 1 O(V) for each edge (u, v) in E O(E) Relax(u, v) // Check for negative length cycles: for each edge (u, v) in E O(E) if d(u) + w(u, v) < d(v) // there is an edge that could still be relaxed then d(u) + w(u, v) = d(v) previous(v) = u The time complexity: O(V) * O(E)
Bellman-Ford is also used in practice. • eg, Routing Information Protocol (RIP) uses something like Bellman-Ford. • Older protocol, not used as much anymore. • Each router keeps a table of distances to every other router. • Periodically we do a Bellman-Ford update. • This also means that if there are changes in the network, this will propagate. (maybe slowly…) Destination Cost to get there Send to whom? 172. 16. 1. 0 34 172. 16. 1. 1 10. 20. 40. 1 10 192. 168. 1. 2 10. 155. 120. 1 9 10. 13. 50. 0
- Slides: 13