Green University of Bangladesh
Department of Computer Science and Engineering (CSE)
Faculty of Sciences and Engineering
Semester: Spring, Year:2024), B.Sc. in CSE (Day)
Lab Report NO # 02
Course Title: Artificial Intelligence Lab
Course Code: CSE 316 Section: 221_D4
Lab Experiment Name:
Write a python code to perform bfs traversal on the given graph and print the order of
the nodes.
Student Details
Name ID
1. Nur Hasan Hasib 221902247
Lab Date : 15/01/25
Submission Date : 22/01/25
Course Teacher’s Name : Mahjabin Rahman Oishe
Lab Report Status
Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
1. TITLE OF THE LAB REPORT EXPERIMENT
Write a python code to perform bfs traversal on the given graph and print the order of the
nodes.
2. OBJECTIVES
Understand BFS Algorithm: Learn the working principle of BFS and how it explores nodes level by
level.
Implement BFS in Python: Write an efficient BFS traversal using a queue-based approach.
Graph Representation: Represent graphs using adjacency lists for efficient traversal.
Explore Connectivity: Identify reachable nodes from a given starting node.
Optimize Traversal: Use sets or Boolean arrays to track visited nodes and avoid redundant processing.
Analyze Time Complexity: Understand that BFS runs in O(V + E) time complexity, where V is vertices
and E is edges.
Apply BFS to Real-World Problems: Utilize BFS for shortest path finding, network broadcasting, and
AI search algorithms.
Compare BFS and DFS: Highlight differences between BFS and Depth-First Search (DFS) in terms of
traversal and applications.
3. PROCEDURE AND IMPLEMENTATION
Initialize Data Structures
Use a queue (FIFO) to store nodes for processing.
Use a set (or list) to track visited nodes.
Start from the Given Node
Enqueue the starting node and mark it as visited.
Process Nodes Level by Level
Dequeue a node, print it, and explore its neighbors.
Enqueue unvisited neighbors and mark them as visited.
Repeat Until the Queue is Empty
Continue dequeuing and processing nodes until all reachable nodes are visited.
4. TEST RESULT / OUTPUT
5. ANALYSIS AND DISCUSSION
The problem involves implementing Breadth-First Search (BFS) traversal on a directed graph,
starting from node 0. BFS is an efficient graph traversal algorithm that explores all neighbors of a
node before moving to the next level. The algorithm follows a FIFO (First-In-First-Out) approach
using a queue, ensuring that nodes are visited in the correct order. An adjacency list representation is
used for the graph, making traversal efficient in terms of space and time. The time complexity is O(V
+ E), where V is the number of vertices and E is the number of edges, since each node and edge is
processed once. The space complexity is O(V), as it requires storage for the visited nodes and queue.
The BFS traversal order for the given graph is 0 → 2 → 1 → 3 → 4 → 5, ensuring that all reachable
nodes are visited systematically.
6. Summary
The problem involves implementing BFS traversal on a directed graph starting from node 0. BFS
explores nodes level by level using a queue and tracks visited nodes with a set, ensuring an efficient
O(V + E) time complexity. The traversal order is 0 → 2 → 1 → 3 → 4 → 5, covering all reachable
nodes. BFS is widely used in shortest pathfinding, network traversal, and AI search algorithms,
making it a crucial technique for structured graph exploration.