AI Presentation
AI Presentation
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 :-
% 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.....
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.
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
The prolog family tree is especially well suited for solving problems
that require objects and relationships between the objects.
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
% 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) }.
% 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].
% 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 --> [a], s, [b].: This rule represents a sentence that starts with 'a', followed by another sentence, and
ends with 'b'.
s --> [a], s, [b].
% 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