Vishvjit Singh
Vishvjit Singh
SESSION 2022-23
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING
J.K. INSTITUTE OF ENGINEERING, BILASPUR (C.G.)
SESSION 2022-23
Aim: Write a prolog program to find the rules for parent, child, male, female, son,
daughter, brother , sister, uncle, aunt, ancestor given the facts about father and wife only.
Rules:
We said earlier a predicate is defined by clauses, which may be facts or rules. A rule is no
more than a stored query. Its syntax is
head :- body.
where
head a predicate definition (just like a fact)
:- the neck symbol, sometimes read as "if"
body one or more goals (a query)
Production rules:
parents father and wife
aunt wife and uncle
Parse tree:
aunt(A,C)
wife uncle
Program:
predicates
1
clauses father(naresh,poonam). father(naresh,abhishek).
father(naresh,sanjay).
wife(kusum,naresh). male(abhishek). male(sanjay). female(poonam).
parents(F,M,C):- father(F,C),wife(M,F). child(C,F):-father(F,C). son(C,F):-
father(F,C),male(C). daughter(D,F):-father(F,D),female(D).
brother(B,C):-father(F,B),father(F,C),male(B), B<>C. sister(Y,C):-
father(F,Y),father(F,C),female(Y), Y<>C. uncle(U,C):-
brother(F,U),father(F,C),male(U).
aunt(A,C):-wife(A,U),uncle(U,C).
goal
son (Who,Whom).
Output:
Who=abhishek, Whom=naresh
Who=sanjay, Whom=naresh
2 Solutions
2
Experiment No. 2
Aim:
Write a prolog program to find length of given list.
Lists:
Lists are powerful data structures for holding and manipulating groups of things.
In Prolog, a list is simply a collection of terms. The terms can be any Prolog data types,
including structures and other lists. Syntactically, a list is denoted by square brackets with
the terms separated by commas. For example, a list of things in the kitchen is represented
as
Rather than having separate location predicates for each thing, we can have one location
predicate per container, with a list of things in the container.
[X | Y]
There is a special list, called the empty list, which is represented by a set of empty
brackets ([]). It is also referred to as nil. It can describe the lack of contents of a place or
thing.
loc_list([], hall)
Unification works on lists just as it works on other data structures. With what we now
know about lists we can ask
?- loc_list(X, kitchen).
X = [apple, broccoli, crackers]
Production rule:
length increment,length
3
Program:
domains
i=integer*
predicates
nondeterm length(i,integer)
clauses length([],0).
length([H|T],N):- length(T,M),N=M+1.
goal
length([4,5,6,7,8,9],Length_of_List).
Output:
Length_of_List=6
1 Solution
4
Experiment No. 3
Aim: Write a program to find the position of an element into a given list.
Program:
domains
namelist = symbol*
predicates
nondeterm lastd(namelist,symbol)
goal lastd([l,k,a,m],X).
Output:
X=m
1 Solution
5
Experiment No. 4
Aim: Write a program to delete the first occurrence and also all occurrences of a
particular element in a given list.
Program:
domains
i= integer*
predicates
nondeterm delete(integer,i,i)
goal
delete(2,[2,5,6,10,7,8,2],A).
Ouput:
A=[5,6,10,7,8]
1 Solution
6
Experiment No. 5
Write a program to find union and intersection of two given sets represented as lists.
Program:
domains
i=char*
predicates
nondeterm member(char,i) nondeterm union(i,i,i) nondeterm subset(i,i)
nondeterm intersection(i,i,i)
goal intersection(['a','b','c','d','1','2'],['d','f','2','3'],B).
%union(['a','b','c','d','1','2'],['d','f'],B).
%subset(['a','z'],['a','b','c','d','1','2']).
Output:
1) B=['d','2']
1 Solution
2) B=['a','b','c','1','2','d','f']
1 Solution
3) no
7
Experiment No. 6
Write a program to read a list at a time and write a list at a time using the well
defined read & write functions.
Term I/O:
read(Term)
Reads next full-stop (period) delimited term from the current input stream, if eof
then returns the atom 'end_of_file'.
write(Term)
Writes a term to the current output stream.
print(Term)
Writes a term to the current output stream. Uses a user defined predicate portray/1 to
write the term, otherwise uses write.
writeq(Term)
Writes a term to the current output stream in a form aceptable as input to read.
Program:
predicates
Clauses
goal readln(X),write(X).
Output:
I love India
I love India X=I love India
1 Solution
8
Experiment No. 7
goal supports(book,cup).
Output:
yes
9
Experiment No. 8
Write a program given the knowledge base,
If Town x is connected to Town y by highway z and bikes are allowed on z, you
can get to y from x by bike.
If Town x is connected to y by z then y is also connected to x by z.
If you can get to town q from p and also to town r from town q, you can get to
town r from town p.
Town A is connected to Town B by Road 1. Town B is connected to Town C by
Road 2.
Town A is connected to Town C by Road 3. Town D is connected to Town E by
Road 4.
Town D is connected to Town B by Road 5. Bikes are allowed on roads 3, 4, 5.
Bikes are only either allowed on Road 1 or on Road 2 every day. Convert the
following into wff’s, clausal form and deduce that
`One can get to town B from town D’.
Program:
predicates
nondeterm connected(symbol,symbol,symbol) nondeterm
canget(symbol,symbol)
nondeterm bikesallowed(symbol)
goal canget(b,d).
Output:
yes
10
Experiment No. 9
Aim:
Write a program for water jug problem.
"You are given two jugs, a 4-gallon one and a 3-gallon one. Neither have any measuring
markers on it. There is a tap that can be used to fill the jugs with water. How can you get
exactly 2 gallons of water into the 4-gallon jug?"
Production Rules:-
R1: (x,y) --> (4,y) if x < 4
R2: (x,y) --> (x,3) if y < 3
R3: (x,y) --> (x-d,y) if x > 0
R4: (x,y) --> (x,y-d) if y > 0
R5: (x,y) --> (0,y) if x > 0
R6: (x,y) --> (x,0) if y > 0
R7: (x,y) --> (4,y-(4-x)) if x+y >= 4 and y > 0
R8: (x,y) --> (x-(3-y),y) if x+y >= 3 and x > 0
R9: (x,y) --> (x+y,0) if x+y =< 4 and y > 0 R10: (x,y) --> (0,x+y) if x+y =< 3 and x >
0
Parse Tree:
11
Program:
12
Output:
Fill the 4-Gallon Jug: (0,0) --> (4,0) Fill the 3-Gallon Jug: (4,0) --> (4,3)
Empty the 4-Gallon jug on ground: (4,3) --> (0,3)
Pour all the water from 3-Gallon jug to 4-gallon: (0,3) --> (3,0)
Fill the 3-Gallon Jug: (3,0) --> (3,3)
Pour water from 3-Gallon jug to 4-gallon until it is full: (3,3) --> (4,2)
Empty the 4-Gallon jug on ground: (4,2) --> (0,2)
Pour all the water from 3-Gallon jug to 4-gallon: (0,2) --> (2,0) yes
13
Experiment No. 10
Program:
Output:
yes
14
Experiment No. 11
c1 c2 c3 c4 c5 carries
D O N A L D
G E R A L D
---------------- R O B E R T
Steps:
1. T is even ( D + D)
2. D >= 5 <=> c5 == 1
D <= 4 <=> c5 == 0
3. L >= 5 <=> c4 == 1
L <= 4 <=> c4 == 0
4. A >= 5 <=> c3 == 1
A <= 4 <=> c3 == 0
7. c2 == 0 <=> E == 0
c2 == 1 <=> E == 9 and c3 + N + R > 9
15
10. By (9): E == 9, c2 == 1
==> c1 = 1
12. By (9): c4 == 1
==> L >= 5
13. By (9): c4 == 1, E == 9
==> A = 4 (A cannot be 9 because E is already 9)
==> c3 = 0
16. By (15),
R == 6 <=> G == 0
R == 7 <=> G == 1
R == 8 <=> G == 2
R == 9 <=> G == 3
17
Output:
D=5, O=2, N=6, A=4, L=8, G=1, E=9, R=7, B=3, T=0
1 Solution
18
Experiment No. 12
Aim
Solve the classical Missionary Cannibals problem of AI.
• There are three missionaries and three cannibals on the left bank of a river.
• They wish to cross over to the right bank using a boat that can only carry two at a
time.
• The number of cannibals on either bank must never exceed the number of
missionaries on the same bank, otherwise the missionaries will become the
cannibals' dinner!
• Plan a sequence of crossings that will take everyone safely across.
• The initial state is shown to the right here, where black triangles represent
missionaries and red circles represent cannibals.
Parse Tree:
Program:
19
20
Output:
21
Experiment No. 13
Aim: Write a program to solve traveling salesman problem.
The well-known "Travelling Salesman Problem" is defined as follows. An instance of the
problem consists of a collection of sites, together with numerical values indicating
distances between pairs of sites. Given an instance, the question is: in what order should
one visit all sites so as to minimize the total distance travelled? Note that we want to return
to the starting point of the tour, so there is no issue regarding which site we should start
from. Each site is to be visited exactly once, and the distance travelled between two
consecutive sites on the tour is always the given distance between that pair of sites. If no
distance is given, that means that the tour is not allowed to have those two sites
consecutively.
Production Rules:
22
23
Output:
aedbc D = 15
aedbc D = 15
adebc D = 24
aebdc D = 25
abedc D = 27
adbec D = 31
abdec D = 24
Finally:
aedbc MIN_D = 15
AllTowns=["a","b","c","d","e"],
AllWays=[rout("a","c",1),rout("a","b",6),rout("a","e",5),rout("a","d",8),rout("c","b",
2),rout(" c","d",7),rout("c","e",10),rout("b","d",3),rout("b","e",9),rout("d","e",4)],
Route=["a","e","d","b","c"], Distance=15
24
Experiment No. 14
Aim:
Solve the classical Blocks World Problem of AI.
A A D
B C D B C
Program :
Output:
25
Experiment No. 15
Aim:
Write a program to search any goal given an input graph using AO* algorithm.
Program :
26