R22-AI Lab Manual
R22-AI Lab Manual
LAB MANUAL
2
1). Write a program to implement Breadth first search using Python
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5') # function calling
Output:
Following is the Breadth-First Search
5 3 7 2 4 8
3
2). Write a program to implement Depth first search using Python
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
Output:
Following is the Depth-First Search
5 3 2 4 8 7
4
3). Write a program to implement Tic-Toc-Toe using Python
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)
5
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)
6
# Evaluates whether there is
# a winner or a tie
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()))
7
Output:
[[0 0 0]
[0 0 0]
[0 0 0]]
Board after 1 move
[[0 0 0]
[0 0 0]
[1 0 0]]
Board after 2 move
[[0 0 0]
[0 2 0]
[1 0 0]]
Board after 3 move
[[0 1 0]
[0 2 0]
[1 0 0]]
Board after 4 move
[[0 1 0]
[2 2 0]
[1 0 0]]
Board after 5 move
[[1 1 0]
[2 2 0]
[1 0 0]]
Board after 6 move
[[1 1 0]
[2 2 0]
[1 2 0]]
Board after 7 move
[[1 1 0]
[2 2 0]
[1 2 1]]
Board after 8 move
[[1 1 0]
[2 2 2]
[1 2 1]]
Winner is: 2
8
4). Write a program to implement 8-Puzzle problem using Python
9
def push(self, key):
heappush(self.heap, key)
10
self.costs = costs
count = 0
for i in range(n):
for j in range(n):
if ((mats[i][j]) and
(mats[i][j] != final[i][j])):
count += 1
return count
11
x2 = new_empty_tile_posi[0]
y2 = new_empty_tile_posi[1]
new_mats[x1][y1], new_mats[x2][y2] = new_mats[x2][y2], new_mats[x1][y1]
for i in range(n):
for j in range(n):
print("%d " % (mats[i][j]), end = " ")
print()
# Printing the path from the root node to the final node
def printPath(root):
if root == None:
return
printPath(root.parent)
printMatsrix(root.mats)
12
print()
13
# destination;
printPath(minimum)
return
if isSafe(new_tile_posi[0], new_tile_posi[1]):
# Main Code
# Initial configuration
# Value 0 is taken here as an empty space
initial = [ [ 1, 2, 3 ],
[ 5, 6, 0 ],
[ 7, 8, 4 ] ]
14
[ 0, 7, 4 ] ]
Output:
1 2 3
5 6 0
7 8 4
1 2 3
5 0 6
7 8 4
1 2 3
5 8 6
7 0 4
1 2 3
5 8 6
0 7 4
15
5). Write a program to implement Water-Jug problem using Python
q = deque()
# Current state
u = q.popleft()
if ((u[0], u[1]) in m):
continue
if ((u[0] > a or u[1] > b or
u[0] < 0 or u[1] < 0)):
continue
path.append([u[0], u[1]])
m[(u[0], u[1])] = 1
if (u[0] == target):
if (u[1] != 0):
path.append([u[0], 0])
else:
if (u[0] != 0):
path.append([0, u[1]])
sz = len(path)
for i in range(sz):
print("(", path[i][0], ",",
path[i][1], ")")
break
16
for ap in range(max(a, b) + 1):
c = u[0] + ap
d = u[1] - ap
c = u[0] - ap
d = u[1] + ap
q.append([a, 0])
q.append([0, b])
if (not isSolvable):
print("Solution not possible")
if __name__ == '__main__':
Output:
17
6). Write a program to implement Travelling Salesman problem using Python
Output:
80
18
7). Write a program to implement Tower of Hanoi using Python
# Driver code
N=3
Output:
19
8). Write a program to implement Monkey Banana Problem using Python
import random
class Monkey:
def __init__(self, bananas):
self.bananas = bananas
def __repr__(self):
return "Monkey with %d bananas." % self.bananas
def number_of_bananas(monkey):
"""Returns number of bananas that monkey has."""
return monkey.bananas
Output:
Random monkeys:
[Monkey with 2 bananas., Monkey with 39 bananas., Monkey with 15 bananas., Monkey with 26
bananas., Monkey with 10 bananas.]
20
9). Write a program to implement Alpha-Beta Pruning using Python
if maximizingPlayer:
best = MIN
return best
else:
best = MAX
21
beta = min(beta, best)
return best
# Driver Code
if __name__ == "__main__":
Output:
22
10). Write a program to implement 8-Queens Problem using Python
23
board = [[0 for x in range(N)] for y in range(N)]
if not solveNQueens(board, 0):
print("No solution found")
Output:
24