You are on page 1of 14

Graph traversal algorithm:

Recapitulation
AM
01-09-2014
Breadth First Search(BFS)
Breadth-first search is one of the simplest algorithms for searching a
graph
Given a graph G =(V,E) and a distinguished source vertex s, breadth-
first search systematically explores the edges of G to discover every
vertex that is reachable from s.
It computes the distance (smallest number of edges) from s to each
reachable vertex.
Intuitively, the basic idea of the breath-first search is this:
send a wave out from sources.
The wave hits all vertices 1 edge froms. From there, the wave hits
all vertices 2 edges froms. Etc.
We use FIFO queue Q to maintain the wavefront: vis in Q if and only
if wave has hit vbut has not come out of vyet.
Strategy of BFS Algorithm
Breadth-first searchstarts at agivenvertex s, whichis at level
0.
In the first stage, we visit all the vertices that are at the
distanceof oneedgeaway, painted as visited areplaced in
level 1.
In the second stage, we visit all the new vertices. Thesenew
vertices, which are adjacent to level 1 vertices and not
previously assigned to alevel, areplaced into level 2, and so
on.
Strategy of BFS Algorithm
To keep track of progress, breadth-first-search colorseach
vertex. Each vertex of the graph is in one of three states:
Undiscovered;
Discovered but not fully explored; and
Fully explored.
The state of a vertex, u, is stored in a color variable as
follows:
color[u] =White - for the "undiscovered" state,
color [u] =Gray - for the "discovered but not fully explored"
state, and
color [u] =Black - for the "fully explored" state.
Algorithm: Breadth-First Search
Traversal
Example
Example: The following figure illustrates the
progress of breadth-first search on the
undirected sample graph.
Analysis
The while-loop in breadth-first search is executed at most |V|
times. The reason is that every vertex enqueuedat most once.
So, we have O(V).
The for-loop inside the while-loop is executed at most |E|
times if G is a directed graph or 2|E| times if G is undirected.
The reason is that every vertex dequeuedat most once and we
examine (u, v) only whenu is dequeued. Therefore, every edge
examined at most once if directed, at most twice if undirected.
So, we have O(E).
Therefore, the total running time for breadth-first search
traversal is O(V +E).

Depth First Search


Overall Strategy of DFS Algorithm
Depth-first search selects a source vertexs in the graph and paint it
as "visited.
Now the vertexs becomes our current vertex. Then, we traverse the
graph by considering an arbitrary edge (u, v) from the current
vertexu.
If the edge (u, v) takes us to a painted vertexv, then we back
down to the vertexu.
On the other hand, if edge (u, v) takes us to an unpainted vertex,
then we paint the vertexv and make it our current vertex, and
repeat the above computation.
Sooner or later, we will get to a dead end, meaning all the
edges from our current vertexu takes us to painted vertices. This
is a deadlock.
To get out of this, we back down along the edge that brought us
here to vertexu and go back to a previously painted vertexv.
Overall Strategy of DFS Algorithm
Like BFS, depth-first search uses [v] to record the parent of
vertexv. We have [v] =NIL if and only if vertexv is the root of a
depth-first tree.
DFS time-stamps each vertex when its color is changed.
When vertex v is changed from white to gray the time is
recorded in d[v].
When vertex v is changed from gray to black the time is
recorded in f[v].
The discovery and the finish times are unique integers, where for
each vertex the finish time is always after the discovery time. That
is, each time-stamp is an unique integer in the range of 1 to 2|V| and
for each vertexv, d[v] <f[v]. In other words, the following
inequalities hold:
1 d[v] <f[v] 2|V|
Algorithm Depth-First Search
The DFS forms a depth-first forest comprised of more than one depth-first trees. Each tree is
made of edges (u, v) such that u is gray andv is white when edge (u, v) is explored. The
following pseudocodefor DFS uses a global timestamp time.
DFS (V, E)
for each vertex u in V[G]
do color[u] WHITE
[u] NIL
time 0
for each vertex u in V[G]
do if color[u] WHITE
then DFS-Visit(u) build a new DFS-tree fromu
DFS-Visit(u)
color[u] GRAY discover u
time time + 1
d[u] time
for each vertexv adjacent tou explore (u, v)
do if color[v] WHITE
then [v] u
DFS-Visit(v)
color[u] BLACK
time time + 1
f[u] time we are done withu
Example
Analysis
The DFS-Visit is called (from DFS or from itself)
once for each vertex in V[G] since each vertex
is changed from white to gray once.
The for-loop in DFS-Visit is executed a total of | E|
times for a directed graph or 2| E| times for an
undirected graph since each edge is explored
once.
Moreover, initialization takes (|V|) time.
Therefore, the running time of DFS is (V + E).
Thank you

You might also like