0% found this document useful (0 votes)
132 views31 pages

5.AI Lab-Manual

The document provides instructions for 10 experiments on artificial intelligence topics to be completed in a laboratory manual. The experiments include implementing Tic-Tac-Toe, breadth-first search, depth-first search, Tower of Hanoi, N-Queens problems, traveling salesman problem, A* algorithm, Prolog programs, semantic networks, and conceptual dependency.

Uploaded by

yogakep350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views31 pages

5.AI Lab-Manual

The document provides instructions for 10 experiments on artificial intelligence topics to be completed in a laboratory manual. The experiments include implementing Tic-Tac-Toe, breadth-first search, depth-first search, Tower of Hanoi, N-Queens problems, traveling salesman problem, A* algorithm, Prolog programs, semantic networks, and conceptual dependency.

Uploaded by

yogakep350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

En. No.

: 210020116018

Ahmedabad Institute of Technology


IT Department
Artificial Intelligence (3161608)

Laboratory Manual
Year: 2024

Prepared By: - Prof. Dhruvi Suthar

1
AI(3161608)
En. No.: 210020116018

INDEX

Page No.
Sr.No. Experiment Date Marks Signature
From To

Write a program to implement Tic-


1. TacToe game problem (use any 3 4
programming language).
Write a program to implement BFS (use
2. any programming language). 5 7

Write a program to implement DFS


3. 8 10
(use any programming language).
Write a program to implement Tower
Of Hanoi Problem (use any
4. 11 11
programming language).

Write a program to solve N-Queens


problems (use any programming
5. 12 13
language).

Write a program to solve traveling


6. salesman problems (use any 14 14
programming language).

Write a program to Implement A*

7. Algorithm (use any programming 15 16


language).

Write a PROLOG program for


8. 17 18
Family Relationship
Convert the Prolog predicates into
9. 19 20
Semantic Net for the given data
Write the Conceptual Dependency for
10. given statements. 21 22

2
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 1 DATE: / /

TITLE: Write a program to implement Tic-Tac-Toe game problems (use any programming
language).

OBJECTIVES: The aim of this project is to develop a


o Tic-Tac-Toe game for mobile devices. The game is supposed to consist of two
parts, one a single player game (a player against a system), and the other a
multiplayer game (two players on their mobile devices, playing against each
other).
o It is aimed to prove the min max algorithm in this game

Theory:

The game Tic Tac Toe is also known as Noughts and Crosses or Xs and Os ,the player
needs to take turns marking the spaces in a 3x3 grid with their own marks,if 3 consecutive
marks (Horizontal, Vertical,Diagonal) are formed then the player who owns these moves
get won.

Assume ,

Player 1 - X
Player 2 - O

So,a player who gets 3 consecutive marks first,they will win the game .
Board's Data Structure:

3
AI(3161608)
En. No.: 210020116018

EXERCISE:

Implement Tic-Tac-Toe game problems (use any programming language).

EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

4
AI(3161608)
En. No.: 210020116018

5
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 2 DATE: / /

TITLE: Write a program to implement BFS

OBJECTIVES: On completion of this experiment students will be able to…


Know the Concept of BFS and its working

THEORY:
Breadth First Search (BFS) algorithm traverses a graph in a breadth ward motion and uses
a queue to remember to get the next vertex to start a search, when a dead end occurs in any
iteration As in the example given above, BFS algorithm traverses from A to B to E to F first then
to C and G lastly to D. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty

Step Traversal Description

Initialize the queue.

6
AI(3161608)
En. No.: 210020116018

We start from visiting S (starting node), and


mark it as visited.

We then see an unvisited adjacent node from S.


In this example, we have three nodes but
alphabetically we choose A, mark it as visited
and enqueue it.

Next, the unvisited adjacent node from S is B.


We mark it as visited and enqueue it.

Next, the unvisited adjacent node from S is C.


We mark it as visited and enqueue it.

7
AI(3161608)
En. No.: 210020116018

Now, S is left with no unvisited adjacent nodes.


So, we dequeue and find A.

From A we have D as unvisited adjacent node.


We mark it as visited and enqueue it

EXERCISE:

Implement BFS algorithm for 8 puzzle problems or Water Jug problems or any AI search
problem (use any programming language).

EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: _______________

8
AI(3161608)
En. No.: 210020116018

9
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 3 DATE: / /

TITLE: Write a program to implement DFS

OBJECTIVES: On completion of this experiment students will be able to…


Understand the concept of DFS algorithm and its working.
THEORY:
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles. Depth
First Search (DFS) algorithm traverses a graph in a depth ward motion and uses a stack to
remember to get the next vertex to start a search, when a dead end occurs in any iteration.

As in the example given above, the DFS algorithm traverses from S to A to D to G to E to B


first, then to F and lastly to C. It employs the following rules.

Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.

Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all
the vertices from the stack, which do not have adjacent vertices.) Rule 3 − Repeat Rule 1
and Rule 2 until the stack is empty.

Step Traversal Description


1

Initialize the stack.

2
Mark S as visited and put it onto the stack. Explore
any unvisited adjacent node from S. We have three
nodes and we can pick any of them. For this
example, we shall take the node in an alphabetical
order.

10
AI(3161608)
En. No.: 210020116018

Mark A as visited and put it onto the stack. Explore


any unvisited adjacent node from A. Both S and D
are adjacent to A but we are concerned for
unvisited nodes only.

Visit D and mark it as visited and put onto the


stack. Here, we have B and C nodes, which are
adjacent to D and both are unvisited. However, we
shall again choose in an alphabetical order.

We choose B, mark it as visited and put onto the


stack. Here B does not have any unvisited adjacent
node. So, we pop B from the stack.

We check the stack top for return to the previous


node and check if it has any unvisited nodes. Here,
we find D to be on the top of the stack.

Only unvisited adjacent node is from D is C now.


So we visit C, mark it as visited and put it onto the
stack.

11
AI(3161608)
En. No.: 210020116018

As C does not have any unvisited adjacent node so we keep popping the stack until we find a node
that has an unvisited adjacent node. In this case, there's none and we keep popping until the stack is
empty.

EXERCISES:

1. Implement DFS algorithm for 8 puzzle problem


2. Implement Water Jug problem (use any programming language).

EVALUATION:

Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

12
AI(3161608)
En. No.: 210020116018

13
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 4 DATE: / /

TITLE: Write a program to implement Tower Of Hanoi Problem (use any programming

language).

OBJECTIVES: The objective of the puzzle is to move the entire stack to another rod,
obeying the simple rules

THEORY:Tower of Hanoi is a mathematical puzzle where we have three rods and n disks.
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3) No disk may be placed on top of a smaller disk. With 3 disks, the puzzle can be solved
in 7 moves. The minimal number of moves required to solve a Tower of Hanoi puzzle is 2n
− 1, where n is the number of disks.

EXERCISE:
Implement Tower Of Hanoi Problem(use any programming language).

EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

14
AI(3161608)
En. No.: 210020116018

15
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 5 DATE: / /

TITLE: Write a program to solve N-Queens problems (use any programming language).
OBJECTIVES: This problem is to find an arrangement of N queens on a chess board, such that no
queen can attack any other queens on the board.
THEORY:
The chess queens can attack in any direction as horizontal, vertical, horizontal and diagonal.
A binary matrix is used to display the positions of N Queens, where no queens can attack other
queens.
For example, the Eight queens problem is a constraint satisfaction problem. The task is to place
eight queens in the 64 available squares in such a way that no queen attacks each other.
So the problem can be formulated with variables x1,x2,x3,x4,x5,x6,x7,x8 and
y1,y2,y3,y4,y5,y6, y7,y8; the xs represent the rows and ys the column. Now a solution for this
problem is to assign values for x and for y such that the constraint is satisfied

Input:
The size of a chess board. Generally, it is 8. as (8 x 8 is the size of a normal chess board.
Output:
The matrix that represents in which row and column the N Queens can be placed.
If the solution does not exist, it will return false.
10000000
00000010
00001000
00000001
01000000
00010000
00000100
00100000

In this output, the value 1 indicates the correct place for the queens.
The 0 denotes the blank spaces on the chess board.

EXERCISE:
Implement N-Queens problem (use any programming language).

EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

16
AI(3161608)
En. No.: 210020116018

17
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 6 DATE: / /

TITLE: Write a program to solve traveling salesman problems (use any programming
language).
OBJECTIVES: The traveling salesman problem consists of a salesman and a set of cities.
The salesman has to visit each one of the cities starting from a certain one (e.g. the
hometown) and returning to the same city. The problem is that the traveling salesman wants
to minimize the total length of the trip.

THEORY:
The traveling salesman problem can be described as
follows: TSP = {(G, f, t): G = (V, E) a complete graph, f
is a function V×V → Z, t ∈ Z,
G is a graph that contains a traveling salesman tour with a cost that does not exceed t}.

For example, consider the graph shown in figure . A TSP tour in the graph is 1-2-4-3-1. The
cost of the tour is 10+25+30+15 which is 80.

EXERCISE:
Implement traveling salesman problems (use any programming language).

EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

18
AI(3161608)
En. No.: 210020116018

19
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 7 DATE: / /

TITLE: Write a program to Implement A* Algorithm (use any programming language).

OBJECTIVES: A* Search algorithm is one of the best and popular techniques used in path-
finding and graph traversals

THEORY:
A* is the most popular choice for pathfinding, because it’s fairly flexible and can be used in a
wide range of contexts.
Informally speaking, A* Search algorithms, unlike other traversal techniques, have
“brains”. What it means is that it is really a smart algorithm which separates it from the
other conventional algorithms. This fact is cleared in detail in the sections below.
And it is also worth mentioning that many games and web-based maps use this algorithm to
find the shortest path very efficiently (approximation).
A* is like Dijkstra’s Algorithm in that it can be used to find a shortest path. A* is like Greedy
Best-First-Search in that it can use a heuristic to guide itself. In the simple case, it is as fast as
Greedy Best-First-Search:

In the example with a concave obstacle, A* finds a path as good as what Dijkstra’s Algorithm
found:
The secret to its success is that it combines the pieces of information that Dijkstra’s
Algorithm uses (favoring vertices that are close to the starting point) and information that
Greedy Best-First-Search uses (favoring vertices that are close to the goal). In the standard
terminology used when talking about A*, g(n) represents the exact cost of the path from
the starting point to any vertex n, and h(n) represents the heuristic estimated cost from
vertex n to the goal. In the above diagrams, the yellow (h) represents vertices far from the
goal and teal (g) represents vertices far from the starting point. A* balances the two as it
moves from the starting point to the goal. Each time through the main loop, it examines the
vertex n that has the lowest f(n) = g(n) + h(n).
t is the best-known form of Best First search. It avoids expanding paths that are already expensive,
but expands most promising paths first.
f(n) = g(n) + h(n), where

20
AI(3161608)
En. No.: 210020116018

● g(n) the cost (so far) to reach the node


● h(n) estimated cost to get from the node to the goal
● f(n) estimated total cost of path through n to goal. It is implemented using priority queue
by increasing f(n).

EXERCISE:

Implement A* Algorithm (use any programming language)..

EVALUATION:
Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

21
AI(3161608)
En. No.: 210020116018

22
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 8 DATE: / /

TITLE: Write a PROLOG program for Family Relationship

OBJECTIVES: On completion of this experiment students will be able to…


Implement Relationship Tree structure using knowledge base(facts and
rules) using queries
THEORY:

Prolog is a language built around the Logical Paradigm: a declarative approach to problem-
solving. There are only three basic constructs in Prolog: facts, rules, and queries.A
collection of facts and rules is called a knowledge base (or a database) and Prolog
programming is all about writing knowledge bases. That is, Prolog programs simply are
knowledge bases, collections of facts and rules which describe some collection of
relationships that we find interesting.So how do we use a Prolog program? By posing
queries. That is, by asking questions about the information stored in the knowledge base.
The computer will automatically find the answer (either True or False) to our queries.

Knowledge Base (Facts & Rules)


Check the following knowledge base used to store the information that appears on a family tree:
For example:
Facts

male(harry).
female(helen).
female(sophie).
female(jess).
female(lily).
parent_of(jess, simon).
parent_of(ali, simon).
parent_of(lily, harry).
parent_of(james, harry)
For example:

Rules

father_of(X,Y):- male(X),
parent_of(X,Y).
mother_of(X,Y):- female(X),
parent_of(X,Y).
Queries
True or False Queries:
The following queries would return either True or False.
For example:

?-mother_of(jess,helen).

23
AI(3161608)
En. No.: 210020116018

?-brother_of(james,simon).
?-ancestor_of(jack,simon).

Other Queries: The following queries would return a solution which could be either:
A unique value
A list of values
False if no solution can be found
For example:
?-
mother_of(X,jess).
?-parent_of(X,simon).
?-sister_of(X,lily).
?-ancestor_of(X,lily).

EXERCISE: Use a range of queries, similar to the one above to interrogate the knowledge base
and generate the family tree using the example given above.

EVALUATION:

Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

24
AI(3161608)
En. No.: 210020116018

25
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 9 DATE: / /

TITLE: Convert the Prolog predicates into Semantic Net for the given data.

OBJECTIVES: On completion of this experiment students will be able to…


learn the concept of Semantic Net and use of Predicates.

THEORY:
Semantic Nets:
Semantic networks are an alternative to predicate logic as a form of knowledge
representation. The idea is that we can store our knowledge in the form of a graph, with
nodes representing objects in the world, and arcs representing relationships between those
objects.
For example, the following:
is intended to represent the
data:
Tom is a cat.
Tom caught a bird.
Tom is owned by John.
Tom is ginger in colour.
Cats like cream.
The cat sat on the mat.
A cat is a mammal.
A bird is an animal.
All mammals are animals.
Mammals have fur.

It is argued that this form of representation is closer to the way humans structure knowledge
by building mental links between things than the predicate logic we considered earlier.
some confusion here which stems from the imprecise nature of semantic nets. A particular
problem is that we haven’t distinguished between nodes representing classes of things, and
nodes representing individual objects. So, for example, the node labeled Cat represents both
the single (nameless) cat who sat on the mat, and the whole class of cats to which Tom
belongs, which are mammals and which like cream. The is_a link has two different
meanings – it can mean that one object is an individual item from a class, for example Tom
is a member of the class of cats, or that one class is a subset of another, for example, the
class of cats is a subset of the class of mammals.

26
AI(3161608)
En. No.: 210020116018

This confusion does not occur in logic, where the use of quantifiers, names and predicates makes
it clear what we mean so:
Tom is a cat is represented by Cat(Tom)
The cat sat on the mat is represented by ∃x∃y(Cat(x)∧Mat(y)∧SatOn(x,y)) A
cat is a mammal is represented by ∀x(Cat(X)→Mammal(x))

EXERCISE:
Convert following Prolog predicates into Semantic
Net cat(tom). cat(cat1). mat(mat1).
sat_on(cat1,mat1).
bird(bird1).
caught(tom,bird1).
like(X,cream) :– cat(X).
mammal(X) :– cat(X).
has(X,fur) :– mammal(X).
animal(X) :– mammal(X).
animal(X) :– bird(X).
owns(john,tom).
is_coloured(tom,ginger).

EVALUATION:

Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

27
AI(3161608)
En. No.: 210020116018

28
AI(3161608)
En. No.: 210020116018

EXPERIMENT NO: 10 DATE: / /

TITLE: Write the Conceptual Dependency for given statements.


OBJECTIVES: On completion of this experiment students will be able to…
What is conceptual dependency?

THEORY:
Frames and scripts, as knowledge representation schemes take into account context and
relationships. These techniques also provide a useful formulation for representing more
complex structures such as objects, scenes and multiple-sentence stories. One of the key
ideas of the script approach is to reduce a sentence or story to a set of semantic primitives
using a formalism called conceptual dependency (CD).

Conceptual dependency (CD) is a theory of natural language processing which mainly deals
with representation of semantics of a language.

The main motivation for the development of CD as a knowledge representation techniques


are given below:

1. To construct computer programs which can understand natural language,

2. To make inferences from the statements and also to identify conditions in which two sentences
can have similar meaning,

3. To provide facilities for the system to take part in dialogues and answer questions,

4. To provide a necessary plank that sentences in one language can be easily translated into other
languages, and

5. To provide a means of representation which are language independent.

Knowledge is represented in CD by elements that are called conceptual structures. What


forms the basis of CD representation is that for two sentences which have identical meaning
there must be only one representation and implicitly packed information must be explicitly
stated. In order that knowledge is represented in CD form, certain primitive actions have
been developed.

conceptual dependency can be written for any Statement using following data

1) Conceptual Primitives
2) Conceptual Primitives for Actions
3) Other Primitive Actions
4) Conceptual Roles
5) Conceptual Syntax Rules
6) Conceptual Tenses and States

29
AI(3161608)
En. No.: 210020116018

Example

EXERCISE:

Write the Conceptual Dependency for following statements.


(a) John gives Mary a book (b) John gave Mary the book yesterday

EVALUATION:

Understanding /
Involvement
Problem solving Timely Completion Total
(3) (10)
(4)
(3)

Signature with date: ________________

30
AI(3161608)
En. No.: 210020116018

31
AI(3161608)

You might also like