Ai File
Ai File
On
ARTIFICIAL INTELLIGENCE
Bachelor of Technology
Computer Science
EXPERIMENT 1
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}
visited = [] # List to keep track of visited nodes.
queue = [] #Initialize a queue
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print (s, end = " ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
# Driver Code
bfs(visited, graph, 'A')
Output:-
A B C D E F
EXPERIMENT 2
Output:-
A
B
D
E
F
C
EXPERIMENT 3
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 x in range(len(board)):
win = True
for y in range(len(board)):
if board[y][x] != player:
win = False
continue
if win == True:
return(win)
return(win)
# Checks whether the player has three # of
their marks in a diagonal row
def diag_win(board, player):
win = True
y=0
for x in range(len(board)):
if board[x, x] != player:
win = False
if win:
return win
win = True
if win:
for x in range(len(board)):
y = len(board) - 1 - x
if board[x, y] != player:
win = False
return win
col_win(board,player) or
diag_win(board,player)):
winner = player
# Driver Code
print("Winner is: " + str(play_game()))
Output:-
[[000]
[000]
[000]]
Board after 1 move
[[000]
[000]
[100]]
Board after 2 move
[[000]
[020]
[100]]
Board after 3 move
[[010]
[020]
[100]]
Board after 4 move
[[010]
[220]
[100]]
Board after 5 move
[[110]
[220]
[100]]
Board after 6 move
[[110]
[220]
[120]]
Board after 7 move
[[110]
[220]
[121]]
Board after 8 move
[[1 1 0]
[2 2 2]
[1 2 1]]
Winner is: 2
EXPERIMENT 4
class Solution:
def solve(self, board):
dict = {}
flatten = []
for i in range(len(board)):
flatten += board[i]
flatten = tuple(flatten)
dict[flatten] = 0
return self.get_paths(dict)
results = []
pos_0 = node.index(0)
for move in moves[pos_0]:
new_node = list(node)
new_node[move], new_node[pos_0] = new_node[pos_0], new_node[move]
results.append(tuple(new_node))
return results
ob = Solution()
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0]
]
print(ob.solve(matrix))
Output:-
4
EXPERIMENT 5
.
if (amt1 == aim and amt2 == 0) or (amt2 == aim and amt1 == 0):
print(amt1, amt2)
return True
print("Steps: ")
waterJugSolver(0, 0)
Output:-
Steps:
00
40
43
03
30
33
4 2
0 2
EXPERIMENT 6
# Mark as visited
v[i] = True
tsp(graph, v, i, n, count + 1,
cost + graph[currPos][i])
# Driver code
# n is the number of nodes i.e. V
if name == 'main':
n=4
graph= [[ 0, 10, 15, 20 ],
[ 10, 0, 35, 25 ],
[ 15, 35, 0, 30 ],
[ 20, 25, 30, 0 ]]
Output:-
80
EXPERIMENT 7
# Driver code
n=4
TowerOfHanoi(n,'A','B','C')
# A, C, B are the name of rods
Output:-
Movedisk1fromrodAtorodB
Movedisk2fromrodAtorodC
Movedisk1fromrodBtorodC
Movedisk3fromrodAtorodB
Movedisk1fromrodCtorodA
Movedisk2fromrodCtorodB
Movedisk1fromrodAtorodB
Movedisk4fromrodAtorodC
Movedisk1fromrodBtorodC
Movedisk2fromrodBtorodA
Movedisk1fromrodCtorodA
Movedisk3fromrodBtorodC
Movedisk1fromrodAtorodB
Movedisk2fromrodAtorodC
Movedisk1fromrodBtorodC
EXPERIMENT 8
'''
Python programming implementation of monkey picking banana problem '''
#Global Variable i
i=0
def Monkey_go_box(x,y):
global i
i=i+1
print('step:',i,'monkey slave',x,'Go to'+y)
def Monkey_move_box(x,y):
global i
i=i+1
print('step:', i, 'monkey take the box from', x, 'deliver to' + y)
def Monkey_on_box():
global i
i=i+1
print('step:', i, 'Monkey climbs up the box')
def Monkey_get_banana():
global i
i=i+1
print('step:', i, 'Monkey picked a banana')
import sys
#Read the input operating parameters,
codeIn=sys.stdin.read()
codeInList=codeIn.split()
#The operating parameters indicate the
locations of monkey, banana, and box
respectively.
monkey=codeInList[0]
banana=codeInList[1]
box=codeInList[2]
print('The steps are as follows:')
#Please use the least steps to complete the monkey picking banana task
Monkey_go_box(monkey, box)
Monkey_move_box(box, banana)
Monkey_on_box()
Monkey_get_banana()
EXPERIMENT 9
if maximizingPlayer:
best = MIN
# Recur for left and right children
for i in range(0, 2):
# Alpha Beta
Pruning
if beta <= alpha:
break
return best
else:
best =
MAX
# Alpha Beta
Pruning
if beta <= alpha:
break
return best
# Driver Code
if name == "main":
Output:-
global
N
N=4
def printSolution(board):
for i in range(N):
for j in range(N):
print board[i]
[j],
print
return True
def solveNQUtil(board, col):
# base case: If all queens are placed
# then return true
if col >= N:
return True
for i in range(N):
if isSafe(board, i, col):
# Place this queen in board[i][col]
board[i][col] = 1
board[i][col] = 0
return False
def solveNQ():
board = [ [0,0,0,0],
[0,0,0,0],
[0,0,0,0]
[0,0,0,0]
if solveNQUtil(board, 0) == False:
print "Solution does not exist"
return False
printSolution(board)
return True # driver program to test
above function
solveNQ()
Output:-
0010
1000
0001
0100