0% found this document useful (0 votes)
31 views

AI Exp 3

The document describes Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms for traversing graphs. BFS uses a queue to visit all nodes level-by-level, while DFS uses a stack to explore nodes as deep as possible along each branch before backtracking. Pseudocode is provided for BFS and DFS algorithms. The algorithms are implemented on a sample graph to find the path from start node 'S' to goal node 'G'.

Uploaded by

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

AI Exp 3

The document describes Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms for traversing graphs. BFS uses a queue to visit all nodes level-by-level, while DFS uses a stack to explore nodes as deep as possible along each branch before backtracking. Pseudocode is provided for BFS and DFS algorithms. The algorithms are implemented on a sample graph to find the path from start node 'S' to goal node 'G'.

Uploaded by

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

Name: Ganesh Panigrahi PRN No.: 211041016 Batch: T3 Roll No.

: 50

Experiment No. - 03
Aim: Program on uninformed search methods (DFS and BFS).
Theory:
Breadth-First Search (BFS):
Objective: BFS explores a graph or tree level by level, visiting all nodes at the current level before moving
on to the next level.
Data Structure: It uses a queue to keep track of the nodes to be visited.
Order of Visiting Nodes: BFS starts at the root (or any specified node) and visits all its neighbors before
moving on to the next level. It proceeds in a breadthward motion.
Completeness: BFS is complete for finite graphs and trees. It will always find a solution if one exists.
Memory Usage: It tends to use more memory compared to DFS as it needs to store all nodes at the current
level.

Depth-First Search (DFS):


Objective: DFS explores a graph or tree by going as deep as possible along one branch before backtracking.
Data Structure: It uses a stack (or recursion) to keep track of the nodes to be visited.
Order of Visiting Nodes: DFS starts at the root (or any specified node) and explores as far as possible along
each branch before backtracking. It proceeds in a depthward motion.
Completeness: DFS is not guaranteed to find a solution for finite graphs or trees, especially if the graph has
infinite branches. However, it can be modified for specific cases.
Memory Usage: DFS can be more memory-efficient in certain scenarios compared to BFS, as it doesn't
need to store all nodes at the current level.

Algorithm:
Breadth-First Search (BFS) Algorithm:
1. Create an empty queue and enqueue the start node.
2. Create a set to track visited nodes.
3. While the queue is not empty:
- Dequeue a node from the queue.
- Mark the node as visited.
- Process the node (print or store in the path).
- Enqueue all unvisited neighbors of the current node.
4. Repeat until the queue is empty or the goal node is reached.
Depth-First Search (DFS) Algorithm:
1. Create an empty stack and push the start node onto it.
2. Create a set to track visited nodes.
3. While the stack is not empty:
- Pop a node from the stack.
- Mark the node as visited.
- Process the node (print or store in the path).
- Push all unvisited neighbors of the current node onto the stack.
4. Repeat until the stack is empty or the goal node is reached.

Program:
1 . Breadth-First Search (BFS):
graph = {
'S' : ['A','B','C'],
'A' : ['D', 'E'],
'B' : [],
'C' : ['F','G'],
'D' : [],
'E' : ['H','I'],
'F' : [],
'G' : [],
'H' : [],
'I' : [],
}

visited = [] # List for visited nodes.


queue = [] # Initialize a queue

def bfs(visited, graph, start, goal): # function for BFS


visited.append(start)
queue.append(start)

while queue: # Creating a loop to visit each node


current_node = queue.pop(0)
print(current_node, end=" ")

if current_node == goal:
print(f"\nGoal node {goal} reached.")
break

for neighbour in graph[current_node]:


if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

# Driver Code
start_node = 'S'
goal_node = 'G'
print(f"Following is the Breadth-First Search from {start_node} to
{goal_node}:")
bfs(visited, graph, start_node, goal_node) # function calling

Output:

2. Depth-First Search (DFS):


graph = {
'S': ['A', 'B', 'C'],
'A': ['D', 'E'],
'B': [],
'C': ['F', 'G'],
'D': [],
'E': ['H', 'I'],
'F': [],
'G': [],
'H': [],
'I': [],
}

visited = set() # Set for visited nodes

def dfs(graph, node, goal, path=[]):


path = path + [node] # Append the current node to the path

if node == goal:
print("Following is the Depth-First Search path:")
print(' '.join(path))
print(f"Goal node {goal} reached.")
return

visited.add(node)

for neighbor in graph[node]:


if neighbor not in visited:
dfs(graph, neighbor, goal, path.copy())

# Driver Code
start_node = 'S'
goal_node = 'G'
dfs(graph, start_node, goal_node)

Output:
Conclusion: Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms were
implemented to traverse a node graph, providing a path from the start node 'S' to the goal node 'G'.

You might also like