Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start...

Preview:

Citation preview

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

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

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 }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

25

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

Recommended