170 likes | 265 Views
6.006- Introduction to Algorithms. Lecture 15 Prof. Silvio Micali. Shortest Paths in a Graph . v 1. v 3. v 5. 4. –2. –5. 1. v 2. v 4. ( p ) = –2. length of edge length of path Given a “source” path from to with You can get from to in length distance from to
E N D
6.006- Introduction to Algorithms Lecture15 Prof. Silvio Micali
Shortest Paths in a Graph v1 v3 v5 4 –2 –5 1 v2 v4 (p) = –2 • length of edge • length of path • Given a “source” • path from to with • You can get from to in length • distance from to • Length of a shortest path from to a
Recall: Dijstra’s Algorithm Distances from when
8 9 13 9 4 T 4 6 5 5 8 3 2 9 4 3 4 P 7 0
Today: Shortest Paths (from ) in Digraphs with General Edge Length Makes sense when G has no negative cycles!
Notation • length of edge • length of path • Given a “source” • path from to with • distance from to • predecessor of on a best path so far • (initially, and
d[s] 0 • π[s] s • for each vÎV – {s} • dod[v] ¥ • π[v] nil initialization while there is an edge (u, v) ÎEs.t. d[v] > d[u] + (u, v) do selectarbitrarily one such edge set d[v] d[u] + (u, v) π[v] u endwhile Relaxation (Improvement) Step A generic start
What if no negative cycle .... 4 2 1 v1 4 v2 4 v3 2 v4 2 v5 1 v6 1 v7 8 10 12 13 14 4 13 10 11 12 11 4 6 8 9 10 9 6 7 8 7
What if no negative cycle .... 4 2 1 v1 4 v2 4 v3 2 v4 2 v5 1 v6 1 v7 Analysis = # of relaxations number of vertices Need to be carefulhow you relax!
HOW? (Bellman Ford) • Arbitrarily fix an ordering of the edges: Until no improvement found do: IFis such that PASS Then Cost of one PASS = How many PASSES ?
Let this be shortest path from to 4 -7 +3 -2 19 0 4 -3 0 -2 17 1st Pass 2nd Pass 3rd Pass 4th Pass 5th Pass Ford’s Total Complexity = And if G=(V,E) had cycles?
Optimal substructure Theorem. A subpath of a shortest path is a shortest path. Proof. By contradiction ...
Proof. d(u, v) u v d(u, x) d(x, v) x Triangle inequality Theorem. For all u, v, xÎV, we have d(u, v) £d(u, x) +d(x, v).
Combinatorics vs. Combinatorial Optimization v1 s vn v2 …