Ai Lab
Ai Lab
COURSE OBJECTIVES:
COURSE OUTCOMES:
import random
def objective_function(x):
def hill_climbing():
current_value = objective_function(current_solution)
while True:
next_value = objective_function(next_solution)
break
Output:
# Base case: If only one disk, move it directly from source to destination
if n == 1:
return
# Move n-1 disks from source to auxiliary, so they are out of the way
# Number of disks
Output:
if (m_left > 0 and m_left < c_left) or (m_right > 0 and m_right < c_right):
return False
return False
return True
print(f"Left side: {m_left} missionaries, {c_left} cannibals | Right side: {m_right} missionaries,
{c_right} cannibals | Boat: {boat}")
# Base case: All missionaries and cannibals have been moved to the right side
print("\nSolution found!")
print(move)
return True
if boat == 'left':
# Try all valid moves from the left side to the right side
new_m_left = m_left - m
new_c_left = c_left - c
new_m_right = m_right + m
new_c_right = c_right + c
# Backtrack
moves.pop()
else:
# Try all valid moves from the right side to the left side
new_m_left = m_left + m
new_c_left = c_left + c
new_m_right = m_right - m
new_c_right = c_right - c
return True
# Backtrack
moves.pop()
return False
def missionaries_and_cannibals():
missionaries_and_cannibals()
Output:
Left side: 3 missionaries, 3 cannibals | Right side: 0 missionaries, 0 cannibals | Boat: left
Left side: 2 missionaries, 2 cannibals | Right side: 1 missionaries, 1 cannibals | Boat: right
Left side: 4 missionaries, 2 cannibals | Right side: 1 missionaries, 1 cannibals | Boat: left
Move and
for i in range(row):
if board[i] == col or \
return False
return True
if row == n:
print_board(board, n)
return True
res = False
board[row] = col
return res
for i in range(n):
print(' '.join(row))
print("\n")
def eight_queens(n=8):
# Initialize the board with -1 (meaning no queen is placed)
board = [-1] * n
eight_queens()
Output:
Q.......
....Q...
......Q.
.Q......
...Q....
.....Q..
..Q.....
.......Q
import heapq
MOVES = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Up, Down, Left, Right
class Node:
return self.f < other.f # For comparison in the priority queue (min-heap)
# Start node with g=0 and h as the heuristic from start to goal
while open_list:
current_node = heapq.heappop(open_list)
if current_node.position == goal:
path = []
while current_node:
path.append(current_node.position)
current_node = current_node.parent
# Explore neighbors
if (0 <= neighbor_pos[0] < len(grid)) and (0 <= neighbor_pos[1] < len(grid[0])) and
grid[neighbor_pos[0]][neighbor_pos[1]] != 1:
if neighbor_pos in closed_list:
continue
h = heuristic(neighbor_pos, goal)
neighbor_node = Node(neighbor_pos, g, h)
# If the neighbor is not in the open list, or has a lower f-value, add it to the open
list
neighbor_node.parent = current_node
heapq.heappush(open_list, neighbor_node)
[0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0]
# Run A* algorithm
if path:
print("Path found:")
print(path)
else:
Output:
Path found:
[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]
visited = set()
queue = deque([start])
visited.add(start)
bfs_order = []
while queue:
node = queue.popleft()
bfs_order.append(node)
queue.append(neighbor)
visited.add(neighbor)
return bfs_order
graph = {
0: [1, 2],
1: [0, 3, 4],
2: [0, 4],
3: [1],
4: [1, 2]
start_node = 0
Output:
Graph:
0 -- 1 -- 3
| |
2 -- 4
Start node: 0
dfs_order = []
while stack:
stack.append(neighbor)
return dfs_order
graph = {
0: [1, 2],
1: [0, 3, 4],
2: [0, 4],
3: [1],
4: [1, 2]
start_node = 0
if visited is None:
if dfs_order is None:
visited.add(node)
dfs_order.append(node)
return dfs_order
# Perform DFS (recursive)
Output:
Graph:
0 -- 1 -- 3
| |
2 -- 4
Start node: 0
siblings = {
("Alice", "Bob"),
("Bob", "Alice"),
("Charlie", "David"),
("David", "Charlie")
}
parents = {
("Alice", "Charlie"),
("Alice", "David"),
("Bob", "Charlie"),
("Bob", "David")
return True
return False
# Query predicates
def main():
if __name__ == "__main__":
main()
Output