BFS Ai
BFS Ai
1. Definition:
• BFS is a search algorithm used to explore a graph or tree by visiting all nodes at the
current depth level before moving to the next level.
2. Process:
3. Steps:
4. Key Properties:
• Optimal: Finds the shortest path in terms of number of edges (if all edges have equal
weight).
• Time Complexity: O(V + E), where V is the number of vertices and E is the number of
edges.
5. Applications:
BFS ensures systematic exploration, making it useful in scenarios where the shortest or shallowest
solution is desired.
Why BFS?
• Guarantees the shortest path: BFS will always find the shortest path in an unweighted
graph.
• Efficiency: It’s simple and avoids getting stuck in loops or taking unnecessarily long
routes.
This makes BFS ideal for scenarios like city navigation, network routing, and solving puzzles where the
shortest sequence of steps is crucial.
1. Un-weighted Graph: In this case, the roads don’t have specific distances (or all roads
have the same “cost”), so we are just interested in finding the route with the fewest number of
intersections.
2. Level-by-Level Exploration: BFS explores each intersection (node) level by level. It checks
all direct roads from the starting point, then moves to the next set of intersections. This guarantees that
the first time you reach your destination (Node G), you’ve done so using the shortest possible route (in
terms of number of roads).
Process:
Scenario: Imagine you are in a city, and you want to find the shortest route from your location (Node A)
to a destination (Node G) using the city’s road network. Each road is represented as an edge, and
intersections are represented as nodes.
Program:-
graph ={
'5':['8','6'],
'8':['1','2'],
'6':['3','4'],
'1':[],
'2':[],
'3':[],
'4':[],
visited=[]
queue=[]
def bfs(visited,graph,node):
visited.append(node)
queue.append(node)
while queue:
m=queue.pop(0)
print(m,end=" ")
visited.append(neighbour)
queue.append(neighbour)
bfs(visited,graph,'5')
This code implements the Breadth-First Search (BFS) algorithm on a graph using Python. Here is the line-
by-line breakdown and explanation of the functions used:
1. graph = {...}:
• A dictionary representing the graph where each key is a node, and the value is a list of
its neighbors (adjacent nodes). For example, node ‘5’ is connected to nodes ‘8’ and ‘6’. Nodes ‘1’, ‘2’, ‘3’,
and ‘4’ have no neighbors (empty lists).
2. visited = []:
• An empty list to keep track of the nodes that have been visited during the BFS traversal.
This ensures that nodes are not visited more than once.
3. queue = []:
• An empty list to simulate a queue (FIFO - First In First Out) for keeping track of nodes to
be visited in the correct BFS order.
5. visited.append(node):
6. queue.append(node):
• Adds the starting node to the queue to begin the BFS traversal.
7. while queue::
• A while loop that runs as long as there are elements in the queue. This loop controls the
BFS traversal process.
8. m = queue.pop(0):
• Removes the first element from the queue (FIFO behavior) and assigns it to m. This
element will be processed next.
• Function used: pop(0) to remove the first item from the list.
• Prints the node m (the current node being processed) without a newline. The end=" "
ensures that subsequent prints appear on the same line separated by spaces.
• A for loop that iterates over all the neighbors (adjacent nodes) of node m in the graph.
12. visited.append(neighbour):
13. queue.append(neighbour):
• Adds the neighbor to the queue so that it can be processed in a future iteration of the
while loop.
• Lists (append, pop): Used for managing the visited nodes and the queue for BFS
traversal.
• Control structures: Loops (while, for) and conditionals (if) manage the flow of the BFS
algorithm.
This code performs a breadth-first traversal starting from node '5' and explores all its neighbors,
ensuring each node is visited once in the correct order.