0% found this document useful (0 votes)
73 views66 pages

BFSand DFS

The document discusses two graph search algorithms: Breadth First Search (BFS) and Depth First Search (DFS). BFS explores the neighbors of a starting node first before moving to neighbors farther away, building a breadth-first tree. DFS explores nodes as far as possible along each branch before backtracking, marking nodes white, gray, and black as it discovers them. Both algorithms run in O(V+E) time on a graph with V vertices and E edges.

Uploaded by

poja_sinha
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)
73 views66 pages

BFSand DFS

The document discusses two graph search algorithms: Breadth First Search (BFS) and Depth First Search (DFS). BFS explores the neighbors of a starting node first before moving to neighbors farther away, building a breadth-first tree. DFS explores nodes as far as possible along each branch before backtracking, marking nodes white, gray, and black as it discovers them. Both algorithms run in O(V+E) time on a graph with V vertices and E edges.

Uploaded by

poja_sinha
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/ 66

Elementary Graph Algorithm

Breadth First Search(BFS)


AND
Depth First Search(DFS)

By :-
Prithviraj Mohanty
Lect.EAST,Bhubaneswar

[email protected] 10/11/2009
Review: Graph searching

•Simplest searching algorithm.

•Application:-Prim`s Algorithm: for finding


minimum spanning tree and Dijkstra`s single
source shortest path algorithm.
•It finds the source vertex s first and discovers
all vertex rechable from s.

•It discovers all vertices at distance k from s


before discovering vertices at distance k+1

[email protected] 10/11/2009
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

[email protected] 10/11/2009
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.

[email protected] 10/11/2009
Review: Breadth-First Search
● Again will associate vertex “colors” to guide the
algorithm
■ White vertices have not been discovered
○ All vertices start out white
■ Gray 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 gray


vertices

[email protected] 10/11/2009
BFS - algorithm
BFS(G, s) // G is the graph and s is the starting node
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE // color of vertex u
3 d[u] ← ∞ // distance from source s to vertex u
4 π[u] ← NIL // predecessor of u
5 color[s] ← GRAY
6 d[s] ← 0
7 π[s] ← NIL
8 Q←Ø // Q is a FIFO - queue

[email protected] 10/11/2009
BFS – algorithm contd…..
9 ENQUEUE(Q, s)
10while Q ≠ Ø // iterates as long as there are gray vertices. Lines 10-18
11do u ← DEQUEUE(Q)
12for each v ∈ Adj[u]
13 do if color[v] = WHITE // discover the undiscovered adjacent
vertices
14 then color[v] ← GRAY // enqueued whenever painted
gray
15 d[v] ← d[u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK // painted black whenever dequeued
[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

   

   
v w x y

[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

 0  

   
v w x y

Q: s
[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

1 0  

 1  
v w x y

Q: w r

[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

1 0 2 

 1 2 
v w x y

Q: r t x

[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

1 0 2 

2 1 2 
v w x y

Q: t x v

[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 
v w x y

Q: x v u

[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 3
v w x y

Q: v u y

[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 3
v w x y

Q: u y

[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 3
v w x y

Q: y
[email protected] 10/11/2009
Breadth-First Search: Example

r s t u

1 0 2 3

2 1 2 3
v w x y

Q: Ø
[email protected] 10/11/2009
Breadth first search - analysis

•Enqueue and Dequeue happen only once


for each node. - O(V).

•Sum of the lengths of adjacency lists – θ(E) (for a


directed graph) and Ө(2E) (for an undirected graph)

•Initialization overhead - O(V)

•Total runtime -O(V+E)

[email protected] 10/11/2009
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
● 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

[email protected] 10/11/2009
Breadth-First Search: Properties
contd……
● Lemma:1.1-Let G=(V,E) be a directed or undirected
graph and let s εV be an arbitary vertex.Then for any
edge (u,v) ε E, (s,v)≤ (s,u)+1
● Lemma:1.2-Let G=(V,E) be a directed or undirected
and suppose that BFS is run on G from a given source
vertex s εV.Then upon termination ,for each vertex
vεV,then value d[v] computed by BFS satisfies
d[v]≥ (s,v)

[email protected] 10/11/2009
Depth-First Search
● It searches ‘deeper’ the graph when possible.
● Starts at the selected node and explores as far as
possible along each branch before backtracking.
● Vertices go through white, gray and black stages of
color.
■ White – initially
■ Gray – when discovered first
■ Black – when finished i.e. the adjacency list of the
vertex is completely examined.

[email protected] 10/11/2009
Depth-First Search contd…….
● Also records timestamps for each vertex
■ d[v] when the vertex is first discovered
■ f[v] when the vertex is finished
T hese timestamps are integers between 1 and
2│V│,since there is one discovery event and one
finishing event for each vertex in │V│.So for
every vertx v ,d[v]<f[v]

[email protected] 10/11/2009
Depth-First Search: algorithm
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;
} }

[email protected] 10/11/2009
Depth-First Search: algorithm
DFS(G)
1 for each vertex u ∈ V [G]
2 do color[u] ← WHITE // color all
vertices white, set their parents NIL
3 π[u] ← NIL
4 time ← 0 // zero out time
5 for each vertex u ∈ V [G] // call only for
unexplored vertices
6 do if color[u] = WHITE // this may result in
multiple sources
7 then DFS-VISIT(u)

[email protected] 10/11/2009
Depth-First Search: algorithm
contd…..
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been
discovered.
2 time ← time +1
3 d[u] ← time // record the discovery time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u // set the parent value
7 DFS-VISIT(v) // recursive call
8 color[u] BLACK ▹ Blacken u; it is finished.
9 f [u] <- time ← time +1
[email protected] 10/11/2009
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?


[email protected] 10/11/2009
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?


[email protected] 10/11/2009
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?


[email protected] 10/11/2009
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
[email protected] 10/11/2009
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?
[email protected] 10/11/2009
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)


[email protected] 10/11/2009
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

[email protected] 10/11/2009
DFS Example
source
vertex

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | | |

| |

| | |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | | |

2 | |

| | |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | | |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 | |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | 6 |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |

[email protected] 10/11/2009
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?
[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | 8 | |

2 | 7 9 |10

3 | 4 5 | 6 |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 | 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 |12 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 |

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|15

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15

[email protected] 10/11/2009
Properties of DFS
● DFS produces a forest.
● Important property of DFS is that discovery and finishing
times have parenthesis structure.
● Parenthesis theorem:-
In any DFS of a(directed or undirected)graph G=(V,E),for
any two vertices u and v extactly one of the following
three conditions holds:
Case-1:The intervals [d[u],f[u]] and [d[v],f[v]] entirely
disjoint and neither u nor v is a descendentof the other in
DFS forest.

[email protected] y 10/11/2009
Properties of DFS contd...
● Case 2:d[u]<d[v]<f[v]<f[u] and v is descendent
of u.
● Case 3:d[v]<d[u]<f[u]<f[v] and u is descendent
of v.
● Theorem(White-path theorem)
v is a descendent of u iff at time d[u],there is a
path from u to v consisting of of only white
vertices.(Except for u,which was just colored
gray)
[email protected] 10/11/2009
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?

[email protected] 10/11/2009
DFS Example
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15

Tree edges
[email protected] 10/11/2009
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)

[email protected] 10/11/2009
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


[email protected] 10/11/2009
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

[email protected] 10/11/2009
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


[email protected] 10/11/2009
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

[email protected] 10/11/2009
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


[email protected] 10/11/2009
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
edges.
[email protected] 10/11/2009
DFS: Kinds Of Edges
● Thm 23.9: If G is undirected, a DFS produces
only tree and back edges
● Proof by contradiction:
source
■ Assume there’s a forward edge F?
○ But F? edge must actually be a
back edge (why?)

[email protected] 10/11/2009
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?

[email protected] 10/11/2009
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

[email protected] 10/11/2009
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)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
[email protected] 10/11/2009
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)
u->color = BLACK;
DFS_Visit(u);
time = time+1;
}
u->f = time;
}
}
[email protected] 10/11/2009
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

[email protected] 10/11/2009

You might also like