You are on page 1of 21

Data Structures & Algorithms

Presentation

Graph traversals – Depth First Search and Breadth


First Search implementation.
Breadth First Search &
Depth First Search
Explore the two fundamental graph traversal algorithms used in computer
science: Breadth First Search (BFS) and Depth First Search (DFS).
Breadth First Search (BFS)
Algorithm
1 Definition 2 Advantages

The BFS algorithm is used to BFS is easy to implement and


search a graph data structure for understand. It uses a simple
a node that meets a set of criteria. queue data structure and a
It starts at the root of the graph straightforward algorithm,
and visits all nodes at the current making it accessible for both
depth level before moving on to programming and problem-
the nodes at the next depth level. solving
It uses queue data structure

3 Applications

Shortest Path Finding, Network Routing, Social Network Analysis, Game


Development
Working of Breadth First Search (BFS)

1 Step 1

Start with a source vertex and enqueue it.

Step 2 2
Dequeue the vertex and visit its adjacent
vertices. 3 Step 3

Enqueue the unvisited adjacent vertices


Step 4 4 and mark them as visited.

Repeat steps 2 and 3 until the queue is


empty.
Step1: Initially queue and visited arrays are
empty.
Step2: Push node 0 into queue and mark it
visited.
Step 3: Remove node 0 from the front of queue and visit
the unvisited neighbours and push them into queue
Step 4: Remove node 1 from the front of queue and visit
the unvisited neighbours and push them into queue
Step 5: Remove node 2 from the front of queue and visit
the unvisited neighbours and push them into queue.
Step 6: Remove node 3 from the front of queue and visit
the unvisited neighbours and push them into queue.
Steps 7: Remove node 4 from the front of queue and visit
the unvisited neighbours and push them into queue.
Depth First Search (DFS) Algorithm
Definition Advantages Applications

Depth First Search, is an It is more memory-efficient Used in maze generation,


edge-based technique. It uses because it only keeps track of network routing, and solving
the Stack data structure and the nodes from the root node puzzles like the Sudoku.
performs two stages, first to the current node in a stack.
visited vertices are pushed
into the stack, and second if
there are no vertices then
visited vertices are popped.
Working of Depth First Search (DFS)

1 Step 1

Start with a source vertex and push it onto


the stack.
Step 2 2
Pop the vertex from the stack, visit it, and
mark it as visited. 3 Step 3

Push the unvisited adjacent vertices onto


Step 4 4 the stack and mark them as visited.

Repeat steps 2 and 3 until the stack is


empty.
Input tree

Therefore Depth first Traversals of this tree will be:

Inorder: 4 2 5 1 3

Preorder: 1 2 4 5 3

Post order: 4 5 2 3 1
Pseudo Code Of Inorder
inorder(tree)
begin
if tree is null, return;

inorder(tree.left_subtree);
print(tree.root);
inorder(tree.right_subtree);
end
Pseudo Code Of Postorder
postorder(tree)
begin
if tree is null, return;

postorder(tree.left_subtree);
postorder(tree.right_subtree);
print(tree.root);
end
Pseudo Code Of Preorder
preorder(tree)
begin
if tree is null, return;

print(tree.root);
preorder(tree.left_subtree);
preorder(tree.right_subtree);
end
Key Differences Between BFS and DFS

BFS DFS

Explores vertices in increasing order of distance Explores vertices in depth-first order.


from the start.

Uses a queue for storing vertices. Uses a stack for storing vertices.

Optimal for finding the shortest path. Optimal for topological sorting.

BFS is more suitable for closer solutions DFS is more suitable for away solutions

It works on the concept of FIFO It works on the concept of LIFO

It builds the tree level by level It builds the tree subtree by subtree
Applications of BFS

Social Network Analysis Web Crawling Puzzle Solving

BFS is used to systematically BFS helps find the shortest


BFS helps uncover hidden explore and collect data from solution to puzzles like the
relationships within complex websites. Rubik's Cube.
social networks.
Applications of DFS

Maze Solving Network Routing Puzzle Solving

DFS is used to find a path from DFS helps find the best path for DFS is used to solve puzzles like
the start to the exit in a maze. routing packets in a network. Sudoku by exploring all possible
configurations.
Submitted By :-
Vikas Sharma (23MCA10022)

Vishwas Jain (23MCA10024)

Arin Shrivastava (23MCA 10080)

Pankaj Meharchandani (23MCA10111)

You might also like