Examples of Shortest Path Problems

21
Examples of Shortest Path Problems Dijkstra's algorithm Bellman-Ford algorithm Modeling AdvOL @ McMaster http://optlab.mcmaster.ca

Transcript of Examples of Shortest Path Problems

Page 1: Examples of Shortest Path Problems

Examples of Shortest Path Problems

Dijkstra's algorithmBellman-Ford algorithm

Modeling

AdvOL @ McMaster http://optlab.mcmaster.ca

Page 2: Examples of Shortest Path Problems

Trip Planning

A

4 cities: A, B, C, D; 3 airliners: Red, Blue, Green; Cheapest way of going from A to D ?

B C DRed $160

$90

$400

A B C DBlue $170

$80

A B C DGreen

$140

$50

$30 $160

Page 3: Examples of Shortest Path Problems

First TryVertices: 4 cities { A, B, C, D } Edges: flights, undirected, weights – pricesObjective: shortest path from A to D (min total weight/cost)

A B C D

$160

$90

$400

$170$80

$140

$50

$30

$160

Multiple edges between some vertices ! (Multi-graph)

Page 4: Examples of Shortest Path Problems

Working on Simple GraphVertices: 4 cities { A, B, C, D } Edges: flights, undirected, weights – pricesObjective: shortest path from A to D (min total weight/cost)

A B C D

$160

$90

$400

$80

$30

$160

Only keep the cheaper flightsDijkstra? Bellman-Ford?

Page 5: Examples of Shortest Path Problems

Fewer TransfersIt is cheap ($200), but have to transfer twice ...You can tolerate at most 1 transfer, what is the cheapest way?

A B C D

$90

$80

$30

Page 6: Examples of Shortest Path Problems

Fewer TransfersIt is cheap ($200), but have to transfer twice ...You can tolerate at most 1 transfer, what is the cheapest way?

The k'th iteration of Bellman-Ford gives the shortest paths withlength at least k

A B C D

$160

$90

$400

$80

$30

$160

Page 7: Examples of Shortest Path Problems

Modified Bellman-FordAn algorithm to find the shortest path with at most k edges:G - directed graph; s - start vertex; t - end vertex

SHORTEST-PATH-WITH-AT-MOST-K-EDGES (G, s, t) d[0][s] = 0 d[0][v] = ∞ for v V and v = s∈ for i = 1..k do d[k][v] = d[k − 1][v] for any (u, v) E do∈ d[k][v] = min(d[k][v], d[k − 1][u] + w[u][v]) return d[k][t]

Page 8: Examples of Shortest Path Problems

Fewer TransfersThe cheapest way of going from A to B with at most 2 transfers:$240

Only 2 iterations in Bellman-Ford algorithm are needed

A B C D

$160

$80

Page 9: Examples of Shortest Path Problems

Wait! I Have a Coupon!Have a $100 coupon :

Can be used with any airliner; Can be used only once. Can NOT be used on a flight whose price is smaller than the coupon value

A B C D

$160

$90

$400

$170$80

$140

$50

$30

$160

Page 10: Examples of Shortest Path Problems

Wait! I Have a Coupon!Have a $100 coupon :

Can be used with any airliner; Can be used only once. Can NOT be used on a flight whose price is smaller than the coupon value

A B C D

$160

$90

$400

$80

$140

$30

$160

Mutiple flights between A and C !

Page 11: Examples of Shortest Path Problems

Modeling

A B C D$160

$90

$400

A B C D

$140

$30 $160

$80

Separate Red flights from the others

Page 12: Examples of Shortest Path Problems

Modeling

A B C D$160

$90

$400

A B C D

$140

$30 $160

$80

Separate Red flights from the others Transfer cost is 0

0 0 0 0

Page 13: Examples of Shortest Path Problems

Modeling

A B C D$160

$90

$400

A B C D

$140

$30 $160

$80

Separate Red flights from the others Transfer cost is 0 Add artificial source and destination (with 0-weight edges)

0 0 0 0S

A

T

0

0 0

0

Page 14: Examples of Shortest Path Problems

Modeling

A B C D$160

$90

$400

A B C D

$140

$30 $160

$80

Separate Red flights from the others Transfer cost is 0 Add artificial source and destination (with 0-weight edges)

0 0 0 0S

A

T

0

0 0

0

How about the coupon?

Page 15: Examples of Shortest Path Problems

A B C D$160

$90

$400

A B C D

$140

$30 $160

$80

0 0 0 0S

A

T

0

0 0

0

A B' C' D'

$140

$30 $160

$80

A'

A' B' C' D'$160

$90

$400

Coupon used:

Coupon used:

Coupon held:

Page 16: Examples of Shortest Path Problems

A B C D

A B C D

0 S

A

T

A B' C' D'

$140

$30 $160

$80

A'

A' B' C' D'$160

$90

$400

Coupon used:

Coupon used:

0 0 0

0

0

Page 17: Examples of Shortest Path Problems

A B C D

A B C D

0 S

A

T

A B' C' D'

$140

$30 $160

$80

A'

A' B' C' D'$160

$90

$400

Use Couponon A -> B

0 0 0

0

0

$60

Page 18: Examples of Shortest Path Problems

A B C D

A B C D

0 S

A

T

A B' C' D'

$140

$30 $160

$80

A'

A' B' C' D'$160

$90

$400

Use Couponon A -> C

0 0 0

0

0

$60

$40

Page 19: Examples of Shortest Path Problems

A B C D

A B C D

S

A

T

A B' C' D'

A'

A' B' C' D'

“Coupon edges” $60

$40

$300

$60$40$40

$60

$40

Page 20: Examples of Shortest Path Problems

A B C D

A B C D

S

A

T

$0

A B' C' D'

$80

A'

A' B' C' D'

Run Dijkstra ...

$0

$0

$60 $60

Page 21: Examples of Shortest Path Problems

A B C D

A B C D

S

A

T

$0

A B' C' D'

$80

A'

A' B' C' D'

Run Dijkstra ...

$0

$0

$60 $60

Optimal trip ($140): Red A -> B (Coupon) Blue B -> D