CS4102 Algorithms Fall 2018 Warm up: Modify Dijkstraβs Algorithm to find the shortest paths by product of edge weights (assume all weights are at least 1) 1
Dijkstraβs Algorithm Initialize π π€ = β for each node π€ Keep a priority queue ππ of nodes, using π π€ as key Pick a start node π‘ , set π π‘ = 0 While ππ is not empty: π€ = ππ . ππ¦π’π πππ’πππ() for each π£ β π s.t. π€, π£ β πΉ : ππ . ππππ πππ‘ππΏππ§(π£, min π π£ , π π€ + π₯ π€, π£ ) Modify Dijkstraβs Algorithm to find the shortest paths by product of edge weights (assume all weights are at least 1) 2
Dijkstraβs Algorithm (for min product) Initialize π π€ = β for each node π€ Keep a priority queue ππ of nodes, using π π€ as key Pick a start node π‘ , set π π‘ = 1 While ππ is not empty: How do we know this works? π€ = ππ . ππ¦π’π πππ’πππ() for each π£ β π s.t. π€, π£ β πΉ : ππ . ππππ πππ‘ππΏππ§(π£, min π π£ , π π€ β π₯ π€, π£ ) β β 8 B E 10 β 6 0 H 7 β 2 A 9 β D 5 9 I 12 3 3 C β 11 β β G 3 F 1 7
Shortest path by product Goal: find the path (π‘ = π€ 1 , π€ 2 , β¦ , π€ πβ1 , π€ π ) which minimizes: π₯ π€ 1 , π€ 2 β π₯ π€ 2 , π€ 3 β β¦ β π₯(π€ πβ1 , π€ π ) Observation: log π¦ β π§ = log π¦ + log π§ log( π₯ π€ 1 ,π€ 2 β π₯ π€ 2 ,π€ 3 β β¦β π₯(π€ πβ1 ,π€ π ) ) = log π₯ π€ 1 ,π€ 2 + log π₯ π€ 2 ,π€ 3 + β― + log π₯ π€ πβ1 ,π€ π New Goal: find the path (π‘ = π€ 1 ,π€ 2 ,β¦ ,π€ πβ1 ,π€ π ) which minimizes: log π₯ π€ 1 ,π€ 2 + log π₯ π€ 2 ,π€ 3 + β― + log(π₯ π€ πβ1 ,π€ π ) 4
Dijkstraβs Algorithm (for min product) Initialize π π€ = β for each node π€ Keep a priority queue ππ of nodes, using π π€ as key Pick a start node π‘ , set π π‘ = 0 While ππ is not empty: π€ = ππ . ππ¦π’π πππ’πππ() π π€ + log(π₯ π€, π£ ) for each π£ β π s.t. π€, π£ β πΉ : ππ . ππππ πππ‘ππΏππ§(π£, min π π£ , π π€ β π₯ π€, π£ ) β β log 8 B log 10 E log 6 β 0 log 7 H log 2 β A log 9 β D log 5 log 9 I log 12 log 3 log 3 C β log 11 β β G 5 F log 1 log 7
Todayβs Keywords β’ Graphs β’ Shortest path β’ Bellman-Ford β OG DP β’ Floyd-Warshall 6
CLRS Readings β’ Chapter 22 β’ Chapter 23 β’ Chapter 24 7
Homeworks β’ HW7 Released β Due Saturday April 21, 11pm β Written (use latex) β Graphs 8
Currency Exchange 1 Dollar = 0.8783121137 Euro 1 Dollar = 3.87 Ringgit 9
Currency Exchange 1 Dollar = 3.87 Ringgit 1 Dollar = 0.8783121137 Euro 1 Dirham= 1.0548325619 Ringgit 1 Euro= 4.1823100458 Dirham 1 Dollar= 0.8783121137 * 4.1823100458 * 1.0548325619 Ringgit = 3.87479406049 Ringgit 10
Currency Exchange 1 Dollar = 3.87479406049 Ringgit 1 Ringgit = 0.2583979328 Dollar 1 Dollar = 3.87479406049 * 0.2583979328 Dollar = 1.00123877526 Dollar Free Money! 11
Best Currency Exchange Best way to transfer USD to MYR: Given a graph of currencies (edges are exchange rates) find the shortest path by product of edge weights Invert edge weights to make it a minimization problem 3.87479406049 USD MYR 0.2583979328 0.8783121137 1.0548325619 Euro 4.1823100458 AED 12
Best Currency Exchange Best way to transfer USD to MYR: Given a graph of currencies (edges are exchange rates) find the shortest path by product of edge weights Take log of edge weights to make summation 0.26 USD MYR 3.87 1.14 0.95 Euro 0.24 AED 13
Best Currency Exchange Best way to transfer USD to MYR: Given a graph of currencies (edges are exchange rates) find the shortest path by product of edge weights Now a shortest path problem! -0.585 USD MYR 0.57 0.06 -.02 Euro -0.63 Negative Edge Weights! AED 14
Problem with negative edges B E H A D I 12 3 -5 C 11 G F 1 7 π₯ π·, πΊ, πΈ, π· = β1 There is no shortest path from A to I! Weight if we take the cycle 0 times: 31 What we need: an algorithm that Weight if we take the cycle 1 time: 30 finds the shortest path in graphs with Weight if we take the cycle 2 times: 29 negative edge weights (if one exists) β¦ 15
Note Any simple path has at most π β 1 edges Pigeonhole Principle! More than π β 1 edges means some node appears twice (i.e., there is a cycle) If there is a shortest path of more than π β 1 edges, there is a negative weight cycle 16
Bellman-Ford Idea: Use Dynamic Programming! πβππ π’ π, π€ = weight of the shortest path from π‘ to π€ using at most π edges A path of π β 1 edges from π‘ to π¦ some node π¦ , then edge (π¦, π€) π‘ π€ Two options: OR A path from π‘ to π€ of at most π β 1 edges π€ π‘ min π¦ ( πβππ π’(π β 1, π¦) + π₯(π¦, π€)) πβππ π’ π, π€ = min πβππ π’(π β 1, π€) 17
Bellman Ford 8 B E 5 10 Start node is E 7 H Initialize all others to β 2 -4 A D 5 9 I weight of the shortest path from -12 πβππ π’ π, π€ = 3 π‘ to π€ using at most π edges -3 C min π¦ (πβππ π’(π β 1, π¦) + π₯(π¦, π€)) -4 G πβππ π’ π, π€ = min F 1 6 πβππ π’(π β 1, π€) π€ = B C D E F G H I A 0 β β β β π β β β β 1 2 3 4 5 6 18 7
Bellman Ford 8 B E 5 10 Start node is E 7 H Initialize all others to β 2 -4 A D 5 9 I weight of the shortest path from -12 πβππ π’ π, π€ = 3 π‘ to π€ using at most π edges -3 C -4 min π¦ (πβππ π’(π β 1, π¦) + π₯(π¦, π€)) G F πβππ π’ π, π€ = min 1 6 πβππ π’(π β 1, π€) π€ = B C D E F G H I A 0 β β β β π β β β β β 8 β 7 π β 5 5 β 1 2 3 4 5 6 19 7
Bellman Ford 8 B E 5 10 Start node is E 7 H Initialize all others to β 2 -4 A D 5 9 I weight of the shortest path from -12 πβππ π’ π, π€ = 3 π‘ to π€ using at most π edges -3 C -4 min π¦ (πβππ π’(π β 1, π¦) + π₯(π¦, π€)) G F πβππ π’ π, π€ = min 1 6 πβππ π’(π β 1, π€) π€ = B C D E F G H I A 0 β β β β π β β β β β 8 β 7 π β 5 5 β 1 2 π 18 8 4 7 4 5 5 7 3 4 5 6 20 7
Bellman Ford 8 B E 5 10 Start node is E 7 H Initialize all others to β 2 -4 A D 5 9 I weight of the shortest path from -12 πβππ π’ π, π€ = 3 π‘ to π€ using at most π edges -3 C -4 min π¦ (πβππ π’(π β 1, π¦) + π₯(π¦, π€)) G F πβππ π’ π, π€ = min 1 6 πβππ π’(π β 1, π€) π€ = B C D E F G H I A 0 β β β β π β β β β β 8 β 7 π β 5 5 β 1 2 π 18 8 4 7 4 5 5 7 3 -8 8 4 7 0 4 3 5 7 4 5 6 21 7
Bellman Ford 8 B E 5 10 Start node is E 7 H Initialize all others to β 2 -4 A D 5 9 I weight of the shortest path from -12 πβππ π’ π, π€ = 3 π‘ to π€ using at most π edges -3 C -4 min π¦ (πβππ π’(π β 1, π¦) + π₯(π¦, π€)) G F πβππ π’ π, π€ = min 1 6 πβππ π’(π β 1, π€) π€ = B C D E F G H I A 0 β β β β π β β β β β 8 β 7 π β 5 5 β 1 2 π 18 8 4 7 4 5 5 7 3 -8 8 4 7 0 4 3 5 7 4 -8 8 4 7 0 4 3 5 7 5 -8 8 4 7 0 4 3 5 7 6 -8 8 4 7 0 4 3 5 7 22 7 -8 8 4 7 0 4 3 5 7
Bellman Ford: Negative cycles 8 B E 5 10 Start node is E 7 H Initialize all others to β 2 -4 A D 5 9 I weight of the shortest path from -12 πβππ π’ π, π€ = 1 π‘ to π€ using at most π edges -3 C -4 min π¦ (πβππ π’(π β 1, π¦) + π₯(π¦, π€)) G F πβππ π’ π, π€ = min 1 6 πβππ π’(π β 1, π€) π€ = B C D E F G H I A 0 β β β β π β β β β If we computed row V, values β 8 β 7 π β 1 change 2 π 4 7 4 3 4 5 0 4 There is a 4 4 5 0 2 negative weight 5 cycle! 3 4 0 1 6 2 3 0 0 23 7 1 2 0 0
Bellman Ford Run Time Intialize array πβππ π’ π π π 2 Initialize πβππ π’ 0 π€ = β for each vertex π Initialize πβππ π’ 0 π‘ = 0 1 For π = 1, β¦ , π β 1 : π times for each π = (π¦, π§) β πΉ : πΉ times πβππ π’ π π§ = min{ πβππ π’ π β 1 π¦ + π₯ π¦, π§ , 1 πβππ π’[π β 1][π§]} 24
Recommend
More recommend