122 Views

Download Presentation
## 資料結構與演算法 ( 下 )

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**資料結構與演算法(下)**呂學一 (Hsueh-I Lu) http://www.csie.ntu.edu.tw/~hil/ Shortest Path Tree**Today**• Shortest path and cycle • Shortest-Path Tree • Bellman-Ford • Lawler • Dijkstra Shortest Path Tree**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**Shortest path and cycles**Shortest Path Tree**Question**• Can a shortest path between node u and v contain a cycle? u v Shortest Path Tree**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**Shortest-path tree problem**Shortest Path Tree**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**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**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**Question**• Such a shortest-path tree always exists? Shortest Path Tree**Perhaps not..**A negative cycle -3 1 r 1 1 1 Shortest Path Tree**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**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**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**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**Bellman-Ford**Shortest Path Tree**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**Lester R. Ford, Jr. 1927~**• A important contributor to the theory of network flow. Shortest Path Tree**[**] ( ) 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**(**) 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**5**1 1 1 1 Illustration 6 3 1 4 2 2 r 7 3 5 6 Shortest Path Tree**6**5 1 1 1 Illustration 6 3 1 4 2 2 r 7 3 5 6 Shortest Path Tree**3**6 5 1 1 Illustration 6 3 1 4 2 2 r 7 3 5 6 Shortest Path Tree**[**] ( ) 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**(**) 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**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**Illustration**Shortest Path Tree**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**Existence of negative cycles**• Question: • How do we know G has negative cycles? Shortest Path Tree**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**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**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**If G is acyclic**Can we speed up Bellman-Ford? Shortest Path Tree**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**Eugene Lawler**• 1933-1994 Shortest Path Tree**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**Illustration**r Shortest Path Tree**Correctness**Shortest Path Tree**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**If w is non-negative**Can we speed up Bellman-Ford? Shortest Path Tree**No negative cycle!**• Since the input graph has no negative edge, it has no negative cycle. Shortest Path Tree**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**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**Illustration**1 ∞ ∞ 10 3 9 2 0 4 6 7 5 ∞ ∞ 2 Shortest Path Tree**Illustration**1 10 ∞ 10 3 9 2 0 4 6 7 5 5 ∞ 2 Shortest Path Tree**Illustration**1 8 14 10 3 9 2 0 4 6 7 5 5 7 2 Shortest Path Tree**Illustration**1 8 13 10 3 9 2 0 4 6 7 5 5 7 2 Shortest Path Tree**Illustration**1 8 9 10 3 9 2 0 4 6 7 5 5 7 2 Shortest Path Tree**Illustration**1 8 9 10 3 9 2 0 4 6 7 5 5 7 2 Shortest Path Tree