43
Bellman-Ford-Moore Algorithm Single-source shortest path algorithm

Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Embed Size (px)

DESCRIPTION

Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information: http://bit.ly/tecn-progr

Citation preview

Page 1: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Bellman-Ford-Moore Algorithm

Single-source shortest path algorithm

Page 2: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Bellman-Ford-Moore Algorithm

A.A. 2012/2013 Tecniche di programmazione 2

Solution to the single-source shortest path (SSSP)

problem in graph theory

Based on relaxation (for every vertex, relax all possible

edges)

Does not work in presence of negative cycles

but it is able to detect the problem

O(V∙E)

Page 3: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Bellman-Ford-Moore Algorithm

A.A. 2012/2013 Tecniche di programmazione 3

dist[s] ← 0 (distance to source vertex is zero) for all v ∈ V–{s} do dist[v] ←∞ (set all other distances to infinity) for i← 0 to |V| for all (u, v) ∈ E do if dist[v] > dist[u] + w(u, v) (if new shortest path found) then d[v] ←d[u] + w(u, v) (set new value of shortest path) (if desired, add traceback code) for all (u, v) ∈ E (sanity check) do if dist[v] > dist[u] + w(u, v) then PANIC!

Page 4: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra’s Algorithm

Yet another SSSP algorithm

Page 5: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra’s algorithm

A.A. 2012/2013 Tecniche di programmazione 5

Solution to the single-source shortest path (SSSP)

problem in graph theory

Works on both directed and undirected graphs

All edges must have nonnegative weights

the algorithm would miserably fail

Greedy

… but guarantees the optimum!

Page 6: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra’s algorithm

A.A. 2012/2013 Tecniche di programmazione 6

dist[s] ←0 (distance to source vertex is zero) for all v ∈ V–{s} do dist[v] ←∞ (set all other distances to infinity) S←∅ (S, the set of visited vertices is initially empty) Q←V (Q, the queue initially contains all vertices) while Q ≠∅ (while the queue is not empty) do u ← mindistance(Q,dist) (select e Q with the min. distance) S←S∪{u} (add u to list of visited vertices) for all v ∈ neighbors[u] do if dist[v] > dist[u] + w(u, v) (if new shortest path found) then d[v] ←d[u] + w(u, v) (set new value of shortest path) (if desired, add traceback code)

Page 7: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 8: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 9: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 10: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 11: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 12: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 13: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 14: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 15: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 16: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra Animated Example

Page 17: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Why it works

A.A. 2012/2013 Tecniche di programmazione 17

A formal proof would take longer than this presentation,

but we can understand how the argument works

intuitively

Think of Djikstra’s algorithm as a water-filling algorithm

Remember that all edge’s weights are positive

Page 18: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Dijkstra efficiency

A.A. 2012/2013 Tecniche di programmazione 18

The simplest implementation is:

O(𝐸 + 𝑉2)

But it can be implemented more efficently:

O(𝐸 + 𝑉 ∙ log𝑉)

Floyd–Warshall: O(V3)

Bellman-Ford-Moore : O(V∙E)

Page 19: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Applications

A.A. 2012/2013 Tecniche di programmazione 19

Dijkstra’s algorithm calculates the shortest path to every

vertex from vertex s (SSSP)

It is about as computationally expensive to calculate the

shortest path from vertex u to every vertex using

Dijkstra’s as it is to calculate the shortest path to some

particular vertex t

Therefore, anytime we want to know the optimal path to

some other vertex t from a determined origin s, we can

use Dijkstra’s algorithm (and stop as soon t exit from Q)

Page 20: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Applications

A.A. 2012/2013 Tecniche di programmazione 20

Traffic Information Systems are most prominent use

Mapping (Map Quest, Google Maps)

Routing Systems

Page 21: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

21

Dijkstra's Shortest Path Algorithm

Find shortest path from s to t

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

s

3

t

2

6

7

4

5

Page 22: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

22

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

0

distance label

S = { }

Q = { s, 2, 3, 4, 5, 6, 7, t }

s

3

t

2

6

7

4

5

Page 23: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

23

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

0

distance label

S = { }

Q = { s, 2, 3, 4, 5, 6, 7, t }

delmin

Page 24: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

24

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

distance label

S = { s }

Q = { 2, 3, 4, 5, 6, 7, t }

decrease key

X

X

X

s

3

t

2

6

7

4

5

Page 25: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

25

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

distance label

S = { s }

Q = { 2, 3, 4, 5, 6, 7, t }

X

X

X

delmin

s

3

t

2

6

7

4

5

Page 26: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

26

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2 }

Q = { 3, 4, 5, 6, 7, t }

X

X

X

s

3

t

2

6

7

4

5

Page 27: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

27

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2 }

Q = { 3, 4, 5, 6, 7, t }

X

X

X

decrease key

X 33

s

3

t

2

6

7

4

5

Page 28: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

28

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2 }

Q = { 3, 4, 5, 6, 7, t }

X

X

X

X 33

delmin s

3

t

2

6

7

4

5

Page 29: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

29

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 6 }

Q = { 3, 4, 5, 7, t }

X

X

X

X 33

44 X

X

32

s

3

t

2

6

7

4

5

Page 30: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

30

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 6 }

Q = { 3, 4, 5, 7, t }

X

X

X

44 X

delmin

X 33 X

32

s

3

t

2

6

7

4

5

Page 31: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

31

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 6, 7 }

Q = { 3, 4, 5, t }

X

X

X

44 X

35 X

59 X

24

X 33 X

32

s

3

t

2

6

7

4

5

Page 32: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

32

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 6, 7 }

Q = { 3, 4, 5, t }

X

X

X

44 X

35 X

59 X

delmin

X 33 X

32

s

3

t

2

6

7

4

5

Page 33: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

33

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 6, 7 }

Q = { 4, 5, t }

X

X

X

44 X

35 X

59 X X 51

X 34

X 33 X

32

s

3

t

2

6

7

4

5

Page 34: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

34

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 6, 7 }

Q = { 4, 5, t }

X

X

X

44 X

35 X

59 X X 51

X 34

delmin

X 33 X

32

24

s

3

t

2

6

7

4

5

Page 35: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

35

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 5, 6, 7 }

Q = { 4, t }

X

X

X

44 X

35 X

59 X X 51

X 34

24

X 50

X 45

X 33 X

32

s

3

t

2

6

7

4

5

Page 36: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

36

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 5, 6, 7 }

Q = { 4, t }

X

X

X

44 X

35 X

59 X X 51

X 34

24

X 50

X 45

delmin

X 33 X

32

s

3

t

2

6

7

4

5

Page 37: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

37

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 4, 5, 6, 7 }

Q = { t }

X

X

X

44 X

35 X

59 X X 51

X 34

24

X 50

X 45

X 33 X

32

s

3

t

2

6

7

4

5

Page 38: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

38

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 4, 5, 6, 7 }

Q = { t }

X

X

X

44 X

35 X

59 X X 51

X 34

X 50

X 45

delmin

X 33 X

32

24

s

3

t

2

6

7

4

5

Page 39: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

39

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 4, 5, 6, 7, t }

Q = { }

X

X

X

44 X

35 X

59 X X 51

X 34

X 50

X 45

X 33 X

32

s

3

t

2

6

7

4

5

Page 40: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

40

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15

5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 4, 5, 6, 7, t }

Q = { }

X

X

X

44 X

35 X

59 X X 51

X 34

X 50

X 45

X 33 X

32

s

3

t

2

6

7

4

5

Page 41: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Shortest Paths wrap-up

A.A. 2012/2013 Tecniche di programmazione 41

Algorithm Problem Efficiency Limitation

Floyd-Warshall AP O(𝑉3) No negative cycles

Bellman–Ford SS O(𝑉 ∙ 𝐸) No negative cycles

Repeated- Bellman-Ford AP O(𝑉2 ∙ 𝐸) No negative cycles

Dijkstra SS O(𝐸 + 𝑉 ∙ log𝑉) No negative edges

Repeated Dijkstra AP O(𝑉 ∙ 𝐸 + 𝑉2 ∙ log𝑉) No negative edges

Page 42: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

JGraphT

A.A. 2012/2013 Tecniche di programmazione 42

public class FloydWarshallShortestPaths<V,E>

public class BellmanFordShortestPath<V,E>

public class DijkstraShortestPath<V,E>

// APSP

List<GraphPath<V,E>> getShortestPaths(V v)

GraphPath<V,E> getShortestPath(V a, V b)

// SSSPs

GraphPath<V,E> getPath()

Page 43: Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm

Licenza d’uso

A.A. 2012/2013 Tecniche di programmazione 43

Queste diapositive sono distribuite con licenza Creative Commons “Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”

Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,

rappresentare, eseguire e recitare quest'opera

di modificare quest'opera

Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori

originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.

Non commerciale — Non puoi usare quest'opera per fini commerciali.

Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.

http://creativecommons.org/licenses/by-nc-sa/3.0/