0% found this document useful (0 votes)
5 views21 pages

DAA - Handout 14 Depth First Search

The document provides an overview of the Depth First Search (DFS) algorithm, detailing the color-coding system for vertices and the process of discovery and finishing times. It explains how each vertex is marked as white, gray, or black during the search, and includes pseudocode for the DFS algorithm. Additionally, it describes the classification of edges encountered during the search, such as back edges, forward edges, and cross edges.

Uploaded by

m.umer.shahid.1
Copyright
© © All Rights Reserved
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)
5 views21 pages

DAA - Handout 14 Depth First Search

The document provides an overview of the Depth First Search (DFS) algorithm, detailing the color-coding system for vertices and the process of discovery and finishing times. It explains how each vertex is marked as white, gray, or black during the search, and includes pseudocode for the DFS algorithm. Additionally, it describes the classification of edges encountered during the search, such as back edges, forward edges, and cross edges.

Uploaded by

m.umer.shahid.1
Copyright
© © All Rights Reserved
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/ 21

Design & Analysis of

Algorithms

Depth First Search


Handout-14
Depth First Search
In DFS
• Each vertex is initially white
– It is grayed when it is discovered in the search, and
– It is blackened when it is finished, that is, when its
adjacency list has been examined completely.
Discovery and Finish Times
• It guarantees that each vertex ends up in exactly one
depth-first tree, so that these trees are disjoint.

• It timestamps each vertex


– the first timestamp d[v] records when v is first
discovered (and grayed), and
– the second timestamp f [v] records when the search
finishes examining v's adjacency list (and blackens v).

• For every vertex u


d[u] < f[u]
Algorithm: Depth First Search

DFS-Visit(u)
DFS(G)
1 color [u] ← GRAY
1 for each vertex u ∈ V [G]
2 time ← time + 1
2 do color [u] ← WHITE
3 d [u] ← time
3 π[u] ← NIL
4 for each v ∈ Adj [u]
4 time ← 0
5 do if color [v] = WHITE
5 for each vertex u ∈ V [G]
6 then π[v] ← u
6 do if color [u] = WHITE
7 DFS-Visit (v)
7 then DFS-Visit (u)
8 color [u] ← BLACK
9 f[u] ← time ← time + 1
Depth First Search
For each vertex u ∈ V(G)
color [u] ← WHITE
π [u] ← NIL
u v w
time ← 0

x y z
Depth First Search
Considering white vertex u
color [u] ← GRAY
d[u] ← time + 1 = 0 + 1 = 1
Adj[u] = v, x
u v w
1/ color [v] = WHITE
π[v] ← u
DFS-VISIT (v)

x y z
Depth First Search

color [v] ← GRAY


d[v] ← time + 1 = 1 + 1 = 2
Adj[v] = y
u v w
1/ 2/ color [y] = WHITE
π[y] ← v
DFS-VISIT (y)

x y z
Depth First Search

color [y] ← GRAY


d[y] ← time + 1 = 2 + 1 = 3
Adj[y] = x
u v w
1/ 2/ color [x] = WHITE
π[x] ← y
DFS-VISIT (x)

3/
x y z
Depth First Search

color [x] ← GRAY


d[x] ← time + 1 = 3 + 1 = 4
u v w
Adj[x] = v
1/ 2/ color [v] ≠ WHITE

4/ 3/
x y z
Depth First Search

The edge (x, v) is a back


u v w edge that is a non tree edge
1/ 2/ and is labeled as B

4/ 3/
x y z
Depth First Search
The vertex x is finished.

color [x] ← BLACK


u v w
f[x] ← time + 1 = 4 + 1 = 5
1/ 2/
B

4/5 3/
x y z
Depth First Search

The vertex y is finished.

color [y] ← BLACK


u v w f[y] ← time + 1 = 5 + 1 = 6
1/ 2/
B

4/5 3/6
x y z
Depth First Search

The vertex v is finished.


u v w
1/ 2/7 color [v] ← BLACK
f[v] ← time + 1 = 6 + 1 = 7
B

4/5 3/6
x y z
Depth First Search
The edge (u, x) is a forward
edge that is a non tree edge
and is labeled as F
u v w
1/ 2/7
F B

4/5 3/6
x y z
Depth First Search
The vertex u is finished.

color [u]  BLACK


u v w
f[u]  time + 1 = 7 + 1 = 8
1/8 2/7
F B

4/5 3/6
x y z
Depth First Search
Considering white vertex w
color [w] ← GRAY
d[w] ← time + 1 = 8 + 1 = 9
u v w
Adj[w] = y, z
1/8 2/7 9/
F B color [y] ≠ WHITE

4/5 3/6 color [z] = WHITE


x y z π[z] ← w
DFS-VISIT (z)
Depth First Search

The edge (w, y) is a cross


edge that is a non tree edge
u v w and is labeled as C
1/8 2/7 9/
F B C

4/5 3/6
x y z
Depth First Search

color [z] ← GRAY


d[z] ← time + 1 = 9 + 1 = 10
u v w Adj[z] = z
1/8 2/7 9/
B C color [z] ≠ WHITE
F

4/5 3/6 10/


x y z
Depth First Search
The edge (z, z) is a back
edge that is a non tree edge
and is labeled as B
u v w
1/8 2/7 9/
F B C B

4/5 3/6 10/


x y z
Depth First Search
The vertex z is finished.

color [z] ← BLACK


u v w f[z] ← time + 1 = 10 + 1 = 11
1/8 2/7 9/
F B C B

4/5 3/6 10/11


x y z
Depth First Search
The vertex w is finished.

color [w] ← BLACK


u v w
f[w] ← time + 1 = 11 + 1 = 12
1/8 2/7 9/12
F B C B

4/5 3/6 10/11


x y z

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy