0% found this document useful (0 votes)
26 views13 pages

Depth-First Search Algorithm: Maqsood Ahmad

The document discusses depth-first search (DFS) algorithms, which traverse graphs by exploring nodes as deep as possible before backtracking. DFS uses a stack data structure to push and pop nodes. It works by selecting a starting node, pushing its unvisited neighbors onto the stack, and popping nodes off to visit until none remain or a solution is found.

Uploaded by

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

Depth-First Search Algorithm: Maqsood Ahmad

The document discusses depth-first search (DFS) algorithms, which traverse graphs by exploring nodes as deep as possible before backtracking. DFS uses a stack data structure to push and pop nodes. It works by selecting a starting node, pushing its unvisited neighbors onto the stack, and popping nodes off to visit until none remain or a solution is found.

Uploaded by

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

Depth-First Search

Algorithm
Maqsood Ahmad
Graph Traversal Algorithm

• Graph traversal is a technique used to search and locate a vertex in a


graph. The search technique evaluates the graph’s order to traverse
each vertex. Graph traversal helps shorten the search steps and finds
the required edges to be involved in a search process without creating
loops.
• There are two ways in which a graph can be traversed — Depth-First
Search (DFS) algorithm and Breadth-First Search (BFS) algorithm.
Hence, the DFS algorithm is a part of the graph traversal algorithm.
Depth-First Search Algorithm

• Depth-First Search Algorithm Defined


• Working of a Depth-First Search Algorithm
• Graph Traversal Algorithm
• Illustration of Depth-First Search Algorithm
Depth-First Search Algorithm
• The DFS algorithm searches each node by moving forward and
backtracking as far as possible.
• When the iteration hits rock bottom, the Depth-First Search
algorithm explores the network in a depthward motion.
• It hence opts for the next vertex to start the traversal using a stack
data structure.
Depth-First Search Algorithm
• The Depth-First Search algorithm is critical when working with data structures. Its recursive
nature helps individuals examine and collect data by joining the vertices of a graph or a tree
data structure.

• DFS in data structure have played a crucial role in searching for DFS trees and graph designs.

• The DFS algorithm has proven to be extremely important in studying data structures.
• This algorithm follows the backtracking principle for performing exhaustive searches of
multiple nodes by backtracking as and when required and moving forward when possible.

• It works node-to-node by pushing the stack from one step to another.


Working of a Depth-First Search
Algorithm
The functioning of the DFS algorithm is illustrated below:
• Step 1: Create a stack with the total number of vertices in a graph.
• Step 2: Select any node in a graph as the root note, start traversing through
the vertices, and push the stack from one vertex to the other.
• Step 3: Push the stack to a non-visited vertex adjacent to the visited one.
• Step 4: Repeat the previous step as far as possible until the stack reaches
the last vertex.
• Step 5: If there are no vertices left to visit, go back to the stack and pop a
vertex from it.
• Step 6: Repeat the previous three steps till the stack becomes empty.
Illustration of Depth-First Search
Algorithm
• Here is an example to better understand the working of a DFS algorithm.
• An undirected graph with 5 vertices has been taken to perform the DFS algorithm.
The traversal starts at vertex 0 by initiating the DFS algorithm as it places itself in
the visited list, and the remaining adjacent vertices are placed in the stack.
• Then we move to the next adjacent unvisited vertex, which is 1. Since we have
already visited 0, the stack is pushed to the next adjacent vertex, 2.
• The adjacent vertex to 2, yet to be visited, is vertex 4. Now vertex 4 is added to
the stack top so that the traversal to vertex 4 can occur.
• The last node in this graph is vertex 3 without any unvisited adjacent vertex.
Hence, all the nodes in the graph have been visited, marking the end of the Depth
First Search algorithm for this graph.
Example Depth-First Search Algorithm
Illustration of Depth-First Search
Algorithm
Depth-Limited Search Algorithm
• A depth-limited search algorithm is similar to depth-first search with a
predetermined limit.

• Depth-limited search can solve the drawback of the infinite path in the Depth-
first search. In this algorithm, the node at the depth limit will treat as it has no
successor nodes further.
Depth-Limited Search Algorithm
Depth-limited search can be terminated with two Conditions of failure:
Standard failure value: It indicates that problem does not have any solution.
Cutoff failure value: It defines no solution for the problem within a given depth
limit.

Advantages:
Depth-limited search is Memory efficient.
Disadvantages:
Depth-limited search also has a disadvantage of incompleteness.
It may not be optimal if the problem has more than one solution.

You might also like