CSE 373: Data Structures and Algorithms Shortest Paths Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...
Four classes of graph problem .. that can be solved efficiently (in polynomial time) 1. Shortest path – find a shortest path between two vertices in a graph 2. Minimum spanning tree – find subset of edges with minimum total weights 3. Matching – find set of edges without common vertices 4. Maximum flow – find the maximum flow from a source vertex to a sink vertex A wide array of graph problems that can be solved in polynomial time are variants of these above problems. In this class, we’ll cover the first two problems – shortest path and minimum spanning tree CSE 373 AU 18 2
<latexit sha1_base64="y87xbtbAFONJkMc1kH2lF/va0=">AD6nicdVJLb9NAEHZsHsW8WjhyWRFVSi5R3ApRISFVQrT0gogSaU4qtbrcbzKetfsIyWy/Ce4ITjyp/g3rB0nSlKYi8cz38w38+1EOaNK9/t/Wq535+69+3sP/IePHj95un/wbKiEkQGRDAhryKsgFEOA01g6tcAs4iBqNo9q7Kj+YgFRX8i17kMnwlNOEqxt6Pqg9cv3wimlBeYTYWkOs0oKcfBxA/PDCcVqlCAJUnL4lziPEXnpR9+1lgDyrCcIcwYsgyaElAIK2T4jIsbjlYgLYZUd0D/tWAgU5CpdJ1BXzrbnXazFSd6j5+OEopA1Q0fRBViAuNIMv1olyzECMlcI3ervliWPJZjMhUTFHb1aonjD6I9BpGgmpOl27z0VSAeh6+vJ/08+3R5vzPmexdJ/bF8K9S8qoa4h+O407B3a0y91RLdyOyHwOtV/Cv9v9Xr82dNsJGqftNHZp3/NVGAtiMstDGFZqHPRzPSlw9T4M7FpGQY7JDE9hbF2OM1CTor6jEh3aSIwSq1YirJZ1dLOiwJlSiyAzrVO3mquC/cmOjk5NJQXluNHCyJEoMs+qi6ihRTCUQzRbWwcRuTwkiKZaYaHu62ywrecrdcK7A2NVFXG1p/63UcENElmEeh/aidC4FKcpDNKiWtMwRoDquiF8J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iBqNo9q7Kj+YgFRX8i17kMnwlNOEqxt6Pqg9cv3wimlBeYTYWkOs0oKcfBxA/PDCcVqlCAJUnL4lziPEXnpR9+1lgDyrCcIcwYsgyaElAIK2T4jIsbjlYgLYZUd0D/tWAgU5CpdJ1BXzrbnXazFSd6j5+OEopA1Q0fRBViAuNIMv1olyzECMlcI3ervliWPJZjMhUTFHb1aonjD6I9BpGgmpOl27z0VSAeh6+vJ/08+3R5vzPmexdJ/bF8K9S8qoa4h+O407B3a0y91RLdyOyHwOtV/Cv9v9Xr82dNsJGqftNHZp3/NVGAtiMstDGFZqHPRzPSlw9T4M7FpGQY7JDE9hbF2OM1CTor6jEh3aSIwSq1YirJZ1dLOiwJlSiyAzrVO3mquC/cmOjk5NJQXluNHCyJEoMs+qi6ihRTCUQzRbWwcRuTwkiKZaYaHu62ywrecrdcK7A2NVFXG1p/63UcENElmEeh/aidC4FKcpDNKiWtMwRoDquiF8JHezKetsZHvUC6386ap+eNJLvOS+cl07HCZzXzqnzwbl0Bg5xPbfrHrnHvO+ez+8n0uo2pqnjtb5v3+C7ARu0=</latexit> BFS on a directed graph 1: function search(Graph G) mark all vertices as unknown 2: toVisit.enqueue(first vertex) 3: mark first vertex as known 4: Run Breadth First Search on this graph starting from s. while toVisit is not empty do 5: What order are vertices placed on the queue? current = toVisit.dequeue() 6: for v : current.outNeighbors() do 7: When processing a vertex insert neighbors in if v is unknown then 8: alphabetical order. toVisit.enqueue(v) 9: mark v as known 10: end if 11: x u end for 12: visited.add(current) 13: end while 14: s t 15: end function v y Queue w CSE 373 AU 18 3
Q1: Do a BFS on this graph, starting at node A B A D C E Add nodes to queue in reverse alphabetical order Add nodes to queue in alphabetical order Queue Queue CSE 373 AU 18 4
Q2: Do a DFS on this graph, starting at node A B A D C E Add nodes to stack in reverse alphabetical order Add nodes to stack in alphabetical order Stack Stack CSE 373 AU 18 5
Shortest paths How does Google Maps figure out this is the fastest way to get to office hours? CSE 373 AU 18 6
Representing maps as graphs How do we represent a map as a graph? What are the vertices and edges? CSE 373 AU 18 7
Representing maps as graphs Question: Does this graph correctly model all the shown paths? K R S H D P CSE 373 AU 18 8
Representing maps as graphs Question: Does this graph correctly model all the shown paths? K 2 1 Not quite. R 2 Some paths are longer than others. S H So we use “weights” to capture that additional information. 4 4 5 D P 3 CSE 373 AU 18 9
Shortest paths length of a path is the sum of the edge weights on that path. The len Shortest t Path th Probl blem Gi Given: a directed graph G and vertices s and t d: the shortest path from s to t Fi Find: y 1 4 3 w t s 5 6 4 5 2 x u v 1 CSE 373 AU 18 10
Unweighted graphs Let’s start with a simpler version: the edges are all the same weight ( un unweigh ghted ) If the graph is unweighted, how do we find a shortest paths? CSE 373 AU 18 11
Unweighted graphs If the graph is unweighted, how do we find a shortest paths? u w s y t v x What’s the shortest path from s to s? - Well….we’re already there. What’s the shortest path from s to u or s to v? - Just go on the edge from s From s to w, x, or y? - Can’t get there directly from s, if we want a length 2 path, have to go through u or v. CSE 373 AU 18 12
<latexit sha1_base64="ZE89WJRyuIgAiLWDjRBcq3cxNg=">AEOXicdVNLb9NAEHYTHsW8WjhyWVFVagWK4kqICqlSJURLyhQklaKo2q9Hser2LtmH2kjywf4W/wSjtwQHPkDjB+JmrbsxZNvHt/MN5MgS7g23e6PlVb71u07d1fvufcfPHz0eG39yUBLqxj0mUykOg2ohoQL6BtuEjNFNA0SOAkmLwt/SdTUJpL8dnMhildCx4xBk1CJ2tr/xXT+AMRc5TcZScROnBVDb+T6B1awMioPIn0cS2VAmx41sS7yQ0WzmBy+JL4VIaiSPR8ARlyQurWicP1jQw0QIwdc9MB8cWCha3avz13p1RNmhxCNZkIeS7mvhruhCgD2SNd1z+JeQIkb0oSromQhkCamdmCj1mlQJQJc+YQambkPJCK5FPyZh7VkdZ8AD6OA6n01jYWOYrKAKxsRdXLou6lSadVS1Rgy3uLSgvoBfFuTsLFhMBAa2xikfdfnabLEk0vqfNOhEdR9cF5qm8lTI0S3N9EOHSTol7trbR7XSrR64bXmNsOM3r4Xm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qvPMe5sOZ7z2tl3js9p+w1qfWRetr61v7e/tn+1f7dx3aWmlynjpLr/3HyUEZ8E=</latexit> <latexit sha1_base64="ZE89WJRyuIgAiLWDjRBcq3cxNg=">AEOXicdVNLb9NAEHYTHsW8WjhyWVFVagWK4kqICqlSJURLyhQklaKo2q9Hser2LtmH2kjywf4W/wSjtwQHPkDjB+JmrbsxZNvHt/MN5MgS7g23e6PlVb71u07d1fvufcfPHz0eG39yUBLqxj0mUykOg2ohoQL6BtuEjNFNA0SOAkmLwt/SdTUJpL8dnMhildCx4xBk1CJ2tr/xXT+AMRc5TcZScROnBVDb+T6B1awMioPIn0cS2VAmx41sS7yQ0WzmBy+JL4VIaiSPR8ARlyQurWicP1jQw0QIwdc9MB8cWCha3avz13p1RNmhxCNZkIeS7mvhruhCgD2SNd1z+JeQIkb0oSromQhkCamdmCj1mlQJQJc+YQambkPJCK5FPyZh7VkdZ8AD6OA6n01jYWOYrKAKxsRdXLou6lSadVS1Rgy3uLSgvoBfFuTsLFhMBAa2xikfdfnabLEk0vqfNOhEdR9cF5qm8lTI0S3N9EOHSTol7trbR7XSrR64bXmNsOM3r4Xm8kPJbIqNsoRqPfS6mRnlVBnOEsDerYaMsgkdwxBNQVPQo7w6y4JsIhKSCKeNJC6kQi9n5DTVepYGJmWZ3XV4I3+YbWRLujnIvMGhCsJopsghKS8sZJyBUwk8zQoAzH54ywmCrKDP4Tlnm+hRX4UyDxdFlWE6Jv3FXcM5kmlIR+njnJlOS5cUm6ZdDInOA2y5xzdxSaO+qrNeNwU7HQ/vjzsb+biP5qvPMe5sOZ7z2tl3js9p+w1qfWRetr61v7e/tn+1f7dx3aWmlynjpLr/3HyUEZ8E=</latexit> Unweighted graphs: Key Idea To find the set of vertices at distance k, just find the set of vertices at distance k-1, and 1: function bfsShortestPaths(Graph G, Vertex source) see if any of them have an outgoing edge toVisit.enqueue(source) 2: to an undiscovered vertex. mark source as known 3: source.dist = 0 4: Do we already know an algorithm that while toVisit is not empty do 5: does something like that? current = toVisit.dequeue() 6: for v : current.outNeighbors() do 7: Yes! BFS! if v is unknown then 8: v.distance = current.distance + 1 9: v.predecessor = current 10: toVisit.enqueue(v) 11: mark v as known 12: end if 13: end for 14: end while 15: 16: end function CSE 373 AU 18 13
Recommend
More recommend