Download Presentation

1 / 54
Download Presentation

資料結構與演算法 ( 下 ) - PowerPoint PPT Presentation

Download Presentation

資料結構與演算法 ( 下 )

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. 資料結構與演算法(下) 呂學一 (Hsueh-I Lu) http://www.csie.ntu.edu.tw/~hil/ Shortest Path Tree

2. Today • Shortest path and cycle • Shortest-Path Tree • Bellman-Ford • Lawler • Dijkstra Shortest Path Tree

3. The graph G = (V, E) is directed with edge length w. The length of each edge could be negative. The setting u 3 2 2 1 1 3 -3 1 2 v Shortest Path Tree

4. Shortest path and cycles Shortest Path Tree

5. Question • Can a shortest path between node u and v contain a cycle? u v Shortest Path Tree

6. Observation • If the graph admits a shortest path from u to v, then the graph admits a shortest path from u to v that has no cycle. u v Shortest Path Tree

7. Shortest-path tree problem Shortest Path Tree

8. The problem • Input: • A directed graph G = (V, E) with edge weight w. • A node r of G • Output: • A tree T rooted at r such that the path of T from r to each node u of T is a shortest path from r to u in G. Shortest Path Tree

9. Without loss of generality • We may assume that each node in the graph is reachable from r. • Why? • Unreachable nodes can be removed in linear time. Shortest Path Tree

10. Single-source shortest-path problem The root ris the “source”. Also known as 6 3 1 4 2 2 r 7 3 5 6 Shortest Path Tree

11. Question • Such a shortest-path tree always exists? Shortest Path Tree

12. Perhaps not.. A negative cycle -3 1 r 1 1 1 Shortest Path Tree

13. As a matter of fact • The input graph G has a shortest-path tree if and only if G does not contain any negative cycle. • The only-if direction is “obvious”. • If a node u belongs to a negative cycle, then there is no shortest-path from r to u. • Q: how about the other direction? Shortest Path Tree

14. G has no negative cycle • Removing cycles from a path does not increase it length. • For each node u of G, there has to be a shortest path from r to node u that do not contain any cycle. • The union of all these n shortest paths has to be a shortest-path tree of G rooted at r. Shortest Path Tree

15. An equivalent problem • The shortest-path tree problem is equivalent to finding the distance from r to each node u in graph G. • The distance from r to node u in G is the length of any shortest path from r to u in G. • By “equivalence” we mean that a solution to either problem can be obtained from a solution from the other problem in linear time. • Why? Shortest Path Tree

16. The rest of the slide • Let d(u) denote the distance from r to u in graph G. We focus only on computing the distance of each node u from r. • Without loss of generality, we may assume that the distance of each node from r is finite. • That is, each node is reachable from r, since we can remove unreachable nodes in linear time. Shortest Path Tree

17. Bellman-Ford Shortest Path Tree

18. Richard Bellman, 1920~1984 • Norbert Wiener Prize in Applied Mathematics, 1970 • Dickson Prize, Carnegie-Mellon University, 1970; • John von Neumann Theory Award, 1976. • IEEE Medal of Honor, 1979, • "For contributions to decision processes and control system theory, particularly the creation and application of dynamic programming." • Fellow of the American Academy of Arts and Sciences, 1975. • Membership in the National Academy of Engineering, 1977. Shortest Path Tree

19. Lester R. Ford, Jr. 1927~ • A important contributor to the theory of network flow. Shortest Path Tree

20. [ ] ( ) d d h d f F W i i t t t t t e u s e u o e s m a e u e s a n c e o u , f r o m r . l F I i i i i t t n a z a o n [ ] d f h d f L G t I e u 1 o r e a c n o e u o = . [ ] d L 0 t I e r = . h f l l f f ¯ l F R i i t t t t e p e a e o o w n g s e p o r s u c e n y n u m - b f h e r o p a s e s . [ ] ( ) h d f d I i t t t I m p r o v e e e s m a e u o r u . Sketch of Bellman-Ford Shortest Path Tree

21. ( ) l d F R i e a x n g e g e u v : ; [ ] [ ] ( ) f d d h I t + I > v u w u v e n ; [ ] [ ] ( ) l d d t + e v u w u v = ; . h f F A i t p a s e o m p r o v e m e n : ( ) h d f d F G I o r e a c e g e u v o o ; ( ) l d r e a x e g e u v ; . Improving the estimate Shortest Path Tree

22. 5 1 1 1 1 Illustration 6 3 1 4 2 2 r 7 3 5 6 Shortest Path Tree

23. 6 5 1 1 1 Illustration 6 3 1 4 2 2 r 7 3 5 6 Shortest Path Tree

24. 3 6 5 1 1 Illustration 6 3 1 4 2 2 r 7 3 5 6 Shortest Path Tree

25. [ ] ( ) h h h d d h l d F T i ¸ t t t r o u g o u e e x e c u o n v v o s , f d o r a n y n o e v . b h h f F L P t t t t t e e a s o r e s p a r o m r o s . d h b h f f F P P t t I o r a n y n o e u o n e s u p a o r o m , h b h h f t t t t t t r o u a s o e a s o r e s - p a r o m r o u . ( ) [ ] ( ) d f d d b f F P i I o r a n y e g e u v o n u u e o r e = ; , h f h f h i t t t t t s o m e p a s e o m p r o v e m e n e n a e r a , [ ] ( ) h f d d l h l d i t p a s e o m p r o v e m e n v v a s o o s = . Observations Shortest Path Tree

26. ( ) l d F R i e a x n g e g e u v : ; [ ] [ ] ( ) f d d h I t + I > v u w u v e n ; [ ] [ ] ( ) l d d t + e v u w u v = ; . h f F A i t p a s e o m p r o v e m e n : ( ) h d f d F G I o r e a c e g e u v o o ; ( ) l d r e a x e g e u v ; . Q: Need how many phases? Shortest Path Tree

27. A: n – 1 phases suffice! • If G contains no negative cycles, then each node u has a shortest path from r to u that has at most n – 1 edges. • Observe that after the first i phases of the improvement via relaxation, the estimate d[u] of d(u) for the first i + 1 nodes u in the path is precise. Shortest Path Tree

28. Illustration Shortest Path Tree

29. l F I i i i i t t n a z a o n ( ) l T O i i t [ ] d f h d f m e c o m p e x y m n L G t = I e u 1 o r e a c n o e u o = . . [ ] d L 0 t I e r = . F R i 1 t t ¡ e p e a n m e s : ( ) h d f F G I o r e a c e g e u v o ; [ ] ( [ ] [ ] ( ) ) d d d L i t + e v m n v u w u v = ; ; . Bellman-Ford Shortest Path Tree

30. Existence of negative cycles • Question: • How do we know G has negative cycles? Shortest Path Tree

31. Using another phase of improvement via relaxation • Run another phase of improvement of the estimate d[u] of d(u) for each node u of G via relaxing all edges of G. • If in the n-th phase, there are still some d[u] being modified, we know that G has negative cycles. Shortest Path Tree

32. i t n e g a v e b l f k d L C i t t e e a n e g a v e c y c e s o n o e s v v v k 1 2 ; ; : : : ; . [ ] l b l f h d f F I i i t t t t t o r n o a o n a r e v y e v v e v o r = k i 1 1 + , . l l k h d h f i i t t a v a r e n o c a n g e n a p a s e o m p r o v e m e n i , h t e n [ ] [ ] ( ) d d · + v v w v v i i i i 1 1 + + ; h l d f h k l l k l S i i i 1 2 o s o r e a c u m m n g a n e q u a - = ; ; : : : ; . k h h f d h f C i i i i t t t t t e s w e n o w a e s u m o e g e w e g s o s , d h h C i i i i i t t t t t t t n o n n e g a v e c o n r a c n g e a s s u m p o n a s , l i t a n e g a v e c y c e . Why? Shortest Path Tree

33. Summary • Bellman-Ford • Time: O(mn) • Detect whether the input graph has negative cycle. • If the input graph does not contain any negative cycle, the algorithm outputs d[u] = d(u) for each node u of G. • A shortest-path tree of G can be obtained from d(u) for all nodes u in O(m + n) time. Shortest Path Tree

34. If G is acyclic Can we speed up Bellman-Ford? Shortest Path Tree

35. i t n e g a v e No negative cycle! • Since the input graph has no cycle, it has no negative cycle. Shortest Path Tree

36. Eugene Lawler • 1933-1994 Shortest Path Tree

37. One phase suffices • We first perform a topological sort on the input directed acyclic graph. • For i = 1 to n • Let vibe the i-th node in the above order. • Relax each outgoing edge (vi, u) from vi. • The running time is O(m + n). Shortest Path Tree

38. Illustration r Shortest Path Tree

39. Correctness Shortest Path Tree

40. Summary • If the input graph is a DAG, then it takes linear time to compute d(u) of each node u. • Therefore, it also takes linear time to compute a shortest-path tree for G. Shortest Path Tree

41. If w is non-negative Can we speed up Bellman-Ford? Shortest Path Tree

42. No negative cycle! • Since the input graph has no negative edge, it has no negative cycle. Shortest Path Tree

43. Edsger W. Dijkstra • Dutch, 1930 ~ 2002 • Turing Award, 1972 • “Goto is considered harmful.” • Inventor of P/V semaphores • to resolve the “dining philosopher problem” Shortest Path Tree

44. Dijkstra’s algorithm (sketch) • We cannot rely on topological sort, since G may contain cycles. • Still, one phase suffices! • Same initialization for d[u] for all nodes u. • The algorithm has n iterations: • Each iteration relaxes the outgoing edges of an unprocessed node u whose d[u] is smallest among all unprocessed nodes. Shortest Path Tree

45. Illustration 1 ∞ ∞ 10 3 9 2 0 4 6 7 5 ∞ ∞ 2 Shortest Path Tree

46. Illustration 1 10 ∞ 10 3 9 2 0 4 6 7 5 5 ∞ 2 Shortest Path Tree

47. Illustration 1 8 14 10 3 9 2 0 4 6 7 5 5 7 2 Shortest Path Tree

48. Illustration 1 8 13 10 3 9 2 0 4 6 7 5 5 7 2 Shortest Path Tree

49. Illustration 1 8 9 10 3 9 2 0 4 6 7 5 5 7 2 Shortest Path Tree

50. Illustration 1 8 9 10 3 9 2 0 4 6 7 5 5 7 2 Shortest Path Tree