Presentation is loading. Please wait.

Presentation is loading. Please wait.

Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Greed: Shortest Path s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6.

Similar presentations


Presentation on theme: "Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Greed: Shortest Path s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6."— Presentation transcript:

1 Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Greed: Shortest Path s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6

2 2 1 3 8 2 6 7 4 5 Directed Graph Directed graph: G = (V, E). n V = set of vertices or nodes. n E  V  V = set of edges or arcs. n n = |V|, m = |E|. n Directed path: s - 2 - 3 - 5 - t. – simple n Directed cycle: 5 - 4 - 3 - 5.

3 3 Networks communication Network telephone exchanges, computers, satellites NodesArcs cables, fiber optics, microwave relays Flow voice, video, packets circuits gates, registers, processors wirescurrent mechanicaljointsrods, beams, springsheat, energy hydraulic reservoirs, pumping stations, lakes pipelinesfluid, oil financialstocks, currencytransactionsmoney transportation airports, rail yards, street intersections highways, railbeds, airway routes freight, vehicles, passengers

4 4 Shortest Path Network Shortest path network: (V, E, s, t, c). n Directed graph (V, E). n Source s  V, sink t  V. n Arc costs c(v, w). n Cost of path = sum of arc costs in path. Cost of path s - 2 - 3 - 5 - t = 9 + 23 + 2 + 16 = 48. s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6

5 5 Shortest Path Shortest path problem. (CLR 25.1-25.2) n Shortest path network (V, E, s, t, c). n Find shortest directed path from s to t. Assumptions. n Network contains directed path from s to every other node. n Network does not contain a negative cost cycle. Application. n Online directions. 3 4 5 -6 7 -4

6 6 Shortest Path: Existence Existence. If some path from s to v contains a negative cost cycle, there does not exist a shortest path. Otherwise, there exists a shortest s-v that is simple.  If negative cycle, can produce arbitrarily negative path by traversing cycle enough times.  If no negative cycle, can remove cycles without increasing cost. sv C c(C) < 0

7 7 Shortest Path: Properties Optimal substructure property. All sub-paths of shortest paths are shortest paths. n Let P 1 be x-y sub-path of shortest s-v path P. n Let P 2 be any x-y path. n c(P 1 )  c(P 2 ), otherwise P not shortest s-v path. Triangle inequality. n Let d*(v, w) be the length of the shortest path from v to w. n Then, d*(v, w)  d*(v, x) + d*(x, w) vw x sv P2P2 P1P1 y x

8 8 Dijkstra's Algorithm Upon termination. n  (v) = distance of shortest s-v path. n pred(v) gives shortest path. for each v  V  (v)   pred(v)  nil  (s)  0 S   init(Q) for each v  V insert(v, Q) while (Q   ) v = delete-min(Q) S  S  {v} for each w s.t (v,w)  E if  (w) >  (v) + c(v,w)  (w)   (v) + c(v,w) pred(w)  v Dijkstra's Algorithm decrease-key

9 9 Dijkstra's Algorithm: Proof of Correctness Invariant. For each vertex v  S,  (v) = d*(s, v). n Proof by induction on |S|. n Base case: |S| = 0 is trivial. n Induction step: – suppose Dijkstra's algorithm adds vertex v to S –  (v) is the length of the some path from s to v – if  (v) is not the length of the shortest s-v path, then let P* be a shortest s-v path – P* must use an edge that leaves S, say (x, y) – then  (v)> d*(s, v) assumption = d*(s, x) + d(x, y) + d*(y, v)optimal substructure  d*(s, x) + d(x, y) nonnegative lengths =  (x) + d(x, y) inductive hypothesis   (y) algorithm so Dijkstra's algorithm would have selected y instead of v S s y v x P*

10 10 Dijkstra 1 make-heap n insert n delete-min m decrease-key Priority Queues and Heaps (CLR 20, 21) make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 N 1 Binomial log N 1 Fibonacci * 1 1 log N 1 1 1 Relaxed 1 1 log N 1 1 1 Linked List 1 N N 1 1 N n (n) + m(1) = O(n 2 ) n (log n) + m(log n) = O(m log n) n (log n) + m(1) = O(m + n log n) is-empty11111 Heaps

11 11 Shortest Path Extensions Variants of shortest path: n Undirected graph. – O(m + n) using Thorup's algorithm n Negative weights but no negative cycles. – O(mn) using Bellman-Ford n Unit weights. – O(m + n) using breadth first search n Integer weights between 0 and constant C. n DAGs. – O(m + n) using topological sort n All-pairs. – O(n 3 ) using Floyd-Warshall – O(mn + n log log n) using Pettie's algorithm

12 Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Shortest Path: Extra Slides

13 13 Shortest Path: Proving Optimality How can we verify that a given solution is really optimal? s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 932 14 15 34 50 45 0

14 14 Shortest Path: Proving Optimality How can we verify that a given solution is really optimal? n Easy if all weights nonnegative, and there exists a zero cost path. s 3 t 2 6 7 4 5 0 0 0 0 0 0 4 10 1 1 0 0 19 1 1


Download ppt "Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Greed: Shortest Path s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6."

Similar presentations


Ads by Google