Bellman-Ford
Bellman-Ford vs. Dijkstra
-
Dijkstra's algorithm:
- find the u with the smallest d[u]
- Update u's neighbors: d[v] = min(d[v], d[u] + w(u, v))
-
Bellman-Ford algorithm:
- Don't bother finding the u with the smallest d[u]
- Slower, but more robust than Dijkstra's algorithm
- Can handle negative edge weights (as long as there are no negative cycles)
- Can do updates in a decentralized setting
Bellman-Ford algorithm
- Initialize d[s] = 0, d[v] = +∞ for all v ≠ s
- for i = 0 to n-2:
- for each edge (u, v) ∈ E:
- [v] = min( , + w(u, v))
- for each edge (u, v) ∈ E:
- Now dist(s,v) = d[v] for all v ∈ V
= length of shortest path from s to v using at most i edges.
Correctness of Bellman-Ford
- Claim: After i iterations, = length of shortest path from s to v using at most i edges.
- Proof: By induction on i.
- Base case: i = 0
- = 0
- = +∞ for all v ≠ s
- Inductive step: i > 0
- Consider any edge (u, v) ∈ E
- = min( , + w(u, v))
- By induction, = length of shortest path from s to u using at most i-1 edges
- So = length of shortest path from s to v using at most i edges
- Base case: i = 0
Simple path
Assume there is not negative cycle.
- Then there is a shortest path from s to t, and moreover there is a simple shortest path from s to t.
- A simple path is a path with no repeated vertices.
What is dynamic programming?
-
A way of solving a complex problem by breaking it down into a collection of simpler subproblems, solving each of those subproblems just once, and storing their solutions.
-
Usually it is for solving optimization problems.
Elements of dynamic programiong:
-
optimal substructure
- optimal solution to the problem contains within it optimal solutions to subproblems
-
overlapping subproblems
- subproblems recur many times, this means that we can save time by solving a sub-problem just onece and storing the answer.
Two ways to implement dynamic programming:
-
Bottom up approach
- solve the small sub-problems first, then use the solutions to solve larger sub-problems until the entire problem is solved.
-
Top down approach
- start with the original problem, and recursively break it down into subproblems.
- Keep track of what small problems you've already solved so that you don't waste time solving the same problem twice.
Floyd-Warshall algorithm
- This is an algorithm for all-pairs shortest paths (APSP).
Naive algorithm:
- Run Bellman-Ford n times, once for each vertex as the source vertex.
- This takes time.
Optimal substructure:
-
Sub-problem(k-1): shortest path from i to j using only vertices in {1, 2, ..., k-1} as intermediate vertices.
-
Sub-problem(k): shortest path from i to j using only vertices in {1, 2, ..., k} as intermediate vertices.
- Case 1: Don't need vertex k,
- Case 2: Need vertex k,
Floyd-Warshall algorithm:
-
Initialize n-by-n arrays for k = 0, 1, ..., n
- for all pairs u, v
- for all u
- if (u,v) ∈ E
-
for k = 1 to n:
- for pairs u,v in V:
- for pairs u,v in V:
-
Now = length of shortest path from u to v
Running time: Space:
Comments
Post a Comment