Graph Search Methods Graph Search Methods • A search method starts at a given vertex v and • A vertex u is reachable from vertex v iff there is a visits/labels/marks every vertex that is reachable path from v to u. from v. 2 2 3 3 8 8 1 1 4 4 5 5 9 9 10 10 6 6 7 11 7 11 Breadth-First Search Graph Search Methods • Many graph problems solved using a search method. • Visit start vertex and put into a FIFO queue. � Path from one vertex to another. • Repeatedly remove a vertex from the queue, visit � Is the graph connected? its unvisited adjacent vertices, put newly visited � Find a spanning tree. vertices into the queue. � Etc. • Commonly used search methods: � Breadth-first search. � Depth-first search. Breadth-First Search Example Breadth-First Search Example FIFO Queue 2 2 3 3 8 8 1 1 1 1 4 4 5 5 9 9 10 10 6 6 7 11 7 11 Start search at vertex 1. Visit/mark/label start vertex and put in a FIFO queue.
Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 3 3 2 4 8 8 1 1 1 1 1 4 4 4 5 5 9 9 10 10 6 6 7 11 7 11 Remove 1 from Q; visit adjacent unvisited vertices; Remove 1 from Q; visit adjacent unvisited vertices; put in Q. put in Q. Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 2 3 3 3 2 4 4 5 3 6 8 8 1 1 1 1 4 4 4 4 5 5 5 9 9 10 10 6 6 6 7 11 7 11 Remove 2 from Q; visit adjacent unvisited vertices; Remove 2 from Q; visit adjacent unvisited vertices; put in Q. put in Q. Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 2 3 3 3 3 4 5 3 6 5 3 6 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 10 10 6 6 6 6 7 11 7 11 Remove 4 from Q; visit adjacent unvisited vertices; Remove 4 from Q; visit adjacent unvisited vertices; put in Q. put in Q.
Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 2 3 3 3 3 5 3 6 3 6 9 7 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 10 10 6 6 6 6 7 11 7 7 11 Remove 5 from Q; visit adjacent unvisited vertices; Remove 5 from Q; visit adjacent unvisited vertices; put in Q. put in Q. Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 2 3 3 3 3 3 6 9 7 6 9 7 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 7 11 7 7 11 Remove 3 from Q; visit adjacent unvisited vertices; Remove 3 from Q; visit adjacent unvisited vertices; put in Q. put in Q. Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 2 3 3 3 3 6 9 7 9 7 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 7 11 7 7 11 Remove 6 from Q; visit adjacent unvisited vertices; Remove 6 from Q; visit adjacent unvisited vertices; put in Q. put in Q.
Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 2 3 3 3 3 9 7 7 8 8 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 7 11 7 7 11 Remove 9 from Q; visit adjacent unvisited vertices; Remove 9 from Q; visit adjacent unvisited vertices; put in Q. put in Q. Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 2 3 3 3 3 7 8 8 8 8 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 7 11 7 7 11 Remove 7 from Q; visit adjacent unvisited vertices; Remove 7 from Q; visit adjacent unvisited vertices; put in Q. put in Q. Breadth-First Search Example Breadth-First Search Example FIFO Queue FIFO Queue 2 2 2 2 3 3 3 3 8 8 8 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 7 11 7 7 11 Remove 8 from Q; visit adjacent unvisited vertices; Queue is empty. Search terminates. put in Q.
Breadth-First Search Property Time Complexity • Each visited vertex is put on (and so removed from) the queue exactly once. • All vertices reachable from the start vertex • When a vertex is removed from the queue, (including the start vertex) are visited. we examine its adjacent vertices. � O(n) if adjacency matrix used � O(vertex degree) if adjacency lists used • Total time � O(mn), where m is number of vertices in the component that is searched (adjacency matrix) Time Complexity Path From Vertex v To Vertex u � O(n + sum of component vertex degrees) (adj. • Start a breadth-first search at vertex v. lists) = O(n + number of edges in component) • Terminate when vertex u is visited or when Q becomes empty (whichever occurs first). • Time � O(n 2 ) when adjacency matrix used � O(n+e) when adjacency lists used (e is number of edges) Is The Graph Connected? Connected Components • Start a breadth-first search at any as yet unvisited vertex of the graph. • Start a breadth-first search at any vertex of • Newly visited vertices (plus edges between the graph. them) define a component. • Graph is connected iff all n vertices get • Repeat until all vertices are visited. visited. • Time � O(n 2 ) when adjacency matrix used � O(n+e) when adjacency lists used (e is number of edges)
Connected Components Time Complexity � O(n 2 ) when adjacency matrix used 2 3 � O(n+e) when adjacency lists used (e 8 1 is number of edges) 4 5 9 10 6 7 11 Spanning Tree Spanning Tree • Start a breadth-first search at any vertex of 2 2 the graph. 3 3 8 8 • If graph is connected, the n-1 edges used to 1 1 get to unvisited vertices define a spanning 4 4 tree (breadth-first spanning tree). 5 5 9 9 • Time � O(n 2 ) when adjacency matrix used 6 6 � O(n+e) when adjacency lists used (e is number 7 7 of edges) Breadth-first search from vertex 1. Breadth-first spanning tree. Depth-First Search Depth-First Search Example 2 2 3 8 1 1 depthFirstSearch(v) { 4 5 9 Label vertex v as reached. 10 for (each unreached vertex u 6 7 11 adjacenct from v) Start search at vertex 1. depthFirstSearch(u); Label vertex 1 and do a depth first search } from either 2 or 4. Suppose that vertex 2 is selected.
Depth-First Search Example Depth-First Search Example 2 2 2 2 3 3 8 8 1 1 1 1 4 4 5 5 5 5 9 9 9 10 10 6 6 7 11 7 11 Label vertex 2 and do a depth first search Label vertex 5 and do a depth first search from either 3, 5, or 6. from either 3, 7, or 9. Suppose that vertex 5 is selected. Suppose that vertex 9 is selected. Depth-First Search Example Depth-First Search Example 2 2 2 2 3 3 8 8 8 8 1 1 1 1 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 7 11 7 11 Label vertex 9 and do a depth first search Label vertex 8 and return to vertex 9. from either 6 or 8. From vertex 9 do a dfs(6). Suppose that vertex 8 is selected. Depth-First Search Example Depth-First Search Example 2 2 2 2 3 3 8 8 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 11 7 7 11 Label vertex 6 and do a depth first search from Label vertex 4 and return to 6. either 4 or 7. Suppose that vertex 4 is selected. From vertex 6 do a dfs(7).
Depth-First Search Example Depth-First Search Example 2 2 2 2 3 3 8 8 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 7 11 7 7 11 Label vertex 7 and return to 6. Return to 9. Return to 5. Depth-First Search Example Depth-First Search Example 2 2 2 2 3 3 3 3 8 8 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 7 11 7 7 11 Label 3 and return to 5. Return to 2. Do a dfs(3). Depth-First Search Example Depth-First Search Example 2 2 2 2 3 3 3 3 8 8 8 8 1 1 1 1 4 4 4 4 5 5 5 5 9 9 9 9 10 10 6 6 6 6 7 7 11 7 7 11 Return to 1. Return to invoking method.
Depth-First Search Properties • Same complexity as BFS. • Same properties with respect to path finding, connected components, and spanning trees. • Edges used to reach unlabeled vertices define a depth-first spanning tree when the graph is connected. • There are problems for which bfs is better than dfs and vice versa.
Recommend
More recommend