Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Directorate of Distance Education

Guru Jambheshwar University of Science & Technology


Hisar
Session 2021-22

Practical File of Python


Programming
MCA- 28 : Software Lab – Artificial Intelligence

Submitted to: Submitted by:

Mr. Neeraj Verma Lalit


Assistant Professor 21151002003
Deptt. Of Computer Science MCA 2nd-Sem

0|Page
INDEX
Sr. No. Content Page No.

1 Write a program to implement BFS/DFS Traversal 2-3

2 Write simple facts for the statements and querying it. 4-5

3 Write a program for Family-tree. 6

4 Write Program for Monkey-banana Problem. 7

5 Write a program to implement Tic-Tac-Toe game. 8-12

1|Page
1- Program to implement BFS/DFS
Traversal
DFS implementation program in a tree structure

Code:

b c

e d f g

i h

Output:

2|Page
BFS implementation in prolog

Code:
a

b c

e d f g

i h

Output:

3|Page
2- Simple fact for the statement and
querying it.
Simple Facts

In Prolog we can make some statements by using facts. Facts either consist of a particular item or a
relation between items. For example we can represent the fact that it is sunny by writing the program:

sunny.

We can now ask a query of Prolog by asking

?- sunny.

?- is the Prolog prompt. To this query, Prolog will answer yes. sunny is true because (from
above) Prolog matches it in its database of facts.

Guidelines for writing simple facts:

 Names of properties/relationships begin with lower case letters.


 The relationship name appears as the first term.
 Objects appear as comma-separated arguments within parentheses.
 A period "." must end a fact.
 Objects also begin with lower case letters. They also can begin with digits (like 1234),
and can be strings of characters enclosed in quotes e.g. color(penink, ‘red’).
 phoneno(agnibha, 1122334455). is also called a predicate or clause.
Syntax
The syntax for facts is as follows −
relation(object1,object2...).
Example
Following is an example of the above concept −
cat(tom).
loves_to_eat(kunal,pasta).
of_color(hair,black).
loves_to_play_games(nawaz).
lazy(pratyusha).

Queries
Queries are used to ask whether something is a logical consequence of a knowledge base.
With propositional queries, a user can ask yes-or-no questions. Queries with variables allow the
system to return the values of the variables that make the query a logical consequence of the
knowledge base.

4|Page
An instance of a query is obtained by substituting terms for the variables in the query. Different
occurrences of a variable must be replaced by the same term. Given a query with free variables,
an answer is either an instance of the query that is a logical consequence of the knowledge base,
or "no", meaning that no instances of the query logically follow from the knowledge base.
Instances of the query are specified by providing values for the variables in the query.
Determining which instances of a query follow from a knowledge base is known as answer
extraction.

% imm_west(W,E) is true if room W is immediately west of room E.

imm_west(r101,r103).
imm_west(r103,r105).
imm_west(r105,r107).
imm_west(r107,r109).
imm_west(r109,r111).
imm_west(r131,r129).
imm_west(r129,r127).
imm_west(r127,r125).

% imm_east(E,W) is true if room E is immediately east of room W.

imm_east(E,W) ←
imm_west(W,E).

% next_door(R1,R2) is true if room R1 is next door to room R2.

next_door(E,W) ←
imm_east(E,W).
next_door(W,E) ←
imm_west(W,E).

% two_doors_east(E,W) is true if room E is two doors east of room W.

two_doors_east(E,W) ←
imm_east(E,M) ∧
imm_east(M,W).

% west(W,E) is true if room W is west of room E.

west(W,E) ←
imm_west(W,E).
west(W,E) ←
imm_west(W,M) ∧
west(M,E).

5|Page
3 – Family Tree
Program:

Output:

6|Page
4-Monkey Banana Problem
Code:

Output:

7|Page
5-Tic-Tac-Toe
Program:

8|Page
9|Page
10 | P a g e
11 | P a g e
Output:

12 | P a g e

You might also like