Professional Documents
Culture Documents
Master of Computer Application (5 Sem) : A Lab Manual ON
Master of Computer Application (5 Sem) : A Lab Manual ON
LAB MANUAL
ON
Artificial Intelligence
In fulfillment towards the award of the degree of
Master of Computer Application(5th SEM)
Under
GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD.
2017-2018
______________________
Internal Guide Signature
1)Write a program to find the factorial of given number.
def recur_factorial(n):
"""Function to return the factorial
of a number using recursion"""
if n == 1:
return n
else:
return n*recur_factorial(n-1)
*****************************Output********************************
2)Write a code in python to solve Tower of Hanoi Problem. Write a function to input no of
disk.
def TowerOfHenoi(n,source,dest,aux):
if(n==1):
return
else:
TowerOfHenoi(n-1,source,aux,dest)
print("Move Disk",n,"From",source,"To",dest)
TowerOfHenoi(n-1,aux,dest,source)
TowerOfHenoi(n,'A','C','B')
************************************Output***********************************
3)Write a program to implement Depth First Search(DFS).
def DFS(graph,start):
##close=[]
while open:
node=open.pop()
if node in close:
continue
close.append(node)
for n in graph[node]:
open.append(n)
return close
6: [7], 7: []}
print(DFS(graph, 1))
# [1, 3, 5, 6, 7, 2, 4]
*************************************Output************************************
4)Write a program to implement Best First Search(BFS).
def BFS(graph,start):
##close=[]
while open:
node=open.pop()
if node in close:
continue
close.append(node)
for n in graph[node]:
open.insert(0,n)
return close
6: [7], 7: []}
print(BFS(graph, 1))
# [1, 2, 3, 4, 5, 6, 7]
**********************************Output****************************************
5)Write a program to implement water jug problem.
capacity=(4,3)
x=capacity[0]
y=capacity[1]
ans=[]
memory={}
def waterjug(stat):
a=stat[0]
b=stat[1]
print("Goal is achived")
ans.append(stat)
return True
if((a,b) in memory):
return False
ans.append(stat)
memory[(a,b)]=1
if(b>0):
if(a+b<=x):
if(waterjug((a+b,0))):
return True
else:
if(waterjug((x,b-(x-a)))):
return True
elif(b==0):
if(waterjug((a,y))):
return True
if(a>0):
if((a+b)<=y):
if(waterjug((0,a+b))):
return True
elif(a+b>=x):
if(waterjug((a-(y-b),y))):
return True
elif(a==x):
if(waterjug((0,b))):
return True
elif(a==0):
if(waterjug((x,b))):
return True
return False
init=(0,0)
waterjug(init)
for i in ans:
print (i)
*********************************Output**************************************
6)WAP to implement 8-puzzle problem using any of algorithm
import os
os.system("clear")
def printstat(stat):
print(stat[0],stat[1],stat[2])
print(stat[3],stat[4],stat[5])
print(stat[6],stat[7],stat[8])
print()
def move_up(stat):
new_node=stat[:]
print(new_node)
index=new_node.index(0)
print(index)
temp=new_node[index-3]
new_node[index-3]=new_node[index]
new_node[index]=temp
return new_node
else:
return None
def move_down(stat):
new_node=stat[:]
index=new_node.index(0)
temp=new_node[index+3]
new_node[index+3]=new_node[index]
new_node[index]=temp
return new_node
else:
return None
def move_left(stat):
new_node=stat[:]
index=new_node.index(0)
temp=new_node[index-1]
new_node[index-1]=new_node[index]
new_node[index]=temp
return new_node
else:
return None
def move_right(stat):
new_node=stat[:]
index=new_node.index(0)
temp=new_node[index+1]
new_node[index+1]=new_node[index]
new_node[index]=temp
return new_node
else:
return None
class Node :
def __init__(self,stat,parent,operator,depth):
self.stat=stat
self.parent=parent
self.operator=operator
self.depth=depth
def create_node(stat,parent,operator,depth):
return Node(stat,parent,operator,depth)
def expand_node(node):
expanded_node=[]
expanded_node.append(create_node(move_up(node.stat),node,"U",node.depth+1))
expanded_node.append(create_node(move_down(node.stat),node,"D",node.depth+1))
expanded_node.append(create_node(move_left(node.stat),node,"L",node.depth+1))
expanded_node.append(create_node(move_right(node.stat),node,"R",node.depth+1))
return expanded_node
def bfs(stat,goal):
nodes=[]
visited=[]
nodes.append(create_node(stat,None,None,0))
visited.append(create_node(stat,None,None,0))
while True:
temp=[]
if(len(nodes)==0):
return None
node=nodes.pop(0)
if(node.stat==goal):
move=[]
tmp=node
while True:
move.append(tmp.operator)
if(tmp.depth<=1):
break
tmp=tmp.parent
return move
temp.extend(expand_node(node))
nodes.insert(0,tempnode)
visited.append(tempnode)
def main():
init_stat=[1,2,3,8,4,0,7,6,5]
goal_stat=[1,2,3,8,0,4,7,6,5]
print("init stat")
printstat(init_stat)
move=bfs(init_stat,goal_stat)
print (move[::-1])
print("goal stat")
printstat(goal_stat)
main()
***********************************Output************************************