DAA Practical-10-1
DAA Practical-10-1
Roll Number 62
Section A
Practical No-10
Aim: To implement and analyze the time complexity of Breadth First Search.
Theory: Breadth First Search (BFS) is a technique for traversing a finite graph.
BFS visits the neighbour vertices before visiting the child vertices, and a queue is
used in the search process. This algorithm is often used to find the shortest path
from one vertex to another.
● Here we start traversing from a selected node (source node) and traverse the
graph layer/level wise thus exploring the neighbour nodes (nodes which are
directly connected to the source node). We must move to the next layer/level
only after traversing the current layer/level completely.
● In short:
i. Move horizontally and visit all the nodes of the current layer/level.
ii. Move to the next layer.
Algorithm: The algorithm starts with examining the source node and all of its
neighbours. In the next step, the neighbours of the nearest node of the source node
are explored. The algorithm then explores all neighbours of all the nodes and
ensures that each node is visited exactly once and no node is visited twice.
STEP 1: Start by putting any one of the graph vertices into the queue.
STEP 2: Take the front item in the queue and add it to the visited list.
STEP 3: Create the list of that vertex adjacent node. Add the ones which are not in
the visited list to the back of the queue.
Practice: