Bellman-Ford Algorithm

Bellman-Ford Algorithm

The many cases offinding 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 different CSE 3353, SMU, Dallas, TX circumstances for weighted graphs Lecture 18 1 Single-source shortest path on a weighted DAG 2 Single-source shortest path on a weighted graph with nonnegative weights (Dijkstra’s algorithm) 3 Single-source shortest path on a weighted graph including negative 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. DYNAMIC PROGRAMMING II weights or costs ���, find cheapest path from node � to node �. ‣ sequence alignment ‣ Hirschberg's algorithm ‣ Bellman-Ford 1 -3 3 5 ‣ distance vector protocols 4 12 �������� 0 -5 ‣ negative cycles in a digraph 8 7 7 2 9 9 -1 1 11 ����������� 5 5 -3 13 4 10 6 ������������� ���������������������������������� 22 3 / 13 4 / 13 �������������������������������� ��������������� 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 1 3 v -8 w 5 -3 -3 Reweighting. Adding a constant to every edge weight can fail. 4 -4 u 5 5 2 2 ���������������������� c(W) = ce < 0 t s e W 6 6 �∈ 3 3 0 v -3 w 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 ����������������������������������������� ������������������������������������ Shortest path problem. Given a digraph ���������� with edge weights ��� and Def. ��������� = cost of shortest �↝� path that uses ≤ � edges. 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 5 -3 ⎧� �∞ ������� 4 2 -3 ⎪� ��������� ⎧� ⎫� ⎨��� ��� ����� ����� ����� ���� � ��������� ⎪� ⎨� − ��� �− � �� � ⎬� t 4 -4 �� ⎩� ⎩� �����∈� ⎭� ������������������� �������������� 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 def bellman f o r d (G, s ) : With negative edges, we can’t select the next vertex to visit cleverly D, P = s:0 , #Zero dist to s; no parents f o r rnd{ in}G:{} #n=len− (G) rounds Instead, we just relax allm edgesn times in a row changed = False #No changes in round so f a r f o ru inG: #Foreveryfrom node ... If, aftern rounds, the upper bounds don’t shrink, then we’ve found − the shortest path from the source f o rv in G[u]: #... and its to nodes ... i f relax(G, u, v, D, P): # Shortcut− to v from u? But if the upper bounds still shrink aftern 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 Running time:Θ(m n) e l s e: # Not done before round n? · r a i s e ValueError(’negative c y c l e ’ ) # Negative cycle detect e returnD,P #Other wise: D and P correct 11 / 13 12 / 13 Bellman-Ford algorithm example t 5 x 2 Edge −4 − Eval. 8 6 3 7 Order − (t,x) 7 9 (t,y) s y z (t,z) (x,t) 2 (y,x) d[Node]: upper bd. dist. froms (y,z) Node init. 1 2 3 4 5 (z,x) s 0 0 0 0 0 0 (z,s) t 66222 (s,t) ∞ x 114 4 4 4 (s,y) ∞ ∞ y 77777 ∞ z 2 2 -2 -2 ∞ ∞ 13 / 13.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us