Breadth First Search and Depth First Search
Breadth First Search and Depth First Search
Presentation
3 Applications
1 Step 1
Step 2 2
Dequeue the vertex and visit its adjacent
vertices. 3 Step 3
1 Step 1
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
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 builds the tree level by level It builds the tree subtree by subtree
Applications of BFS
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)