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

On Depth First Search

The document provides an overview of the Depth First Search (DFS) algorithm, describing it as an aggressive exploration strategy that delves deeply into a graph before backtracking. It explains the implementation of DFS using a stack data structure and contrasts it with Breadth First Search (BFS) in terms of exploration order. The document includes examples and details on both recursive and iterative implementations of DFS.

Uploaded by

rezafahim81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

On Depth First Search

The document provides an overview of the Depth First Search (DFS) algorithm, describing it as an aggressive exploration strategy that delves deeply into a graph before backtracking. It explains the implementation of DFS using a stack data structure and contrasts it with Breadth First Search (BFS) in terms of exploration order. The document includes examples and details on both recursive and iterative implementations of DFS.

Uploaded by

rezafahim81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

If breadth-first search is the cautious and tentative exploration strategy, depth-first search is its more aggressive cousin,

always exploring from the most recently discovered vertex and backtracking only when necessary (like exploring a maze).

Depth First Search

Prepared by : Debajyoti Sengupta (IEM Faculty)


For 2nd year students of ECE Department
The Depth First Search Algorithm
Another natural method to find the nodes reachable from s is the approach you
might take if the graph G were truly a maze of interconnected rooms and you
were walking around in it. You’d start from s and try the first edge leading out of
it, to a node v. You’d then follow the first edge leading out of v, and continue in
this way until you reached a “dead end”—a node for which you had already
explored all its neighbors. You’d then backtrack until you got to a node with an
unexplored neighbor, and resume from there. We call this algorithm Depth First
Search (DFS), since it explores G by going as deeply as possible and only
retreating when necessary.

While DFS ultimately visits exactly the same set of nodes as BFS, it typically does so in a
very different order; it probes its way down long paths, potentially getting very far from s,
before backing up to try nearer unexplored nodes.
The Depth First Search Algorithm

Watchout the Simulation


An Example

Graph to be explored using DFS


An Example

Like BFS, DFS marks a vertex as explored the first time


it discovers it. Because it begins its exploration at the
starting vertex s, for the graph in the adjoining Figure,
the first iteration of DFS examines the edges (s, a) and
(s, b), in whatever order these edges appear in s’s
adjacency list. Let’s say (s, a) comes first, leading DFS
to discover the vertex a and mark it as explored. The
second iteration of DFS is where it diverges from BFS—
rather than considering next s’s other layer-1 neighbor
b, DFS immediately proceeds to exploring the
neighbors of a. (It will eventually get back to exploring
(s, b).) Perhaps from a it checks s first (which is already
marked as explored) and then discovers the vertex c,
which is where it travels next
An Example
Then DFS examines in some order the
neighbors of c, the most recently
discovered vertex. To keep things
interesting, let’s say that DFS discovers d
next, followed by e.

From e, DFS has nowhere to go—both of e’s


neighbors are already marked as explored.
DFS is forced to retreat to the previous
vertex, namely d, and resume exploring the
rest of its neighbors. From d, DFS will
discover the final vertex b (perhaps after
checking c and finding it marked as
explored).
An Example

Once at b, the dominoes fall quickly. DFS discovers that all of


b’s neighbors have already been explored, and must backtrack
to the previously visited vertex, which is d. Similarly, because
all of d’s remaining neighbors are already marked as explored,
DFS must rewind further, to c. DFS then retreats further to a
(after checking that all of c’s remaining neighbors are marked
as explored), then to s. It finally stops once it checks s’s
remaining neighbor (which is b) is marked as explored.
The Depth First Search Algorithm
But how to implement Depth First Search?
But how to implement Depth First Search?

DFS is depth first search, so you have to traverse a whole branch of tree then
you can traverse the adjacent nodes. So for keep tracking on the current node
it requires last in first out approach which can be implemented by stack, after it
reaches the depth of a node then all the nodes will be popped out of stack.
Next it searches for adjacent nodes which are not visited yet. If it was
implemented with queue i.e. first in first out approach we could not reach to
the depth, before that it would dequeue the current node.

A stack maintains a list of objects, and you can add an object to the beginning of the list
(a “push”) or remove one from the beginning of the list (a “pop”) in constant time.
Recursive and Iterative
Implementation
Depth First Search
The Depth First Search Algorithm
DFS is also a particular implementation of the generic component-growing
algorithm. It is most easily described in recursive form: we can invoke DFS
from any starting point but maintain global knowledge of which nodes
have already been explored.
The Depth First Search Algorithm
Iterative Implementation

One way to think about and implement


DFS is to start from the code for BFS and
make two changes: (i) swap in a stack
data structure (which is last-in first-out)
for the queue (which is first-in first-out);
and (ii) postpone checking whether a
vertex has already been explored until
after removing it from the data
structure.
The Depth First Search Algorithm
Iterative Implementation : An Example In the first iteration of DFS’s while loop pops the vertex s
and pushes its two neighbors onto the stack in some order,
say, with b first and a second. Because a was the last to be
pushed, it is the first to be popped, in the second iteration
of the while loop. This causes s and c to be pushed onto
the stack, let’s say with c first. The vertex s is popped in the
next iteration; since it has already been marked as
explored, the algorithm skips it. Then c is popped, and all
of its neighbors (a, b, d, and e) are pushed onto the stack,
joining the first occurrence of b. If d is pushed last, and also
b is pushed before e when d is popped in the next
iteration, then we recover the order of exploration as was
shown before.
The Depth First Search Algorithm
The Depth First Search Algorithm
Consider the graph G given in the below figure. The adjacency list of G is also given.
Suppose we want to print all the nodes that can be reached from the node H (including
H itself). One way is to use a depth-first search of G starting at node H.
The Depth First Search Algorithm
The Depth First Search Algorithm
The Depth First Search Algorithm

Since the STACK is now empty, the depth-first search of G starting at node
H is complete and the nodes which were printed are:

H, I, F, C, G, B, E

These are the nodes which are reachable from the node H.

You might also like