You are on page 1of 14

Lab – 2

OBJECTIVE: 1. Write a program for following:


a) Breadth First Search
b) Depth First Search
c) Depth Limited Search
2. Write a program for following by taking user input: -
a) Breadth First Search
b) Depth First Search
c) Depth Limited Search (Limit 3)

1. (a): Breadth First Search

Algorithm:
1. Initialize an empty queue `queue`.
2. Initialize an empty set `visited` to keep track of visited nodes.
3. Enqueue the tuple `(root, "")` into the queue, where the second element of the tuple is an empty string
representing the direction from the parent node.
4. While the queue is not empty:
a. Dequeue a node `current_node` and its associated `direction` from the queue.
b. Construct the `path` by appending the current node's value to the `direction`.
c. Print "Visiting [current_node.value] from [direction]" for visualization.
d. If the value of `current_node` is equal to the `goal`:
- Return the `path` as the path to the goal node.
e. Mark `current_node` as visited by adding it to the `visited` set.
f. Iterate through each child `child` of `current_node`:
i). If `child` is not in the `visited` set:

Himanshu A20405220035
- Enqueue the tuple `(child, path)` into the queue to pass the updated path.
5. If the goal node is not found after processing all nodes in the queue, return False.

Program:
class Node: def
__init__(self, value):
self.value = value
self.children = [] def
bfs(root, goal): if
root is None:
return False queue
= [root] visited =
set() while queue:
current_node = queue.pop(0)
print("Visiting node:", current_node.value)
if current_node.value == goal:
return True
visited.add(current_node) for
child in current_node.children:
if child not in visited and child not in queue:
queue.append(child)
return False
A = Node("A")
B = Node("B")
C = Node("C")
D = Node("D")
E = Node("E")
F = Node("F")
G = Node("G")
H = Node("H")
I = Node("I")
J = Node("J")
A.children = [B, C, D]
B.children = [G, H]
Himanshu A20405220035
D.children = [E, F]
E.children = [I, J]
goal_node = "J" found =
bfs(A, goal_node) if
found:
print(f"Goal node '{goal_node}' found!") else:
print(f"Goal node '{goal_node}' not found.")

Output:
Visiting node: A
Visiting node: B
Visiting node: C
Visiting node: D
Visiting node: G
Visiting node: H
Visiting node: E
Visiting node: F
Visiting node: I Visiting
node: J
Goal node 'J' found!

1. (b): Depth First Search

Algorithm:
1.Initialize an empty stack to store nodes to be visited.
2.Initialize an empty set (or another data structure) to keep track of visited nodes.

Himanshu A20405220035
3.Push the starting node onto the stack and mark it as visited.
4.Enter a loop that continues until the stack is empty:
a. Pop a node from the stack. This node is the current node.
b. Print or process the current node.
c. If the current node's value is equal to the goal value, return True (goal found).
d. Otherwise, iterate through the children of the current node:
e. For each unvisited child node, mark it as visited, and push it onto the stack.
5.If the loop completes without finding the goal node, return False (goal not found).

Program:
class Node: def
__init__(self, value):
self.value = value
self.children = [] def
dfs(node, goal, visited):
print("Visiting node:", node.value)
if node.value == goal:
return True
visited.add(node) for
child in node.children:
if child not in visited:
found = dfs(child, goal, visited)
if found:
return True

return False
A = Node("A")
B = Node("B")
C = Node("C")
D = Node("D")
E = Node("E")
F = Node("F")
G = Node("G")
H = Node("H")
Himanshu A20405220035
I = Node("I")
J = Node("J")
A.children = [B, C, D]
B.children = [G, H]
D.children = [E, F] E.children = [I, J]
goal_node = "J" visited_nodes = set()
found = dfs(A, goal_node, visited_nodes)
if found:
print(f"Goal node '{goal_node}' found!") else:
print(f"Goal node '{goal_node}' not found.")

Output:
Visiting node: A
Visiting node: B
Visiting node: G
Visiting node: H
Visiting node: C
Visiting node: D Visiting
node: E
Visiting node: I
Visiting node: J
Goal node 'J' found!

1. (c): Depth Limit Search

Himanshu A20405220035
Algorithm:
1. Initialize a stack `stack` to store nodes to be visited.
2. Push a tuple `(node, 0)` onto the stack, where the second element is the depth (starting from 0).
3. While the stack is not empty:
a. Pop a node `current_node` and its associated `depth` from the stack.
b. Print "Visiting [current_node.value] at depth [depth]" for visualization.
c. If `current_node.value` is equal to `goal` and `depth` is less than or equal to `max_depth`:
- Return True (goal found within the maximum depth). d. If `depth` is less than
`max_depth`:
- Iterate through each child `child` of `current_node`:
i. Push a tuple `(child, depth + 1)` onto the stack to explore deeper.
4. If the loop completes without finding the goal node within the maximum depth, return False.

Program:
class Node:
def __init__(self, value):
self.value = value
self.children = [] def dls(node, goal,
limit, depth=0): print("Visiting
node:", node.value) if node.value
== goal:
return True
if depth >= limit:
return False for
child in
node.children:

found = dls(child, goal, limit, depth + 1)


if found: return True return False
A = Node("A")
B = Node("B")
C = Node("C")
D = Node("D")
E = Node("E")

Himanshu A20405220035
F = Node("F")
G = Node("G")
H = Node("H")
I = Node("I")
J = Node("J")
A.children = [B, C, D]
B.children = [G, H]
D.children = [E, F]
E.children = [I, J]
goal_node = "J" depth_limit
=3
found = dls(A, goal_node, depth_limit) if
found:
print(f"Goal node '{goal_node}' found within depth limit {depth_limit}!") else:
print(f"Goal node '{goal_node}' not found within depth limit {depth_limit}.")

Output:
Visiting node: A
Visiting node: B
Visiting node: G
Visiting node: H
Visiting node: C
Visiting node: D
Visiting node: E
Visiting node: I
Visiting node: J

Goal node 'J' found within depth limit 3!

Himanshu A20405220035
2. (a): Breadth First Search with User Input
Algorithm:
1. Initialize an empty queue `queue`.
2. Initialize an empty set `visited` to keep track of visited nodes.
3. Enqueue the tuple `(root, "")` into the queue, where the second element of the tuple is an empty string
representing the direction from the parent node.
4. While the queue is not empty:
a. Dequeue a node `current_node` and its associated `direcion` from the queue.
b. Construct the `path` by appending the current node's value to the `direction`.
c. Print "Visiting [current_node.value] from [direction]" for visualization.
d. If the value of `current_node` is equal to the `goal`:
- Return the `path` as the path to the goal node.
e. Mark `current_node` as visited by adding it to the `visited` set.
f. Iterate through each child `child` of `current_node`:
i). If `child` is not in the `visited` set:
- Enqueue the tuple `(child, path)` into the queue to pass the updated path.
5. If the goal node is not found after processing all nodes in the queue, return False.

Program:
class Node: def
__init__(self, value):
self.value = value
self.children = [] def
bfs(root, goal): if
root is None:
return False queue
= [root] visited =
set() while queue:
current_node = queue.pop(0)
print("Visiting node:", current_node.value)
if current_node.value == goal:
return True

Himanshu A20405220035
visited.add(current_node) for child in
current_node.children: if child not in visited
and child not in queue:
queue.append(child)
return False nodes = {}
root_node = input("Enter the root node: ")
root = Node(root_node) nodes[root_node]
= root while True:
parent_node = input(f"Enter parent node (or 'done' to finish): ")
if parent_node == 'done':
break
child_nodes = input(f"Enter child nodes for {parent_node} (comma-separated): ").split(',')
if parent_node not in nodes:
nodes[parent_node] = Node(parent_node)
for child_node in child_nodes: if
child_node not in nodes:
nodes[child_node] = Node(child_node)
nodes[parent_node].children.append(nodes[child_node])
goal_node = input("Enter the goal node: ") found = bfs(root,
goal_node) if found:
print(f"Goal node '{goal_node}' found!") else:
print(f"Goal node '{goal_node}' not found.")

Output:
Enter the root node: A
Enter parent node (or 'done' to finish): A
Enter child nodes for A (comma-separated): B,C,D
Enter parent node (or 'done' to finish): B
Enter child nodes for B (comma-separated): G,H
Enter parent node (or 'done' to finish): D
Enter child nodes for D (comma-separated): E,F
Enter parent node (or 'done' to finish): E
Enter child nodes for E (comma-separated): I,J
Himanshu A20405220035
Enter parent node (or 'done' to finish): done
Enter the goal node: J
Visiting node: A
Visiting node: B
Visiting node: C
Visiting node: D
Visiting node: G
Visiting node: H
Visiting node: E
Visiting node: F
Visiting node: I
Visiting node: J Goal
node 'J' found!

2. (b): Depth First Search with User input Algorithm:


1.Initialize an empty stack to store nodes to be visited.
2.Initialize an empty set (or another data structure) to keep track of visited nodes.
3.Push the starting node onto the stack and mark it as visited.
4.Enter a loop that continues until the stack is empty:
a. Pop a node from the stack. This node is the current node.
b. Print or process the current node.
c. If the current node's value is equal to the goal value, return True (goal found).
d. Otherwise, iterate through the children of the current node:
e. For each unvisited child node, mark it as visited, and push it onto the stack.
5.If the loop completes without finding the goal node, return False (goal not found).

Program:
class Node: def
__init__(self, value):
self.value = value
self.children = [] def
dfs(node, goal, visited):
print("Visiting node:", node.value)
if node.value == goal:

Himanshu A20405220035
return True
visited.add(node) for
child in node.children:
if child not in visited:
found = dfs(child, goal, visited)
if found: return True
return False nodes = {}
root_node = input("Enter the root node: ")
root = Node(root_node) nodes[root_node]
= root while True:
parent_node = input(f"Enter parent node (or 'done' to finish): ")
if parent_node == 'done':
break
child_nodes = input(f"Enter child nodes for {parent_node} (comma-separated): ").split(',')
if parent_node not in nodes:
nodes[parent_node] = Node(parent_node)
for child_node in child_nodes: if
child_node not in nodes:
nodes[child_node] = Node(child_node)
nodes[parent_node].children.append(nodes[child_node])
goal_node = input("Enter the goal node: ") visited_nodes =
set()
found = dfs(root, goal_node, visited_nodes) if
found:
print(f"Goal node '{goal_node}' found!")
else:
print(f"Goal node '{goal_node}' not found.")

Output:
Enter the root node: A
Enter parent node (or 'done' to finish): A
Enter child nodes for A (comma-separated): B,C,D
Enter parent node (or 'done' to finish): B
Himanshu A20405220035
Enter child nodes for B (comma-separated): G,H
Enter parent node (or 'done' to finish): D
Enter child nodes for D (comma-separated): E,F
Enter parent node (or 'done' to finish): E
Enter child nodes for E (comma-separated): I,J
Enter parent node (or 'done' to finish): done
Enter the goal node: J
Visiting node: A
Visiting node: B
Visiting node: G
Visiting node: H
Visiting node: C
Visiting node: D
Visiting node: E
Visiting node: I
Visiting node: J Goal
node 'J' found!

2. (c): Depth Limit Search with User input


Algorithm:
1. Initialize a stack `stack` to store nodes to be visited.
2. Push a tuple `(node, 0)` onto the stack, where the second element is the depth (starting from 0).
3. While the stack is not empty:
a. Pop a node `current_node` and its associated `depth` from the stack.
b. Print "Visiting [current_node.value] at depth [depth]" for visualization.
c. If `current_node.value` is equal to `goal` and `depth` is less than or equal to `max_depth`:
- Return True (goal found within the maximum depth). d. If `depth` is less than
`max_depth`:
- Iterate through each child `child` of `current_node`:
i. Push a tuple `(child, depth + 1)` onto the stack to explore deeper.
4. If the loop completes without finding the goal node within the maximum depth, return False.

Program:
class Node: def

__init__(self, value):
Himanshu A20405220035
self.value = value

self.children = [] def dls(node, goal,

limit, depth=0): print("Visiting

node:", node.value) if node.value

== goal:

return True if depth

>= limit: return False

for child in node.children:

found = dls(child, goal, limit, depth + 1)

if found: return True return False

nodes = {}

root_node = input("Enter the root node: ")

root = Node(root_node) nodes[root_node]

= root while True:

parent_node = input(f"Enter parent node (or 'done' to finish): ") if parent_node ==

'done': break child_nodes = input(f"Enter child nodes for {parent_node} (comma-

separated): ").split(',') if parent_node not in nodes:

nodes[parent_node] = Node(parent_node)

for child_node in child_nodes: if

child_node not in nodes:

nodes[child_node] = Node(child_node)

nodes[parent_node].children.append(nodes[child_node])

goal_node = input("Enter the goal node: ") depth_limit =

int(input("Enter the depth limit for DLS: ")) found = dls(root,

goal_node, depth_limit) if found:

print(f"Goal node '{goal_node}' found within depth limit {depth_limit}!")

else: print(f"Goal node '{goal_node}' not found within depth limit

{depth_limit}.")

Output:
Enter the root node: A
Himanshu A20405220035
Enter parent node (or 'done' to finish): A
Enter child nodes for A (comma-separated): B,C,D
Enter parent node (or 'done' to finish): B
Enter child nodes for B (comma-separated): G,H
Enter parent node (or 'done' to finish): D
Enter child nodes for D (comma-separated): E,F
Enter parent node (or 'done' to finish): E
Enter child nodes for E (comma-separated): I,J
Enter parent node (or 'done' to finish): done
Enter the goal node: J
Enter the depth limit for DLS: 3
Visiting node: A
Visiting node: B
Visiting node: G
Visiting node: H
Visiting node: C
Visiting node: D
Visiting node: E
Visiting node: I
Visiting node: J
Goal node 'J' found within depth limit 3!

Himanshu A20405220035

You might also like