CS261&DataStructures& - Oregon State...
Transcript of CS261&DataStructures& - Oregon State...
![Page 1: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/1.jpg)
CS261 Data Structures
Dijkstra’s Algorithm – Edge List Representa@on
![Page 2: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/2.jpg)
Weighted Graphs Representa@on: Edge List
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix Pendleton: {Pueblo:8, Phoenix:4} Pensacola: {Phoenix:5} Peoria: {Pueblo:3, PiEsburgh:5} Phoenix: {Pueblo:3, Peoria:4, PiEsburgh:10} Pierre: {Pendleton:2} PiEsburgh: {Pensacola:4} Princeton: {PiEsburgh:2} Pueblo: {Pierre:3}
2
8
4
3 3
3
4
5
10
5
4
2
What’s reachable AND what is the cheapest cost to get there?
![Page 3: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/3.jpg)
Ini@alize map of reachable ver@ces, and add source vertex,vi, to a priority queue with distance zero While priority queue is not empty
Getmin from priority queue and assign to v If v is not in reachable add v with given cost to map of reachable ver@ces For all neighbors, vj, of v If vj is not is set of reachable ver@ces, combine cost of reaching v with cost to travel from v to vj, and add to priority queue
Dijkstra’s Algorithm
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4 3
3
4
3
5
4 10
8
5
2 Cost -‐First Search
![Page 4: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/4.jpg)
Example: What is the distance from Pierre?
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
2
4 3
3
4
3
5
4 10
8
5
2
Pqueue Reachable
0 pierre(0) {}
![Page 5: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/5.jpg)
Example: What is the distance from Pierre?
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
2
4 3
3
4
3
5
4 10
8
5
2
Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
![Page 6: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/6.jpg)
Example: What is the distance from Pierre? -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4
3
3
4
3
5
4 10
8
5
2
Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
2 phoenix(6), pueblo(10)
pendleton(2)
![Page 7: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/7.jpg)
Example: What is the distance from Pierre? -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4
3
3
4
3
5
4 10
8
5
2
Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
2 phoenix(6), pueble(10)
pendleton(2)
3 pueblo(9), peoria(10), piEsburgh(16), pueblo(10)
phoenix(6)
NOTE: Reachable is only showing the latest node added to the collec@on!
![Page 8: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/8.jpg)
Example: What is the distance from Pierre? -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4
3
3
4
3
5
4 10
8
5
2
Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
2 phoenix(6), pueble(10)
pendleton(2)
3 pueblo(9), peoria(10), piEsburgh(16), pueblo(10)
phoenix(6)
4 peoria(10), piEsburgh(16), pueblo(10)
pueblo(9)
![Page 9: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/9.jpg)
Example: What is the distance from Pierre? -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4
3
3
4
3
5
4 10
8
5
2
Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
2 phoenix(6), pueble(10)
pendleton(2)
3 pueblo(9), peoria(10), piEsburgh(16), pueblo(10)
phoenix(6)
4 peoria(10), piEsburgh(16), pueblo(10)
pueblo(9)
5 pueblo(10) piEsburgh(15), piEsburgh(16),
peoria(10)
![Page 10: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/10.jpg)
Example: What is the distance from Pierre? -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0 Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
2 phoenix(6), pueble(10)
pendleton(2)
3 pueblo(9), peoria(10), piEsburgh(16), pueblo(10)
phoenix(6)
4 peoria(10), piEsburgh(16), pueblo(10)
pueblo(9)
5 pueblo(10) piEsburgh(15), piEsburgh(16),
peoria(10)
6 piEsburgh(15), piEsburgh(16)
-‐-‐
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4
3
3
4
3
5
4 10
8
5
2
![Page 11: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/11.jpg)
Example: What is the distance from Pierre? -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4
3
3
4
3
5
4 10
8
5
2
Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
2 phoenix(6), pueble(10)
pendleton(2)
3 pueblo(9), peoria(10), piEsburgh(16), pueblo(10)
phoenix(6)
4 peoria(10), piEsburgh(16), pueblo(10)
pueblo(9)
5 pueblo(10) piEsburgh(15), piEsburgh(16),
peoria(10)
6 piEsburgh(15), piEsburgh(16)
-‐-‐
7 piEsburgh(16), pensacola(19)
piEsburgh(15)
![Page 12: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/12.jpg)
Example: What is the distance from Pierre? -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
2 phoenix(6), pueble(10)
pendleton(2)
3 pueblo(9), peoria(10), piEsburgh(16), pueblo(10)
phoenix(6)
4 peoria(10), piEsburgh(16), pueblo(10)
pueblo(9)
5 pueblo(10) piEsburgh(15), piEsburgh(16),
peoria(10)
6 piEsburgh(15), piEsburgh(16)
-‐-‐
7 piEsburgh(16), pensacola(19)
piEsburgh(15)
8 pensacola(19) -‐-‐
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4
3
3
4
3
5
4 10
8
5
2
![Page 13: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/13.jpg)
Example: What is the distance from Pierre? -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Pierre: 0
Pendleton
Pierre
Pensacola
Princeton
PiEsburgh
Peoria
Pueblo
Phoenix
2
4
3
3
4
3
5
4 10
8
5
2
Pqueue Reachable
0 pierre(0) {}
1 pendleton(2) pierre(0)
2 phoenix(6), pueble(10)
pendleton(2)
3 pueblo(9), peoria(10), piEsburgh(16), pueblo(10)
phoenix(6)
4 peoria(10), piEsburgh(16), pueblo(10)
pueblo(9)
5 pueblo(10) piEsburgh(15), piEsburgh(16),
peoria(10)
6 piEsburgh(15), piEsburgh(16)
-‐-‐
7 piEsburgh(16), pensacola(19)
piEsburgh(15)
8 pensacola(19) -‐-‐
9 {} pensacola(19)
![Page 14: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/14.jpg)
Dijkstra’s
• Cost-‐first search • Always explores the next node with the CUMULATIVE least cost
• Our implementa@on: O(V+E Log E) – Key observa@on: Inner loop runs at most E @mes – Time to add/rem from pqueue is bounded by logE since all neighbors, or edges, can poten@ally be on the pqueue
– V comes from the ini@aliza@on of reachable • assume reachable is an array or hashTable
![Page 15: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/15.jpg)
Ini@alize map of reachable ver@ces, and add source vertex,vi, to a priority queue with distance zero While priority queue is not empty
Getmin from priority queue and assign to v If v is not in reachable add v with given cost to map of reachable ver@ces For all neighbors, vj, of v If vj is not is set of reachable ver@ces, combine cost of reaching v with cost to travel from v to vj, and add to priority queue
• O(V+ E Log E) – Key observa@on: Inner loop runs at most E @mes – Time to add/rem from pqueue is bounded by logE since all neighbors, or edges, can poten@ally be on the pqueue
![Page 16: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/16.jpg)
Summary
• Same code, three different ADTs result in three kinds of searches!!! – DFS (Stack) – BFS (Queue) – Dijkstras Cost-‐First Search (Pqueue) Ini@alize set of reachable ver@ces and add vi to a [stack, queue, pqueue] While [stack, queue, pqueue] is not empty
Get and remove [top, first, min] vertex v from [stack, queue, pqueue] if vertex v is not in reachable,
add it to reachable For all neighbors, vj, of v , not already in reachable add to [stack, queue, pqueue] (in case of pqueue, add with cumula@ve cost)
![Page 17: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/17.jpg)
Implementa@on of Dijkstra’s
• Pqueue: dynamic array heap • Reachable: – Array indexed by node num – map: name, distance – hashMap
• Graph Representa@on: edge list with weights[map of maps] – Key: Node name – Value: Map of Neighboring nodes
• Key: node name of one of the neighbors • Value: weight to that neighbor
![Page 18: CS261&DataStructures& - Oregon State Universityclasses.engr.oregonstate.edu/eecs/fall2013/cs261-001/Week_9/Graph... · Example:&Whatis&the&distance&from&Pierre? & Pendleton& Pierre](https://reader031.fdocuments.net/reader031/viewer/2022022501/5aa6e1097f8b9a6d5a8b92d1/html5/thumbnails/18.jpg)
Your Turn
• Complete Worksheet #42: Dijkstra’s Algorithm