You are on page 1of 5

Heaven’s Light is Our Guide

Rajshahi University of Engineering & Technology

Department of Mechatronics Engineering (MTE)

LAB REPORT

Course No: CSE 4288


Course Name: Artificial Intelligence sessional

Submitted By Submitted To
Name: Md.
Dibya Hafiz
Joy Paul Ahamed
Roll: Lecturer
Experiment No: 2
Experiment Name: Study of Breadth First Search and Depth First Search
Algorithms.
Objective:
1) To know about Breadth First Search Algorithm .
2) To Know about Depth First Search Algorithm.
3) To know how to build BFS and DFS algorithm from Scratch in python.
Theory :
Searching is a fundamental operation in computer science, used to find data or
information from a given dataset. Two common search algorithms are Breadth-
First Search (BFS) and Depth-First Search (DFS). These algorithms differ in the
way they explore and search through a dataset.
Breadth-First Search (BFS): BFS is an algorithm that traverses a graph or tree
by exploring all the nodes at each level before moving on to the next level. In
BFS, the search starts at the root node and examines all the adjacent nodes at the
same level before moving on to the next level. BFS is implemented using a queue
data structure, where the visited nodes are added to the queue, and the next nodes
to be visited are dequeued. BFS guarantees the shortest path from the starting
node to the target node, making it an ideal choice for finding the shortest path in
an unweighted graph or tree.
Strengths:
 Guaranteed to find the shortest path from the starting node to the target
node.
 Suitable for finding the shortest path in unweighted graphs or trees.
 Can be used to find all the nodes that are reachable from the starting node.
Weaknesses:
 Requires more memory than DFS, as it needs to maintain a queue data
structure to store visited nodes.
 Can be slower than DFS in dense graphs, as it has to explore all the nodes at
each level.
Depth-First Search (DFS): DFS is an algorithm that traverses a graph or tree by
exploring as far as possible along each branch before backtracking. In DFS, the
search starts at the root node and explores as far as possible along each branch
before backtracking to the previous node and exploring the next branch. DFS is
implemented using a stack data structure, where the visited nodes are added to the
stack, and the next nodes to be visited are popped from the stack. DFS does not
guarantee the shortest path from the starting node to the target node, making it an
ideal choice for finding a path between two nodes in a weighted graph or tree.
Strengths:
 Uses less memory than BFS, as it only needs to maintain a stack data
structure to store visited nodes.
 Can be faster than BFS in dense graphs, as it does not have to explore all the
nodes at each level.
 Suitable for finding a path between two nodes in a weighted graph or tree.
Weaknesses:
 Does not guarantee the shortest path from the starting node to the target
node.
 Can get stuck in infinite loops if the graph contains cycles.

Code In Python with Output:

Breadth-first Search Algorithm


# Sample dataset representing a tree
graph = {
'A': ['B', 'C', 'D'],
'B': ['E', 'F'],
'C': ['G'],
'D': [],
'E': [],
'F': ['H', 'I'],
'G': [],
'H': [],
'I': []
}

# Breadth-first search function


def bfs(graph, start):
visited = []
queue = [start]

while queue:
node = queue.pop(0)
if node not in visited:
visited.append(node)
neighbours = graph[node]

for neighbour in neighbours:


queue.append(neighbour)
return visited

# Example usage
print(bfs(graph, 'A'))

['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']

Depth-first Search Algorithm


# Sample dataset representing a graph
graph = {
'A': ['B', 'D', 'E'],
'B': ['C'],
'C': [],
'D': ['F'],
'E': ['G', 'H'],
'F': [],
'G': ['I'],
'H': [],
'I': []
}

# Depth-first search function


def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)

for next_node in graph[start]:


if next_node not in visited:
dfs(graph, next_node, visited)
return visited
# Example usage
print(dfs(graph, 'A'))

{'H', 'B', 'F', 'G', 'C', 'E', 'D', 'A', 'I'}

Conclusion:
Both BFS and DFS are fundamental search algorithms used in computer science
to search through datasets. BFS is suitable for finding the shortest path in an
unweighted graph or tree, while DFS is suitable for finding a path between two
nodes in a weighted graph or tree. The choice of which algorithm to use depends
on the problem being solved and the structure of the dataset. It is important to
understand the strengths and weaknesses of each algorithm before selecting the
appropriate one.

You might also like