Top Banner
1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem for a directed graph with nonnegative edge weights.
27

1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

Dec 27, 2015

Download

Documents

Jason Norman
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

1

Dijkstras Algorithm

Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem for a directed graph with nonnegative edge weights.

Page 2: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

2

Dijkstras Algorithm

It should be noted that distance between nodes can also be referred to as weight.

Create a distance list, a previous vertex list, a visited list, and a current vertex.

All the values in the distance list are set to infinity except the starting vertex which is set to zero.

All values in visited list are set to false. All values in the previous list are set to a special value signifying that

they are undefined, such as null. Current vertex is set as the starting vertex. Mark the current vertex as visited. Update distance and previous lists based on those vertices which can

be immediately reached from the current vertex. Update the current vertex to the unvisited vertex that can be reached by

the shortest path from the starting vertex. Repeat (from step 6) until all nodes are visited.

Page 3: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

3

Dijkstra’s

It works by keeping for each vertex v the cost d[v] of the shortest path found so far between s and v.

Initially, this value is 0 for the source vertex s (d[s]=0), and infinity for all other vertices, representing the fact that we do not know any path leading to those vertices (d[v]=∞ for every v in V, except s).

When the algorithm finishes, d[v] will be the cost of the shortest path from s to v — or infinity, if no such path exists.

Page 4: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

4

Dijkstra’s

The basic operation of Dijkstra's algorithm is edge relaxation: if there is an edge from u to v, then the shortest known path

from s to u (d[u]) can be extended to a path from s to v by adding edge (u,v) at the end.

This path will have length d[u]+w(u,v). If this is less than the current d[v], we can replace the

current value of d[v] with the new value. Edge relaxation is applied until all values d[v] represent the

cost of the shortest path from s to v. The algorithm is organized so that each edge (u,v) is relaxed only once, when d[u] has reached its final value.

Page 5: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

5

Dijkstra’s

The algorithm maintains two sets of vertices S and Q. Set S contains all vertices for which we know that the value

d[v] is already the cost of the shortest path and set Q contains all other vertices.

Set S starts empty, and in each step one vertex is moved from Q to S.

This vertex is chosen as the vertex with lowest value of d[u]. When a vertex u is moved to S, the algorithm relaxes every outgoing edge (u,v).

Page 6: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

6

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

Page 7: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

7

Dijkstra's Shortest Path Algorithms 0

2 ∞

3 ∞

4 ∞

5 ∞

6 ∞

7 ∞

t ∞

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 null

2 null

3 null

4 null

5 null

6 null

7 null

t null

d

pi

Page 8: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

8

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 }

Extract_Min

Page 9: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

9

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

Page 10: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

10

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

Extract_MinExtract_Min

Page 11: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

11

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

Page 12: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

12

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

Page 13: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

13

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

Extract_MinExtract_Min

Page 14: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

14

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

44X

X

32

Page 15: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

15

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

44X

Extract_MinExtract_Min

X 33X

32

Page 16: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

16

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

44X

35X

59 X

24

X 33X

32

Page 17: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

17

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

44X

35X

59 X

Extract_MinExtract_Min

X 33X

32

Page 18: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

18

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

44X

35X

59 XX 51

X 34

X 33X

32

Page 19: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

19

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

44X

35X

59 XX 51

X 34

Extract_MinExtract_Min

X 33X

32

24

Page 20: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

20

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

44X

35X

59 XX 51

X 34

24

X 50

X 45

X 33X

32

Page 21: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

21

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

44X

35X

59 XX 51

X 34

24

X 50

X 45

Extract_MinExtract_Min

X 33X

32

Page 22: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

22

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

44X

35X

59 XX 51

X 34

24

X 50

X 45

X 33X

32

Page 23: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

23

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

44X

35X

59 XX 51

X 34

X 50

X 45

Extract_MinExtract_Min

X 33X

32

24

Page 24: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

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

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

X

X

X

44X

35X

59 XX 51

X 34

X 50

X 45

X 33X

32

Page 25: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

25

Dijkstra’s Pseudo Code•u := Extract_Min(Q) searches for the vertex u in the vertex set Q that has the least d[u] value. •That vertex is removed from the set Q and returned to the user.

If we are only interested in a shortest path between vertices s and t, we can terminate the search at line 9 if u = t.

Page 26: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

26

Dijkstra’s Shortest Path

Through iteration, we can find the shortest path

Page 27: 1 Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path.

27

Dijkstra Complexity

We can express the running time of Dijkstra's algorithm on a graph with E edges and V vertices as a function of |E| and |V| using the Big-O notation.

The simplest implementation of the Dijkstra's algorithm stores vertices of set Q in an ordinary linked list or array, and operation Extract-Min(Q) is simply a linear search through all vertices in Q. In this case, the running time is O(V2).

For sparse graphs, that is, graphs with much less than V2 edges, Dijkstra's algorithm can be implemented more efficiently by storing the graph in the form of adjacency lists and using a: Binary heap Fibonacci heap

as a priority queue to implement the Extract-Min function. With a binary heap, the algorithm requires O((E+V) lg V) time, and the Fibonacci heap improves this to O(E + V lg V).