the many cases of fi nding shortest paths dynamic
play

The many cases of fi nding shortest paths Dynamic programming - PowerPoint PPT Presentation

The many cases of fi nding shortest paths Dynamic programming Bellman-Ford algorithm Weve already seen how to calculate the shortest path in an Tyler Moore unweighted graph (BFS traversal) Well now study how to compute the shortest path


  1. The many cases of fi nding shortest paths Dynamic programming Bellman-Ford algorithm We’ve already seen how to calculate the shortest path in an Tyler Moore unweighted graph (BFS traversal) We’ll now study how to compute the shortest path in di ff erent CSE 3353, SMU, Dallas, TX circumstances for weighted graphs Single-source shortest path on a weighted DAG 1 Lecture 18 Single-source shortest path on a weighted graph with nonnegative 2 weights (Dijkstra’s algorithm) Single-source shortest path on a weighted graph including negative 3 weights (Bellman-Ford algorithm) Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos . Some code reused from Python Algorithms by Magnus Lie Hetland. 2 / 13 �������������� Shortest path problem. Given a digraph � ���� � �� � � , with arbitrary edge 6. D YNAMIC P ROGRAMMING II weights or costs � �� , find cheapest path from node � to node � . ‣ sequence alignment ‣ Hirschberg's algorithm ‣ Bellman-Ford 3 1 -3 ‣ distance vector protocols 5 4 12 �������� -5 0 ‣ negative cycles in a digraph 8 9 7 2 7 9 -1 1 11 � ������� ��� 5 5 -3 13 6 4 10 ������������� ���������������������������������� 22 3 / 13 4 / 13

  2. �������������������������������� ��������������� Dijkstra. Can fail if negative edge weights. Def. A negative cycle is a directed cycle such that the sum of its edge weights is negative. s 2 u 3 1 v w -8 5 -3 -3 Reweighting. Adding a constant to every edge weight can fail. 4 -4 u 5 5 2 2 � ���������������������� c ( W ) = c e < 0 t s e ∈ W 6 6 3 3 0 w v -3 23 24 5 / 13 6 / 13 ���������������������������������� ���������������������������������� Lemma 1. If some path from � to � contains a negative cycle, then there Lemma 2. If � has no negative cycles, then there exists a cheapest path does not exist a cheapest path from � � to � . from � to � that is simple (and has ≤ �� � ���� edges). Pf. If there exists such a cycle � , then can build a � ↝ � path of arbitrarily Pf. negative weight by detouring around cycle as many times as desired. ▪ � Consider a cheapest � ↝ � path � that uses the fewest number of edges. � If � contains a cycle � , can remove portion of � corresponding to � without increasing the cost. ▪ v v t t � � �������� ����� ≥ �� 25 26 7 / 13 8 / 13

  3. ����������������������������������������� ������������������������������������ Def. ��� � � �� � � = cost of shortest � ↝ � path that uses ≤ � edges. Shortest path problem. Given a digraph � ���� � �� � � with edge weights � �� and no negative cycles, find cheapest � ↝ � path for each node � . � Case 1: Cheapest � ↝ � path uses ≤ �� ��� edges. � ��� � � �� � �������� �� ����� � � Negative cycle problem. Given a digraph � ���� � �� � � with edge weights � �� , optimal substructure property find a negative cycle (if one exists). (proof via exchange argument) � Case 2: Cheapest � ↝ � path uses exactly � edges. � if � � �� � � is first edge, then ��� uses � � �� � � , and then selects best � ↝ � path using ≤ �� ��� edges 1 5 ∞ ⎧ � � � ����� � � -3 -3 5 -3 4 2 ⎪ � ��� � � � � � � ⎧ � ⎫ � ⎨ � ����� ��� � � − �� � � � ��� � � − �� � � � � �� ⎨ � � � ⎬ � ��������� ��� ⎪ � ⎩ � ⎩ � � � � � � ∈ � ⎭ � �� 4 -4 t �������������� ������������������� Observation. If no negative cycles, ��� � �� ����� � � = cost of cheapest � ↝ � path. Pf. By Lemma 2, cheapest � ↝ � path is simple. ▪ 27 28 9 / 13 10 / 13 Bellman-Ford algorithm intuition Bellman-Ford algorithm code b el l ma n f ord (G, s ) : def D, P = { s :0 } , {} # Zero − d i s t to s ; no parents With negative edges, we can’t select the next vertex to visit cleverly rnd in G: # n = l e n (G) rounds for Instead, we just relax all m edges n times in a row changed = False # No changes in round so f a r for u in G: # For every from − node . . . If, after n rounds, the upper bounds don’t shrink, then we’ve found for v in G[ u ] : # . . . and i t s to − nodes . . . the shortest path from the source i f r e l a x (G, u , v , D, P ) : # Shortcut to v from u? But if the upper bounds still shrink after n rounds, then there is a changed = True # Yes ! So something changed negative cycle in the graph, which is a problem (why?) i f not changed : break # No change in round : Done else : # Not done be f ore round n? Running time: Θ ( m · n ) r a i s e ValueError ( ’ ne ga t i v e c y c l e ’ ) # Negative c y c l e detecte return D, P # Otherwise : D and P c o r r e c t 11 / 13 12 / 13

  4. Bellman-Ford algorithm example t 5 x Edge − 2 − Eval. 4 8 − 3 6 Order 7 (t,x) (t,y) 7 9 y s z (t,z) (x,t) 2 (y,x) d[Node] : upper bd. dist. from s (y,z) Node init. 1 2 3 4 5 (z,x) s 0 0 0 0 0 0 (z,s) t 6 6 2 2 2 (s,t) ∞ x 114 4 4 4 (s,y) ∞ ∞ y 7 7 7 7 7 ∞ z 2 2 -2 -2 ∞ ∞ 13 / 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend