Sie sind auf Seite 1von 4

# Name: Dil Prasad Kuwor Date: 10/5/2010

## Roll no. : 02/064

Subject: Artificial Intelligence
Assignment no. : Lab No. 3

Q.1Write a program to find all possible path from source node ‘a’ to the destination node ‘f’ on
the following graph .
5
c d
5
1

5 5 f
a 5

5 4
Prolog Code: b e
DOMAINS 9
NODE=SYMBOL
DISTANCE=integer
PREDICATES
nondeterm path(node,node,distance)
CLAUSES
path(S,D,TDist):-
path(S,D,TDist):-
GOAL
path(a,f,TotalDistance).

Output:

1
Q.2 Write a program to traverse the following tree in depth first order.

Animal

Vertebrate Invertebrate

## Mammal Bird Insect Worm

Prolog Code:

DOMAINS
treetype = tree(string, treetype, treetype) ; empty()
PREDICATES
traverse(treetype)
CLAUSES
traverse(empty).
traverse(tree(Name,Left,Right)):-
write(Name,'\n'),
traverse(Left),
traverse(Right).
GOAL
traverse(tree("Animal",
tree("Vertebrate",
tree("Mammal", empty, empty),
tree("Bird", empty, empty)),
tree("Invertibrates",
tree("Insect", empty, empty),
tree("Worm", empty, empty)))).

Output:

2
Q.3 Write a program to create the above tree of question no. 2 .

Prolog Code:

## /* create_tree(A, B) puts A in the data field of a one-cell tree */

/* giving B insert_left(A, B, C) inserts A as left subtree of B giving C */
/* insert_right(A, B, C) inserts A as right subtree of B giving C */

DOMAINS
treetype = tree(string,treetype,treetype) ; empty()

PREDICATES
inquire
create_tree(string,treetype)
insert_left(treetype,treetype,treetype)
insert_right(treetype, treetype, treetype)

CLAUSES
create_tree(A,tree(A,empty,empty)).
insert_left(X,tree(A,_,B),tree(A,X,B)).
insert_right(X,tree(A,B,_),tree(A,B,X)).
inquire:-

## /* First create some one-cell trees... */

create_tree("Mammal",M),
create_tree("Bird",B),
create_tree("Insect",I),
create_tree("Worm",W),
create_tree("Vertebrate",V),
create_tree("Invertebrate",IV),
create_tree("Animal",A),

## /* ...then link them up... */

insert_left(M, V, X),
insert_right(B, X,Y),
insert_left(I, IV, P),
insert_right(W, P, Q),
insert_left(Y, A, R),
insert_right(Q, R, S),

## /*...print the result...*/

write(S, '\n').

GOAL
inquire.

3
Q. 4 Write a program which creates the tree of following binary tree.

2 3

4 5 6 7
Prolog Code:

DOMAINS
treetype = tree(integer,treetype,treetype) ; empty()
PREDICATES
inquire
create_tree(integer,treetype)
insert_left(treetype,treetype,treetype)
insert_right(treetype, treetype, treetype)
CLAUSES
create_tree(A,tree(A,empty,empty)).
insert_left(X,tree(A,_,B),tree(A,X,B)).
insert_right(X,tree(A,B,_),tree(A,B,X)).
inquire:-
create_tree(1,One),
create_tree(2,Two),
create_tree(3,Three),
create_tree(4,Four),
create_tree(5,Five),
create_tree(6,Six),
create_tree(7,Seven),
insert_left(Six,Three ,P),
insert_right(Seven,P,Q),
insert_left(Four,Two, R),
insert_right(Five, R,S),
insert_left(S,One, T),
insert_right(Q,T, U),
write(U, '\n').
GOAL
inquire.
Output:

The End
4