0% found this document useful (0 votes)
38 views27 pages

Depth-First Search: COMP171 Fall 2005

This document summarizes the depth-first search (DFS) algorithm for traversing graphs. DFS recursively explores as far as possible along each branch before backtracking. It works by: 1) marking the starting node as visited, 2) visiting unvisited neighbors recursively through function calls, and 3) backtracking when no unvisited neighbors remain. DFS can determine if a graph contains cycles and records the search path through each node's predecessor.

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views27 pages

Depth-First Search: COMP171 Fall 2005

This document summarizes the depth-first search (DFS) algorithm for traversing graphs. DFS recursively explores as far as possible along each branch before backtracking. It works by: 1) marking the starting node as visited, 2) visiting unvisited neighbors recursively through function calls, and 3) backtracking when no unvisited neighbors remain. DFS can determine if a graph contains cycles and records the search path through each node's predecessor.

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

COMP171

Fall 2005

Depth-first search
Graph / Slide 2

Depth-First Search (DFS)


 DFS is another popular graph search strategy
 Idea is similar to pre-order traversal (visit children
first)

 DFS can provide certain information about


the graph that BFS cannot
 It can tell whether we have encountered a cycle or
not
 More in COMP271
Graph / Slide 3

DFS Algorithm
 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.

 Note: it is possible that w2 was unvisited when we


recursively visit w1, but became visited by the time
we return from the recursive call.
u

v w3
w1 w2
Graph / Slide 4

DFS Algorithm

Flag all vertices as not


visited

Flag yourself as visited

For unvisited neighbors,


call RDFS(w) recursively

We can also record the paths using pred[ ].


Graph / Slide 5

Example
Adjacency List Visited Table (T/F)
0 0 F -

8 1 F -

2 F -

source 2 9
3 F -

4 F -
1
5 F -

3 7 6 F -

6 7 F -
4
5 8 F -

9 F -

Pred
Initialize visited
table (all False)

Initialize Pred to -1
Graph / Slide 6

Example
Adjacency List Visited Table (T/F)
0 0 F -

1 F -
8 2 T -

3 F -
source 2 9 4 F -

1 5 F -

6 F -
3 7
7 F -
6
4 8 F -
5 9 F -

Pred
Mark 2 as visited

RDFS( 2 )
Now visit RDFS(8)
Graph / Slide 7

Example
Adjacency List Visited Table (T/F)
0
0 F -
8 1 F -

2 T -
source 2 9 3 F -

1 4 F -

5 F -

3 7 6 F -
6 7 F -
4
5 8 T 2

9 F -

Pred
Mark 8 as visited
Recursive
calls RDFS( 2 )
RDFS(8) mark Pred[8]
2 is already visited, so visit RDFS(0)
Graph / Slide 8

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 F -

2 T -
source 2 9 3 F -

1 4 F -

5 F -

3 7 6 F -
6 7 F -
4
5 8 T 2

9 F -

Pred
Mark 0 as visited
Recursive
calls RDFS( 2 )
RDFS(8) Mark Pred[0]
RDFS(0) -> no unvisited neighbors, return
to call RDFS(8)
Graph / Slide 9

Example Back to 8
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 F -

2 T -
source 2 9 3 F -

1 4 F -

5 F -

3 7 6 F -
6 7 F -
4
5 8 T 2

9 F -

Pred

Recursive
calls RDFS( 2 )
RDFS(8)
Now visit 9 -> RDFS(9)
Graph / Slide 10

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 F -

2 T -
source 2 9 3 F -

1 4 F -

5 F -

3 7 6 F -
6 7 F -
4
5 8 T 2

9 T 8

Pred
Mark 9 as visited
Recursive
calls RDFS( 2 )
RDFS(8) Mark Pred[9]
RDFS(9)
-> visit 1, RDFS(1)
Graph / Slide 11

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9

2 T -
source 2 9 3 F -

1 4 F -

5 F -

3 7 6 F -
6 7 F -
4
5 8 T 2

9 T 8

Pred
Mark 1 as visited
Recursive
calls RDFS( 2 )
RDFS(8) Mark Pred[1]
RDFS(9)
RDFS(1)
visit RDFS(3)
Graph / Slide 12

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9

2 T -
source 2 9 3 T 1

1 4 F -

5 F -

3 7 6 F -
6 7 F -
4
5 8 T 2

9 T 8

Pred
Mark 3 as visited
Recursive
calls RDFS( 2 )
RDFS(8) Mark Pred[3]
RDFS(9)
RDFS(1)
RDFS(3)
visit RDFS(4)
Graph / Slide 13

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9

2 T -
source 2 9 3 T 1

1 4 T 3

5 F -

3 7 6 F -
6 7 F -
4
5 8 T 2

9 T 8

Pred
RDFS( 2 ) Mark 4 as visited
Recursive RDFS(8)
calls RDFS(9) Mark Pred[4]
RDFS(1)
RDFS(3)
RDFS(4)  STOP all of 4’s neighbors have been visited
return back to call RDFS(3)
Graph / Slide 14

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9

2 T -
source 2 9 3 T 1

1 4 T 3

5 F -

3 7 6 F -
6 7 F -
4
5 8 T 2

9 T 8
Back to 3
Pred
RDFS( 2 )
Recursive RDFS(8)
calls RDFS(9)
RDFS(1)
RDFS(3)
visit 5 -> RDFS(5)
Graph / Slide 15

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9

2 T -
source 2 9 3 T 1

1 4 T 3

5 T 3

3 7 6 F -
6 7 F -
4
5 8 T 2

9 T 8

Pred
RDFS( 2 )
Recursive RDFS(8) Mark 5 as visited
calls RDFS(9)
RDFS(1) Mark Pred[5]
RDFS(3)
RDFS(5)
3 is already visited, so visit 6 -> RDFS(6)
Graph / Slide 16

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9

2 T -
source 2 9 3 T 1

1 4 T 3

5 T 3

3 7 6 T 5
6 7 F -
4
5 8 T 2

9 T 8

Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9) Mark 6 as visited
calls
RDFS(1) Mark Pred[6]
RDFS(3)
RDFS(5)
RDFS(6)
visit 7 -> RDFS(7)
Graph / Slide 17

Example
Adjacency List Visited Table (T/F)
0
0 T 8
8 1 T 9

2 T -
source 2 9 3 T 1

1 4 T 3

5 T 3

3 7 6 T 5
6 7 T 6
4
5 8 T 2

9 T 8

Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9) Mark 7 as visited
calls
RDFS(1) Mark Pred[7]
RDFS(3)
RDFS(5)
RDFS(6)
RDFS(7) -> Stop no more unvisited neighbors
Graph / Slide 18

Example
Adjacency List Visited Table (T/F)
0 0 T 8

1 T 9
8
2 T -

3 T 1
source 2 9 4 T 3
1 5 T 3

3 7 6 T 5

6 7 T 6

4 8 T 2
5
9 T 8

Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9)
calls
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6) -> Stop
Graph / Slide 19

Example
Adjacency List Visited Table (T/F)
0 0 T 8

1 T 9
8
2 T -

3 T 1
source 2 9 4 T 3
1 5 T 3

3 7 6 T 5

6 7 T 6

4 8 T 2
5
9 T 8

Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9)
calls
RDFS(1)
RDFS(3)
RDFS(5) -> Stop
Graph / Slide 20

Example
Adjacency List Visited Table (T/F)
0 0 T 8

1 T 9
8
2 T -

3 T 1
source 2 9 4 T 3
1 5 T 3

3 7 6 T 5

6 7 T 6

4 8 T 2
5
9 T 8

Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9)
calls
RDFS(1)
RDFS(3) -> Stop
Graph / Slide 21

Example
Adjacency List Visited Table (T/F)
0 0 T 8

1 T 9
8
2 T -

3 T 1
source 2 9 4 T 3
1 5 T 3

3 7 6 T 5

6 7 T 6

4 8 T 2
5
9 T 8

Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9)
calls
RDFS(1) -> Stop
Graph / Slide 22

Example
Adjacency List Visited Table (T/F)
0 0 T 8

1 T 9
8
2 T -

3 T 1
source 2 9 4 T 3
1 5 T 3

3 7 6 T 5

6 7 T 6

4 8 T 2
5
9 T 8

Pred
RDFS( 2 )
RDFS(8)
Recursive RDFS(9) -> Stop
calls
Graph / Slide 23

Example
Adjacency List Visited Table (T/F)
0 0 T 8

1 T 9
8
2 T -

3 T 1
source 2 9 4 T 3
1 5 T 3

3 7 6 T 5

6 7 T 6

4 8 T 2
5
9 T 8

Pred
RDFS( 2 )
RDFS(8) -> Stop
Recursive
calls
Graph / Slide 24

Example
Adjacency List Visited Table (T/F)
0 0 T 8

1 T 9
8
2 T -

3 T 1
source 2 9 4 T 3
1 5 T 3

3 7 6 T 5

6 7 T 6

4 8 T 2
5
9 T 8

Pred
RDFS( 2 ) -> Stop

Recursive
calls
Graph / Slide 25

Example
0 Adjacency List Visited Table (T/F)
8 0 T 8

1 T 9

source 2 9
2 T -

3 T 1
1
4 T 3

3 7 5 T 3

6 6 T 5
4
5 7 T 6

8 T 2

9 T 8

Pred
Check our paths, does DFS find valid paths? Yes.

Try some examples.


Path(0) ->
Path(6) ->
Path(7) ->
Graph / Slide 26

Time Complexity of DFS


(Using adjacency list)
 We never visited a vertex more than once

 We had to examine all edges of the vertices


 We know Σvertex v degree(v) = 2m where m is the number of edges

 So, the running time of DFS is proportional to the number


of edges and number of vertices (same as BFS)
 O(n + m)

 You will also see this written as:


 O(|v|+|e|) |v| = number of vertices (n) |e| = number of
edges (m)
Graph / Slide 27

DFS Tree
Resulting DFS-tree.
Notice it is much “deeper”
than the BFS tree.

Captures the structure of the recursive calls


- when we visit a neighbor w of v, we add w as
child of v
- whenever DFS returns from a vertex v, we
climb up in the tree from v to its parent

You might also like