0% found this document useful (0 votes)
71 views6 pages

A Lab Manual ON: Artificial Intelligence

This document contains a lab manual on Artificial Intelligence with 6 sections covering Python programs and functions for various AI concepts and problems: 1) Programs to find factorial and solve the Tower of Hanoi problem. 2) Functions to implement depth-first search and breadth-first search on a graph. 3) A program to solve the water jug problem using recursion. 4) Suggested functions to implement tic-tac-toe and the 8-puzzle problem including displaying the board, user input, and valid moves.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views6 pages

A Lab Manual ON: Artificial Intelligence

This document contains a lab manual on Artificial Intelligence with 6 sections covering Python programs and functions for various AI concepts and problems: 1) Programs to find factorial and solve the Tower of Hanoi problem. 2) Functions to implement depth-first search and breadth-first search on a graph. 3) A program to solve the water jug problem using recursion. 4) Suggested functions to implement tic-tac-toe and the 8-puzzle problem including displaying the board, user input, and valid moves.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

A

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

SUBMITTED BY: INTERNAL GUIDE:-

Name:Patel Krishna M. Prof.Upasana Mehta

Enrollment No:175043693048

pg. 1
CERTIFICATE

This is to certify that the “ AI LAB Manual “ was prepared by

Name: Patel Krishna M. Enrollment No:175043693048

under my guidance for fulfillment of the degree of Master of Computer Applications


of Gujarat Technological University, Ahmedabad during the academic year 2016-
17.
I certify that this manual is his/her original work and not copied from other sources.

______________________
Internal Guide Signature

pg. 2
(1) W.A.P to find factorial Number.
Code:-
n=int(input("Enter number:"))
fact=1
while(n>0):
fact=fact*n
n=n-1
print("Factorial of the number is: ")
print(fact)
Output:-
Enter number:4
Factorial of the number is:
24

(2) Write a code in python to solve Tower of Hanoi Problem. Write a


function to input no of disk.

Code:-

def TowerOfHenoi(n,source,dest,aux):
if(n==1):
print ("Move Disk 1 From",source,"To",dest)
return
else:
TowerOfHenoi(n-1,source,aux,dest)
print("Move Disk",n,"From",source,"To",dest)
TowerOfHenoi(n-1,aux,dest,source)

n=int(input("Enter The Disk ="))


TowerOfHenoi(n,'A','C','B');

Output:-
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Move disk 3 from A to B
Move disk 1 from C to A
Move disk 2 from C to B
Move disk 1 from A to B
Move disk 4 from A to C
Move disk 1 from B to C
Move disk 2 from B to A
Move disk 1 from C to A
Move disk 3 from B to C
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C

pg. 3
(3) WAP to implement DFS.
Code:-
def DFS(graph,start):
open, close =[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

graph = {1: [2, 3], 2: [4, 5],


3: [5], 4: [6], 5: [6],
6: [7], 7: []}

print(DFS(graph, 1))
# [1, 3, 5, 6, 7, 2, 4]
Output:-

(4) WAP to implement BFS.


Code:-
def BFS(graph,start):
open, close =[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

graph = {1: [2, 3], 2: [4, 5],


3: [5], 4: [6], 5: [6],
6: [7], 7: []}

print(BFS(graph, 1))
# [1, 2, 3, 4, 5, 6, 7]
Output:-

pg. 4
(5) WAP to implement water jug problem.
Code:-
capacity=(4,3)
x=capacity[0]
y=capacity[1]

#final path
ans=[]

#to mark visites states


memory={}

def waterjug(stat):

#Let the 2 jugs be called a,b


a=stat[0]
b=stat[1]

if(a==2 and b==0):


print("Goal is achived")
print("In Goal State a=%d b=%d"%(a,b))
#print("Total Setps : %d"%(i))
ans.append(stat)
return True
# if current state is already visited earlier

if((a,b) in memory):
return False

ans.append(stat) #add state element in ans[]


memory[(a,b)]=1

if(b>0):
#empty b into a
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

pg. 5
elif(a==0):
if(waterjug((x,b))):
return True
return False
init=(0,0)
waterjug(init)
for i in ans:
print (i)
Output:-

(6)

(1)Write following functions to implement tic-tac-toe game for two players in


python.
(1)Display board(3)
(2)Userinput(3)
(3)Necessary Condition for win/draw(6)

(2)Write the following functions required to solve 8-puzzle problem in python.


a.Display board(3)
b.Left and Right move(3)
c.Up and Down move(3)
4. Append node(3)

pg. 6

You might also like