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

AIML-Lab1

The document presents implementations of Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms in Python for navigating a graph defined by nodes and their neighbors. BFS finds the shortest path in an unweighted graph using a queue, while DFS explores paths deeply using a stack, potentially not guaranteeing the shortest path. Both algorithms are demonstrated with a specific example of finding a path from node 'C' to node 'G'.
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)
6 views

AIML-Lab1

The document presents implementations of Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms in Python for navigating a graph defined by nodes and their neighbors. BFS finds the shortest path in an unweighted graph using a queue, while DFS explores paths deeply using a stack, potentially not guaranteeing the shortest path. Both algorithms are demonstrated with a specific example of finding a path from node 'C' to node 'G'.
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/ 4

AIML Lab-1

Name: Divya Pratap Singh Roll No.: 23UEC536

1. BFS:
import collections

# Graph with nodes and their neighbors

Graph_Nodes = {

'C': [('A', 2), ('B', 1)],

'A': [('B', 4), ('D', 3), ('C', 2)],

'B': [('D', 3), ('E', 7), ('F', 1), ('A', 4), ('C', 1)],

'D': [('E', 2), ('F', 1), ('B', 3), ('A', 3)],

'E': [('F', 1), ('D', 2), ('B', 7)],

'F': [('G', 1), ('E', 1), ('H', 1), ('D', 1), ('B', 1)],

'G': [('H', 1), ('F', 1)],

'H': [('G', 1), ('F', 1)]

def BFS(graph, start, end):

queue = collections.deque([[start, [start]]]) # Initialize queue with start node

visited = set() # Track visited nodes

while queue:

(vertex, path) = queue.popleft() # Get the current node and path

vertex = vertex[0] # Ignore weight and get the node name

if vertex in visited: # Skip if already visited

continue

elif vertex == end: # Return path if end node is found

return path

else:
visited.add(vertex) # Mark node as visited

for neighbor in graph[vertex]: # Add neighbors to the queue

queue.append((neighbor[0], path + [neighbor[0]]))

print("Path not found") # If no path exists

return None

# Find path from 'C' to 'G'

BFS(Graph_Nodes, 'C', 'G')


2. DFS:
import collections

# Graph with nodes and their neighbors

Graph_Nodes = {

'C': [('A', 2), ('B', 1)],

'A': [('B', 4), ('D', 3), ('C', 2)],

'B': [('D', 3), ('E', 7), ('F', 1), ('A', 4), ('C', 1)],

'D': [('E', 2), ('F', 1), ('B', 3), ('A', 3)],

'E': [('F', 1), ('D', 2), ('B', 7)],

'F': [('G', 1), ('E', 1), ('H', 1), ('D', 1), ('B', 1)],

'G': [('H', 1), ('F', 1)],

'H': [('G', 1), ('F', 1)]

def DFS(graph, start, end):

stack = [[start, [start]]] # Stack to track nodes and paths

visited = set() # Track visited nodes

while stack:
(vertex, path) = stack.pop() # Get the current node and path

vertex = vertex[0] # Get node name (ignore weights)

if vertex in visited: # Skip if already visited

continue

if vertex == end: # Return path if target is found

return path

visited.add(vertex) # Mark current node as visited

# Add neighbors to stack with updated path

for neighbor in graph[vertex]:

stack.append((neighbor[0], path + [neighbor[0]]))

print("Path not found") # If no path exists

return None
# Find path from 'C' to 'G'

DFS(Graph_Nodes, 'C', 'G')

Logic for Each Algorithm


1. BFS (Breadth-First Search):
BFS (Breadth-First Search) finds the shortest path in an unweighted graph by using a
queue. It begins at a starting node, systematically visiting and marking nodes while
exploring their neighbors level by level. If it encounters the target node, it returns
the path. Since BFS expands uniformly across all directions, it ensures the shortest
path in an unweighted graph.

2. DFS (Depth-First Search):


Depth-First Search (DFS) navigates a graph using recursion or a stack, delving as deep as
possible along a path before retracing its steps. Starting from an initial node, it continues
exploring until it either finds the target or reaches a dead-end, at which point it backtracks.
While DFS is effective in locating a solution, it does not always ensure the shortest path.

You might also like