cs4102 algorithms
play

CS4102 Algorithms Fall 2018 Warm up: Modify Dijkstras Algorithm to - PowerPoint PPT Presentation

CS4102 Algorithms Fall 2018 Warm up: Modify Dijkstras Algorithm to find the shortest paths by product of edge weights (assume all weights are at least 1) 1 Dijkstras Algorithm Initialize = for each node Keep a


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. Today’s Keywords β€’ Graphs β€’ Shortest path β€’ Bellman-Ford – OG DP β€’ Floyd-Warshall 6

  7. CLRS Readings β€’ Chapter 22 β€’ Chapter 23 β€’ Chapter 24 7

  8. Homeworks β€’ HW7 Released – Due Saturday April 21, 11pm – Written (use latex) – Graphs 8

  9. Currency Exchange 1 Dollar = 0.8783121137 Euro 1 Dollar = 3.87 Ringgit 9

  10. 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

  11. 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

  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 Invert edge weights to make it a minimization problem 3.87479406049 USD MYR 0.2583979328 0.8783121137 1.0548325619 Euro 4.1823100458 AED 12

  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 Take log of edge weights to make summation 0.26 USD MYR 3.87 1.14 0.95 Euro 0.24 AED 13

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

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