Graph Search Methods Spring 2007 CSE POSTECH Graph

  • Slides: 44
Download presentation
Graph Search Methods Spring 2007 CSE, POSTECH

Graph Search Methods Spring 2007 CSE, POSTECH

Graph Search Methods l l A vertex u is reachable from vertex v iff

Graph Search Methods l l A vertex u is reachable from vertex v iff there is a path from v to u. A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v. 2 1 3 8 4 5 9 6 10 11 7

Graph Search Methods l Many graph problems solved by a search method – –

Graph Search Methods l Many graph problems solved by a search method – – l Finding a path from one vertex to another. Determining whether a graph is connected Find a spanning tree Finding a minimum-cost path/spanning tree Commonly used search methods – – Breadth-first search (BFS) Depth-first search (DFS)

Breadth-First Search (BFS) Algorithm l l l BFS algorithm is the method of starting

Breadth-First Search (BFS) Algorithm l l l BFS algorithm is the method of starting at a vertex and identifying all vertices reachable from it Read Section 16. 8. 1 and the pseudo-code of BFS in Figure 16. 7 Similar to the level-order traversal of a binary tree

Breadth-First Search (BFS) Algorithm l l Visit the starting vertex and put into a

Breadth-First Search (BFS) Algorithm l l Visit the starting vertex and put into a FIFO queue Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue All vertices reachable from the start vertex (including the start vertex) are visited When the queue becomes empty, the search is terminated

Breadth-First Search Example 2 1 3 8 4 5 9 6 11 7 l

Breadth-First Search Example 2 1 3 8 4 5 9 6 11 7 l 10 Start search at vertex 1.

Breadth-First Search Example 2 1 FIFO queue 1 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 1 3 8 4 5 9 6 10 11 7 l l Mark/label start vertex and put in a FIFO queue. Remove 1 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue.

Breadth-First Search Example 2 1 FIFO queue 24 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 24 3 8 4 5 9 6 10 11 7 l l Remove 2 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices?

Breadth-First Search Example 2 1 FIFO queue 4536 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 4536 3 8 4 5 9 6 11 7 l l 10 Remove 4 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices?

Breadth-First Search Example 2 1 FIFO queue 536 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 536 3 8 4 5 9 6 10 11 7 l l Remove 5 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices?

Breadth-First Search Example 2 1 FIFO queue 3697 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 3697 3 8 4 5 9 6 10 11 7 l l Remove 3 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices?

Breadth-First Search Example 2 1 FIFO queue 697 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 697 3 8 4 5 9 6 10 11 7 l l Remove 6 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices?

Breadth-First Search Example 2 1 FIFO queue 97 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 97 3 8 4 5 9 6 10 11 7 l l Remove 9 from Queue; Visit adjacent unvisited vertices; Put the visited vertices in Queue. Are there any newly visited vertices?

Breadth-First Search Example 2 1 FIFO queue 78 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 78 3 8 4 5 9 6 11 7 l 10 Remove 7 from Queue; Visit adjacent unvisited vertices; Put in Queue.

Breadth-First Search Example 2 1 FIFO queue 8 3 8 4 5 9 6

Breadth-First Search Example 2 1 FIFO queue 8 3 8 4 5 9 6 11 7 l 10 Remove 8 from Queue; Visit adjacent unvisited vertices; Put in Queue.

Breadth-First Search Example 2 1 FIFO queue 3 8 4 5 9 6 10

Breadth-First Search Example 2 1 FIFO queue 3 8 4 5 9 6 10 11 7 l Queue is empty, thus the search terminates.

Breadth-First Search Example l l What was the order of visited vertices? 1, 2,

Breadth-First Search Example l l What was the order of visited vertices? 1, 2, 4, 5, 3, 6, 9, 7, 8 What is the subgraph formed by the edges used to reach new vertices during the BFS? 2 1 3 8 4 5 9 6 7

Time Complexity l l Each visited vertex is put on the queue exactly once.

Time Complexity l l Each visited vertex is put on the queue exactly once. When a vertex is removed from the queue, we examine its adjacent vertices. – – l O(n) if adjacency matrix used. O(vertex degree) if adjacency lists used. Total time – – O(n 2) if adjacency matrix used O(n+e) if adjacency lists used where e is the sum of vertex degrees and therefore e is also the number of edges.

Path Finding Problem from vertex u to vertex v l l Start a breadth-first

Path Finding Problem from vertex u to vertex v l l Start a breadth-first search at vertex u. Terminate if either of the following occurs – – l successfully when vertex v is visited or unsuccessfully when queue becomes empty but v is not visited Time – – O(n 2) when adjacency matrix used O(n+e) when adjacency lists used (e is number of edges)

Is a Graph Connected? l l l Start a breadth-first search at any vertex

Is a Graph Connected? l l l Start a breadth-first search at any vertex of the graph. A graph is connected iff all n vertices are visited. Time – – O(n 2) when adjacency matrix used O(n+e) when adjacency lists used (e is number of edges)

Connected Components l l Start a breadth-first search at any unvisited vertex of the

Connected Components l l Start a breadth-first search at any unvisited vertex of the graph. Newly visited vertices (plus edges between them) define a component. Repeat until all vertices are visited. Time – – O(n 2) when adjacency matrix used O(n+e) when adjacency lists used (e is number of edges)

Connected Components 2 1 3 8 4 5 9 6 10 11 7

Connected Components 2 1 3 8 4 5 9 6 10 11 7

Breath-First Spanning Tree l l l Start a breadth-first search at any vertex of

Breath-First Spanning Tree l l l Start a breadth-first search at any vertex of the graph. If a graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree breadth-first spanning tree Time – – O(n 2) when adjacency matrix used O(n+e) when adjacency lists used (e is number of edges)

Breadth-First Spanning Tree Digraph Example BF Spanning Tree of Digraph (a)

Breadth-First Spanning Tree Digraph Example BF Spanning Tree of Digraph (a)

Depth-First Search (DFS) Algorithm l l l DFS algorithm is an alternative to BFS

Depth-First Search (DFS) Algorithm l l l DFS algorithm is an alternative to BFS Similar to the pre-order traversal of a binary tree See Figure 16. 18 for the pseudo-code of DFS

Depth-First Search (DFS) Algorithm l l l Visit the starting vertex v and mark

Depth-First Search (DFS) Algorithm l l l Visit the starting vertex v and mark it as reached Select an unreached vertex u adjacent from v If such a vertex does not exist, the search terminates, otherwise a depth-first search from u is initiated When this search is completed, we select another unreached vertex adjacent to from v If such a vertex does not exist, then the search terminates, otherwise a depth-first search starts from this vertex and so on…. .

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7 l l Start search at vertex 1. Label vertex 1 as reached and do a depth-first search from either 2 or 4.

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7 l Label vertex 2 and do a depth-first search from either 3, 5 or 6.

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7 l Label vertex 5 and do a depth-first search from either 3, 7 or 9.

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7 l Label vertex 9 and do a depth-first search from either 6 or 8.

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7 l l Label vertex 8 and return to vertex 9. From vertex 9 do a DFS(6).

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7 l Label vertex 6 and do a depth-first search from either 4 or 7.

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l l 10 Label vertex 4 and return to 6. From vertex 6 do a DFS(7).

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l l 10 Label vertex 7 and return to 6. Return to 9.

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l Return to 5. 10

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l Do a DFS(3). 10

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l

Depth-First Search Example 2 1 3 8 4 5 9 6 11 7 l l l Label 3 and return to 5. Return to 2. Return to 1. 10

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7

Depth-First Search Example 2 1 3 8 4 5 9 6 10 11 7 l DFS is done and return to invoking method.

Depth-First Search Example l l What was the order of visited vertices? 1, 2,

Depth-First Search Example l l What was the order of visited vertices? 1, 2, 5, 9, 8, 6, 4, 7, 3 What is the subgraph formed by the edges used to reach new vertices during the BFS? 2 1 3 8 4 5 9 6 7

Depth-First Search Properties l l l Same complexity as BFS. Same properties with respect

Depth-First Search Properties l l l 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.

Exercise 16. 41 – for graph of Figure 16. 4(a) 1 l 28 2

Exercise 16. 41 – for graph of Figure 16. 4(a) 1 l 28 2 10 14 6 16 7 3 l l 24 25 18 5 22 4 12 l Draw a linked adjacency-list representation Starting from vertex 4, the order of visited vertices using BFS are? Show the subgraph formed in part (b) Redo parts (b) & (c) using DFS

Exercise 16. 41 – solution (a) & (b) (a) Draw a linked adjacencylist representation

Exercise 16. 41 – solution (a) & (b) (a) Draw a linked adjacencylist representation (b) Starting from vertex 4, the order of visited vertices are 4, 3, 5, 7, 2, 6, 1

Exercise 16. 41 – solution (c) & (d) (c) Show the subgraph formed (d)

Exercise 16. 41 – solution (c) & (d) (c) Show the subgraph formed (d) Redo parts (b) & (c) using in part (b) DFS order: 4, 3, 2, 1, 6, 5, 7

READING l l Do Exercise 16. 41 using Figure 16. 16(a) starting from vertex

READING l l Do Exercise 16. 41 using Figure 16. 16(a) starting from vertex 1 Read Chapter 16