AI Artificial Intelligence
AI Artificial Intelligence
Session 2024-25
Submitted to:
Mr. Ajeet Singh
(Assistant Professor)
Mission
M1: To impart high quality education in the science of computing.
M2: To prepare educated and skilled computer professionals.
M3: To create excellence by providing comprehensive knowledge of the
latest tools and technologies in the domain of computer science, so that
students strive to become leaders.
M4: To inculcate ethical values in students so that they understand their
responsibility towards the nation with focus on upliftment of all sections of
society.
M5: To facilitate establishment of research centers and encourage students
to solve complex technological problems.
Program Educational Objectives
PEO1: Make valuable contributions to design, development and production
in the practice of computer science and engineering in related engineering
areas or application areas, and at the interface of computers and physical
systems.
PSO2: Ability to design and develop software for web based and mobiles
androids under real world environment.
while queue:
vertex = queue.popleft()
print(vertex, end=" ")
# Example usage:
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
bfs(graph, 'A')
Experiment No: 02
Write a program to implement Depth First Search using python.
while stack:
vertex = stack.pop() # Get the last node from the stack
if vertex not in visited:
print(vertex, end=" ") # Print the visited node
visited.add(vertex) # Mark the node as visited
stack.extend(graph[vertex] - visited) # Add unvisited neighbors to the stack
def create_board():
return(np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
def possibilities(board):
l = []
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return(l)
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
for y in range(len(board)):
if board[y][x] != player:
win = False
continue
if win == True:
return(win)
return(win)
def evaluate(board):
winner = 0
winner = player
def play_game():
board, winner, counter = create_board(), 0, 1
print(board)
sleep(2)
while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + " move")
print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return(winner)
# Driver Code
print("Winner is: " + str(play_game()))
Experiment No: 04
Write a program to implement 8 Puzzle problem using python.
# Constructor to initialize a
# Priority Queue
def __init__(self):
self.heap = []
# Node structure
class node:
count = 0
for i in range(n):
for j in range(n):
if ((mat[i][j]) and
(mat[i][j] != final[i][j])):
count += 1
return count
for i in range(n):
for j in range(n):
print("%d " % (mat[i][j]), end = " ")
print()
if root == None:
return
printPath(root.parent)
printMatrix(root.mat)
print()
if isSafe(new_tile_pos[0], new_tile_pos[1]):
# Driver Code
# Initial configuration
# Value 0 is used for empty space
initial = [ [ 1, 2, 3 ],
[ 5, 6, 0 ],
[ 7, 8, 4 ] ]
while q:
jug1, jug2, steps = q.popleft()
if jug1 == d or jug2 == d:
return steps
# 1: Fill jug1
if not visited[m][jug2]:
visited[m][jug2] = True
q.append((m, jug2, steps + 1))
# 2: Fill jug2
if not visited[jug1][n]:
visited[jug1][n] = True
q.append((jug1, n, steps + 1))
# 3: Empty jug1
if not visited[0][jug2]:
visited[0][jug2] = True
q.append((0, jug2, steps + 1))
# 4: Empty jug2
if not visited[jug1][0]:
visited[jug1][0] = True
q.append((jug1, 0, steps + 1))
return -1
if __name__ == "__main__":
Output:
Steps:
0 0
4 0
4 3
0 3
3 0
3 3
4 2
0 2
Experiment No: 06
Write a program to implement Travelling Salesman problem using python.
# memoization
if memo[i][mask] != -1:
return memo[i][mask]
# we have to travel all nodes j in mask and end the path at ith node
# so for every node j in mask, recursively calculate cost of
# travelling all nodes in mask
# except i and then travel back from node j to node i taking
# the shortest path take the minimum of all possible j nodes
for j in range(1, n+1):
if (mask & (1 << j)) != 0 and j != i and j != 1:
res = min(res, fun(j, mask & (~(1 << i))) + dist[j][i])
memo[i][mask] = res # storing the minimum value
return res
# Driver program to test above logic
ans = 10**9
for i in range(1, n+1):
# try to go from node 1 visiting all nodes in between to i
# then return from i taking the shortest route to 1
ans = min(ans, fun(i, (1 << (n+1))-1) + dist[i][1])
# Driver code
n=4
TowerOfHanoi(n,'A','B','C')
# A, C, B are the name of rods
Experiment No: 08
Write a program to implement Monkey Banana problem using python.
class State:
def __init__(self, monkey, box, banana):
self.monkey = monkey # Position of the monkey
self.box = box # Position of the box
self.banana = banana # Position of the banana
def __str__(self):
return f"Monkey: {self.monkey}, Box: {self.box}, Banana: {self.banana}"
def push_box(state):
if not state.box and not state.monkey:
return State(state.monkey, True, state.banana)
return state
def climb_box(state):
if state.box and not state.monkey:
return State(True, state.box, state.banana)
return state
def grab_banana(state):
if state.monkey and state.banana:
print("Banana grabbed!")
return State(state.monkey, state.box, True)
return state
def monkey_banana_problem():
initial_state = State(False, False, False)
print("Initial State:", initial_state)
state = push_box(initial_state)
print("After pushing the box:", state)
state = climb_box(state)
print("After climbing the box:", state)
state = grab_banana(state)
if __name__ == "__main__":
monkey_banana_problem()
Experiment No: 09
Write a program to implement Alpha-Beta pruning using python.
# Python3 program to demonstrate
# working of Alpha-Beta Pruning
if maximizingPlayer:
best = MIN
return best
else:
best = MAX
# Recur for left and
# right children
for i in range(0, 2):
return best
# Driver Code
if __name__ == "__main__":
global N
N=4
def printSolution(board):
for i in range(N):
for j in range(N):
print (board[i][j],end=' ')
print()
if isSafe(board, i, col):
# Place this queen in board[i][col]
board[i][col] = 1
if solveNQUtil(board, 0) == False:
print ("Solution does not exist")
return False
printSolution(board)
return True