0% found this document useful (0 votes)
5 views

BFS and DFS

Uploaded by

Siam Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

BFS and DFS

Uploaded by

Siam Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

BFS AND DFS:

ALGORITHMS

Graph Algorithms
Review: Graphs
• A graph G = (V, E)
• V = set of vertices, E = set of edges
• Dense graph: |E| ≈ |V|2; Sparse graph: |E| ≈ |V|
• Undirected graph:
• Edge (u,v) = edge (v,u)
• No self-loops
• Directed graph:
• Edge (u,v) goes from vertex u to vertex v, notated u→v
• A weighted graph associates weights with either the edges or the
vertices
Review: Representing Graphs
• Assume V = {1, 2, …, n}
• An adjacency matrix represents the graph as a n x n
matrix A:
• A[i, j] = 1 if edge (i, j) ∈ E (or weight of edge)
= 0 if edge (i, j) ∉ E
• Storage requirements: O(V2)
• A dense representation
• But, can be very efficient for small graphs
• Especially if store just one bit/edge
• Undirected graph: only need one diagonal of
matrix
Review: Graph Searching
• Given: a graph G = (V, E), directed or undirected
• Goal: methodically explore every vertex and every edge
• Ultimately: build a tree on the graph
• Pick a vertex as the root
• Choose certain edges to produce a tree
• Note: might also build a forest if graph is not connected
Review: Breadth-First Search
• “Explore” a graph, turning it into a tree
• One vertex at a time
• Expand frontier of explored vertices across the breadth of the
frontier
• Builds a tree over the graph
• Pick a source vertex to be the root
• Find (“discover”) its children, then their children, etc.
Review: Breadth-First Search
• Again will associate vertex “colors” to guide the
algorithm
• White vertices have not been discovered
• All vertices start out white
• Grey vertices are discovered but not fully explored
• They may be adjacent to white vertices
• Black vertices are discovered and fully explored
• They are adjacent only to black and gray vertices

• Explore vertices by scanning adjacency list of


grey vertices
Review: Breadth-First Search
BFS(G, s) {
initialize vertices;
Q = {s}; // Q is a queue (duh); initialize to s
while (Q not empty) {
u = RemoveTop(Q);
for each v ∈ u->adj {
if (v->color == WHITE)
v->color = GREY;
v->d = u->d + 1;
What does v->d represent?
v->p = u;
Enqueue(Q, v); What does v->p represent?
}
u->color = BLACK;
}
}
Breadth-First Search: Example
r s t u

∞ ∞ ∞ ∞

∞ ∞ ∞ ∞
v w x y
Breadth-First Search: Example
r s t u

∞ 0 ∞ ∞

∞ ∞ ∞ ∞
v w x y

Q: s
Breadth-First Search: Example
r s t u

1 0 ∞ ∞

∞ 1 ∞ ∞
v w x y

Q: w r
Breadth-First Search: Example
r s t u

1 0 2 ∞

∞ 1 2 ∞
v w x y

Q: r t x
Breadth-First Search: Example
r s t u

1 0 2 ∞

2 1 2 ∞
v w x y

Q: t x v
Breadth-First Search: Example
r s t u

1 0 2 3

2 1 2 ∞
v w x y

Q: x v u
Breadth-First Search: Example
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: v u y
Breadth-First Search: Example
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: u y
Breadth-First Search: Example
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: y
Breadth-First Search: Example
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: Ø
BFS: The Code Again
BFS(G, s) {
initialize vertices; Touch every vertex: O(V)
Q = {s};
while (Q not empty) {
u = RemoveTop(Q);
u = every vertex, but only once
for each v ∈ u->adj {
if (v->color == WHITE)
(Why?)
v->color = GREY;
So v = every vertex
that appears in v->d = u->d + 1;
v->p = u;
some other vert’s Enqueue(Q, v);
adjacency l}ist
u->color = BLACK;
} What will be the running time?
} Total running time: O(V+E)
BFS: The Code Again
BFS(G, s) {
initialize vertices;
Q = {s};
while (Q not empty) {
u = RemoveTop(Q);
for each v ∈ u->adj {
if (v->color == WHITE)
v->color = GREY;
v->d = u->d + 1;
v->p = u;
Enqueue(Q, v);
}
What will be the storage cost
u->color = BLACK;
}
in addition to storing the graph?
} Total space used:
O(max(degree(v))) = O(E)
Breadth-First Search: Properties
• BFS calculates the shortest-path distance to the source
node
• Shortest-path distance δ(s,v) = minimum number of edges from s
to v, or ∞ if v not reachable from s
• Proof given in the book (p. 472-5)

• BFS builds breadth-first tree, in which paths to root


represent shortest paths in G
• Thus can use BFS to calculate shortest path from one vertex to
another in O(V+E) time
Depth-First Search
• Depth-first search is another strategy for exploring a
graph
• Explore “deeper” in the graph whenever possible
• Edges are explored out of the most recently discovered vertex v
that still has unexplored edges
• When all of v’s edges have been explored, backtrack to the vertex
from which v was discovered
Depth-First Search
• Vertices initially colored white
• Then colored gray when discovered
• Then black when finished
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u ∈ G->V
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V DFS_Visit(v);
{
}
if (u->color == WHITE)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u ∈ G->V
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V DFS_Visit(v);
{
}
if (u->color == WHITE)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
What does u->d
represent?
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u ∈ G->V
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V DFS_Visit(v);
{
}
if (u->color == WHITE)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
What does u->f
represent?
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u ∈ G->V
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V DFS_Visit(v);
{
}
if (u->color == WHITE)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
Will all vertices eventually be colored black?
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u ∈ G->V
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V DFS_Visit(v);
{
}
if (u->color == WHITE)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
What will be the running time?
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u ∈ G->V
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V DFS_Visit(v);
{
}
if (u->color == WHITE)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
Running time: O(n2) because call DFS_Visit on each vertex,
and the loop over Adj[] can run as many as |V| times
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u ∈ G->V
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V DFS_Visit(v);
{
}
if (u->color == WHITE)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
BUT, there is actually a tighter bound.
How many times will DFS_Visit() actually be called?
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{ {
u->color = GREY;
for each vertex u ∈ G->V
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V DFS_Visit(v);
{
}
if (u->color == WHITE)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}

So, running time of DFS = O(V+E)


Depth-First Sort Analysis
• This running time argument is an informal example of
amortized analysis
• “Charge” the exploration of edge to the edge:
• Each loop in DFS_Visit can be attributed to an edge in the graph
• Runs once/edge if directed graph, twice if undirected
• Thus loop will run in O(E) time, algorithm O(V+E)
• Considered linear for graph, b/c adj list requires O(V+E) storage
• Important to be comfortable with this kind of reasoning and analysis
DFS Example
source
vertex
DFS Example
source
vertex
d f
1 | | |

| |

| | |
DFS Example
source
vertex
d f
1 | | |

2 | |

| | |
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | | |
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 | |
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | |
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 9 |

3 | 4 5 | 6 |

What is the structure of the grey vertices?


What do they represent?
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|15
DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15
DFS: Kinds of edges
• DFS introduces an important distinction among edges in
the original graph:
• Tree edge: encounter new (white) vertex
• The tree edges form a spanning forest
• Can tree edges form cycles?Why or why not?
DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15

Tree edges
DFS: Kinds of edges
• DFS introduces an important distinction among edges in
the original graph:
• Tree edge: encounter new (white) vertex
• Back edge: from descendent to ancestor
• Encounter a grey vertex (grey to grey)
DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15

Tree edges Back edges


DFS: Kinds of edges
• DFS introduces an important distinction among edges in
the original graph:
• Tree edge: encounter new (white) vertex
• Back edge: from descendent to ancestor
• Forward edge: from ancestor to descendent
• Not a tree edge, though
• From grey node to black node
DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15

Tree edges Back edges Forward edges


DFS: Kinds of edges
• DFS introduces an important distinction among edges in
the original graph:
• Tree edge: encounter new (white) vertex
• Back edge: from descendent to ancestor
• Forward edge: from ancestor to descendent
• Cross edge: between a tree or subtrees
• From a grey node to a black node
DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15

Tree edges Back edges Forward edges Cross edges


DFS: Kinds of edges
• DFS introduces an important distinction among edges in
the original graph:
• Tree edge: encounter new (white) vertex
• Back edge: from descendent to ancestor
• Forward edge: from ancestor to descendent
• Cross edge: between a tree or subtrees

• Note: tree & back edges are important; most algorithms


don’t distinguish forward & cross
DFS: Kinds Of Edges
• Thm 23.9: If G is undirected, a DFS produces only tree
and back edges
• Proof by contradiction:
• Assume there’s a forward edge source
• But F? edge must actually be F?
a back edge (why?)
DFS: Kinds Of Edges
• Thm 23.9: If G is undirected, a DFS produces only tree
and back edges
• Proof by contradiction:
• Assume there’s a cross edge source
• But C? edge cannot be cross:
• must be explored from one of the
vertices it connects, becoming a tree
vertex, before other vertex is
explored
• So in fact the picture is
wrong…both lower tree edges
cannot in fact be tree edges

C?
DFS And Graph
Cycles
• Thm: An undirected graph is acyclic iff a DFS yields no
back edges
• If acyclic, no back edges (because a back edge implies a cycle
• If no back edges, acyclic
• No back edges implies only tree edges (Why?)
• Only tree edges implies we have a tree or a forest
• Which by definition is acyclic

• Thus, can run DFS to find whether a graph has a cycle


DFS And
Cycles
• How would you modify the code to detect cycles?
DFS(G) DFS_Visit(u)
{ {
for each vertex u ∈ G->V u->color = GREY;
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V
DFS_Visit(v);
{
if (u->color == WHITE) }
DFS_Visit(u); u->color = BLACK;
} time = time+1;
} u->f = time;
}
DFS And
Cycles
• What will be the running time?
DFS(G) DFS_Visit(u)
{ {
for each vertex u ∈ G->V u->color = GREY;
time = time+1;
{ u->d = time;
u->color = WHITE;
for each v ∈ u->Adj[]
}
{
time = 0; if (v->color == WHITE)
for each vertex u ∈ G->V
DFS_Visit(v);
{
if (u->color == WHITE) }
DFS_Visit(u); u->color = BLACK;
} time = time+1;
} u->f = time;
}
DFS And
Cycles
• What will be the running time?
• A: O(V+E)
• We can actually determine if cycles exist in O(V) time:
• In an undirected acyclic forest, |E| ≤ |V| - 1
• So count the edges: if ever see |V| distinct edges, must have seen
a back edge along the way

You might also like