26
Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) N = {s}, D s = 0, “s is distance zero from itself” D j =C sj for all j s, distances of directly- connected neighbors Step A: (Find next closest node i ) Find i N such that D i = min Dj for j N Add i to N If N contains all the nodes, stop Step B: (update minimum costs) For each node j N D j = min (D j , D i +C ij ) Minimum distance from s to j through node i in N

Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

Embed Size (px)

Citation preview

Page 1: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

Dijkstra’s algorithmN: set of nodes for which shortest path already foundInitialization: (Start with source node s)

N = {s}, Ds = 0, “s is distance zero from itself”

Dj=Csj for all j s, distances of directly-connected

neighbors

Step A: (Find next closest node i) Find i N such that Di = min Dj for j N Add i to N If N contains all the nodes, stop

Step B: (update minimum costs) For each node j N Dj = min (Dj, Di+Cij) Go to Step A

Minimum distance from s to j through node i in N

Page 2: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

2

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 3: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

3

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 = { }PQ = { s, 2, 3, 4, 5, 6, 7, t }

Page 4: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

4

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 = { }PQ = { s, 2, 3, 4, 5, 6, 7, t }

delmin

Page 5: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

5

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 }PQ = { 2, 3, 4, 5, 6, 7, t }

decrease key

X

X

X

Page 6: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

6

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 }PQ = { 2, 3, 4, 5, 6, 7, t }

X

X

X

delmin

Page 7: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

7

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 }PQ = { 3, 4, 5, 6, 7, t }

X

X

X

Page 8: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

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

15

9

14

0

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

X

X

X

decrease key

X 33

Page 9: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

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

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

X

X

X

X 33

delmin

Page 10: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

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

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

X

X

X

X 33

44X

X

32

Page 11: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

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, 6 }PQ = { 3, 4, 5, 7, t }

X

X

X

44X

delmin

X 33X

32

Page 12: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

12

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 }PQ = { 3, 4, 5, t }

X

X

X

44X

35X

59 X

24

X 33X

32

Page 13: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

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, 6, 7 }PQ = { 3, 4, 5, t }

X

X

X

44X

35X

59 X

delmin

X 33X

32

Page 14: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

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, 3, 6, 7 }PQ = { 4, 5, t }

X

X

X

44X

35X

59 XX 51

X 34

X 33X

32

Page 15: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

15

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 }PQ = { 4, 5, t }

X

X

X

44X

35X

59 XX 51

X 34

delmin

X 33X

32

24

Page 16: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

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, 3, 5, 6, 7 }PQ = { 4, t }

X

X

X

44X

35X

59 XX 51

X 34

24

X 50

X 45

X 33X

32

Page 17: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

17

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 }PQ = { 4, t }

X

X

X

44X

35X

59 XX 51

X 34

24

X 50

X 45

delmin

X 33X

32

Page 18: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

18

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 }PQ = { t }

X

X

X

44X

35X

59 XX 51

X 34

24

X 50

X 45

X 33X

32

Page 19: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

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, 4, 5, 6, 7 }PQ = { t }

X

X

X

44X

35X

59 XX 51

X 34

X 50

X 45

delmin

X 33X

32

24

Page 20: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

20

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 }PQ = { }

X

X

X

44X

35X

59 XX 51

X 34

X 50

X 45

X 33X

32

Page 21: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

21

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 }PQ = { }

X

X

X

44X

35X

59 XX 51

X 34

X 50

X 45

X 33X

32

Page 22: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

Modified Dijkstra’s algorithmDijkstra-aux (G, target-node,sub-path)N: set of nodes for which shortest path already foundInitialization: Start with node s= (pop sub-path)//last node on sub-path

V’ = V – {sub-path} //search over nodes not already in sub-path N = {s}, Ds = 0 for s sub-path, “s is distance zero from itself”

Dj=Csj for all jV’, j s, distances of directly-connected neighbors

Step A: (Find next closest node i) Find i N such that Di = min Dj for j N Add i to N If N contains j=target-node,

– return N, Csj

– Else return //no path to target-node

Step B: (update minimum costs) For each node j N Dj = min (Dj, Di+Cij) Go to Step A

Page 23: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

Modified Dijkstra’s k-Path algorithmDijkstra-recurse (G, target-node, Path, count)

Do while count< k and Path – New-Path = Dijkstra-aux (G, target-node, Path)// min-cost path to

target-node If New-Path //another min-cost path

– count=count+1; Path-set=Path-setNew-Path– E’ = E – {(pop-Path, target-node)//remove edge from graph– New-Path=Dijkstra-aux (G(V,E’), target-node, pop-Path,

count) // graph with edge deleted to prevent finding same path

Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path, count)

End while Return Path-set

Page 24: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

Modified Dijkstra’s k-Path algorithmDijkstra (G, target-node)Initialization: Start with node s= source node

V’ = V – {s} //search over all nodes Path-set = //set of min-cost paths count=0 //path counter Path = {s} Do while count< k and Path

– New-Path = Dijkstra-aux (G, target-node, Path)// min-cost path to target-node

If New-Path //another min-cost path– count=count+1; Path-set=Path-setNew-Path– E’ = E – {(pop-Path, target-node)//remove edge from

graph– New-Path=Dijkstra-aux (G(V,E’), target-node, pop-

Path )// min-cost path to target-node Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path )

End while Return Path-set

Page 25: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

25

Page 26: Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) n N = {s}, D s = 0, “s is distance

Modified Dijkstra’s k-Path algorithmDijkstra-recurse (G, target-node, Path, count)

Do while count< k and Path – New-Path = Dijkstra-aux (G, target-node, Path)// min-cost path to

target-node If New-Path //another min-cost path

– count=count+1; Path-set=Path-setNew-Path Else New-Path=Dijkstra-aux (G(V,E), target-node, pop-Path,

count) End while Return Path-set