5. OVERVIEW
DFS stands for Depth First Search. DFS is an algorithm for
traversing or searching a tree or graph data structures. It uses a
stack data structure for implementation. In DFS one starts at
the root and explores as far as possible along each branch
before backtracking. The algorithm starts at the root node
(selecting some arbitrary node as the root node in the case of a
graph) and explores as far as possible along each branch before
backtracking. Extra memory, usually a stack, is needed to keep
track of the nodes discovered so far along a specified branch
which helps in backtracking of the graph.
6. Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures.
The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a
graph) and explores as far as possible along each branch before backtracking. It uses last in- first-out
strategy and hence it is implemented using a stack.
It entails conducting exhaustive searches of all nodes by moving forward if possible and
backtracking, if necessary. To visit the next node, pop the top node from the stack and push all of its
nearby nodes into a stack. Topological sorting, scheduling problems, graph cycle detection, and
solving puzzles with just one solution, such as a maze or a sudoku puzzle, all employ depth-first
search algorithms. Other applications include network analysis, such as determining if a graph is
bipartite.
INTRODUCTION
7. ALGORITHM
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
8. METHODS TO IMPLEMENT OF DFS ALGORITHM
The following two methods are typically employed to implement DFS
efficiently- recursive and iterative.
Recursive Approach:
In the recursive approach, the algorithm starts at the source vertex and marks
it as visited. It recursively explores every unvisited neighbor of the source
vertex. When no more unvisited neighbors are left, the algorithm backtracks
to the previous vertex in the stack and repeats the process until all vertices in
the graph are visited.
Iterative Approach:
In the iterative approach, the algorithm uses a stack to keep track of the
vertices that need to be visited next. It starts at the source vertex, marks it as
visited, and pushes it onto the stack. Then, it repeatedly pops the top vertex
from the stack, explores its unvisited neighbors, and makes them onto the
stack until all are visited.
9. APPLICATIONS OF DEPTH FIRST SEARCH
1. Detecting cycle in a graph: A graph has a cycle if and only if we see a back edge during DFS. So we can run DFS for
the graph and check for back edges.
2. Path Finding: We can specialize the DFS algorithm to find a path between two given vertices u and z.
3. Topological Sorting: Topological Sorting is mainly used for scheduling jobs from the given dependencies among
jobs. In computer science, applications of this type arise in instruction scheduling, ordering of formula cell evaluation
when recomputing formula values in spreadsheets, logic synthesis, determining the order of compilation tasks to
perform in make files, data serialization, and resolving symbol dependencies in linkers.
4. To test if a graph is bipartite: We can augment either BFS or DFS when we first discover a new vertex, color it
opposite its parents, and for each other edge, check it doesn’t link two vertices of the same color. The first vertex in any
connected component can be red or black.
5. Finding Strongly Connected Components of a graph: A directed graph is called strongly connected if there is a path
from each vertex in the graph to every other vertex. (See this for DFS-based algo for finding Strongly Connected
Components)
10. 6. Solving puzzles with only one solution: such as mazes. (DFS can be adapted to find all solutions to a
maze by only including nodes on the current path in the visited set.).
7. Web crawlers: Depth-first search can be used in the implementation of web crawlers to explore the links
on a website.
8. Maze generation: Depth-first search can be used to generate random mazes.
9. Model checking: Depth-first search can be used in model checking, which is the process of checking that
a model of a system meets a certain set of properties.
10. Backtracking: Depth-first search can be used in backtracking algorithms.
11. CLASSIFICATION OF EDGES
Tree edge: Edge (u,v) is a tree edge if v was first discovered by exploring edge (u,v). White color indicates
tree edge.
Back edge: Edge (u,v) is a back edge if it connects a vertex u to a ancestor v in a depth first tree. Gray color
indicates back edge.
Forward edge: Edge (u,v) is a forward edge if it is non-tree edge and it connects a vertex u to a descendant
v in a depth first tree. Black color indicates forward edge.
Cross edge: rest all other edges are called the cross edge. Black color indicates forward edge.
12. Initialization complexity is O(V)
DFS_VISIT is called exactly once for each vertex
And DFS VISIT scans all the edges which causes cost of O(E)
Thus overall complexity is O(V + E)
Pseudocode:
DFS(G,v) ( v is the vertex where the search starts )
Stack S := {}; ( start with an empty stack )
for each vertex u, set visited[u] := false;
push S, v;
while (S is not empty) do
u := pop S;
if (not visited[u]) then
visited[u] := true;
for each unvisited neighbour w of uu
push S, w;
end if
end while
END DFS()
COMPLEXITY & PSEUDOCODE
13. ADVANTAGES & DISADVANTAGES
ADVANTAGES DISADVANTAGES
The time complexity of a depth-first Search to depth d
and branching factor b is O(bd) since it generates the
same set of nodes as breadth-first search, but simply in a
different order. Thus practically depth-first search is
time-limited rather than space-limited.
If depth-first search finds solution
without exploring much in a path then
the time and space it takes will be very
less.
DFS requires less memory since only the
nodes on the current path are stored. By
chance DFS may find a solution without
examining much of the search space at all.
The disadvantage of Depth-First Search is that
there is a possibility that it may down the left-
most path forever. Even a finite graph can
generate an infinite tree One solution to this
problem is to impose a cutoff depth on the search.
Depth-First Search is not guaranteed to
find the solution.
And there is no guarantee to find a
minimal solution, if more than one
solution.
14. SUMMARY
The Depth-First Search (DFS) algorithm is a simple yet powerful technique for graph
traversal. It starts from a starting node, explores as far as possible along each branch
before backtracking, and uses a stack to keep track of visited nodes. DFS is widely
used in various applications such as maze generation, topological sorting, and cycle
detection. However, it may not necessarily visit all nodes in a graph, especially if the
graph is disconnected. Despite its limitations, DFS is a fundamental computer science
algorithm widely used in numerous real-world applications.