# CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.

## Presentation on theme: "CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort."— Presentation transcript:

CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort

CS 473Lecture 142 Depth-First Search Graph G  (V,E) directed or undirected Adjacency list representation Goal: Systematically explore every vertex and every edge Idea: search deeper whenever possible –Using a LIFO queue (Stack; FIFO queue used in BFS)

CS 473Lecture 143 Depth-First Search Maintains several fields for each v  V Like BFS, colors the vertices to indicate their states. Each vertex is –Initially white, –grayed when discovered, –blackened when finished Like BFS, records discovery of a white v during scanning Adj[u] by  [v]  u

CS 473Lecture 144 Depth-First Search Unlike BFS, predecessor graph G  produced by DFS forms spanning forest G   (V,E  ) where E   {(  [v],v): v  V and  [v]  NIL } G   depth-first forest (DFF) is composed of disjoint depth-first trees (DFTs)

CS 473Lecture 145 Depth-First Search DFS also timestamps each vertex with two timestamps d[ v ]: records when v is first discovered and grayed f[ v ]: records when v is finished and blackened Since there is only one discovery event and finishing event for each vertex we have 1  d[ v ]  f[ v ]  2|V| 12 d[v]d[v] f[v]f[v] 2|V| Time axis for the color of a vertex

CS 473Lecture 146 Depth-First Search DFS(G) for each u  V do color[u]  white  [u]  NIL time  0 for each u  V do if color[u]  white then DFS-V ISIT (G, u) color[u]  gray d[u]  time  time  1 for each v  Adj[u] do if color[v]  white then  [v]  u DFS-V ISIT (G, v) color[u]  black f[u]  time  time  1

CS 473Lecture 147 Depth-First Search Running time:  (V  E) Initialization loop in DFS :  (V) Main loop in DFS :  (V) exclusive of time to execute calls to DFS-V ISIT DFS-V ISIT is called exactly once for each v  V since –DFS-V ISIT is invoked only on white vertices and –DFS-V ISIT (G, u) immediately colors u as gray For loop of DFS-V ISIT (G, u) is executed | Adj[u] | time Since  | Adj[u] |  E, total cost of executing loop of DFS-V ISIT is  (E)

CS 473Lecture 148 Depth-First Search: Example

CS 473Lecture 149 Depth-First Search: Example

CS 473Lecture 1410 Depth-First Search: Example

CS 473Lecture 1411 Depth-First Search: Example

CS 473Lecture 1412 Depth-First Search: Example

CS 473Lecture 1413 Depth-First Search: Example

CS 473Lecture 1414 Depth-First Search: Example

CS 473Lecture 1415 Depth-First Search: Example

CS 473Lecture 1416 Depth-First Search: Example

CS 473Lecture 1417 Depth-First Search: Example

CS 473Lecture 1418 Depth-First Search: Example

CS 473Lecture 1419 Depth-First Search: Example

CS 473Lecture 1420 Depth-First Search: Example

CS 473Lecture 1421 Depth-First Search: Example

CS 473Lecture 1422 Depth-First Search: Example

CS 473Lecture 1423 Depth-First Search: Example

CS 473Lecture 1424 Depth-First Search: Example

CS 473Lecture 1425 Depth-First Search: Example

CS 473Lecture 1426 Depth-First Search: Example

CS 473Lecture 1427 Depth-First Search: Example

CS 473Lecture 1428 Depth-First Search: Example

CS 473Lecture 1429 Depth-First Search: Example

CS 473Lecture 1430 Depth-First Search: Example

CS 473Lecture 1431 Depth-First Search: Example

CS 473Lecture 1432 Depth-First Search: Example DFS(G) terminatedDepth-first forest (DFF)

Download ppt "CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort."

Similar presentations