79
Shortest Path Problem I SWE2016-44

Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

  • Upload
    others

  • View
    33

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Shortest Path Problem I

SWE2016-44

Page 2: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Problem Statement

2

Find a path between two vertices in a graph such that the sum of the weights of its constituent edges is minimized.

A graph is a series of nodes connected by edges. Graphs can be weighted and directional.

Page 3: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Shortest Path Algorithms

3

• Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight.

• Bellman–Ford algorithm: solves the single-source problem if edge weights may be negative.

• A* search algorithm: solves for single pair shortest path using heuristics to try to speed up the search.

• Floyd–Warshall algorithm: solves all pairs shortest paths.

• Johnson's algorithm: solves all pairs shortest paths, and may be faster than Floyd–Warshall on sparse graphs.

• Viterbi algorithm: solves the shortest stochastic path problem with an additional probabilistic weight on each node.

Page 4: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

4

Page 5: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

5

C

A

B

D

10

20

5

20

16

10

15

26

0

Source

Similar to Prim’s Algorithm

Page 6: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

6

1. Create an empty set (sptSet) that keeps track of vertices included in shortest path tree

2. Initialize all vertices distances as INFINITE except for the source vertex. Initialize the source distance=0.

3. While sptSet doesn’t include all vertices

1) Pick a vertex u which is not there in sptSet and has minimum distance value.

2) Include u to the set.

3) Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value of u and weight of edge u-v, is less than the distance value of v, then update the distance value of v.

Page 7: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

7

Source

Vertex d 𝝅

A 0 NIL

B ∞ NIL

C ∞ NIL

D ∞ NIL

E ∞ NIL

F ∞ NIL

G ∞ NIL

H ∞ NIL

I ∞ NIL

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

Page 8: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

8

Source

Vertex d 𝝅

A 0 NIL

B ∞ NIL

C ∞ NIL

D ∞ NIL

E ∞ NIL

F ∞ NIL

G ∞ NIL

H ∞ NIL

I ∞ NIL

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

Update B: d[u]+4=4 < ∞Update H: d[u]+8=8 < ∞

u

0

Page 9: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

9

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C ∞ NIL

D ∞ NIL

E ∞ NIL

F ∞ NIL

G ∞ NIL

H 8 A

I ∞ NIL

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

Update B: d[u]+4=4 < ∞Update H: d[u]+8=8 < ∞

u

0

Page 10: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

10

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C ∞ NIL

D ∞ NIL

E ∞ NIL

F ∞ NIL

G ∞ NIL

H 8 A

I ∞ NIL

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

u

4

Update C: d[u]+8=12 < ∞Update H: d[u]+11=15 > 8

Page 11: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

11

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D ∞ NIL

E ∞ NIL

F ∞ NIL

G ∞ NIL

H 8 A

I ∞ NIL

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

u

4

Update C: d[u]+8=12 < ∞Update H: d[u]+11=15 > 8

Page 12: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

12

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D ∞ NIL

E ∞ NIL

F ∞ NIL

G ∞ NIL

H 8 A

I ∞ NIL

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

u

8

Update G: d[u]+1=9 < ∞Update I: d[u]+7=15 < ∞

Page 13: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

13

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D ∞ NIL

E ∞ NIL

F ∞ NIL

G 9 H

H 8 A

I 15 H

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

u

8

Update G: d[u]+1=9 < ∞Update I: d[u]+7=15 < ∞

Page 14: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

14

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D ∞ NIL

E ∞ NIL

F ∞ NIL

G 9 H

H 8 A

I 15 H

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

u8 9

Update F: d[u]+2=11 < ∞Update I: d[u]+6=15 = 15

Page 15: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

15

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D ∞ NIL

E ∞ NIL

F 11 G

G 9 H

H 8 A

I 15 H

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

u8 9

Update F: d[u]+2=11 < ∞Update I: d[u]+6=15 = 15

Page 16: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

16

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D ∞ NIL

E ∞ NIL

F 11 G

G 9 H

H 8 A

I 15 H

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

u8 9 11Update C: d[u]+4=15 > 12

Update D: d[u]+14=25 < ∞Update E: d[u]+10=21 < ∞

Page 17: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

17

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 25 F

E 21 F

F 11 G

G 9 H

H 8 A

I 15 H

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

u8 9 11Update C: d[u]+4=15 > 12

Update D: d[u]+14=25 < ∞Update E: d[u]+10=21 < ∞

Page 18: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

18

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 25 F

E 21 F

F 11 G

G 9 H

H 8 A

I 15 H

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4 u

8 9 11

12

Update D: d[u]+7=19 < 25

Update I: d[u]+2=14 < 15

Page 19: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

19

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 19 C

E 21 F

F 11 G

G 9 H

H 8 A

I 14 C

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4 u

8 9 11

12

Update D: d[u]+7=19 < 25

Update I: d[u]+2=14 < 15

Page 20: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

20

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 19 C

E 21 F

F 11 G

G 9 H

H 8 A

I 14 C

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

u

8 9 11

12

14

Page 21: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

21

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 19 C

E 21 F

F 11 G

G 9 H

H 8 A

I 14 C

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4 u

8 9 11

12

14

19

Update E: d[u]+9=28 > 21

Page 22: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

22

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 19 C

E 21 F

F 11 G

G 9 H

H 8 A

I 14 C

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

8 9 11

12

14

19

u

21

Page 23: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

23

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 19 C

E 21 F

F 11 G

G 9 H

H 8 A

I 14 C

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

8 9 11

12

14

19

21

Page 24: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

24

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 19 C

E 21 F

F 11 G

G 9 H

H 8 A

I 14 C

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

11

2

67

144

10

9

0

4

8 9 11

12

14

19

21

Page 25: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Dijkstra's algorithm

25

Source

Vertex d 𝝅

A 0 NIL

B 4 A

C 12 B

D 19 C

E 21 F

F 11 G

G 9 H

H 8 A

I 14 C

distance parent

H

A

B

G

C

F

D

EI

4

8 7

1 2

8

2

10

0

4

8 9 11

12

14

19

21

Page 26: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Complexity

26

Time Complexity: O(V2)

If the input graph is represented using adjacency list, it can be reduced to O(E log V) with the help of binary heap.

Page 27: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Pseudo-Code

27

𝑑 𝑠 ← 0𝐟𝐨𝐫 each 𝑣 ∈ 𝑉 − 𝑠 :

𝐝𝐨 𝑑 𝑣 ← ∞𝑆 ← ∅𝑄 ← 𝑉𝐰𝐡𝐢𝐥𝐞 𝑄 ≠ ∅:

𝐝𝐨 𝑢 ← Extract − Min 𝑄 :𝑆 ← 𝑆 ∪ 𝑢𝐟𝐨𝐫 each 𝑣 ∈ 𝐴𝑑𝑗 𝑢 :

𝐢𝐟 𝑑 𝑣 > 𝑑 𝑢 + 𝑤 𝑢, 𝑣 :𝑑 𝑣 = 𝑑 𝑢 + 𝑤(𝑢, 𝑣)

Page 28: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Implementation

28

Page 29: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

29

Page 30: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

30

1. Initialize distances from source to all vertices as infinite and distance to source itself as 0. Create an array dist[] of size |V| with all values as infinite except dist[src] where src is source vertex.

2. Calculate shortest distances. Do following |V|-1 times for each edge u-v:

1) If dist[v] > dist[u]+weight of edge uv, then update dist[v] (=dist[u]+weight of edge uv).

3. This step reports if there is a negative weight cycle in graph. Do following for each edge u-v

1) If dist[v] > dist[u]+weight of edge uv, then “Graph contains negative weight cycle”

Page 31: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

31

Source Vertex d 𝝅

A 0 NIL

B ∞ NIL

C ∞ NIL

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

Let all edges are processed in following order:

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

Page 32: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

32

Source Vertex d 𝝅

A 0 NIL

B ∞ NIL

C ∞ NIL

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(B, E), (D, B), (B, D): d[u]+edge(u,v)=∞ = ∞

∞∞

0

Iteration 1

Page 33: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

33

Source Vertex d 𝝅

A 0 NIL

B ∞ NIL

C ∞ NIL

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(A, B): d[u]+edge(u,v)=0+(-1) < ∞

∞∞

0

Iteration 1

Page 34: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

34

Source Vertex d 𝝅

A 0 NIL

B -1 A

C ∞ NIL

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(A, B): d[u]+edge(u,v)=0+(-1) < ∞

-1

∞∞

0

Iteration 1

Page 35: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

35

Source Vertex d 𝝅

A 0 NIL

B -1 A

C ∞ NIL

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(A, C): d[u]+edge(u,v)=0+4 < ∞

-1

∞∞

0

Iteration 1

Page 36: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

36

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 4 A

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(A, C): d[u]+edge(u,v)=0+4 < ∞

-1

∞4

0

Iteration 1

Page 37: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

37

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 4 A

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(D, C): d[u]+edge(u,v)=∞ > 4

-1

∞4

0

Iteration 1

Page 38: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

38

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 4 A

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(B, C): d[u]+edge(u,v)=(-1)+3 < 4

-1

∞4

0

Iteration 1

Page 39: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

39

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(B, C): d[u]+edge(u,v)=(-1)+3=2 < 4

-1

∞2

0

Iteration 1

Page 40: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

40

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

(E, D): d[u]+edge(u,v)= ∞ = ∞

-1

∞2

0

Iteration 1

Page 41: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

41

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

∞2

0

Iteration 1

Page 42: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

42

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D ∞ NIL

E ∞ NIL

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

∞2

0

Iteration 2

(B, E): d[u]+edge(u,v)=(-1)+2=1 < ∞

Page 43: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

43

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D ∞ NIL

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

∞2

0

Iteration 2

(B, E): d[u]+edge(u,v)=(-1)+2=1 < ∞

Page 44: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

44

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D ∞ NIL

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

∞2

0

Iteration 2

(D, B): d[u]+edge(u,v)=∞ = ∞

Page 45: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

45

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D ∞ NIL

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

∞2

0

Iteration 2

(B, D): d[u]+edge(u,v)=(-1)+2=1 < ∞

Page 46: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

46

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D 1 B

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

12

0

Iteration 2

(B, D): d[u]+edge(u,v)=(-1)+2=1 < ∞

Page 47: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

47

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D 1 B

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

12

0

Iteration 2

(A, B): d[u]+edge(u,v)=0+(-1)=-1 = -1

Page 48: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

48

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D 1 B

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

12

0

Iteration 2

(A, C): d[u]+edge(u,v)=0+4=4 > 2

Page 49: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

49

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D 1 B

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

12

0

Iteration 2

(D, C): d[u]+edge(u,v)=1+5=6 > 2

Page 50: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

50

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D 1 B

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

12

0

Iteration 2

(B, C): d[u]+edge(u,v)=(-1)+3=2 > 2

Page 51: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

51

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D 1 B

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

12

0

Iteration 2

(E, D): d[u]+edge(u,v)=1+(-3)=-2 < 1

Page 52: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

52

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 2

(E, D): d[u]+edge(u,v)=1+(-3)=-2 < 1

Page 53: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

53

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 2

Page 54: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

54

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 3

(B, E): d[u]+edge(u,v)=(-1)+2=1 = 1

Page 55: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

55

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 3

(D, B): d[u]+edge(u,v)=(-2)+1=-1 = -1

Page 56: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

56

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 3

(B, D): d[u]+edge(u,v)=(-1)+2=1 > -2

Page 57: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

57

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 3

(A, B): d[u]+edge(u,v)=0+(-1)=-1 = -1

Page 58: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

58

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 3

(A, C): d[u]+edge(u,v)=0+2=2 = 2

Page 59: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

59

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 3

(D, C): d[u]+edge(u,v)=(-2)+5=3 > 2

Page 60: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

60

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 3

(B, C): d[u]+edge(u,v)=(-1)+3=2 = 2

Page 61: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

61

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 3

(E, D): d[u]+edge(u,v)=1+(-3)=-2 = -2

Page 62: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

62

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

(B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D)

-1

1

-22

0

Iteration 4

Page 63: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

63

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

-1

1

-22

0

Page 64: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

64

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

-1

1

-22

0

Page 65: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

65

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

-1

1

-22

0

Page 66: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

66

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

-1

1

-22

0

Page 67: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman Ford's algorithm

67

Source Vertex d 𝝅

A 0 NIL

B -1 A

C 2 B

D -2 E

E 1 B

distance parent

C

A

B

D

E

-1

5

4

31

-3

2

2

-1

1

-22

0

Page 68: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Complexity

68

Time Complexity: O(VE)

Page 69: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Why do we need |V|-1 times

69

A B C D

0 ∞ ∞ ∞

2 3 2

C → D

B → C

A → B

1st Iteration

A → B

B → C

C → D

Page 70: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Why do we need |V|-1 times

70

A B C D

0 ∞ ∞

2 3 2

C → D

B → C

A → B

1st Iteration 2

A → B

B → C

C → D

Page 71: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Why do we need |V|-1 times

71

A B C D

0 2 ∞ ∞

2 3 2

C → D

B → C

A → B

2nd Iteration

A → B

B → C

C → D

Page 72: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Why do we need |V|-1 times

72

A B C D

0 2 ∞

2 3 2

C → D

B → C

A → B

2nd Iteration 5

A → B

B → C

C → D

Page 73: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Why do we need |V|-1 times

73

A B C D

0 2 5 ∞

2 3 2

C → D

B → C

A → B

3rd Iteration

A → B

B → C

C → D

Page 74: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Why do we need |V|-1 times

74

A B C D

0 2 5 7

2 3 2

C → D

B → C

A → B

3rd Iteration

A → B

B → C

C → D

Page 75: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Bellman‐Ford in practice

75

Distance‐vector routing protocol

– Repeatedly relax edges until convergence

– Relaxation is local!

On the Internet

– Routing Information Protocol (RIP)

– Interior Gateway Routing Protocol (IGRP)

Page 76: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Complexity

76

Time Complexity: O(VE)

Page 77: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Pseudo-Code

77

𝐟𝐨𝐫 𝑣 in 𝑉:𝑣. 𝑑 = ∞𝑣. 𝜋 = None

𝑠. 𝑑 = 0𝐟𝐨𝐫 𝑖 from 1 to |𝑉| − 1:

𝐟𝐨𝐫 (𝑢, 𝑣) in 𝐸:𝐫𝐞𝐥𝐚𝐱 𝑢, 𝑣 :

𝐢𝐟 𝑣. 𝑑 > 𝑢. 𝑑 + 𝑤(𝑢, 𝑣):𝑣. 𝑑 = 𝑢. 𝑑 + 𝑤(𝑢, 𝑣)𝑣. 𝜋 = 𝑢

Page 78: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Implementation

78

Page 79: Shortest Path Problem I - SKKU...Shortest Path Algorithms 3 •Dijkstra's algorithm: solves the single-source shortest path problem with non-negative edge weight. •Bellman–Ford

Reference

• Charles Leiserson and Piotr Indyk, “Introduction to Algorithms”, September 29, 2004

• https://www.geeksforgeeks.org

• https://en.wikipedia.org/wiki