0% found this document useful (0 votes)
9 views9 pages

22CD3014 Lab2

Uploaded by

Gautam kumar
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)
9 views9 pages

22CD3014 Lab2

Uploaded by

Gautam kumar
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/ 9

22CD3014 Assignment2

BFS

from collec ons import deque

graph = {

'A': ['B', 'C'],

'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': ['F'],

'F': []

def bfs(graph, start):

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

queue = deque([start]) # Ini alize a queue with the start node.

while queue:

node = queue.pople () # Dequeue a node from the front of the queue.

if node not in visited:

visited.append(node) # Mark the node as visited.

# Enqueue all unvisited neighbors.

queue.extend(neighbor for neighbor in graph[node] if neighbor not in visited)

return visited

# Example usage:

bfs_result = bfs(graph, 'A')

print("BFS:", bfs_result)
def dfs(graph, start):

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

stack = [start] # Ini alize a stack with the start node.

while stack:

node = stack.pop() # Pop a node from the top of the stack.

if node not in visited:

visited.append(node) # Mark the node as visited.

# Push all unvisited neighbors onto the stack.

stack.extend(neighbor for neighbor in graph[node] if neighbor not in visited)

return visited

# Example usage:

dfs_result = dfs(graph, 'A')

print("DFS:", dfs_result)

OUPUT

DFS Code (Using Stack)


from collec ons import deque

graph = {

'A': ['B', 'C'],


'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': ['F'],

'F': []

def dfs(graph, start):

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

stack = [start] # Ini alize a stack with the start node.

while stack:

node = stack.pop() # Pop a node from the top of the stack.

if node not in visited:

visited.append(node) # Mark the node as visited.

# Push all unvisited neighbors onto the stack.

stack.extend(neighbor for neighbor in graph[node] if neighbor not in visited)

return visited

# Example usage:

dfs_result = dfs(graph, 'A')

print("DFS:", dfs_result)
DFS Code (Using Recursion)
from collec ons import deque

graph = {

'A': ['B', 'C'],

'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': ['F'],

'F': []

def dfs_recursive(graph, node, visited=None):

if visited is None:

visited = []

visited.append(node)

for neighbor in graph[node]:

if neighbor not in visited:

dfs_recursive(graph, neighbor, visited)

return visited

# Example usage:

dfs_recursive_result = dfs_recursive(graph, 'A')

print("DFS Recursive:", dfs_recursive_result)


BFS with Goal Node
from collec ons import deque

graph = {

'A': ['B', 'C'],

'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': ['F'],

'F': []

from collec ons import deque

def bfs_with_goal(graph, start, goal):

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

queue = deque([(start, [start])]) # Queue holds tuples of (current node, path to this node).

while queue:

node, path = queue.pople () # Dequeue a node along with the path to reach it.

if node not in visited:

visited.append(node) # Mark the node as visited.

if node == goal: # Check if the current node is the goal.


return path # Return the path to the goal.

# Enqueue all unvisited neighbors with the updated path.

for neighbor in graph[node]:

if neighbor not in visited:

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

return None # Return None if the goal is not found.

# Example usage:

bfs_goal_result = bfs_with_goal(graph, 'A', 'F')

print("BFS Path to Goal:", bfs_goal_result)

DFS with Goal Node (Using Stack)


from collec ons import deque

graph = {

'A': ['B', 'C'],

'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': ['F'],

'F': []

}
def dfs_with_goal(graph, start, goal):

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

stack = [(start, [start])] # Stack holds tuples of (current node, path to this node).

while stack:

node, path = stack.pop() # Pop a node along with the path to reach it.

if node not in visited:

visited.append(node) # Mark the node as visited.

if node == goal: # Check if the current node is the goal.

return path # Return the path to the goal.

# Push all unvisited neighbors with the updated path.

for neighbor in graph[node]:

if neighbor not in visited:

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

return None # Return None if the goal is not found.

# Example usage:

dfs_goal_result = dfs_with_goal(graph, 'A', 'F')

print("DFS Path to Goal:", dfs_goal_result)


DFS with Goal Node (Using Recursion)
from collec ons import deque

graph = {

'A': ['B', 'C'],

'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': ['F'],

'F': []

def dfs_recursive_with_goal(graph, node, goal, path=None, visited=None):

if visited is None:

visited = []

if path is None:

path = [node]

visited.append(node)

if node == goal: # Check if the current node is the goal.

return path # Return the path to the goal.

for neighbor in graph[node]:

if neighbor not in visited:

result = dfs_recursive_with_goal(graph, neighbor, goal, path + [neighbor], visited)

if result: # If the goal is found in the recursion, return the result.

return result

return None # Return None if the goal is not found.


# Example usage:

dfs_recursive_goal_result = dfs_recursive_with_goal(graph, 'A', 'F')

print("DFS Recursive Path to Goal:", dfs_recursive_goal_result)

You might also like