GROUP 1
TOPIC- BREADTH FIRST SEARCH (BFS)
21CE10033 KSHITIJ BHASKAR
21CE10034 MANNE CHIMYANG
OUTLINE
• Introductionabout trees
• Terminology
• Level wise input
• BFS
• Pros and cons
• Example
• Application
BFS 3
INTRODUCTION ABOUT TREES
The tree is a nonlinear hierarchical
data structure and comprises a
collection of entities known as
nodes. It connects each node in the
tree data structure using "edges”,
both directed and undirected.
Trees are non-linear hierarchical data structures. It is a
collection of nodes connected to each other by means of “edges”
which are either directed or undirected. One of the nodes is
designated as “Root node” and the remaining nodes are called
child nodes or the leaf nodes(nodes with no child nodes).The
image represents the tree data structure :
TERMINOLOGY 5
- Ancestor Node: It is any predecessor node on a path from the root to that node.
- Root node: This is the topmost node in the tree hierarchy.
- Leaf node: These are the bottommost nodes in a tree hierarchy. The leaf nodes do
not have any child nodes. They are also known as external nodes.
- Key: It represents the data stored in a node.
- Level: Represents the generation of a node. A root node is always at level 1. Child
nodes of the root are at level 2, grandchildren of the root are at level 3 and so on. In
general, each node is at a level higher than its parent.
- Path: The path is a sequence of consecutive edges.
- Degree: Degree of a node indicates the number of children that a node has.
BFS 6
LEVELWISE ORDER INPUT :
Level order tree traversal algorithm is a breadth-first tree traversal
algorithm. It means that while traversing a tree, we first traverse all the
elements at the current level before moving to the next level.
7
Breadth First Search
Breadth First Search (BFS) algorithm traverses a graph in a breadth-
ward motion and uses a queue to remember to get the next vertex to
start a search, when a dead end occurs in any iteration.
As in the example given above, BFS algorithm
traverses from A to B to C if didn’t get the search
then to D first to E and F lastly to G. It employs the
following rules.
•Rule 1 − Visit the adjacent unvisited vertex. Mark it
as visited. Display it. Insert it in a queue.
•Rule 2 − If no adjacent vertex is found, remove the
first vertex from the queue.
•Rule 3 − Repeat Rule 1 and Rule 2 until the queue is
empty
8
Level 2
Level 1
Level 3
Level 4
Level5
Patna
Kolkata - Here the root is
Kolkata.
Pune Bhubaneswar
Mumbai
Hyderabad
- If the destination is
cochin, then the
search start from the
root to level 2 and to
Madras
level 3 and so on until
Cochin
the destination is
reach.
BFS 9
PROS AND CONS
• Advantages of BFS:
1. The solution will definitely found out by BFS if there is some solution.
2. BFS will never get trapped in a blind alley, which means unwanted nodes.
3. If there is more than one solution then it will find a solution with minimal
steps.
• Disadvantages Of BFS:
1. Memory Constraints As it stores all the nodes of the present level to go for the
next level.
2. If a solution is far away then it consumes time i.e, Time complexity.
BFS 10
APPLICATIONS
- Finding the shortest path
- Checking graph with petiteness.
- Peer to Peer Networks- Breadth First Search is used to find all
neighbor nodes.
- Social Networking Websites: In social networks, we can find
people within a given distance ‘k’ from a person using Breadth
First Search till ‘k’ levels.
CODE: 11
CODE: 12
CODE: 13
SAMPLE INPUT 14
OUTPUT:
PATH: HOWRAH->DUMDUM->SEALDAH->BALLYGUNG->SONAPUR->BARUIPUR->DIAMONDHABOUR
THANK
YOU - Group
1