Download presentation

Presentation is loading. Please wait.

Published byCristian Easter Modified over 2 years ago

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

2
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)

3
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

4
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)

5
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

6
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

7
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)

8
CS 473Lecture 148 Depth-First Search: Example

9
CS 473Lecture 149 Depth-First Search: Example

10
CS 473Lecture 1410 Depth-First Search: Example

11
CS 473Lecture 1411 Depth-First Search: Example

12
CS 473Lecture 1412 Depth-First Search: Example

13
CS 473Lecture 1413 Depth-First Search: Example

14
CS 473Lecture 1414 Depth-First Search: Example

15
CS 473Lecture 1415 Depth-First Search: Example

16
CS 473Lecture 1416 Depth-First Search: Example

17
CS 473Lecture 1417 Depth-First Search: Example

18
CS 473Lecture 1418 Depth-First Search: Example

19
CS 473Lecture 1419 Depth-First Search: Example

20
CS 473Lecture 1420 Depth-First Search: Example

21
CS 473Lecture 1421 Depth-First Search: Example

22
CS 473Lecture 1422 Depth-First Search: Example

23
CS 473Lecture 1423 Depth-First Search: Example

24
CS 473Lecture 1424 Depth-First Search: Example

25
CS 473Lecture 1425 Depth-First Search: Example

26
CS 473Lecture 1426 Depth-First Search: Example

27
CS 473Lecture 1427 Depth-First Search: Example

28
CS 473Lecture 1428 Depth-First Search: Example

29
CS 473Lecture 1429 Depth-First Search: Example

30
CS 473Lecture 1430 Depth-First Search: Example

31
CS 473Lecture 1431 Depth-First Search: Example

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

Similar presentations

OK

Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.

Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google