shortest paths
play

Shortest Paths Section 4.44.5 Dr. Mayfield and Dr. Lam Department - PowerPoint PPT Presentation

Shortest Paths Section 4.44.5 Dr. Mayfield and Dr. Lam Department of Computer Science James Madison University Nov 6, 2015 For SSSP on Weighted Graph but without Negative Weight Cycle For SSSP on Weighted Graph but without Negative Weight


  1. Shortest Paths Section 4.4–4.5 Dr. Mayfield and Dr. Lam Department of Computer Science James Madison University Nov 6, 2015

  2. For SSSP on Weighted Graph but without Negative Weight Cycle For SSSP on Weighted Graph but without Negative Weight Cycle DIJKSTRA’s CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS Nov 6, 2015 Shortest Paths 2 of 15

  3. Single ‐ Source Shortest Paths (1) Single Source Shortest Paths (1) • If the graph is un weighted , we can use BFS – But what if the graph is weighted ? g p g • UVa 341 (Non Stop Travel) • Solution: Dijkstra O((V+E) log V) (( ) ) – A Greedy Algorithm y g – Use Priority Queue CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS Nov 6, 2015 Shortest Paths 3 of 15

  4. Modified Dijkstra’s Modified Dijkstra s – Example (1) Example (1) pq = {(0, 2)} 3 3 3 1 � We store this pair of information to the � 7 2 priority queue: (D[vertex], vertex), 2 0 0 sorted by increasing D[vertex], and 5 5 6 then if ties, by vertex number 0 � 6 See that our priority queue is “clean” at 1 1 the beginning of (modified) Dijsktra’s the beginning of (modified) Dijsktra s algorithm, it only contains (0, the source s) 4 4 � � Nov 6, 2015 Shortest Paths 4 of 15

  5. Modified Dijkstra’s Modified Dijkstra s – Example (2) Example (2) pq = {(0, 2)} 3 3 3 pq = {(2, 1), (6, 0), (7, 3)} {(2 1) (6 0) (7 3)} 1 7 2 7 2 We greedily take the vertex in the front of 2 0 0 the queue (here, it is vertex 2, the source), e queue ( e e, s e e , e sou ce), 5 5 6 and then successfully relax all its neighbors 0 (vertex 0, 1, 3). 6 6 1 1 P i Priority Queue will order these 3 vertices as it Q ill d th 3 ti 1, 0, 3, with shortest path estimate of 2, 6, 7, respectively. 4 4 � � Nov 6, 2015 Shortest Paths 5 of 15

  6. Modified Dijkstra’s Modified Dijkstra s – Example (3) Example (3) Vertex 3 appears twice in the priority queue, but this does not matter, as we will take only the first (smaller) one pq = {(0, 2)} 3 3 3 pq = {(2, 1), (6, 0), (7, 3)} {(2 1) (6 0) (7 3)} 1 5 2 pq = {(5, 3), (6, 0), (7, 3), (8, 4)} 7 2 2 0 0 5 5 We greedily take the vertex in the front of We greedily take the vertex in the front of 6 the queue (now, it is vertex 1), then 0 successfully relax all its neighbors (vertex 3 6 6 and 4). ) 1 1 Priority Queue will order the items as 4 4 3, 0, 3, 4 with shortest path estimate of 8 8 5 6 7 8 respectively 5, 6, 7, 8, respectively. Nov 6, 2015 Shortest Paths 6 of 15

  7. Modified Dijkstra’s Modified Dijkstra s – Example (4) Example (4) pq = {(0, 2)} 3 3 3 pq = {(2, 1), (6, 0), (7, 3)} {(2 1) (6 0) (7 3)} 1 5 2 pq = {(5, 3), (6, 0), (7, 3), (8, 4)} 7 2 2 pq = {(6, 0), (7, 3), (8, 4)} 0 0 5 5 6 We greedily take the vertex in the front of 0 6 the queue (now, it is vertex 3), then try to 6 1 1 relax all its neighbors (only vertex 4). relax all its neighbors (only vertex 4). However D[4] is already 8. Since D[3] + w(3, 4) = 5 + 5 is worse than 8, we do not 4 4 do anything. 8 8 Priority Queue will now have these items 0, 3, 4 with shortest path estimate of 6, 7, 8, respectively. , , , p y Nov 6, 2015 Shortest Paths 7 of 15

  8. Modified Dijkstra’s Modified Dijkstra s – Example (5) Example (5) pq = {(0, 2)} 3 3 3 pq = {(2, 1), (6, 0), (7, 3)} {(2 1) (6 0) (7 3)} 1 5 pq = {(5, 3), (6, 0), (7, 3), (8, 4)} 2 7 2 2 pq = {(6, 0), (7, 3), (8, 4)} 0 0 5 5 6 pq = {(7, 3), (7, 4), (8, 4)} 0 6 6 We greedily take the vertex in the front of 1 1 the queue (now, it is vertex 5), then the queue (now it is vertex 5) then successfully relax all its neighbors (only vertex 4). 4 4 7 7 Priority Queue will now have these items 3, 4, 4 with shortest path estimate of 7, 7, 8, respectively. Nov 6, 2015 Shortest Paths 8 of 15

  9. Modified Dijkstra’s Modified Dijkstra s – Example (6) Example (6) Remember that vertex 3 appeared twice in the priority queue but this Dijkstra’s queue, but this Dijkstra s algorithm will only consider the first (shorter) one pq = {(0, 2)} 3 3 3 pq = {(2, 1), (6, 0), (7, 3)} {(2 1) (6 0) (7 3)} 1 5 2 pq = {(5, 3), (6, 0), (7, 3), (8, 4)} 7 2 2 pq = {(6, 0), (7, 3), (8, 4)} 0 0 5 5 6 pq = {(7, 3), (7, 4), (8, 4)} 0 pq = {(7, 4), (8, 4)} 6 6 pq pq = {(8, 4)} {( , )} 1 1 pq = {} Similarly for vertex 4. The one 4 4 with shortest path estimate 7 7 7 will be processed first and the one with shortest path estimate 8 will be ignored, although nothing is changed although nothing is changed anymore Nov 6, 2015 Shortest Paths 9 of 15

  10. Dijkstra’s Algorithm (using STL) Dijkstra s Algorithm (using STL) vi dist(V, INF); dist[s] = 0; // INF = 2B i di ( ) di [ ] 0 // 2 priority_queue< ii, vector<ii>, greater<ii> > pq; pq.push(ii(0, s)); // sort based on increasing distance while (!pq.empty()) { // main loop ii top = pq.top(); pq.pop(); // greedy int d = top.first, u = top.second; p , p ; if (d == dist[u]) { for (int j = 0; j < (int)AdjList[u].size(); j++) { ii ii v = AdjList[u][j]; // all outgoing edges from u v = AdjList[u][j]; // all outgoing edges from u if (dist[u] + v.second < dist[v.first]) { dist[v.first] = dist[u] + v.second; // relax pq.push(ii(dist[v.first], v.first)); ii i i i } // enqueue this neighbor regardless it is } // already in pq or not } } CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS Nov 6, 2015 Shortest Paths 10 of 15

  11. int[] dist = new int[adjList.length]; for (int i = 0; i < dist.length; i++) { dist[i] = 99999999; } dist[src] = 0; PriorityQueue<Edge> pq = new PriorityQueue<Edge>(); pq.add(new Edge(0, src)); while (pq.size() > 0) { Edge top = pq.poll(); int u = top.dest; if (top.weight == dist[u]) { for (int j = 0; j < adjList[u].length; j++) { Edge v = adjList[u][j]; if (dist[u] + v.weight < dist[v.dest]) { dist[v.dest] = dist[u] + v.weight; pq.add(new Edge(dist[v.dest], v.dest)); } } } } Nov 6, 2015 Shortest Paths 11 of 15

  12. For All ‐ Pairs Shortest Paths For All Pairs Shortest Paths FLOYD WARSHALL’s CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS Nov 6, 2015 Shortest Paths 11 of 15

  13. UVa 11463 – Commandos (1) ( ) Al ‐ Khawarizmi, Malaysia National Contest 2008 • Given: – A table that stores the amount of minutes to travel between buildings (there are at most 100 buildings) – 2 special buildings: startB and endB – K soldiers to bomb all the K buildings in this mission – Each of them start at the same time from startB, choose one building B that has not been bombed by other h b ildi h h b b b d b h soldier (bombing time negligible), and then gather in (destroyed) building endB and then gather in (destroyed) building endB. • What is the minimum time to complete the mission? CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS Nov 6, 2015 Shortest Paths 12 of 15

  14. UVa 11463 – Commandos (2) ( ) Al ‐ Khawarizmi, Malaysia National Contest 2008 • How long do you need to solve this problem? • Solution: – The answer is determined by sp from starting building, detonate furthest building , and sp from that furthest building to end building • max(dist[start][i] + dist[i][end]) for all i � V • How to compute sp for many pairs of vertices? CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS Nov 6, 2015 Shortest Paths 13 of 15

  15. UVa 11463 – Commandos (3) ( ) Al ‐ Khawarizmi, Malaysia National Contest 2008 • This problem is called: All ‐ Pairs Shortest Paths • Two options to solve this: – Call SSSP algorithms multiple times • Dijkstra O(V * (V+E) * log V), if E = V 2 � O(V 3 log V) • Bellman Ford O(V * V * E), if E = V 2 � O(V 4 ) • Slow to code – Use Floyd Warshall, a clever DP algorithm l d h ll l l i h • O(V 3 ) algorithm • Very easy to code! • Very easy to code! • In this problem, V is <= 100, so Floyd Warshall is DOABLE!! CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS Nov 6, 2015 Shortest Paths 14 of 15

  16. Floyd Warshall Floyd Warshall – Template Template • O(V 3 ) since we have three nested loops! • Use adjacency matrix: G[MAX_V][MAX_V] ; _ _ – So that weight of edge(i, j) can be accessed in O(1) for (int k = 0; k < V; k++) for (int k = 0; k < V; k++) for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) j j j G[i][j] = min(G[i][j], G[i][k] + G[k][j]); – See more explanation of this three ‐ liner DP See more explanation of this three liner DP algorithm in CP CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS Nov 6, 2015 Shortest Paths 15 of 15

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