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

cs261 data structures
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS261 Data Structures

Dijkstra’s Algorithm – Edge List Representation

slide-2
SLIDE 2

Weighted Graphs Representation: Edge List

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

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} 2 8 4 3 3 4 5 10 5 4 2 What’s reachable AND what is the cheapest cost to get there?

slide-3
SLIDE 3

Initialize map of reachable vertices and distance, 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 vertices For all neighbors, vj, of v combine cost of reaching v with cost to travel from v to vj, and modify keys in the priority queue

Dijkstra’s Algorithm

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

Cost --First Search

slide-4
SLIDE 4

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton inf Pueblo inf Phoenix inf Peoria inf Pittsburgh inf Pensacola inf Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pierre

slide-5
SLIDE 5

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo inf Phoenix inf Peoria inf Pittsburgh inf Pensacola inf Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pendleton 2

slide-6
SLIDE 6

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 10 Phoenix 6 Peoria inf Pittsburgh inf Pensacola inf Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pendleton 2 City Name Priority Phoenix 6 Pueblo 10

slide-7
SLIDE 7

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 9 Phoenix 6 Peoria 10 Pittsburgh 16 Pensacola inf Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pendleton 2 City Name Priority Pueblo 9 Pueblo 10 Peoria 10 Pittsburgh 16

slide-8
SLIDE 8

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 9 Phoenix 6 Peoria 10 Pittsburgh 16 Pensacola inf Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pendleton 2 City Name Priority Pueblo 10 Peoria 10 Pittsburgh 16

slide-9
SLIDE 9

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 9 Phoenix 6 Peoria 10 Pittsburgh 16 Pensacola inf Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pendleton 2 City Name Priority Peoria 10 Pittsburgh 16

slide-10
SLIDE 10

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 9 Phoenix 6 Peoria 10 Pittsburgh 15 Pensacola inf Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pendleton 2 City Name Priority Pittsburgh 15 Pittsburgh 16

slide-11
SLIDE 11

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 9 Phoenix 6 Peoria 10 Pittsburgh 15 Pensacola 19 Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pendleton 2 City Name Priority Pittsburgh 16 Pensacola 19

slide-12
SLIDE 12

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 9 Phoenix 6 Peoria 10 Pittsburgh 15 Pensacola 19 Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pittsburgh 16 Pensacola 19

slide-13
SLIDE 13

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 9 Phoenix 6 Peoria 10 Pittsburgh 15 Pensacola 19 Princeton inf

Distance array Priority Queue (heap)

City Name Priority Pensacola 19

slide-14
SLIDE 14

Example: What is the distance from Pierre?

Pendleton Pierre Pensacola Princeton Pittsburgh Peoria Pueblo Phoenix

2 4 3 3 4 3 5 4 10 8 5 2

City Name Distance Pierre Pendleton 2 Pueblo 9 Phoenix 6 Peoria 10 Pittsburgh 15 Pensacola 19 Princeton inf

Distance array Priority Queue (heap)

City Name Priority City Name Priority

slide-15
SLIDE 15

Dijkstra’s

  • Cost-‐firstsearch
  • 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
slide-16
SLIDE 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,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 vertices For all neighbors, vj, of v If vj is not is set of reachable vertices, combine cost of reachingv with cost to travel from v to vj, and add to priority queue

slide-17
SLIDE 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 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 cumulative cost)

slide-18
SLIDE 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
slide-19
SLIDE 19

Your Turn

  • Complete Worksheet #42: Dijkstra’s Algorithm