09.07.2015 Views

slides - Computer Science - University of San Francisco

slides - Computer Science - University of San Francisco

slides - Computer Science - University of San Francisco

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

17-1: Shortest Path ExampleShortest path ≠ path containing fewest edges1B24CA85E42D1Shortest Path from A to E?


17-2: Shortest Path ExampleShortest path ≠ path containing fewest edges1B24CA85E42D1Shortest Path from A to E:A, B, C, D, E


17-3: Single Source Shortest PathTo find the shortest path from vertex x to vertex y,we need (worst case) to find the shortest path fromx to all other vertices in the graphWhy?


17-5: Single Source Shortest PathIf all edges have unit weight ...


17-6: Single Source Shortest PathIf all edges have unit weight,We can use Breadth First Search to compute theshortest pathBFS Spanning Tree contains shortest path to eachnode in the graphNeed to do some more work to create & saveBFS spanning treeWhen edges have differing weights, this obviouslywill not work


17-7: Single Source Shortest PathDivide the vertices into two sets:Vertices whose shortest path from the initialvertex is knownVertices whose shortest path from the initialvertex is not knownInitially, only the initial vertex is knownMove vertices one at a time from the unknown setto the known set, until all vertices are known


17-8: Single Source Shortest Path2AB4 1 3 102 2C D E5 8 4 6F1Start with the vertex AG


17-9: Single Source Shortest PathNode Distance2AA 0BB4 1 3 10CC2 2DD EE5 8 4 6 FGF1GKnown vertices are circled in redWe can now extend the known set by 1 vertex


17-10: Single Source Shortest PathNode2AABB4 1 3 10CC2 2DD EE5 8 4 6 FGF1GWhy is it safe to add D, with cost 1?Distance01


17-11: Single Source Shortest PathA2 2C D EF24 1 3 105 8 4 61BGNodeABCDEFGDistanceWhy is it safe to add D, with cost 1?Could we do better with a more roundaboutpath?01


17-12: Single Source Shortest PathA2 2C D EF24 1 3 105 8 4 61BGNodeABCDEFGDistance0Why is it safe to add D, with cost 1?Could we do better with a more roundaboutpath?No – to get to any other node will cost at least 1No negative edge weights, can’t do better than11


17-13: Single Source Shortest PathNode Distance2AA 0BB4 1 3 10CC2 2D 1D EE5 8 4 6 FGF1GWe can now add another vertex to our known list ...


17-14: Single Source Shortest PathA2 2C D EF24 1 3 105 8 4 61BGNodeABCDEFGDistanceHow do we know that we could not get to Bcheaper than by going through D?021


17-15: Single Source Shortest PathA2 2C D EF24 1 3 105 8 4 61BGNodeABCDEFGDistanceHow do we know that we could not get to Bcheaper than by going through D?Costs 1 to get to DCosts at least 2 to get anywhere from DCost at least (1+2 = 3) to get to B through D021


17-16: Single Source Shortest Path2AB4 1 3 102 2C D E5 8 4 6FG1Next node we can add ...NodeABCDEFGDistance021


17-17: Single Source Shortest PathNode Distance2AA 0BB 24 1 3 10C 3C2 2D 1D EE5 8 4 6 FGF1G(We also could have added E for this step)Next vertex to add to Known ...


17-18: Single Source Shortest Path2AB4 1 3 102 2C D E5 8 4 6FG1Cost to add F is 8 (through C)Cost to add G is 5 (through D)NodeABCDEFGDistance02313


17-19: Single Source Shortest Path2AB4 1 3 102 2C D E5 8 4 6FG1Last node ...NodeABCDEFGDistance023135


17-20: Single Source Shortest Path2AB4 1 3 102 2C D E5 8 4 6FG1NodeABCDEFGDistance0231356We now know the length <strong>of</strong> the shortest path fromA to all other vertices in the graph


17-21: Dijkstra’s AlgorithmKeep a table that contains, for each vertexIs the distance to that vertex known?What is the best distance we’ve found so far?Repeat:Pick the smallest unknown distancemark it as knownupdate the distance <strong>of</strong> all unknown neighbors <strong>of</strong>that nodeUntil all vertices are known


17-22: Dijkstra’s Algorithm ExampleB1DA7511C321E72FNode Known DistanceA false 0B false ∞C false ∞D false ∞E false ∞F false ∞


17-23: Dijkstra’s Algorithm ExampleB1DA7511C321E72FNode Known DistanceA true 0B false 7C false 5D false ∞E false ∞F false 1


17-24: Dijkstra’s Algorithm ExampleB1DA7511C321E72FNode Known DistanceA true 0B false 7C false 5D false 8E false 3F true 1


17-25: Dijkstra’s Algorithm ExampleB1DA7511C321E72FNode Known DistanceA true 0B false 7C false 4D false 8E true 3F true 1


17-26: Dijkstra’s Algorithm ExampleB1DA7511C321E72FNode Known DistanceA true 0B false 5C true 4D false 6E true 3F true 1


17-27: Dijkstra’s Algorithm ExampleB1DA7511C321E72FNode Known DistanceA true 0B true 5C true 4D false 6E true 3F true 1


17-28: Dijkstra’s Algorithm ExampleB1DA7511C321E72FNode Known DistanceA true 0B true 5C true 4D true 6E true 3F true 1


17-29: Dijkstra’s AlgorithmAfter Dijkstra’s algorithm is complete:We know the length <strong>of</strong> the shortest pathWe do not know what the shortest path isHow can we modify Dijstra’s algorithm to computethe path?


17-30: Dijkstra’s AlgorithmAfter Dijkstra’s algorithm is complete:We know the length <strong>of</strong> the shortest pathWe do not know what the shortest path isHow can we modify Dijstra’s algorithm to computethe path?Store not only the distance, but the immediateparent that led to this distance


17-31: Dijkstra’s Algorithm ExampleC321AF551D3451BG2E1Node Known Dist PathA false 0B false ∞C false ∞D false ∞E false ∞F false ∞G false ∞


17-32: Dijkstra’s Algorithm ExampleC321AF551D3451BG2E1Node Known Dist PathA true 0B false 5 AC false 3 AD false ∞E false ∞F false ∞G false ∞


17-33: Dijkstra’s Algorithm ExampleC321AF551D3451BG2E1Node Known Dist PathA true 0B false 5 AC true 3 AD false 4 CE false ∞F false ∞G false ∞


17-34: Dijkstra’s Algorithm ExampleC321AF551D3451BG2E1Node Known Dist PathA true 0B false 5 AC true 3 AD true 4 CE false 9 DF false 9 DG false 7 D


17-35: Dijkstra’s Algorithm ExampleC321AF551D3451BG2E1Node Known Dist PathA true 0B true 5 AC true 3 AD true 4 CE false 9 DF false 9 DG false 7 D


17-36: Dijkstra’s Algorithm ExampleC321AF551D3451BG2E1Node Known Dist PathA true 0B true 5 AC true 3 AD true 4 CE false 9 DF false 8 GG true 7 D


17-37: Dijkstra’s Algorithm ExampleC321AF551D3451BG2E1Node Known Dist PathA true 0B true 5 AC true 3 AD true 4 CE false 9 DF true 8 GG true 7 D


17-38: Dijkstra’s Algorithm ExampleC321AF551D3451BG2E1Node Known Dist PathA true 0B true 5 AC true 3 AD true 4 CE true 9 DF true 8 GG true 7 D


17-39: Dijkstra’s AlgorithmGiven the “path” field, we can construct theshortest pathWork backward from the end <strong>of</strong> the pathFollow the “path” pointers until the start node isreachedWe can use a sentinel value in the “path” field<strong>of</strong> the initial node, so we know when to stop


17-40: Dijkstra Codevoid Dijkstra(Edge G[], int s, tableEntry T[]) {int i, v;Edge e;for(i=0; iT[v].distance + e.cost) {T[e.neighbor].distance = T[v].distance + e.cost;T[e.neighbor].path = v;}}}


int minUnknownVertex(tableEntry T[]) {int i;int minVertex = -1;int minDistance = Integer.MAX_VALUE;for (i=0; i < T.length; i++) {if ((!T[i].known) &&(T[i].distance < MinDistance)) {minVertex = i;minDistance = T[i].distance;}}return minVertex;}17-41: minUnknownVertexCalculating minimum distance unknown vertex:


17-42: Dijkstra Running TimeTime for initialization:for(i=0; i


17-43: Dijkstra Running TimeTime for initialization:for(i=0; i


17-44: Dijkstra Running TimeTotal time for all calls to minUnknownVertex, andsetting T[v].known = true (for all iterations <strong>of</strong> theloop)for (i=0; i < G.length; i++) {v = minUnknownVertex(T); < These two linesT[v].known = true; < ---------------for (e = G[v]; e != null; e = e.next) {if (T[e.neighbor].distance >T[v].distance + e.cost) {T[e.neighbor].distance = T[v].distance + e.cost;T[e.neighbor].path = v;}}}


17-45: Dijkstra Running TimeTotal time for all calls to minUnknownVertex, andsetting T[v].known = true (for all iterations <strong>of</strong> theloop)for (i=0; i < G.length; i++) {v = minUnknownVertex(T); < These two linesT[v].known = true; < ---------------for (e = G[v]; e != null; e = e.next) {if (T[e.neighbor].distance >T[v].distance + e.cost) {T[e.neighbor].distance = T[v].distance + e.cost;T[e.neighbor].path = v;}}}Θ(V 2 )


17-46: Dijkstra Running TimeTotal # <strong>of</strong> times the if statement will be executed:for (i=0; i < G.length; i++) {v = minUnknownVertex(T);T[v].known = true;for (e = G[v]; e != null; e = e.next) {|> if (T[e.neighbor].distance >|> T[v].distance + e.cost) {|> T[e.neighbor].distance = T[v].distance + e.cost;|> T[e.neighbor].path = v;}}}


17-47: Dijkstra Running TimeTotal # <strong>of</strong> times the if statement will be executed:for (i=0; i < G.length; i++) {v = minUnknownVertex(T);T[v].known = true;for (e = G[v]; e != null; e = e.next) {|> if (T[e.neighbor].distance >|> T[v].distance + e.cost) {|> T[e.neighbor].distance = T[v].distance + e.cost;|> T[e.neighbor].path = v;}}}E


17-48: Dijkstra Running TimeTotal running time for all iterations <strong>of</strong> the inner forstatement:for (i=0; i < G.length; i++) {v = minUnknownVertex(T);T[v].known = true;|> for (e = G[v]; e != null; e = e.next) {|> if (T[e.neighbor].distance >|> T[v].distance + e.cost) {|> T[e.neighbor].distance = T[v].distance + e.cost;|> T[e.neighbor].path = v;}}}


17-49: Dijkstra Running TimeTotal running time for all iterations <strong>of</strong> the inner forstatement:for (i=0; i < G.length; i++) {v = minUnknownVertex(T);T[v].known = true;|> for (e = G[v]; e != null; e = e.next) {|> if (T[e.neighbor].distance >|> T[v].distance + e.cost) {|> T[e.neighbor].distance = T[v].distance + e.cost;|> T[e.neighbor].path = v;}}}Θ(V + E)Why Θ(V + E) and not just Θ(E)?


17-50: Dijkstra Running TimeTotal running time:Sum <strong>of</strong>:Time for initializationTime for executing all calls tominUnknownVertexTime for executing all distance / path updates= Θ(V + V 2 + (V + E)) = Θ(V 2 )


17-51: Improving DijkstraCan we do better than Θ(V 2 )For dense graphs, we can’t do betterTo ensure that the shortest path to all verticesis computed, need to look at all edges in thegraphA dense graph can have Θ(V 2 ) edgesFor sparse graphs, we can do betterWhere should we focus our attention?


17-52: Improving DijkstraCan we do better than Θ(V 2 )For dense graphs, we can’t do betterTo ensure that the shortest path to all verticesis computed, need to look at all edges in thegraphA dense graph can have Θ(V 2 ) edgesFor sparse graphs, we can do betterWhere should we focus our attention?Finding the unknown vertex with minimum cost!


17-53: Improving DijkstraTo improve the running time <strong>of</strong> Dijkstra:Place all <strong>of</strong> the vertices on a min-heapKey value for min-heap = distance <strong>of</strong> vertexfrom initialWhile min-heap is not empty:Pop smallest value <strong>of</strong>f min-heapUpdate tableProblems with this method?


17-54: Improving DijkstraTo improve the running time <strong>of</strong> Dijkstra:Place all <strong>of</strong> the vertices on a min-heapKey value for min-heap = distance <strong>of</strong> vertexfrom initialWhile min-heap is not empty:Pop smallest value <strong>of</strong>f min-heapUpdate tableProblems with this method?When we update the table, we need torearrange the heap


17-55: Rearranging the heapStore a pointer for each vertex back into the heapWhen we update the table, we need to do adecrease-key operationDecrease-key can take up to time O(V ).(Examples!)


17-56: Rearranging the heapTotal time:O(V ) remove-mins – O(V lg V )O(E) dercrease-keys – O(E lg V )Total time: O(V lg V + E lg V ) ∈ O(E lg V )


17-57: Improving DijkstraStore vertices in heapWhen we update the table, we need to rearrangethe heapAlternate Solution:When the cost <strong>of</strong> a vertex decreases, add anew copy to the heap


17-58: Improving DijkstraCreate a new priority queue, add start nodeWhile the queue is not empty:Remove the vertex v with the smallest distancein the heapIf v is not knownMark v as knownFor each neigbor w <strong>of</strong> v• If distance[w] > distance[v] + cost((v,w))• Set distance[w] = distance[v] +cost((v,w))• Add w to priority queue with prioritydistance[w]


17-59: Improved Dijkstra TimeEach vertex can be added to the heap once foreach incoming edgeSize <strong>of</strong> the heap can then be up to Θ(E)E inserts, on heap that can be up to size EE delete-mins, on heap that can be upto to sizeETotal: Θ(E lg E) ∈ Theta(E lg V )


17-60: Improved? Dijkstra TimeDon’t use priority queue, running time is Θ(V 2 )Do use a prioroty queue, running time is Θ(E lg E)Which is better?


17-61: Improved? Dijkstra TimeDon’t use priority queue, running time is Θ(V 2 )Do use a prioroty queue, running time is Θ(E lg E)Which is better?For dense graphs, (E ∈ Θ(V 2 )), Θ(V 2 ) is betterFor sparse graphs (E ∈ Θ(V )), Θ(E lg E) isbetter


17-62: All-Source Shortest PathWhat if we want to find the shortest path from allvertices to all other vertices?How can we do it?


17-63: All-Source Shortest PathWhat if we want to find the shortest path from allvertices to all other vertices?How can we do it?Run Dijktra’s Algorithm V timesHow long will this take?


17-64: All-Source Shortest PathWhat if we want to find the shortest path from allvertices to all other vertices?How can we do it?Run Dijktra’s Algorithm V timesHow long will this take?Θ(V E lg E) (using priority queue)for sparse graphs, Θ(V 2 lg V )for dense graphs, Θ(V 3 lg V )Θ(V 3 ) (not using a priority queue)


17-65: Floyd’s AlgorithmAlternate solution to all pairs shortest pathYields Θ(V 3 ) running time for all graphs


17-66: Floyd’s AlgorithmVertices numbered from 0..(n-1)k-path from vertex v to vertex u is a path whoseintermediate vertices (other than v and u) containonly vertices numbered less than or equal to k-1-path is a direct link


17-67: k-path Examples0115 357211Shortest -1-path from 0 to 4: 5Shortest 0-path from 0 to 4: 5Shortest 1-path from 0 to 4: 4Shortest 2-path from 0 to 4: 4Shortest 3-path from 0 to 4: 3341


17-68: k-path Examples0115 357211Shortest -1-path from 0 to 2: 7Shortest 0-path from 0 to 2: 7Shortest 1-path from 0 to 2: 6Shortest 2-path from 0 to 2: 6Shortest 3-path from 0 to 2: 6Shortest 4-path from 0 to 2: 4341


17-69: Floyd’s AlgorithmShortest n-path = Shortest pathShortest -1-path:∞ if there is no direct linkCost <strong>of</strong> the direct link, otherwise


17-70: Floyd’s AlgorithmShortest n-path = Shortest pathShortest -1-path:∞ if there is no direct linkCost <strong>of</strong> the direct link, otherwiseIf we could use the shortest k-path to find theshortest (k + 1) path, we would be set


17-71: Floyd’s AlgorithmShortest k-path from v to u either goes throughvertex k, or it does notIf not:Shortest k-path = shortest (k − 1)-pathIf so:Shortest k-path = shortest k − 1 path from v tok, followed by the shortest k − 1 path from k tow


17-72: Floyd’s AlgorithmIf we had the shortest k-path for all pairs (v,w), wecould obtain the shortest k + 1-path for all pairsFor each pair v,w, compare:length <strong>of</strong> the k-path from v to wlength <strong>of</strong> the k-path from v to k appended tothe k-path from k to wSet the k + 1 path from v to w to be theminimum <strong>of</strong> the two paths above


17-73: Floyd’s AlgorithmLet D k [v,w] be the length <strong>of</strong> the shortest k-pathfrom v to w.D 0 [v,w] = cost <strong>of</strong> arc from v to w (∞ if no directlink)D k [v,w] = MIN(D k−1 [v,w], D k−1 [v,k] + D k−1 [k,w])Create D −1 , use D −1 to create D 0 , use D 0 tocreate D 1 , and so on – until we have D n−1


17-74: Floyd’s AlgorithmUse a doubly-nested loop to create D k from D k−1Use the same array to store D k−1 and D k – justoverwrite with the new valuesEmbed this loop in a loop from 1..k


17-75: Floyd’s AlgorithmFloyd(Edge G[], int D[][]) {int i,j,kInitialize D, D[i][j] = cost from i to j}for (k=0; k


17-76: Floyd’s AlgorithmWe’ve only calculated the distance <strong>of</strong> the shortestpath, not the path itselfWe can use a similar strategy to the PATH field forDijkstra to store the pathWe will need a 2-D array to store the paths:P[i][j] = last vertex on shortest path from i to j

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!