cs261 data structures
play

CS261 Data Structures Dijkstras Algorithm Edge List Representation - PowerPoint PPT Presentation

CS261 Data Structures Dijkstras Algorithm Edge List Representation Weighted Graphs Representation: Edge List Pierre Peoria 2 5 Whats reachable AND 3 what is the cheapest Pendleton Pittsburgh 8 cost to get there? 2 4 Pueblo


  1. CS261 Data Structures Dijkstra’s Algorithm – Edge List Representation

  2. Weighted Graphs Representation: Edge List Pierre Peoria 2 5 What’s reachable AND 3 what is the cheapest Pendleton Pittsburgh 8 cost to get there? 2 4 Pueblo 10 Princeton 4 4 3 5 Phoenix Pensacola Pendleton: {Pueblo:8, Phoenix:4} Pensacola: {Phoenix:5} Peoria: {Pueblo:3, Pittsburgh:5} Phoenix: {Pueblo:3, Peoria:4, Pittsburgh:10} Pierre: {Pendleton:2} Pittsburgh: {Pensacola:4} Princeton: {Pittsburgh:2} Pueblo: {Pierre:3}

  3. Dijkstra’s Algorithm Pierre Peoria 2 5 3 3 Cost --First Pendleton Pittsburgh 8 2 Search Pueblo 10 4 4 Princeton 4 3 5 Phoenix Pensacola Initialize map of reachable vertices and distance, and add source vertex,v i , 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 vertices For all neighbors, v j , of v combine cost of reaching v with cost to travel from v to v j , and modify keys in the priority queue

  4. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton inf Pueblo 10 4 4 Princeton Pueblo inf 4 3 Phoenix inf Peoria inf 5 Phoenix Pensacola Pittsburgh inf Priority Queue (heap) Pensacola inf Princeton inf City Name Priority Pierre 0

  5. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo inf 4 3 Phoenix inf Peoria inf 5 Phoenix Pensacola Pittsburgh inf Priority Queue (heap) Pensacola inf Princeton inf City Name Priority Pendleton 2

  6. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 10 4 3 Phoenix 6 Peoria inf 5 Phoenix Pensacola Pittsburgh inf Priority Queue (heap) Pensacola inf Princeton inf City Name City Name Priority Priority Pendleton Phoenix 2 6 Pueblo 10

  7. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 9 4 3 Phoenix 6 Peoria 10 5 Phoenix Pensacola Pittsburgh 16 Priority Queue (heap) Pensacola inf Princeton inf City Name City Name Priority Priority Pendleton Pueblo 2 9 Pueblo 10 Peoria 10 Pittsburgh 16

  8. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 9 4 3 Phoenix 6 Peoria 10 5 Phoenix Pensacola Pittsburgh 16 Priority Queue (heap) Pensacola inf Princeton inf City Name City Name Priority Priority Pendleton Pueblo 10 2 Peoria 10 Pittsburgh 16

  9. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 9 4 3 Phoenix 6 Peoria 10 5 Phoenix Pensacola Pittsburgh 16 Priority Queue (heap) Pensacola inf Princeton inf City Name City Name Priority Priority Pendleton Peoria 10 2 Pittsburgh 16

  10. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 9 4 3 Phoenix 6 Peoria 10 5 Phoenix Pensacola Pittsburgh 15 Priority Queue (heap) Pensacola inf Princeton inf City Name City Name Priority Priority Pendleton Pittsburgh 15 2 Pittsburgh 16

  11. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 9 4 3 Phoenix 6 Peoria 10 5 Phoenix Pensacola Pittsburgh 15 Priority Queue (heap) Pensacola 19 Princeton inf City Name City Name Priority Priority Pendleton Pittsburgh 16 2 Pensacola 19

  12. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 9 4 3 Phoenix 6 Peoria 10 5 Phoenix Pensacola Pittsburgh 15 Priority Queue (heap) Pensacola 19 Princeton inf City Name Priority Pittsburgh 16 Pensacola 19

  13. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 9 4 3 Phoenix 6 Peoria 10 5 Phoenix Pensacola Pittsburgh 15 Priority Queue (heap) Pensacola 19 Princeton inf City Name Priority Pensacola 19

  14. Example: What is the distance from Pierre? Distance array Pierre Peoria 2 5 City Name Distance 3 3 Pendleton Pierre 0 Pittsburgh 8 2 Pendleton 2 Pueblo 10 4 4 Princeton Pueblo 9 4 3 Phoenix 6 Peoria 10 5 Phoenix Pensacola Pittsburgh 15 Priority Queue (heap) Pensacola 19 Princeton inf City Name City Name Priority Priority

  15. Dijkstra’s • Cost- ‐first search • Always explores the next node with the CUMULATIVE least cost • Our implementation: O(V+E Log E) – Key observation: Inner loop runs at most E times – Time to add/rem from pqueue is bounded by logE since all neighbors, or edges, can potentially be on the pqueue – V comes from the initialization of reachable • assume reachable is an array or hashTable

  16. • O(V+ E Log E) – Key observation: Inner loop runs at most E times – Time to add/rem from pqueue is bounded by logE since all neighbors, or edges, can potentially be on the pqueue Initialize map of reachable vertices, and add source vertex,v i , 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 vertices For all neighbors, v j , of v If v j is not is set of reachable vertices, combine cost of reachingv with cost to travel from v to v j , and add to priority queue

  17. Summary • Same code, three different ADTs result in three kinds of searches!!! – DFS (Stack) – BFS (Queue) – Dijkstras Cost--First Search (Pqueue) Initialize set of reachable vertices and add v i 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, v j , of v , not already in reachable add to [stack, queue, pqueue] (in case of pqueue, add with cumulative cost)

  18. Implementation of Dijkstra’s • Pqueue: dynamic array heap • Reachable: – Array indexed by node num – map: name, distance – hashMap • Graph Representation: 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

  19. Your Turn • Complete Worksheet #42: Dijkstra’s Algorithm

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend