16
CS 225 Data Structures April 25 – Dijkstra’s Algorithm Wade Fagen-Ulmschneider

PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

CS 225Data Structures

April 25 – Dijkstra’s AlgorithmWade Fagen-Ulmschneider

Page 2: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Shortest Path

Page 3: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM
Page 4: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

A

C

D

E

B

F G

H7

54

107

5

3

6

25

4

3

DijkstraSSSP(G, s):

foreach (Vertex v : G):

d[v] = +inf

p[v] = NULL

d[s] = 0

PriorityQueue Q // min distance, defined by d[v]

Q.buildHeap(G.vertices())

Graph T // "labeled set"

repeat n times:

Vertex u = Q.removeMin()

T.add(u)

foreach (Vertex v : neighbors of u not in T):

if _______________ < d[v]:

d[v] = __________________

p[v] = m

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Page 5: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

A

C

D

E

B

F G

H7

54

107

5

3

6

25

4

3

DijkstraSSSP(G, s):

foreach (Vertex v : G):

d[v] = +inf

p[v] = NULL

d[s] = 0

PriorityQueue Q // min distance, defined by d[v]

Q.buildHeap(G.vertices())

Graph T // "labeled set"

repeat n times:

Vertex u = Q.removeMin()

T.add(u)

foreach (Vertex v : neighbors of u not in T):

if cost(u, v) + d[u] < d[v]:

d[v] = cost(u, v) + d[u]

p[v] = m

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Page 6: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

A

C

D

E

B

F G

H7

54

107

5

3

6

25

4

3

Dijkstra gives us the shortest path from our path (single source) to every connected vertex!

A B C D E F G H

NULL A B B F A F C

0 10 17 15 12 7 11 21

p:

d:

Page 7: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

A

FD E

B

C G

H

10

Q: How does Dijkstra handle a single heavy-weight path vs. many light-weight paths?

1

11 1 1

1

1

Page 8: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

A

FD E

B

C G

H

5

Q: How does Dijkstra handle a single heavy-weight path vs. many light-weight paths?

1

11 1 1

1

1

Page 9: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

A

FD E

B

C G

H

3

Q: How does Dijkstra handle undirected graphs?

1

11 1 1

1

1

Page 10: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

A

C

D

E

B

F G

H7

54

107

-5

3

-6

25

4

3

Q: How does Dijkstra handle negative weight cycles?

Page 11: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

A

C

D

E

B

F G

H7

54

107

5

3

6

-22

3

3

Q: How does Dijkstra handle negative weight edges, without a negative weight cycle?

Page 12: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Dijkstra’s Algorithm (SSSP)

DijkstraSSSP(G, s):

foreach (Vertex v : G):

d[v] = +inf

p[v] = NULL

d[s] = 0

PriorityQueue Q // min distance, defined by d[v]

Q.buildHeap(G.vertices())

Graph T // "labeled set"

repeat n times:

Vertex u = Q.removeMin()

T.add(u)

foreach (Vertex v : neighbors of u not in T):

if cost(u, v) + d[u] < d[v]:

d[v] = cost(u, v) + d[u]

p[v] = m

return T

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

What is Dijkstra’s running time?

Page 13: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Landmark Path ProblemSuppose you want to travel from A to G.Q1: What is the shortest path from A to G?

A

C

D

E

B

F G

H3

33

33

3

3

3

33

3

3

J

K

L

M

3

3

3

3

3

3

3

3

Page 14: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Landmark Path ProblemSuppose you want to travel from A to G.Q2: What is the fastest algorithm to use to find the shortest path?

A

C

D

E

B

F G

H3

33

33

3

3

3

33

3

3

J

K

L

M

3

3

3

3

3

3

3

3

Page 15: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Landmark Path ProblemIn your journey between A and G, you also want to visit the landmark L.Q3: What is the shortest path from A to G that visits L?

A

C

D

E

B

F G

H3

33

33

3

3

3

33

3

3

J

K

L

M

3

3

3

3

3

3

3

3

Page 16: PowerPoint Presentation...3 3 3 3 3. Title: PowerPoint Presentation Author: Wade Fagen Created Date: 4/25/2018 10:20:04 AM

Landmark Path ProblemIn your journey between A and G, you also want to visit the landmark L.Q4: What is the fastest algorithm to find this path?Q5: What are the specific call(s) to this algorithm?

A

C

D

E

B

F G

H3

33

33

3

3

3

33

3

3

J

K

L

M

3

3

3

3

3

3

3

3