dijkstra’s algorithm: single source shortest paths david kauchak cs62 spring 2010

37
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Upload: duane-harmon

Post on 13-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Dijkstra’s Algorithm:single source shortest paths

David Kauchak

cs62

Spring 2010

Page 2: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

What is the shortest path from a to d?

A

B

C E

D

Page 3: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

BFS

A

B

C E

D

Page 4: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

What is the shortest path from a to d?

A

B

C E

D

1

1

3

2

23

4

Page 5: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

23

4

Page 6: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

23

4

A

B

C E

D

Page 7: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

We can still use BFS

A

B

C E

D

Page 8: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

What is the problem?

A

B

C E

D

Page 9: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths Running time is dependent on the weights

A

B

C4

1

2

A

B

C200

50

100

Page 10: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

A

B

C200

50

100

A

B

C

Page 11: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

A

B

C

Page 12: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

A

B

C

Page 13: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths

A

B

C

Nothing will change as we expand the frontier until we’ve gone out 100 levels

Page 14: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Dijkstra’s algorithmmap<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Uses a priority queue to keep track of the next shortest path from the starting vertex

Vertices are kept in three sets:• “visited”: those vertices who’s

correct paths have been found. This occurs when a vertex is popped off the queue

• “frontier”: those vertices that we know about and have a path for, but not necessarily the vertices’ shortest paths. Vertices on the frontier are in the queue

• “rest”: the remaining vertices that we have not seen yet

Page 15: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Dijkstra’s algorithmmap<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

enqueue start; while (queue not empty) { dequeue v;      if (v is not visited) {      visit v;      enqueue all of v’s neighbors;    } }

BFS

Page 16: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Dijkstra’s algorithmmap<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

enqueue start; while (queue not empty) { dequeue v;      if (v is not visited) {      visit v;      enqueue all of v’s neighbors;    } }

BFS

- “parents” keeps track of shortest path

- only keep track of what the next vertex on the shortest path is

Page 17: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Dijkstra’s algorithmmap<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

enqueue start; while (queue not empty) { dequeue v;      if (v is not visited) {      visit v;      enqueue all of v’s neighbors;    } }

BFS

Page 18: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Dijkstra’s algorithmmap<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

enqueue start; while (queue not empty) { dequeue v;      if (v is not visited) {      visit v;      enqueue all of v’s neighbors;    } }

BFS

Page 19: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Dijkstra’s algorithmmap<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

enqueue start; while (queue not empty) { dequeue v;      if (v is not visited) {      visit v;      enqueue all of v’s neighbors;    } }

BFS

Page 20: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Page 21: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap

A 0

Parent

A: A

Page 22: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: A

Page 23: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: A

Page 24: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: A

B 3

Page 25: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: A

B 3

Page 26: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: AC: A

C 1B 3

Page 27: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: AC: A

B 3

Page 28: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: AC: A

B 3

Page 29: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: AC: A

B 3

Page 30: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: CC: A

B 2

Page 31: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: CC: A

B 2

Page 32: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: CC: AE: C

B 2E 5

Page 33: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: CC: AE: C

B 2E 5

Frontier: all nodes reachable from starting node within a given distance

Page 34: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: CC: AD: BE: B

E 3D 5

Page 35: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: CC: AD: BE: B

D 5

Page 36: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

3

21

4

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: CC: AD: BE: B

Page 37: Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

A

B

C E

D

1

1

3

1

map<int,int> shortest paths(int start, const map<int,list<pair<int,int> > > & graph) { map<int,int> parents; priorityqueue62 frontier;

parents[start]=start; frontier.push(start, 0);

while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents;}

Heap Parent

A: AB: CC: AD: BE: B