cse 326 data structures
play

CSE 326: Data Structures Shortest path algorithms Graphs, Paths - PowerPoint PPT Presentation

Todays Outline CSE 326: Data Structures Shortest path algorithms Graphs, Paths & Dijkstras 1. Unweighted graphs: BFS 2. Weighted graphs without negative cost edges: Algorithm Dijkstras Algorithm 3. Negative cost edges but no


  1. Today’s Outline CSE 326: Data Structures Shortest path algorithms Graphs, Paths & Dijkstra’s 1. Unweighted graphs: BFS 2. Weighted graphs without negative cost edges: Algorithm Dijkstra’s Algorithm 3. Negative cost edges but no negative cost Hal Perkins cycles Spring 2007 Lectures 22-23 Reading: Weiss, Ch. 9 2 Graph Connectivity Graph Traversals Undirected graphs are connected if there is a path between any two vertices • Breadth-first search (and depth-first search) work for arbitrary (directed or undirected) graphs - not just mazes! Directed graphs are strongly connected if there is a path from any – Must mark visited vertices so you do not go into an infinite one vertex to any other loop! • Either can be used to determine connectivity : – Is there a path between two given vertices? Directed graphs are weakly connected if there is a path between – Is the graph (weakly) connected? any two vertices, ignoring direction • Which one: – Uses a queue? A complete graph has an edge between every pair of vertices – Uses a stack? – Always finds the shortest path (for unweighted graphs)? 3 4 1

  2. Single Source Shortest Paths (SSSP) The Shortest Path Problem Given a graph G, edge costs c i,j , and vertices s Given a graph G, edge costs c i,j , and vertex s , and t in G , find the shortest path from s to t . find the shortest paths from s to all vertices in G. For a path p = v 0 v 1 v 2 … v k – Is this harder or easier than the previous problem? – unweighted length of path p = k (a.k.a. length ) – weighted length of path p = ∑ i =0.. k -1 c i,i +1 (a.k.a cost ) Path length equals path cost when ? 5 6 All Pairs Shortest Paths (APSP) Variations of SSSP Given a graph G and edge costs c i,j , find the shortest paths between all pairs of vertices in G. – Weighted vs. unweighted – Directed vs undirected – Is this harder or easier than SSSP? – Cyclic vs. acyclic – Positive weights only vs. negative weights allowed – Shortest path vs. longest path – Could we use SSSP as a subroutine to solve this? – … 7 8 2

  3. Applications SSSP: Unweighted Version Ideas? – Network routing – Driving directions – Cheap flight tickets – Critical paths in project management (see textbook) – … 9 10 Weighted SSSP: void Graph::unweighted (Vertex s){ The Quest For Food Queue q(NUM_VERTICES); Vertex v, w; Home q.enqueue(s); Ben & Jerry’s Cedars s.dist = 0; Neelam’s 40 U Village 285 Delfino’s 70 75 365 25 while (!q.isEmpty()){ 375 Coke Closet v = q.dequeue(); each edge examined 350 The Ave 350 10 at most once – if adjacency for each w adjacent to v 25 HUB lists are used ALLEN if (w.dist == INFINITY){ 35 35 15 Café Allegro w.dist = v.dist + 1; Schultzy’s Vending Machine in EE1 w.path = v; each vertex enqueued 15,356 q.enqueue(w); at most once } Parent’s Home } } total running time: O( ) Can we calculate shortest distance to all nodes from Allen Center? 11 12 3

  4. Dijkstra, Edsger Wybe Dijkstra’s Algorithm: Idea Adapt BFS to handle Legendary figure in computer science; weighted graphs was a professor at University of Texas. Supported teaching introductory Two kinds of vertices: computer courses without computers – Finished or known (pencil and paper programming) vertices • Shortest distance has Supposedly wouldn’t (until very late in been computed life) read his e-mail; so, his staff had to E.W. Dijkstra (1930-2002) – Unknown vertices print out messages and put them in his • Have tentative box. distance 1972 Turning Award Winner, Programming Languages, semaphores, and … 13 14 Dijkstra’s Algorithm: Idea Dijkstra’s Algorithm: Pseudocode Initialize the cost of each node to ∞ At each step: Initialize the cost of the source to 0 1) Pick closest unknown vertex 2) Add it to known vertices While there are unknown nodes left in the graph 3) Update distances Select an unknown node b with the lowest cost Mark b as known For each node a adjacent to b a ’s cost = min( a ’s old cost, b ’s cost + cost of ( b , a )) 15 16 4

  5. 2 v 1 s v 0 void Graph::dijkstra(Vertex s){ Vertex v,w; 5 1 1 2 1 1 v 2 v 3 v 4 Initialize s.dist = 0 and set dist of all other vertices to infinity 6 2 while (there exist unknown vertices, find the 5 3 one b with the smallest distance) V Known Dist path b.known = true; 10 v 5 v 6 v0 for each a adjacent to b v1 if (!a.known) if (b.dist + Cost_ba < a.dist){ v2 decrease(a.dist to= b.dist + Cost_ba); a.path = b; v3 } v4 } } v5 v6 17 18 Dijkstra’s Alg: Implementation Dijkstra’s Algorithm: Summary Initialize the cost of each node to ∞ • Classic algorithm for solving SSSP in weighted graphs without negative weights Initialize the cost of the source to 0 While there are unknown nodes left in the graph • A greedy algorithm (irrevocably makes decisions Select the unknown node b with the lowest cost without considering future consequences) Mark b as known For each node a adjacent to b a ’s cost = min( a ’s old cost, b ’s cost + cost of ( b , a )) • Intuition for correctness: – shortest path from source vertex to itself is 0 What data structures should we use? – cost of going to adjacent nodes is at most edge weights – cheapest of these must be shortest path to that node – update paths for new node and continue picking cheapest Running time? path 19 20 5

  6. Correctness: The Cloud Proof Correctness: Inside the Cloud Next shortest path from Prove by induction on # of nodes in the cloud: inside the known cloud Initial cloud is just the source with shortest path 0 V Assume: Everything inside the cloud has the correct Better path shortest path to V? No! The Known Inductive step: Only when we prove the shortest Cloud path to some node v (which is not in the cloud) is W correct, we add it to the cloud Source How does Dijkstra’s decide which vertex to add to the Known set next? When does Dijkstra’s algorithm not work? • If path to V is shortest, path to W must be at least as long (or else we would have picked W as the next vertex) 21 22 • So the path through W to V cannot be any shorter! The Trouble with Dijkstra’s vs BFS Negative Weight Cycles At each step: At each step: 1) Pick vertex from queue 1) Pick closest unknown vertex 2) Add it to visited vertices 2) Add it to finished vertices 3) Update queue with neighbors 3) Update distances 2 A B 10 Breadth-first Search Dijkstra’s Algorithm -5 E 1 2 Some Similarities : C D What’s the shortest path from A to E? Problem? 23 24 6

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