0% found this document useful (0 votes)
11 views5 pages

Ai Exp 5

The document outlines an experiment for implementing uninformed search techniques, specifically Breadth-First Search (BFS) and Depth-First Search (DFS), in a computer engineering course. It provides theoretical explanations of both algorithms, along with Python code for their implementation. The experiment concludes with a successful execution of both search techniques.

Uploaded by

krutikapandya23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Ai Exp 5

The document outlines an experiment for implementing uninformed search techniques, specifically Breadth-First Search (BFS) and Depth-First Search (DFS), in a computer engineering course. It provides theoretical explanations of both algorithms, along with Python code for their implementation. The experiment concludes with a successful execution of both search techniques.

Uploaded by

krutikapandya23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Universal College of Engineering, Kaman

Department of Computer Engineering


Subject: Artificial Intelligence

Experiment No: 5
Roll No: 55 Name: Krutika pandya Div: B Batch: B1

Aim: Implement Uninformed search techniques (DFS, BFS).

Theory:-
BFS (Breadth-First Search): BFS is an algorithm used to traverse or search through graphs and trees.
It starts from a given node and explores all neighboring nodes at the present level before moving on
to nodes at the next level. BFS ensures that nodes are explored level by level, making it ideal for
finding the shortest path in an unweighted graph. It uses a queue to keep track of nodes to be visited.

DFS (Depth-First Search): DFS is another graph traversal algorithm that explores as far down a
branch of the graph as possible before backtracking to explore other branches. It starts from a node
and follows each path to its deepest node before backtracking and exploring new paths. DFS is often
implemented using recursion or a stack. It’s useful for tasks like finding connected components and
topological sorting.

Code-

BFS Code:

# Define the graph

graph = {
'5': ['3', '7'],

'3': ['2', '4'],

'7': ['8'],

'4': ['8'],

'2': [],

'8': []

# Define the BFS function

def bfs(visited, graph, node):

queue = []

visited.append(node)

queue.append(node)

while queue:

m = queue.pop(0)

print(m, end=" ")

for neighbour in graph[m]:

if neighbour not in visited:

visited.append(neighbour)

queue.append(neighbour)

# Driver Code

visited = [] # List to keep track of visited nodes

print("Following is the Breadth-First Search:")

bfs(visited, graph, '5') # Function call

DFS Code:
# Define the graph

graph = {

'5': ['3', '7'],

'3': ['2', '4'],

'7': ['8'],

'4': ['8'],

'2': [],

'8': []

# Define the DFS function

def dfs(visited, graph, node):

stack = []

visited.append(node)

stack.append(node)

while stack:

m = stack.pop() # Pop from the end of the stack (DFS behavior)

print(m, end=" ")

# Visit the neighbors in reverse order to keep the correct DFS order

for neighbour in reversed(graph[m]):

if neighbour not in visited:

visited.append(neighbour)

stack.append(neighbour)

# Driver Code visited = [] # List to keep track of visited nodes

print("Following is the Depth-First Search:") dfs(visited, graph, '5') # Function call starting from
node '5'
Output:-

Conclusion:- Thus we have successfully implemented BFS and DFS

You might also like