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

the many cases of fi nding shortest paths dynamic
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Dynamic programming

Bellman-Ford algorithm Tyler Moore

CSE 3353, SMU, Dallas, TX

Lecture 18

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.

The many cases of finding shortest paths

We’ve already seen how to calculate the shortest path in an unweighted graph (BFS traversal) We’ll now study how to compute the shortest path in different circumstances for weighted graphs

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)

2 / 13

  • 6. DYNAMIC PROGRAMMING II
  • sequence alignment
  • Hirschberg's algorithm
  • Bellman-Ford
  • distance vector protocols
  • negative cycles in a digraph

3 / 13

22

  • Shortest path problem. Given a digraph , with arbitrary edge

weights or costs , find cheapest path from node to node .

7 1 3

  • 1

8 5 7 5 4

  • 3
  • 5

12 10 13 9

  • 4

5 2 6 9

  • 3

1 11 4 / 13

slide-2
SLIDE 2

23

  • Dijkstra. Can fail if negative edge weights.
  • Reweighting. Adding a constant to every edge weight can fail.

u s t w v 2 2 3 3

  • 3

5 5 6 6 s v u 2

  • 8

w 1 3 5 / 13

24

  • Def. A negative cycle is a directed cycle such that the sum of its edge

weights is negative.

  • 3

5

  • 3
  • 4

4

c(W) =

  • e∈W

ce < 0

6 / 13

25

  • Lemma 1. If some path from to contains a negative cycle, then there

does not exist a cheapest path from to .

  • Pf. If there exists such a cycle , then can build a ↝ path of arbitrarily

negative weight by detouring around cycle as many times as desired. ▪

  • v

t 7 / 13

26

  • Lemma 2. If has no negative cycles, then there exists a cheapest path

from to that is simple (and has ≤ edges). Pf.

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 t 8 / 13

slide-3
SLIDE 3

27

  • Shortest path problem. Given a digraph with edge weights and

no negative cycles, find cheapest ↝ path for each node . Negative cycle problem. Given a digraph with edge weights , find a negative cycle (if one exists).

  • 3

5

  • 3
  • 4

4

  • 4

t 1

  • 3
  • 5

2 9 / 13

28

  • Def. = cost of shortest ↝ path that uses ≤ edges.

Case 1: Cheapest ↝ path uses ≤ edges. Case 2: Cheapest ↝ path uses exactly edges.

if is first edge, then uses , and then selects best ↝ path using ≤ edges

  • Observation. If no negative cycles, = cost of cheapest ↝ path.
  • Pf. By Lemma 2, cheapest ↝ path is simple. ▪

  • ⎧

⎨ ⎩ ⎫ ⎬ ⎭

  • ⎧

⎨ ⎪ ⎩ ⎪

  • ptimal substructure property

(proof via exchange argument) 10 / 13

Bellman-Ford algorithm intuition

With negative edges, we can’t select the next vertex to visit cleverly Instead, we just relax all m edges n times in a row If, after n rounds, the upper bounds don’t shrink, then we’ve found the shortest path from the source But if the upper bounds still shrink after n rounds, then there is a negative cycle in the graph, which is a problem (why?) Running time: Θ(m · n)

11 / 13

Bellman-Ford algorithm code

def b el l ma n f ord (G, s ) : D, P = { s :0} , {} # Zero−d i s t to s ; no parents for rnd in G: # n = l e n (G) rounds changed = False # No changes in round so f a r for u in G: # For every from−node . . . for v in G[ u ] : # . . . and i t s to−nodes . . . i f r e l a x (G, u , v , D, P ) : # Shortcut to v from u? changed = True # Yes ! So something changed i f not changed : break # No change in round : Done else : # Not done be f ore round 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

12 / 13

slide-4
SLIDE 4

Bellman-Ford algorithm example

s t y z x

6 7 8 9 7 − 4 −3 5 −2 2

d[Node]: upper bd. dist. from s Node init. 1 2 3 4 5 s t ∞ 6 6 2 2 2 x ∞ ∞ 114 4 4 4 y ∞ 7 7 7 7 7 z ∞ ∞ 2 2

  • 2
  • 2

Edge Eval. Order (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)

13 / 13