CS473-Algorithms I - PowerPoint PPT Presentation

About This Presentation
Title:

CS473-Algorithms I

Description:

CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort Depth-First Search Graph G (V,E) directed or undirected Adjacency list ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 33
Provided by: Cevd
Category:

less

Transcript and Presenter's Notes

Title: CS473-Algorithms I


1
CS473-Algorithms I
  • Lecture 15
  • Graph Searching
  • Depth-First Search and Topological Sort

2
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
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 Adju by ?v? u

4
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
Depth-First Search
  • DFS also timestamps each vertex with two
    timestamps
  • dv records when v is first discovered and
    grayed
  • fv records when v is finished and blackened
  • Since there is only one discovery event and
    finishing event for each vertex we have 1? dv ?
    fv? 2V

Time axis for the color of a vertex
dv
fv
1
2
2V
6
Depth-First Search
  • DFS(G)
  • for each u?V do
  • coloru? white
  • ?u ? NIL
  • time ? 0
  • for each u?V do
  • if coloru ? white then
  • DFS-VISIT(G, u)

DFS-VISIT(G, u) coloru? gray du? time ?
time ?1 for each v? Adju do if colorv ?
white then ?v ? u
DFS-VISIT(G, v) coloru? black fu? time ?
time ?1
7
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-VISIT
  • DFS-VISIT is called exactly once for each v?V
    since
  • DFS-VISIT is invoked only on white vertices and
  • DFS-VISIT(G, u) immediately colors u as gray
  • For loop of DFS-VISIT(G, u) is executed Adju
    time
  • Since ? Adju ? E, total cost of executing
    loop of
  • DFS-VISIT is ?(E)

8
Depth-First Search Example
9
Depth-First Search Example
10
Depth-First Search Example
11
Depth-First Search Example
12
Depth-First Search Example
13
Depth-First Search Example
14
Depth-First Search Example
15
Depth-First Search Example
16
Depth-First Search Example
17
Depth-First Search Example
18
Depth-First Search Example
19
Depth-First Search Example
20
Depth-First Search Example
21
Depth-First Search Example
22
Depth-First Search Example
23
Depth-First Search Example
24
Depth-First Search Example
25
Depth-First Search Example
26
Depth-First Search Example
27
Depth-First Search Example
28
Depth-First Search Example
29
Depth-First Search Example
30
Depth-First Search Example
31
Depth-First Search Example
32
Depth-First Search Example
DFS(G) terminated
Depth-first forest (DFF)
Write a Comment
User Comments (0)
About PowerShow.com