0% found this document useful (0 votes)
25 views34 pages

AI Presentation

The document discusses several AI and machine learning algorithms including the Tower of Hanoi algorithm, list concatenation in Prolog, hill climbing search, best first search, and backtracking search for solving sudoku puzzles.

Uploaded by

Akansha Sharma
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)
25 views34 pages

AI Presentation

The document discusses several AI and machine learning algorithms including the Tower of Hanoi algorithm, list concatenation in Prolog, hill climbing search, best first search, and backtracking search for solving sudoku puzzles.

Uploaded by

Akansha Sharma
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/ 34

AI NEP

PROGRAMS
Tower of Hanoi Algorithm
CODE :-
% Define the Tower of Hanoi predicate with four parameters: number of disks (N),
source rod (A), auxiliary rod (B), and destination rod (C).
tower_of_hanoi(1, A, _, C) :-
write('Move a disk from '), write(A), write(' to '), write(C), nl.

tower_of_hanoi(N, A, B, C) :-
N > 1,
N1 is N - 1,
% Move N-1 disks from A to B using C as an auxiliary rod
tower_of_hanoi(N1, A, C, B),
% Move the remaining disk from A to C LINK
tower_of_hanoi(1, A, _, C),
% Move the N-1 disks from B to C using A as an auxiliary rod
tower_of_hanoi(N1, B, A, C).
Tower of Hanoi Output
Concatenation of LISTS

CODE :- output :-

conc(L1, L2, L3) :-


append(L1, L2, L3).

% Example query
% ?- conc([1, 2], [3, 4], L3).
% L3 = [1, 2, 3, 4].
Hill Climbing Search Algorithm
Hill climbing algorithm is a local search algorithm which continuously moves
in the direction of increasing elevation/value to find the peak of the mountain
or best solution to the problem. It terminates when it reaches a peak value
where no neighbor has a higher value.

EG:- 2,4,8,1,10.....

Hill climbing algorithm is a technique which is used for optimizing the


mathematical problems.

It is also called greedy local search as it only looks to its good immediate
neighbor state and not beyond that.

In this algorithm, we don't need to maintain and handle the search tree or
graph as it only keeps a single current state.
Best First Search Algorithm
Greedy best-first search algorithm always selects the
path which appears best at that moment.

It is the combination of depth-first search and


breadth-first search algorithms.

It uses the heuristic function and search.


1. f(n)= g(n).
Were, h(n)= estimated cost from node n to the goal.
Q7. Implement a sudoku problem (minimum 9X9 size) using
constraint satisfaction problem.

Q8. Write a Prolog program to implement the family tree and


demonstrate the family relationship.

Q9. Write a Prolog program to implement the knowledge


representation using frames with appropriate examples.

Presented by Anirban Bhattacharjee


Q7. Implement a sudoku problem (minimum 9X9 size) using
constraint satisfaction problem.

CONSTRAINTS
Fill 9X9 grid with the digits
1-9.
Each column contains all the
digits from 1-9.
Each row contains all the
digits from 1-9.
Each box contains all the
digits from 1-9.
Consider the blue cell -
It cannot have the value 5, 3 or 7 because
those already appear in the row.
It cannot have the value 8 because it
already appear in that column.
It cannot have the values 5, 3, 6, 9 or 8
because it already appears in that box.
This leaves the following values that are
still consistent with the constraints : 1, 2, 4
and 7
When it comes to solving sudoku problems,
there are several strategies for picking cells
and numbers.
Most of us just pick a random number from
the set of remaining values i.e 1, 2, 4 and 7 of
the blue cell.
However a computer is fast and can
effectively solve a sudoku problem by
picking a random number.
We will explore that strategy called
backtracking search in the remaining slides.
Backtracking Search

1. Consider the variables in some order


2. Pick an unassigned variable and give it a provisional value such that its
consistent with the all of the constraints.
3. If no such assignment can be made, we have reached a dead end and need to
backtrack to the previous variable.
4. Continue this process untill a solution is found or we have to backtrack to the
initial variable and have exhausted all the possible values.
Backtracking Example
1. Consider the sudoku problem on the right.
2. Lets assign variables by row, left to right
1 2 3
1. We will begin by setting the value of
cell <0,3> to 1.
2. Notice that 1 is consistent with the
row, column and box constraints.
3. We set the value of cell <0,4> to 2.
4. We set the value of cell <0,6> to 3. But
3 already appears in that row. Hence
backtrack.
Q8. Write a Prolog program to implement the family tree and
demonstrate the family relationship.

The prolog family tree is especially well suited for solving problems
that require objects and relationships between the objects.

Prolog is a user-friendly programming language because it uses logic


in its programming.

Prolog family trees can be constructed by using the facts, rules, and
queries, collection of facts and rules is called a knowledge-base,
which describe the relationship of their objects.
Syntax of Prolog Family Tree

mother(M,N): parent(M,N), female(M).


sister(M,N): parent(O,M), parent(O,N), female(M).
grandparent(M,N): parent(M,O), parent(O,N).
grandmother(M,O): mother(M,N), parent(N,O).
grandfather(M,O): father(M,N), parent(N,O).
wife(M,N): parent(M,O), parent(N,O), female(M),male(N).
uncle(M,O): brother(M,N), parent(N,O)
father(M,N): parent(M,N),female(M).
haschild(M): parent(M,_).
brother(M,N): parent(O,M), parent(O,N), male(M).
Q9. Write a Prolog program to implement the knowledge
representation using frames with appropriate examples.

Frames are more structured form of packaging knowledge, - used for


representing objects, concepts etc.
Frames are organized into hierarchies or network of frames.
Lower level frames can inherit information from upper level frames in
network.
Parse Tree
Parse tree is the hierarchical representation of
terminals or non-terminals.
These symbols (terminals or non-terminals) represent
the derivation of the grammar to yield input strings.
In parsing, the string springs using the beginning
symbol.
The starting symbol of the grammar must be used as
the root of the Parse Tree.
Leaves of parse tree represent terminals.
Each interior node represents productions of a
grammar.
CODE :-
% Defining the grammar rules

% sentence: Represents a complete sentence and consists of a noun phrase followed by a verb phrase.
sentence(Tree) --> noun_phrase(NP), verb_phrase(VP), { Tree = s(NP, VP) }.

% noun_phrase: Represents a noun phrase and consists of an article followed by a noun.


noun_phrase(Tree) --> article(A), noun(N), { Tree = np(A, N) }.

% verb_phrase: Represents a verb phrase and consists of a verb followed by a noun phrase.
verb_phrase(Tree) --> verb(V), noun_phrase(NP), { Tree = vp(V, NP) }.

% Lexical items
article(the) --> [the].
article(a) --> [a].
noun(cat) --> [cat].
noun(dog) --> [dog].
noun(bird) --> [bird].
verb(chases) --> [chases].
verb(eats) --> [eats].
verb(sings) --> [sings].

% Entry point for parsing


parse(Tree, Sentence) :- sentence(Tree, Sentence, []).

% Example usage:
% ?- parse(Tree, [the, cat, chases, a, bird]).
% Tree = s(np(the, cat), vp(chases, np(a, bird))).
CODE :-
OUPUT :-
CODE :-
% Define the grammar rules

% s --> [].: This rule represents an empty sentence.


s --> [].

% s --> [a], s, [b].: This rule represents a sentence that starts with 'a', followed by another sentence, and
ends with 'b'.
s --> [a], s, [b].

% Entry point for parsing


parse(Input) :- phrase(s, Input).

% Example usage:
% ?- parse([a, a, b, b]).
% true.
% ?- parse([a, b, a, b]).
% false.
OUPUT :-
Thank you
very much!
Aaditya suman - 21HCS4101
Anuska Banerjee - 21HCS4122
Anirban - 21HCS4116
Bhavana Kashyap - 21HCS4131

You might also like