AI lab
AI lab
1.Basic Implementation of Breadth First Search and Depth First Search using Python
BFS
queue = deque([start])
visited = set()
traversal_order = []
visited.add(start)
while queue:
node = queue.popleft()
traversal_order.append(node)
visited.add(neighbor)
queue.append(neighbor)
return traversal_order
# Example usage
if __name__ == "__main__":
graph = {
'D': ['B'],
start_node = 'A'
OUTPUT
BFS Traversal Order: ['A', 'B', 'C', 'D', 'E', 'F']
DFS
if visited is None:
visited = set() # Initialize the visited set if not provided
print(start) # Process the current node (here we are just printing it)
return visited
# Example usage
graph = {
'D': ['B'],
dfs(graph, 'A')
OUTPUT
A
B
D
E
F
C
3.Implementation of constraint satisfaction problem using python
def solutions():
all_solutions = list()
if len(set([s, e, n, d, m, o, r, y])) == 8:
return all_solutions
print(solutions())
OUTPUT
[(9567, 1085, 10652)]
4.Basic implementation of missionaries-Cannibals problems using python
def is_valid_state(state):
m_right = 3 - m_left
c_right = 3 - c_left
return False
if (m_left > 0 and m_left < c_left) or (m_right > 0 and m_right < c_right):
return False
return True
def get_neighbors(state):
neighbors = []
possible_moves = [(-1, 0), (-2, 0), (0, -1), (0, -2), (-1, -1)]
possible_moves = [(1, 0), (2, 0), (0, 1), (0, 2), (1, 1)]
if is_valid_state(new_state):
neighbors.append(new_state)
return neighbors
def bfs():
visited = set()
visited.add(initial_state)
while queue:
if current_state == goal_state:
visited.add(neighbor)
return None
# Run BFS to find the solution
solution = bfs()
if solution:
print("Solution found:")
print(state)
else:
OUTPUT
(3, 3, 1)
(3, 1, 0)
(3, 2, 1)
(3, 0, 0)
(3, 1, 1)
(1, 1, 0)
(2, 2, 1)
(0, 2, 0)
(0, 3, 1)
(0, 1, 0)
(1, 1, 1)
(0, 0, 0)
jug1_capacity = 4
jug2_capacity = 3
target_volume = 2
# Initial state with both jugs empty
initial_state = (0, 0)
def is_valid_state(state):
return 0 <= jug1 <= jug1_capacity and 0 <= jug2 <= jug2_capacity
if from_jug == 1:
else:
return None
def water_jug_dfs():
visited = set()
stack = deque()
stack.append((initial_state, [])) # State and actions taken
while stack:
return actions
visited.add(current_state)
for action in ["Fill Jug 1", "Fill Jug 2", "Empty Jug 1", "Empty Jug 2", "Pour Jug 1 to Jug 2", "Pour Jug 2
to Jug 1"]:
new_state = None
new_state = (current_state[0], 0)
stack.append((new_state, new_actions))
return None
solution = water_jug_dfs()
if solution:
print("Solution Found:")
else:
OUTPUT
Solution Found:
Step 1: Fill Jug 2
Step 2: Pour Jug 2 to Jug 1
Step 3: Fill Jug 2
Step 4: Pour Jug 2 to Jug 1
def tictactoe_grid(value):
print("\n")
print("\t | |")
print('\t_____|_____|_____')
print('\t_____|_____|____')
print("\t | |")
print('\t | |')
print("\n")
tictactoe_grid("xoxxxooox")
OUTPUT
8. Basic implementation of hill climbing using python
import random
def objective_function(x):
if initial_solution is None:
current_solution = random.uniform(-1, 1)
else:
current_solution = initial_solution
current_value = objective_function(current_solution)
new_value = objective_function(new_solution)
current_value = new_value
# Print progress
OUTPUT
Iteration 1: x = 1.166075167803011, f(x) = 3.498225503409033
Iteration 2: x = 1.6532551301231317, f(x) = 4.959765390369396
Iteration 3: x = 1.6532551301231317, f(x) = 4.959765390369396
Iteration 4: x = 1.9915037465335874, f(x) = 5.974511239600762
Iteration 5: x = 1.9915037465335874, f(x) = 5.974511239600762
Iteration 6: x = 2.78210045266811, f(x) = 8.34630135800433
Iteration 7: x = 4.487500683318849, f(x) = 13.462502049956548
Iteration 8: x = 6.47754102348248, f(x) = 19.43262307044744
Iteration 9: x = 6.47754102348248, f(x) = 19.43262307044744
Iteration 10: x = 6.703137229175036, f(x) = 20.109411687525107
import math
# Each node contains either a value (for leaf nodes) or None (for internal nodes)
game_tree = {
if isinstance(tree[node], int):
return tree[node]
if maximizing_player:
max_eval = -math.inf
return max_eval
else:
min_eval = math.inf
return min_eval
OUTPUT
Best value: 6