CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic...

29
CSE 326: Data Structures Dijkstra’s Algorithm James Fogarty Autumn 2007

Transcript of CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic...

Page 1: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

CSE 326: Data StructuresDijkstra’s Algorithm

James FogartyAutumn 2007

Page 2: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

2

Dijkstra, Edsger Wybe

Legendary figure in computer science; was a professor at University of Texas.

Supported teaching introductory computer courses without computers (pencil and paper programming)

Supposedly wouldn’t (until very late in life) read his e-mail; so, his staff had to print out messages and put them in his box.

E.W. Dijkstra (1930-2002)

1972 Turning Award Winner, Programming Languages, semaphores, and …

Page 3: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

3

Dijkstra’s Algorithm: Idea

Adapt BFS to handle weighted graphs

Two kinds of vertices:– Finished or known

vertices• Shortest distance

hasbeen computed

– Unknown vertices• Have tentative

distance

Page 4: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

4

Dijkstra’s Algorithm: Idea

At each step:1) Pick closest unknown

vertex2) Add it to known

vertices3) Update distances

Page 5: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

5

Dijkstra’s Algorithm: Pseudocode

Initialize the cost of each node to ∞

Initialize the cost of the source to 0

While there are unknown nodes left in the graphSelect an unknown node b with the lowest costMark b as knownFor each node a adjacent to b

a’s cost = min(a’s old cost, b’s cost + cost of (b, a))a’s prev path node = b

Page 6: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

6

Important Features

• Once a vertex is made known, the cost of the shortest path to that node is known

• While a vertex is still not known, another shorter path to it might still be found

• The shortest path itself can found by following the backward pointers stored in node.path

Page 7: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

7

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2 2 3

110 23

111

7

1

9

2

4

Vertex Visited? Cost Found byA 0B ??C ??D ??E ??F ??G ??H ??

Page 8: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

8

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2

1

2 2 3

110 23

111

7

1

9

2

4

4

Vertex Visited? Cost Found byA Y 0

AAA

B <=2C <=1D <=4E ??F ??G ??H ??

Page 9: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

9

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2

1

12

2 2 3

110 23

111

7

1

9

2

4

4

Vertex Visited? Cost Found byA Y 0

AAAC

B <=2C Y 1D <=4E <=12F ??G ??H ??

Page 10: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

10

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2 4

1

12

2 2 3

110 23

111

7

1

9

2

4

4

Vertex Visited? Cost Found byA Y 0

AAACB

B Y 2C Y 1D <=4E <=12F <=4G ??H ??

Page 11: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

11

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2 4

1

12

2 2 3

110 23

111

7

1

9

2

4

4

Vertex Visited? Cost Found byA Y 0

AAACB

B Y 2C Y 1D Y 4E <=12F <=4G ??H ??

Page 12: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

12

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2 4 7

1

12

2 2 3

110 23

111

7

1

9

2

4

4

Vertex Visited? Cost Found byA Y 0

AAACB

F

B Y 2C Y 1D Y 4E <=12F Y 4G ??H <=7

Page 13: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

13

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2 4 7

1

12

8

2 2 3

110 23

111

7

1

9

2

4

4

Vertex Visited? Cost Found byA Y 0

AAACBHF

B Y 2C Y 1D Y 4E <=12F Y 4G <=8H Y 7

Page 14: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

14

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2 4 7

1

11

8

2 2 3

110 23

111

7

1

9

2

4

4

Vertex Visited? Cost Found byA Y 0

AAAGBHF

B Y 2C Y 1D Y 4E <=11F Y 4G Y 8H Y 7

Page 15: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

15

Dijkstra’s Algorithm in actionA B

DC

F H

E

G

0 2 4 7

1

11

8

2 2 3

110 23

111

7

1

9

2

4

4

Vertex Visited? Cost Found byA Y 0

AAAGBHF

B Y 2C Y 1D Y 4E Y 11F Y 4G Y 8H Y 7

Page 16: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

16

Your turn

v3

v6

v1

v2 v4

v5

v0s

1

2

2

21

1 1

5 3

5

6

10

V Visited? Cost Found by

v0

v1

v2

v3

v4

v5

v6

Page 17: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

17

Dijkstra’s Alg: Implementation

Initialize the cost of each node to ∞Initialize the cost of the source to 0While there are unknown nodes left in the graph

Select the unknown node b with the lowest costMark b as knownFor each node a adjacent to b

a’s cost = min(a’s old cost, b’s cost + cost of (b, a))a’s prev path node = b (if we updated a’s cost)

What data structures should we use?

Running time?

Page 18: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

18

void Graph::dijkstra(Vertex s){Vertex v,w;

Initialize s.dist = 0 and set dist of all other vertices to infinity

while (there exist unknown vertices, find the one b with the smallest distance)b.known = true;

for each a adjacent to bif (!a.known)

if (b.dist + weight(b,a) < a.dist){a.dist = (b.dist + weight(b,a));a.path = b;

}}

}

Sounds like deleteMin on

a heap…Sounds like adjacency

listsSounds like

decreaseKey

Running time: O(|E| log |V|) – there are |E| edges to examine, and each one causes a heap operation of time O(log |V|)

Page 19: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

19

Dijkstra’s Algorithm: Summary

• Classic algorithm for solving SSSP in weighted graphs without negative weights

• A greedy algorithm (irrevocably makes decisions without considering future consequences)

• Intuition for correctness:– shortest path from source vertex to itself is 0– cost of going to adjacent nodes is at most edge weights– cheapest of these must be shortest path to that node– update paths for new node and continue picking cheapest path

Page 20: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

20

The Known Cloud

V

Next shortest path from inside the known cloud

W

Better path to V? No!

Correctness: The Cloud Proof

Source

How does Dijkstra’s decide which vertex to add to the Known set next?• If path to V is shortest, path to W must be at least as long

(or else we would have picked W as the next vertex)• So the path through W to V cannot be any shorter!

Page 21: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

21

Correctness: Inside the CloudProve by induction on # of nodes in the cloud:

Initial cloud is just the source with shortest path 0

Assume: Everything inside the cloud has the correct shortest path

Inductive step: Only when we prove the shortest path to some node v (which is not in the cloud) is correct, we add it to the cloud

When does Dijkstra’s algorithm not work?

Page 22: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

22

The Trouble with Negative Weight Cycles

A B

C D

E

2 10

1-5

2

What’s the shortest path from A to E?

Problem?

Page 23: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

23

Dijkstra’s vs BFSAt each step:

1) Pick closest unknown vertex2) Add it to finished vertices3) Update distances

Dijkstra’s Algorithm

At each step:1) Pick vertex from queue2) Add it to visited vertices3) Update queue with neighbors

Breadth-first Search

Some Similarities:

Page 24: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

24

Single-Source Shortest Path• Given a graph G = (V, E) and a single

distinguished vertex s, find the shortest weighted path from s to every other vertex in G.

All-Pairs Shortest Path:• Find the shortest paths between all

pairs of vertices in the graph.• How?

Page 25: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

25

Analysis• Total running time for Dijkstra’s:

O(|V| log |V| + |E| log |V|) (heaps)

What if we want to find the shortest path from each point to ALL other points?

Page 26: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

26

Dynamic Programming

Algorithmic technique that systematically records the answers to sub-problems in a table and re-uses those recorded results (rather than re-computing them).

Simple Example: Calculating the Nth Fibonacci number.

Fib(N) = Fib(N-1) + Fib(N-2)

Page 27: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

27

Floyd-Warshallfor (int k = 1; k =< V; k++)for (int i = 1; i =< V; i++)for (int j = 1; j =< V; j++)if ( ( M[i][k]+ M[k][j] ) < M[i][j] )

M[i][j] = M[i][k]+ M[k][j]

Invariant: After the kth iteration, the matrix includes the shortest paths for all pairs of vertices (i,j) containing only vertices 1..k as intermediate vertices

Page 28: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

28

a b c d e

a 0 2 - -4 -

b - 0 -2 1 3

c - - 0 - 1

d - - - 0 4

e - - - - 0

b

c

d e

a 2-2

1

31-4

4

Initial state of the matrix:

M[i][j] = min(M[i][j], M[i][k]+ M[k][j])

Page 29: CSE 326: Data Structures Dijkstra’s Algorithm€¦ · v1 v2 v3 v4 v5 v6. 17 ... • Classic algorithm for solving SSSP in weighted graphs without negative weights •A greedy algorithm

29

a b c d ea 0 2 0 -4 0b - 0 -2 1 -1c - - 0 - 1d - - - 0 4e - - - - 0

b

c

d e

a 2-2

1

31-4

4

Floyd-Warshall -for All-pairs shortest path

Final Matrix Contents