Module 2 Lecture 3
Module 2 Lecture 3
01/24/2025 BMEE407L-Module 2 1
Depth First Search (DFS)
• DFS in Data Structure, also known as depth-first traversal,
01/24/2025 BMEE407L-Module 2 2
Basic Steps
Begin from the root or arbitrary node and keep the node marked.
Move to the unmarked adjacent node and continue this loop until there is no
unmarked adjoining node.
Then backtrack and examine the other nodes that are unmarked and traverse
them.
The final step is to print the nodes within the path.
01/24/2025 BMEE407L-Module 2 3
Algorithm
• Each node of a graph in a standard DFS implementation is divided into either of
two categories:
1. Not Visited
2. Visited
• The algorithm is used to pinpoint each node and mark them as visited and, at
the same time, avoid cycles.
01/24/2025 BMEE407L-Module 2 4
Algorithm Steps
Put any particular node of the graph on a stack.
The item on top of the stack should be added to the visited list.
Make a list of adjoining nodes of that node and add those not in the visited list
on the top of the stack.
Keep repeating the previous steps till the stack empties.
01/24/2025 BMEE407L-Module 2 5
Graph with cycles
• The DFS for a graph is almost the same as the DFS for a tree.
• The only difference is that the graphs may have cycles,
unlike trees. A node might be visited multiple times, and to
avoid processing the node, a Boolean visited array is used.
01/24/2025 BMEE407L-Module 2 6
Example
01/24/2025 BMEE407L-Module 2 8
Step 6 - POP the top element from
the stack, i.e., B, and print it. Now,
PUSH all the neighbors of B onto the
stack that are in ready state.
STACK: C
Step 7 - POP the top element from the
stack, i.e., C, and print it. Now, PUSH
all the neighbors of C onto the stack
that are in ready state.
STACK: E, G
01/24/2025 BMEE407L-Module 2 9 9
Step 8 - POP the top element from
the stack, i.e., G and PUSH all the
neighbors of G onto the stack that
are in ready state.
STACK: E
Step 9 - POP the top element from
the stack, i.e., E and PUSH all the
neighbors of E onto the stack that
are in ready state.
01/24/2025 BMEE407L-Module 2 10 10
Advantages
• Linear memory requirement in DFS compared to BFS due to storing only
the path from the root to the current node.
• Time complexity of DFS is similar to BFS but with different node
generation order.
• DFS is time-limited rather than space-limited, exploring paths
differently.
• If DFS finds a solution early in a path, time and space requirements are
significantly reduced.
• DFS demands less memory as it stores only nodes on the current path.
• There's a chance DFS may find a solution without extensively exploring
the entire search space.
01/24/2025 BMEE407L-Module 2 11
Disadvantages
• DFS may get stuck down the left-most path indefinitely, known
as infinite loop possibility.
• Imposing a cutoff depth is a solution to above issue, but the
ideal depth (solution depth d) is often unknown beforehand.
• If the cutoff depth is less than d, DFS fails to find a solution; if
greater, it incurs a substantial execution time cost, potentially
yielding suboptimal solutions.
• In the presence of multiple solutions, DFS does not guarantee
finding the minimal solution.
01/24/2025 BMEE407L-Module 2 12
Applications
Finding the shortest path between two nodes in a graph
Finding all of the possible solutions to a problem, such as
finding all of the possible ways to arrange a set of objects
Detecting cycles in a graph
Topological sorting of a graph
Finding all of the strongly connected components in a graph
Solving Single solution puzzles like mazes.
Searching for bi-connectivity in graphs.
01/24/2025 BMEE407L-Module 2 13
PRACTISE EXAMPLE-1
01/24/2025 BMEE407L-Module 2 14
01/24/2025 BMEE407L-Module 2 15
Iterative Deepening Search(IDS)
There are two common ways to traverse a graph: BFS and
DFS.
Considering a Tree (or Graph) of enormous height and width,
BFS and DFS are inefficient for the following reasons.
01/24/2025 BMEE407L-Module 2 16
Iterative Deepening Search(IDS)
DFS first traverses nodes, going through one adjacent to the
root, then the next adjacent.
The problem with this approach is that if there is a node close
to the root, but not in the first few subtrees explored by DFS,
then DFS reaches that node very late.
Also, DFS may need help finding the shortest path to a node
(in terms of the number of edges).
01/24/2025 BMEE407L-Module 2 17
Acyclic graph
The goal node is R where we have
to find the depth and the path to
reach it.
The depth from the figure is 4.
In this example, we consider the
tree as a finite tree, while we can
consider the same procedure for
the infinite tree as well.
We knew that in the algorithm of
IDDFS we first do DFS till a
specified depth and then increase
the depth at each loop.
This special step forms the part of
Here in the given tree, the DLS or Depth Limited Search.
starting node is A and the Thus the following traversal shows
depth initialized to 0. the IDDFS search.
01/24/2025 BMEE407L-Module 2 18
01/24/2025 BMEE407L-Module 2 19
PARCTISE EXAMPLE-1
01/24/2025 BMEE407L-Module 2 20
01/24/2025 BMEE407L-Module 2 21
Cyclic graph
01/24/2025 BMEE407L-Module 2 22
01/24/2025 BMEE407L-Module 2 23
BOTH IDS AND DFS WERE IMPLEMENTED
01/24/2025 BMEE407L-Module 2 24
Advantages
IDDFS improves search efficiency by refining depth,
heuristics, and scores.
Swift responsiveness and early results contribute to its
effectiveness.
Outperforms single BFS and DFS operations, with multiple
refinements after each iteration.
Comprehensive approach ensures a solution is found in the
search space.
Memory-efficient, storing nodes up to the current depth
limit.
Versatile application for both trees and graphs, widely used
in various contexts.
01/24/2025 BMEE407L-Module 2 25
Disadvantages
The time taken to reach the goal node is exponential.
The main problem with IDDFS is the time and wasted
calculations at each depth.
IDS has the disadvantage of potentially visiting certain nodes
more than once, which might slow down the search. The
benefits of completeness and optimality frequently exceed this
disadvantage. In addition, by employing strategies like
memory or caching, repeated trips can be minimized.
The IDDFS might fail when the BFS fails.
01/24/2025 BMEE407L-Module 2 26
Applications
Iterative deepening depth-first search is a hybrid algorithm
emerging out of BFS and DFS.
IDDFS might not be used directly in many applications of
Computer Science, yet the strategy is used in searching data
of infinite space by incrementing the depth limit by
progressing iteratively.
This is quite useful and has applications in AI and the
emerging data sciences industry.
01/24/2025 BMEE407L-Module 2 27
Thank yoou
01/24/2025 BMEE407L-Module 2 28