dijkstra’s algorithm: single source shortest paths · dijkstra’s algorithm: single source...

46
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011

Upload: others

Post on 04-Aug-2020

25 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Dijkstra’s Algorithm: single source shortest paths

David Kauchak cs62

Spring 2011

Page 2: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

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 · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

How can we find this?

A

B

C E

D

Page 4: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C E

D

BFS

How does BFS work?

Page 5: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Breadth first search

enqueue start;

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

BFS Looks a lot like DFS

How are they different?

Page 6: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C E

D

BFS

Page 7: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C E

D

BFS

queue

C

B

Page 8: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C E

D

BFS

queue

B

E

Page 9: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C E

D

BFS

queue

E

D

Page 10: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C E

D

BFS

queue

D

Page 11: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C E

D

BFS

queue

Page 12: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C E

D

BFS

queue

Page 13: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths What is the shortest path from a to d?

A

B

C E

D

1

1

3

2

2 3

4

Page 14: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths Can we modify the graph to still use BFS?

A

B

C E

D

1

1

3

2

2 3

4

Page 15: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

2 3

4

A

B

C E

D

Page 16: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

We can still use BFS

A

B

C E

D

Page 17: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

What is the problem?

A

B

C E

D

Page 18: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

Running time is dependent on the weights

A

B

C 4

1

2

A

B

C 200

50

100

Page 19: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C 200

50

100

A

B

C

Page 20: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C

Page 21: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C

Page 22: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Shortest paths

A

B

C

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

Ideas?

Page 23: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Dijkstra’s algorithm 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_key(); 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 24: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Dijkstra’s algorithm 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_key(); 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 25: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Dijkstra’s algorithm 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_key(); 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 26: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Dijkstra’s algorithm 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_key(); 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 27: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Dijkstra’s algorithm 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_key(); 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 28: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

Dijkstra’s algorithm 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_key(); 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 29: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 30: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 31: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 32: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 33: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: A

B 3

Page 34: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: A

B 3

Page 35: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: A C: A

C 1 B 3

Page 36: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: A C: A

B 3

Page 37: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: A C: A

B 3

Page 38: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: A C: A

B 3

Page 39: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: C C: A

B 2

Page 40: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: C C: A

B 2

Page 41: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: C C: A E: C

B 2 E 5

Page 42: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: C C: A E: C

B 2 E 5

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

Page 43: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: C C: A D: B E: B

E 3 D 5

Page 44: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: C C: A D: B E: B

D 5

Page 45: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

A

B

C E

D

1

1

3

3

2 1

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_key(); 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 B: C C: A D: B E: B

Page 46: Dijkstra’s Algorithm: single source shortest paths · Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2011 . Shortest paths What is the shortest path

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_key(); 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 B: C C: A D: B E: B