Depth-First Search - PowerPoint PPT Presentation

About This Presentation
Title:

Depth-First Search

Description:

Title: binary search tree Author: taicl Last modified by: UTNguyen Created Date: 9/13/2005 2:58:53 PM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 30
Provided by: tai649
Category:

less

Transcript and Presenter's Notes

Title: Depth-First Search


1
Depth-First Search
  • CSE 2011
  • Winter 2011

2
Depth-First Search (DFS)
  • DFS is another popular graph search strategy
  • Idea is similar to pre-order traversal (visit
    node, then visit children recursively)
  • DFS will continue to visit neighbors in a
    recursive pattern
  • Whenever we visit v from u, we recursively visit
    all unvisited neighbors of v. Then we backtrack
    (return) to u.

3
DFS Traversal Example
4
DFS Algorithm
Flag all vertices as notvisited
Flag v as visited print v
print v
For unvisited neighbors,call RDFS(w) recursively
We can also record the paths using prev . Where
do we insert the code for prev ?
5
Example
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
F
F
F
F
F
F
F
F
F
F
-
-
-
-
-
-
-
-
-
-
source
Pred
Initialize visited table (all False) Initialize
Pred to -1
6
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
F
F
T
F
F
F
F
F
F
F
-
-
-
-
-
-
-
-
-
-
source
Pred
Mark 2 as visited
RDFS( 2 ) Now visit RDFS(8)
7
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
F
F
T
F
F
F
F
F
T
F
-
-
-
-
-
-
-
-
2
-
source
Pred
Mark 8 as visited mark Pred8
Recursivecalls
RDFS( 2 ) RDFS(8) 2 is already visited,
so visit RDFS(0)
8
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
F
T
F
F
F
F
F
T
F
8
-
-
-
-
-
-
-
2
-
source
Pred
Mark 0 as visited Mark Pred0
Recursivecalls
RDFS( 2 ) RDFS(8) RDFS(0) -gt no
unvisited neighbors, return
to call RDFS(8)
9
Back to 8
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
F
T
F
F
F
F
F
T
F
8
-
-
-
-
-
-
-
2
-
source
Pred

Recursivecalls
RDFS( 2 ) RDFS(8) Now visit 9 -gt
RDFS(9)
10
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
F
T
F
F
F
F
F
T
T
8
-
-
-
-
-
-
-
2
8
source
Pred
Mark 9 as visited Mark Pred9
Recursivecalls
RDFS( 2 ) RDFS(8) RDFS(9) -gt visit 1,
RDFS(1)
11
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
F
F
F
F
F
T
T
8
9
-
-
-
-
-
-
2
8
source
Pred
Mark 1 as visited Mark Pred1
Recursivecalls
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) visit RDFS(3)
12
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
F
F
F
F
T
T
8
9
-
1
-
-
-
-
2
8
source
Pred
Mark 3 as visited Mark Pred3
Recursivecalls
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3)
visit RDFS(4)
13
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
F
F
F
T
T
8
9
-
1
3
-
-
-
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3)
RDFS(4) ? STOP all of 4s neighbors have been
visited
return back to call RDFS(3)

Mark 4 as visited Mark Pred4
Recursivecalls
14
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
F
F
F
T
T
8
9
-
1
3
-
-
-
2
8
source
Back to 3
Pred
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3)
visit 5 -gt RDFS(5)

Recursivecalls
15
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
F
F
T
T
8
9
-
1
3
3
-
-
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3)
RDFS(5)
3 is already visited, so visit 6 -gt RDFS(6)


Mark 5 as visited Mark Pred5
Recursivecalls
16
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
F
T
T
8
9
-
1
3
3
5
-
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3)
RDFS(5)
RDFS(6)
visit 7 -gt RDFS(7)


Mark 6 as visited Mark Pred6
Recursivecalls
17
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3)
RDFS(5)
RDFS(6)
RDFS(7) -gt Stop no more unvisited neighbors



Mark 7 as visited Mark Pred7
Recursivecalls
18
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3)
RDFS(5)
RDFS(6) -gt Stop



Recursivecalls
19
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3)
RDFS(5) -gt Stop




Recursivecalls
20
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9)
RDFS(1) RDFS(3) -gt Stop





Recursivecalls
21
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1)
-gt Stop




Recursivecalls
22
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
RDFS( 2 ) RDFS(8) RDFS(9) -gt Stop





Recursivecalls
23
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
RDFS( 2 ) RDFS(8) -gt Stop





Recursivecalls
24
Example Finished
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
RDFS( 2 ) -gt Stop





Recursive calls finished
25
Time Complexity of DFS
  • We never visited a vertex more than once.
  • We had to examine the adjacency lists of all
    vertices.
  • Svertex v degree(v) 2E
  • So, the running time of DFS is proportional to
    the number of edges and number of vertices (same
    as BFS)
  • O(V E)

26
Enhanced DFS Algorithm
  • What if a graph is not connected (strongly
    connected)?
  • Use an enhanced version of DFS, which is similar
    to the enhanced BFS algorithm.
  • DFSearch( G )
  • i 1 // component number
  • for every vertex v
  • flagv false
  • for every vertex v
  • if ( flagv false )
  • print ( Component i )
  • RDFS( v )

27
Applications of DFS
  • Is there a path from source s to a vertex v?
  • Is an undirected graph connected?
  • Is a directed graph strongly connected?
  • To output the contents (e.g., the vertices) of a
    graph
  • To find the connected components of a graph
  • To find out if a graph contains cycles and report
    cycles.
  • To construct a DSF tree/forest from a graph

28
DFS Path Tracking
Adjacency List
Visited Table (T/F)
0
1
2
3
4
5
6
7
8
9
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
source
Pred
DFS find out path too
Try some examples. Path(0) -gt Path(6) -gt Path(7)
-gt
29
Next time
  • Applications of BFS and DFS
  • Review
Write a Comment
User Comments (0)
About PowerShow.com