• No results found

Dijkstra

N/A
N/A
Protected

Academic year: 2020

Share "Dijkstra"

Copied!
78
0
0

Loading.... (view fulltext now)

Full text

(1)

Dijkstra’s Algorithm

Used to find the shortest path between vertices

in a directed graph

Does not work with negative edges

(2)

Algorithm Steps

Start with a matrix for the distances between

the source and all other vertices

Initialize all distances to infinity

Initialize the source vertex to 0

Extract the minimum vertex

Vertex with the lightest edge

Relax the edges from the vertex that was

extracted

(3)

Complexity

Can be O(V

2

) or O (E log(V))

E = edges

V = vertices

(4)

Graph

A B C

D E F

G H I

(5)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

(6)

Extractable Set

(7)

Iteration 1

(8)

Extractable Set

A = 0

(9)

Graph

9

A B C

D E F

G H I

(10)

Relaxation

The distance from A to B is 7 because

The distance from A to A is zero

The weight of edge (A, B) is 7

0 + 7 = 7

The distance from A to D is 20 because

(11)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

(12)

Graph

A B C

D E F

G H I

(13)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

(14)

Extractable Set

(15)

Iteration 2

(16)

Extractable Set

B = 7

D = 20

(17)

Graph

17

A B C

D E F

G H I

(18)

Relaxation

The distance from A to C is 10 because

The distance from A to B is 7

The weight of edge (B, C) is 3

7 + 3 = 10

The distance from A to E is 9 because

(19)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

(20)

Graph

A B C

D E F

G H I

(21)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

(22)

Extractable Set

C = 10

D = 20

(23)

Iteration 3

(24)

Extractable Set

C = 10

D = 20

E = 9

(25)

Graph

25

A B C

D E F

G H I

(26)

Relaxation

The distance from A to H is 27 because

The distance from A to E is 9

The weight of edge (E, H) is 18

9 + 18 = 27

The distance from A to D is 20 because

The distance from A to E is 9

The weight of edge (E, D) is 14

(27)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

(28)

Graph

A B C

D E F

G H I

(29)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

(30)

Extractable Set

C = 10

D = 20

(31)

Iteration 4

(32)

Extractable Set

C = 10

D = 20

H = 27

(33)

Graph

33

A B C

D E F

G H I

Extract vertex C and relax all edges outgoing from vertex C

(34)

Relaxation

The distance from A to F is 13 because

(35)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

(36)

Graph

A B C

D E F

G H I

(37)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

(38)

Extractable Set

D = 20

F = 13

(39)

Iteration 5

(40)

Extractable Set

D = 20

F = 13

H = 27

(41)

Graph

41

A B C

D E F

G H I

(42)

Relaxation

The distance from A to E does not change

Vertex E was already extracted

There are no negative edges

The distance from A to I is 38 because

The distance from A to F is 13

The weight of edge (F, I) is 25

(43)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

(44)

Graph

A B C

D E F

G H I

(45)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

(46)

Extractable Set

D = 20

H = 27

(47)

Iteration 6

(48)

Extractable Set

D = 20

H = 27

I = 38

(49)

Graph

49

A B C

D E F

G H I

(50)

Relaxation

The distance from A to G is 28 because

(51)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

D 0 7 10 20 9 13 28 27 38

(52)

Graph

A B C

D E F

G H I

(53)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

D 0 7 10 20 9 13 28 27 38

(54)

Extractable Set

(55)

Iteration 7

(56)

Extractable Set

G = 28

H = 27

I = 38

(57)

Graph

57

A B C

D E F

G H I

(58)

Relaxation

The distance from A to I is still 38 because

The distance from A to H is 27

The weight of edge (H, I) is 58

27 + 58 = 85

(59)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

D 0 7 10 20 9 13 28 27 38

H 0 7 10 20 9 13 28 27 38

(60)

Graph

A B C

D E F

G H I

(61)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

D 0 7 10 20 9 13 28 27 38

H 0 7 10 20 9 13 28 27 38

(62)

Extractable Set

(63)

Iteration 8

(64)

Extractable Set

G = 28

I = 38

(65)

Graph

65

A B C

D E F

G H I

(66)

Relaxation

The distance from A to G is still 28 because

(67)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

D 0 7 10 20 9 13 28 27 38

H 0 7 10 20 9 13 28 27 38

G 0 7 10 20 9 13 28 27 38

(68)

Graph

A B C

D E F

G H I

(69)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

D 0 7 10 20 9 13 28 27 38

H 0 7 10 20 9 13 28 27 38

G 0 7 10 20 9 13 28 27 38

(70)

Extractable Set

(71)

Iteration 9

(72)

Extractable Set

I = 38

(73)

Graph

73

A B C

D E F

G H I

(74)

Relaxation

(75)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

D 0 7 10 20 9 13 28 27 38

H 0 7 10 20 9 13 28 27 38

G 0 7 10 20 9 13 28 27 38

I 0 7 10 20 9 13 28 27 38

(76)

Graph

A B C

D E F

G H I

(77)

Table

A B C D E F G H I

x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞

B 0 7 10 20 9 ∞ ∞ ∞ ∞

E 0 7 10 20 9 ∞ ∞ 27 ∞

C 0 7 10 20 9 13 ∞ 27 ∞

F 0 7 10 20 9 13 ∞ 27 38

D 0 7 10 20 9 13 28 27 38

H 0 7 10 20 9 13 28 27 38

G 0 7 10 20 9 13 28 27 38

I 0 7 10 20 9 13 28 27 38

(78)

Extractable Set

None

References

Related documents