5 searching

  • Published on
    23-Jun-2015

  • View
    55

  • Download
    2

Embed Size (px)

Transcript

<ul><li> 1. Searching in a Graph Jeff Edmonds York University COSC 3101LectureLecture 55 </li></ul><p> 2. Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First Search Linear Order Network Flow Linear Programming 3. Graph a c b Node ~ city or computer Edge ~ road or network Undirected or Directed A surprisingly large number of problems in computer science can be expressed as a graph theory problem. 4. Graph Search Specification: Reachability-from-single-source s Preconditions: The input is a graph G (either directed or undirected) and a source node s. Postconditions: Output all the nodes u that are reachable by a path in G from s. 5. Graph Search Basic Steps: s u Suppose you know that u is reachable from s v &amp; there is an edge from u to v You know that v is reachable from s Build up a set of reachable nodes. 6. Graph Search s reachable f u k reachable d v w reachable e h d reachable b t reachable d v w reachable e h d reachable b t How do we keep track of all of this information? How do we avoid cycling? 7. s a c h k f i l m j e b g d Graph Search 8. s a c h k f i l m j e b g d Graph Search 9. s a c h k f i l m j e b g d Graph Search 10. s a c h k f i m j e b g d We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search l 11. s a c h k f i m j e b g d We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search l Handle some foundNotHandled node i.e. find its neighbors Dont refind a node. 12. We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Handle some foundNotHandled node i.e. find its neighbors s a c h k f i m j e b g d l 13. We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search # of found nodes. s a c h k f i m j e b g d l measure progress # of handled nodes. Might not increase. 14. We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Node s is foundNotHandled s a c h k f i l m j e b g d Other nodes NotFound 15. We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search All nodes found. Exit a c h k f i m j e b g d l When cant make any more progress. No. Might not find all. When all found nodes are have been handled. Handle some foundNotHandled node s 16. Graph Search We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. a c h k f i m j e b g d l Exit All found nodes are handled. Exit Postconditions: Output all the nodes u that are reachable by a path in G from s. Output Found nodes 17. We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Exit a c h k f i m j e b g d l All found nodes are handled. Exit Found nodes are reachable from s. Reachable nodes have been Found. Postconditions: 18. We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Graph Search Exit a c h k f i m j e b g d l All found nodes are handled. Exit Reachable nodes have been Found. Postconditions: found = handled handled Notfound [A B] = [B A] NotFound nodes not reachable. 19. Graph Search Specification of Reachability-from-single-source s Preconditions: The input is a graph G (either directed or undirected) and a source node s. Postconditions: Output all the nodes u that are reachable by a path in G from s. EndingInitial ConditionsMake Progress Maintain Loop InvDefine Exit ConditionDefine Step Define Measure of Progress Define Loop Invariants Define Problem km 79 km to school Exit Exit 79 km 75 km Exit Exit 0 km Exit 20. Graph Search # of handled nodes. Handle some foundNotHandled node Time = O(n) f u k O(n) iterations, but iteration takes more than O(1) O(n) neighbors= O(n2 ) Could be fewer? Each edge visited, times.2 = O(E) Size = O(E) Linear time. 21. Graph Search Handle some foundNotHandled node Queue: Handle in order found. Breadth-First Search Stack: Handle most recently found Depth-First Search Priority Queue: Handle node that seems to be closest to s. Shortest (Weighted) Paths: 22. Breadth First Search Pre &amp; Post Conditions Algorithm 23. BFS s a c h k f i l m j e b g d Found Not Handled Queue 24. BFS s a c h k f i l m j e b g d Found Not Handled Queue s 25. BFS s a c h k f i l m j e b g d Found Not Handled Queue a b g d 26. BFS s a c h k f i l m j e b g d Found Not Handled Queue b g d c f 27. BFS s a c h k f i l m j e b g d Found Not Handled Queue b g c f m e 28. BFS s a c h k f i l m j e b g d Found Not Handled Queue b c f m e j 29. BFS s a c h k f i l m j e b g d Found Not Handled Queue c f m e j 30. BFS s a c h k f i l m j e b g d Found Not Handled Queue f m e j h i 31. BFS s a c h k f i l m j e b g d Found Not Handled Queue m e j h i 32. BFS s a c h k f i l m j e b g d Found Not Handled Queue e j h i l 33. BFS s a c h k f i l m j e b g d Found Not Handled Queue j h i l 34. BFS s a c h k f i l m j e b g d Found Not Handled Queue h i l 35. BFS s a c h k f i l m j e b g d Found Not Handled Queue i l k 36. BFS s a c h k f i l m j e b g d Found Not Handled Queue l k 37. BFS s a c h k f i l m j e b g d Found Not Handled Queue k 38. BFS s a c h k f i l m j e b g d Found Not Handled Queue 39. BFS What order are the nodes found? i.e. Breadth First Search Algorithm So far, the nodes have been found in order of length from s. 40. BFS s a c h k f i l m j e b g d Found Not Handled Queue 41. BFS s a c h k f i l m j e b g d Found Not Handled Queue s d=0 d=0 42. BFS Found Not Handled Queue d=0 a b g d d=1 s a c h k f i l m j e b g d d=0 d=1 43. BFS s a c h k f i l m j e b g d Found Not Handled Queue a b g d d=0 d=1 d=1 44. BFS s a c h k f i l m j e b g d Found Not Handled Queue b g d c f d=0 d=1 d=2 d=1 d=2 45. BFS s a c h k f i l m j e b g d Found Not Handled Queue b g c f m e d=0 d=1 d=2 d=1 d=2 46. BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 b j c f m e d=1 d=2 47. BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 j c f m e d=1 d=2 48. BFS s a c h k f i l m j e b g d Found Not Handled Queue c f m e j d=0 d=1 d=2 d=2 49. BFS s a c h k f i l m j e b g d Found Not Handled Queue f m e j h i d=0 d=1 d=2 d=3 d=2 d=3 50. BFS s a c h k f i l m j e b g d Found Not Handled Queue m e j h i d=0 d=1 d=2 d=3 d=2 d=3 51. BFS s a c h k f i l m j e b g d Found Not Handled Queue e j h i l d=0 d=1 d=2 d=3 d=2 d=3 52. BFS s a c h k f i l m j e b g d Found Not Handled Queue j h i l d=0 d=1 d=2 d=3 d=2 d=3 53. BFS s a c h k f i l m j e b g d Found Not Handled Queue h i l d=0 d=1 d=2 d=3 d=2 d=3 54. BFS s a c h k f i l m j e b g d Found Not Handled Queue h d=0 d=1 d=2 d=3 i l d=3 55. BFS s a c h k f i l m j e b g d Found Not Handled Queue i l k d=0 d=1 d=2 d=3 d=4 d=3 d=4 56. BFS s a c h k f i l m j e b g d Found Not Handled Queue l k d=0 d=1 d=2 d=3 d=4 d=3 d=4 57. BFS s a c h k f i l m j e b g d Found Not Handled Queue k d=0 d=1 d=2 d=3 d=4 d=3 d=4 58. BFS s a c h k f i l m j e b g d Found Not Handled Queue k d=0 d=1 d=2 d=3 d=4 d=4 59. BFS s a c h k f i l m j e b g d Found Not Handled Queue d=0 d=1 d=2 d=3 d=4 d=4 d=5 60. BFS What order are the nodes found? So far, the nodes have been found in order of length from s. Post Conditions: Finds a shortest path from s to each node v and its length. To prove path is shortest: Prove there is a path of this length. Prove there are no shorter paths. Give a path (witness) 61. Basic Steps: s u The shortest path to u has length d v &amp; there is an edge from u to v There is a path to v with length d+1. BFS 62. BFS What order are the nodes found? So far, the nodes have been found in order of length from s. Post Conditions: Finds a shortest path from s to each node v and its length. When we find v, we know there isn't a shorter path to it because ? Prove there are no shorter paths. Otherwise, we would have found it already. 63. BFS Data structure for storing tree: For each node v, store (v) to be parent of v. 64. Basic Steps: s u Path to u v &amp; there is an edge from u to v Parent of v is (v) (v) = u. BFS 65. Graph Search Handle some foundNotHandled node Queue: Handle in order found. Breadth-First Search Stack: Handle most recently found Depth-First Search Priority Queue: Handle node that seems to be closest to s. Shortest (Weighted) Paths: 66. Dijkstra's Shortest-Weighted Paths Specification: Dijkstra's Shortest-Weighted Paths Reachability-from-single-source s Preconditions: The input is a graph G (either directed or undirected) with positive edge weights and a source node s. Postconditions: Finds a shortest weighted path from s to each node v and its length. 67. Dijkstra's Shortest-Weighted Paths s u v 100 1 1 1 1 w r Length of shortest path from s to u? 4 68. So far, the nodes have been found in order of length from s. BFS s u v 100 1 1 1 1 w r Which node is found first? Is the same true for Dijkstra's Alg? 69. So far, the nodes have been found in order of length from s. s u v 100 1 1 1 1 w r It has the longest path from s. Which node is found first? Is the same true for Dijkstra's Alg? BFS 70. So far, the nodes have been found in order of length from s. s u v 100 1 1 1 1 w r In what order do we handle the FoundNotHandled nodes? handled Dijkstra's Handle node that seems to be closest to s. 71. To prove path is shortest: Prove there is a path of this length. Prove there are no shorter paths. Give a path (witness) Dijkstra's So far, the nodes have been handled in order of length from s. Post Conditions: Finds a shortest weighted path from s to each node v and its length. 72. To prove path is shortest: Prove there is a path of this length. Prove there are no shorter paths. Dijkstra's When we handle v, we know there isn't a shorter path to it because ? Otherwise, we would have handled it already. Post Conditions: Finds a shortest weighted path from s to each node v and its length. So far, the nodes have been handled in order of length from s. 73. Basic Steps: u The shortest of handled paths to u has length d(u) Dijkstra's &amp; there is an edge from u to v w vs The shortest of handled paths to v has length d(v) The shortest known path to v has length min( d(v), d(u)+w ). Handle node that seems to be closest to s. Need to keep approximate shortest distances. 74. 75. Dijkstra's The shortest of handled paths to v has length d(v) u vs 76. Dijkstra's u vs Handled nodes Found nodes Handled Edges? The shortest of handled paths to v has length d(v) 77. Dijkstra's u vs Handled Nodes Found Nodes Handled Edges The shortest of handled paths to v has length d(v) Handled Paths? 78. Dijkstra's u vs Handled Nodes Found Nodes Handled Edges The shortest of handled paths to v has length d(v) Handled Paths 79. Dijkstra's u vs Handled Nodes The shortest of handled paths to v has length d(v) d(v) is the length of the shortest handled paths to v. For handled u, d(u) is the length of the shortest paths to u. NotFound d(v)= 80. Exit 81. Dijkstra's For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). For handled u, d(u) is the length of the shortest paths to u. d(v) is the length of the shortest handled path to v. 82. Dijkstra's For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). For handled u, d(u) is the length of the shortest paths to u. d(u) is the length of the shortest handled path to u. d(u) 83. Dijkstra's For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). For handled u, d(u) is the length of the shortest paths to u. d(u) d(u) is the length of the shortest handled path to u. First not handled node in path. 84. Dijkstra's For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). For handled u, d(u) is the length of the shortest paths to u. d(u) First not handled node in path.d(u) d(u) &amp; d(u) are the length of the shortest handled paths to u and u. 85. Dijkstra's d(v) is the length of the shortest handled path to v. For handled w, d(w) is the length of the shortest paths to w. Handle node with smallest d(u). d(v) is the length of the shortest handled path to v. d(u) d(v) w d(v) = min( d(v), d(u)+w ). 86. Time = O(E) Each edge visited, times.2 ? Dijkstra's This number of times following an edge. Must update Priority Queue. Takes time O(log(n)) Time = O(E log(n)) For each update, d(v) = min( d(v), d(u)+w ). Heap 87. Dijkstra's Shortest-Weighted Paths Specification: Dijkstra's Shortest-Weighted Paths Reachability-from-single-source s Preconditions: The input is a graph G (either directed or undirected) with positive edge weights and a source node s. Postconditions: Finds a shortest weighted path from s to each node v and its length. EndingInitial ConditionsMake Progress Maintain Loop InvDefine Exit ConditionDefine Step Define Measure of Progress Define Loop Invariants Define Problem km 79 km to school Exit Exit 79 km 75 km Exit Exit 0 km Exit 88. Dijkstra's 89. Dijkstra's 90. Graph Search Handle some foundNotHandled node Queue: Handle in order found. Breadth-First Search Stack: Handle most recently found Depth-First Search Priority Queue: Handle node that seems to be closest to s. Shortest (Weighted) Paths: 91. DFS s a c h k f i l m j e b g d Found Not Handled Stack 92. DFS s a c h k f i l m j e b g d Found Not Handled Stack s 93. DFS s a c h k f i l m j e b g d Found Not Handled Stack a b g d 94. DFS s a c h k f i l m j e b g d Found Not Handled Stack a m g d e 95. DFS 96. DFS The nodes in the stack form a path starting at s. 97. DFS s a c h k f i l m j e b g d Found Not Handled Stack 98. DFS s a c h k f i l m j e b g d Found Not Handled Stack s,0 99. DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,0 100. DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,0 101. DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,0 102. DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,1 k,0 103. DFS s a c h k f i l m j e b g d s,1 Found Not Handled Stack a,1 c,1 h,1 Tree Edge Path on Stac...</p>