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

β–Ά
cs4102 algorithms
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS4102 Algorithms

Fall 2018

1

Warm up: Modify Dijkstra’s Algorithm to find the shortest paths by product of edge weights (assume all weights are at least 1)

slide-2
SLIDE 2

Dijkstra’s Algorithm

2

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)

slide-3
SLIDE 3

Dijkstra’s Algorithm (for min product)

3

10 2 7 11 9 5 6 3 7 3 1 8 12 9 A B C D E F G I H

Initialize 𝑒𝑀 = ∞ for each node 𝑀 Keep a priority queue 𝑄𝑅 of nodes, using 𝑒𝑀 as key Pick a start node 𝑑, set 𝑒𝑑 = 1 While 𝑄𝑅 is not empty: 𝑀 = 𝑄𝑅. π‘“π‘¦π‘’π‘ π‘π‘‘π‘’π‘›π‘—π‘œ() for each 𝑣 ∈ π‘Š s.t. 𝑀, 𝑣 ∈ 𝐹: 𝑄𝑅. 𝑒𝑓𝑑𝑠𝑓𝑏𝑑𝑓𝐿𝑓𝑧(𝑣, min 𝑒𝑣, 𝑒𝑀 β‹… π‘₯ 𝑀, 𝑣 )

∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

How do we know this works?

slide-4
SLIDE 4

Shortest path by product

Goal: find the path (𝑑 = 𝑀1, 𝑀2, … , π‘€π‘™βˆ’1, 𝑀𝑙) which minimizes: π‘₯ 𝑀1, 𝑀2 β‹… π‘₯ 𝑀2, 𝑀3 β‹… … β‹… π‘₯(π‘€π‘™βˆ’1, 𝑀𝑙)

4

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,𝑀𝑙 )

slide-5
SLIDE 5

Dijkstra’s Algorithm (for min product)

5

log 10 log 2 log 7 log 11 log 9 log 5 log 6 log 3 log 7 log 3 log 1 log 8 log 12 log 9 A B C D E F G I H

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 𝑒𝑣, 𝑒𝑀 β‹… π‘₯ 𝑀, 𝑣 )

∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

𝑒𝑀 + log(π‘₯ 𝑀, 𝑣 )

slide-6
SLIDE 6

Today’s Keywords

  • Graphs
  • Shortest path
  • Bellman-Ford

– OG DP

  • Floyd-Warshall

6

slide-7
SLIDE 7

CLRS Readings

  • Chapter 22
  • Chapter 23
  • Chapter 24

7

slide-8
SLIDE 8

Homeworks

  • HW7 Released

– Due Saturday April 21, 11pm – Written (use latex) – Graphs

8

slide-9
SLIDE 9

Currency Exchange

9

1 Dollar = 3.87 Ringgit 1 Dollar = 0.8783121137 Euro

slide-10
SLIDE 10

Currency Exchange

10

1 Dollar = 0.8783121137 Euro 1 Euro= 4.1823100458 Dirham 1 Dirham= 1.0548325619 Ringgit

1 Dollar = 3.87 Ringgit

1 Dollar= 0.8783121137 * 4.1823100458 * 1.0548325619 Ringgit = 3.87479406049 Ringgit

slide-11
SLIDE 11

Currency Exchange

11

1 Ringgit = 0.2583979328 Dollar 1 Dollar = 3.87479406049 Ringgit 1 Dollar = 3.87479406049 * 0.2583979328 Dollar = 1.00123877526 Dollar

Free Money!

slide-12
SLIDE 12

Best Currency Exchange

12

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

0.8783121137 USD Euro AED MYR 4.1823100458 1.0548325619 0.2583979328 3.87479406049

Invert edge weights to make it a minimization problem

slide-13
SLIDE 13

Best Currency Exchange

13

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

1.14 USD Euro AED MYR 0.24 0.95 3.87 0.26

Take log of edge weights to make summation

slide-14
SLIDE 14

Best Currency Exchange

14

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.06 USD Euro AED MYR

  • 0.63
  • .02

0.57

  • 0.585

Negative Edge Weights!

slide-15
SLIDE 15

Problem with negative edges

15

7 11

  • 5

3 1 12 A B C D E F G I H π‘₯ 𝐷, 𝐺, 𝐸, 𝐷 = βˆ’1 Weight if we take the cycle 0 times: 31 Weight if we take the cycle 1 time: 30 Weight if we take the cycle 2 times: 29 …

There is no shortest path from A to I! What we need: an algorithm that finds the shortest path in graphs with negative edge weights (if one exists)

slide-16
SLIDE 16

Note

Any simple path has at most π‘Š βˆ’ 1 edges

16

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

slide-17
SLIDE 17

Bellman-Ford

17

Idea: Use Dynamic Programming! π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges Two options: A path of 𝑗 βˆ’ 1 edges from 𝑑 to some node 𝑦, then edge (𝑦, 𝑀) A path from 𝑑 to 𝑀 of at most 𝑗 βˆ’ 1 edges OR

𝑑 𝑀

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 ( π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

𝑑 𝑀 𝑦

slide-18
SLIDE 18

Bellman Ford

18

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 3 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7

Start node is E Initialize all others to ∞

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges

slide-19
SLIDE 19

Bellman Ford

19

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ ∞ 8 ∞ 7 𝟏 ∞ 5 5 ∞ 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 3 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7

Start node is E Initialize all others to ∞

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges

slide-20
SLIDE 20

Bellman Ford

20

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ ∞ 8 ∞ 7 𝟏 ∞ 5 5 ∞ 18 8 4 7 𝟏 4 5 5 7 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 3 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7

Start node is E Initialize all others to ∞

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges

slide-21
SLIDE 21

Bellman Ford

21

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ ∞ 8 ∞ 7 𝟏 ∞ 5 5 ∞ 18 8 4 7 𝟏 4 5 5 7

  • 8

8 4 7 4 3 5 7 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 3 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7

Start node is E Initialize all others to ∞

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges

slide-22
SLIDE 22

Bellman Ford

22

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ ∞ 8 ∞ 7 𝟏 ∞ 5 5 ∞ 18 8 4 7 𝟏 4 5 5 7

  • 8

8 4 7 4 3 5 7

  • 8

8 4 7 4 3 5 7

  • 8

8 4 7 4 3 5 7

  • 8

8 4 7 4 3 5 7

  • 8

8 4 7 4 3 5 7 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 3 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7

Start node is E Initialize all others to ∞

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges

slide-23
SLIDE 23

Bellman Ford: Negative cycles

23

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ ∞ 8 ∞ 7 𝟏 ∞ 4 7 𝟏 4 4 5 4 4 5 2 3 4 1 2 3 1 2 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 1 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7

Start node is E Initialize all others to ∞

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges

If we computed row V, values change There is a negative weight cycle!

slide-24
SLIDE 24

Bellman Ford Run Time

Intialize array π‘‡β„Žπ‘π‘ π‘’ π‘Š π‘Š Initialize π‘‡β„Žπ‘π‘ π‘’ 0 𝑀 = ∞ for each vertex Initialize π‘‡β„Žπ‘π‘ π‘’ 0 𝑑 = 0 For 𝑗 = 1, … , π‘Š βˆ’ 1: for each 𝑓 = (𝑦, 𝑧) ∈ 𝐹: π‘‡β„Žπ‘π‘ π‘’ 𝑗 𝑧 = min{ π‘‡β„Žπ‘π‘ π‘’ 𝑗 βˆ’ 1 𝑦 + π‘₯ 𝑦, 𝑧 , π‘‡β„Žπ‘π‘ π‘’[𝑗 βˆ’ 1][𝑧]}

24

π‘Š2 π‘Š 1 π‘Š times 𝐹 times 1

slide-25
SLIDE 25

Why Use Bellman-Ford?

  • Dijkstra’s:

– only works for positive edge weights – Run Time: Θ(𝐹 log π‘Š) – Not good for dynamic graphs (where edge weights are variable)

  • Must recalculate β€œfrom scratch”
  • Bellman-Ford:

– Works for negative edge weights – Run Time: Θ(𝐹 β‹… π‘Š) – More efficient for dynamic graphs

  • Θ(𝐹) time to recalculate

25

slide-26
SLIDE 26

Bellman Ford: Dynamic

26

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ ∞ 5 ∞ 7 𝟏 ∞ 5 5 ∞ 18 8 4 7 𝟏 4 5 5 7

  • 8

8 4 7 4 3 5 7

  • 8

8 4 7 4 3 5 7

  • 8

8 4 7 4 3 5 7

  • 8

8 4 7 4 3 5 7

  • 8

8 4 7 4 3 5 7 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 3 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7

Each node will update its neighbors if edge weight changes

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges 5

slide-27
SLIDE 27

Bellman Ford: Dynamic

27

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ ∞ 5 ∞ 7 𝟏 ∞ 5 5 ∞ 15 5 1 7 𝟏 4 5 5 7

  • 8

5 1 7 4 3 5 7

  • 8

5 1 7 4 3 5 7

  • 8

5 1 7 4 3 5 7

  • 8

5 1 7 4 3 5 7

  • 8

5 1 7 4 3 5 7 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 3 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7 π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges 5

Each node will update its neighbors if edge weight changes

slide-28
SLIDE 28

Bellman Ford: Dynamic

28

π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = min min

𝑦 (π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑦) + π‘₯(𝑦, 𝑀))

π‘‡β„Žπ‘π‘ π‘’(𝑗 βˆ’ 1, 𝑀)

∞ ∞ ∞ ∞ 𝟏 ∞ ∞ ∞ ∞ ∞ 5 ∞ 7 𝟏 ∞ 5 5 ∞ 15 5 1 7 𝟏 4 5 5 7

  • 11

5 1 4 4 3 5 7

  • 11

5 1 4 4 3 5 7

  • 11

5 1 4 4 3 5 7

  • 11

5 1 4 4 3 5 7

  • 11

5 1 4 4 3 5 7 𝑀 = A 1 3 4 5 6 2 10 2 6

  • 4

9 5 5

  • 3

7 3 1 8

  • 12
  • 4

A B C D E F G I H B C D E F G H I 7 π‘‡β„Žπ‘π‘ π‘’ 𝑗, 𝑀 = weight of the shortest path from 𝑑 to 𝑀 using at most 𝑗 edges 5

Each node will update its neighbors if edge weight changes

slide-29
SLIDE 29

All Pairs Shortest Path

29

Find the quickest way to get from each place to every other place Given a graph 𝐻 = (π‘Š, 𝐹) for each start node 𝑑 ∈ π‘Š and destination node 𝑀 ∈ π‘Š find the least-weight path from 𝑑 β†’ 𝑀

  • 10

2 6 11 9

  • 5

8 3 7

  • 3

1 8 12 9

slide-30
SLIDE 30

All-Pairs Shortest Path

  • Can clearly be found in 𝑃(π‘Š2 β‹… 𝐹)

– Run Bellman-Ford with each node being the start

30

for each 𝑑 ∈ π‘Š: πΆπ‘“π‘šπ‘šπ‘›π‘π‘œπΊπ‘π‘ π‘’(𝑑)

π‘Š times 𝑃(π‘Š β‹… 𝐹)

slide-31
SLIDE 31

Floyd-Warshall

  • Finds all-pairs shortest paths in Θ(π‘Š3)
  • Uses Dynamic Programming

31

π‘‡β„Žπ‘π‘ π‘’ 𝑗, π‘˜, 𝑙 = the length of the shortest path from node 𝑗 to node π‘˜ using only intermediate nodes 1,… , 𝑙

Two options: Shortest path from 𝑗 to π‘˜ includes 𝑙 Shortest path from 𝑗 to π‘˜ excludes 𝑙 OR

𝑗 π‘˜

π‘‡β„Žπ‘π‘ π‘’ 𝑗, π‘˜, 𝑙 = min π‘‡β„Žπ‘π‘ π‘’(𝑗, 𝑙, 𝑙 βˆ’ 1) + π‘‡β„Žπ‘π‘ π‘’(𝑙, π‘˜, 𝑙 βˆ’ 1) π‘‡β„Žπ‘π‘ π‘’(𝑗, π‘˜, 𝑙 βˆ’ 1)

𝑗 π‘˜ 𝑙 𝑙