lecture 11 dijkstra s algorithm
play

Lecture 11 Dijkstras Algorithm Sanjoy Dasgupta Russell - PowerPoint PPT Presentation

Lecture 11 Dijkstras Algorithm Sanjoy Dasgupta Russell Impagliazzo Ragesh Jaiswal CSE101, Spring 2020, Week-03 Edge lengths BFS treats all edges as having the same length. This is rarely true in applications. Denote the length of


  1. Lecture 11 – Dijkstra’s Algorithm Sanjoy Dasgupta Russell Impagliazzo Ragesh Jaiswal CSE101, Spring 2020, Week-03

  2. Edge lengths BFS treats all edges as having the same length. This is rarely true in applications. Denote the length of edge e = (u,v) by l(e) or l e or l(u,v)

  3. Extending BFS Suppose G has positive integral (i) G’ has unit-length edges edge lengths (ii) For the “real” nodes, distance in G = distance in G’ b 5 2 So run BFS on G’ ! G a d 2 Problem: efficiency 1 6 c b 500 200 Simple trick: add dummy nodes b a d 200 100 600 c d G’ If edge lengths in G are large: a (i) G’ is enormous (ii) BFS wastes a lot of time computing distances to dummy nodes we don’t care about c

  4. Extending BFS First 99 time steps: BFS (on G’) slowly b 500 200 G advances along a—b and a—c. Boring! a d 200 Can we snooze and have an alarm wake up us whenever BFS reaches a real node? 100 600 c Alarm for each real node: estimated time of arrival based on edges currently being b traversed. T = 0 set alarms for b (500), c (100) snooze G’ T = 100 wake up, BFS is at c set alarms for b (300), d (700) snooze d T = 300 wake up, BFS is at b a set alarm for d (500) snooze T = 500 wake up, BFS is at d dist[c] = 100 dist[b] = 300 c dist[d] = 500

  5. Alarm clock algorithm (Given graph G and starting node s) How to implement alarm? Answer: priority queue (aka heap) set an alarm for node s at time 0 if the next alarm goes off at time T, for node u: A priority queue H stores: distance[u] = T - a set of elements (our nodes) for each edge (u,v) in E: -associated key values (alarm times) if no alarm for v, set one for T + l(u,v) and supports these operations: if there is an alarm for v, but later than insert(H,x) insert new set a new T + l(u,v), then reset to this earlier time element into H alarm deletemin(H) return element which alarm Exactly simulates BFS on G’... with smallest key is going off we no longer need to construct G’! value, remove next? from H decreasekey(H,x) allow x’s key allow alarm b 500 200 value to be to be reset G decreased to an earlier a d time 200 makequeue(S) make a queue initialize 100 600 out of the alarms c elements in S (and their keys)

  6. Dijkstra’s algorithm procedure dijkstra(G,l,s) 1 5 b d f 2 input: graph G = (V,E); node s; positive edge lengths l e 5 1 2 a 3 1 output: for each node u, dist[u] is 6 c e g set to its distance from s 1 for u in V: dist[u] = 1 dist[s] = 0 H = makequeue(V) // key = dist[] while H is not empty: u = deletemin(H) for each edge (u,v) in E: if dist[v] > dist[u] + l(u,v): dist[v] = dist[u] + l(u,v) decreasekey(H,v)

  7. Another example procedure dijkstra(G,l,s) 2 b d 4 for u in V: 4 dist[u] = 1 a 3 1 1 prev[u] = nil 3 2 dist[s] = 0 c e 5 H = makequeue(V) // key = dist[] while H is not empty: u = deletemin(H) for each edge (u,v) in E: if dist[v] > dist[u] + l(u,v): dist[v] = dist[u] + l(u,v) prev[v] = u decreasekey(H,v)

  8. Running time procedure dijkstra(G,l,s) Time: O(V + E) + for u in V: V x deletemin + dist[u] = 1 V x insert + dist[s] = 0 E x decreasekey H = makequeue(V) // key = dist[] while H is not empty: u = deletemin(H) for each edge (u,v) in E: if dist[v] > dist[u] + l(u,v): dist[v] = dist[u] + l(u,v) decreasekey(H,v)

Recommend


More recommend