AI (Exp 2)
AI (Exp 2)
Experiment 2
Experiment No. - 2
formation/correction/ Answer to
(03) (03)
3.1 Aim:
To write simple programs using PROLOG as an AI programming language for various tasks such as
establishing relations in a family tree, factorial calculation, LCD and GCD, and solving the Missionaries
and Cannibals problem.
CO 2: Apply the most suitable search strategy and represent a natural language description of statements
in logic and apply the inference rules to design problem-solving agents.
1. Write a PROLOG program to establish relationships between two persons in a family tree.
2. Write a PROLOG program to calculate the factorial of a number.
3. Write a PROLOG program to find the LCD (Least Common Divisor) and GCD (Greatest Common
Divisor) of two numbers.
In PROLOG:
Facts represent basic assertions about objects or relationships. For example, parent(rahul, isha).
states that Rahul is a parent of Isha.
Rules are conditional statements that define relationships between facts. For example, grandparent(X,
Y) :- parent(X, Z), parent(Z, Y). defines the grandparent relationship.
Queries are questions posed to the system to determine if certain statements are true based on the facts
and rules provided.
1. Backtracking: PROLOG uses backtracking to explore different possibilities and find solutions to a
problem.
2. Unification: PROLOG automatically attempts to unify variables and constants to satisfy queries.
3. Recursion: Like many logical languages, PROLOG heavily relies on recursion to solve complex
problems.
PROLOG is particularly suited for solving AI problems because it allows expressing problems in terms
of relationships and logical inference. Below are some examples of simple PROLOG programs that
demonstrate how the language works.
parent(rahul, isha).
parent(rahul, sahil).
parent(isha, kiran).
parent(sahil, joy).
% Define relationships
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
% Queries
2. Factorial Program:
% Query
% ?- factorial(5, F). (Answer: F = 120)
gcd(0, X, X) :- X > 0.
gcd(X, 0, X) :- X > 0.
gcd(A, B, G) :-
A > 0, B > 0,
A1 is A mod B,
gcd(B, A1, G).
% LCD is calculated using the formula: LCM * GCD = Product of two numbers
lcm(A, B, L) :-
gcd(A, B, G),
L is (A * B) // G.
% Query
% ?- gcd(48, 18, G). (Answer: G = 6)
% ?- lcm(48, 18, L). (Answer: L = 144)
3.6 Procedure:
3.7 Conclusion:
Hence, we have learned how o write simple programs using PROLOG as an AI programming language for
various tasks such as establishing relations in a family tree, factorial calculation, LCD and GCD, and
solving the Missionaries and Cannibals problem.
3.8 Questions:
6. What is unification in PROLOG, and how does it assist in solving logical queries?
7. Describe how the Missionaries and Cannibals problem is solved using PROLOG.
8. Explain the process of finding the GCD and LCD in PROLOG.
9. How would you modify the family tree program to include grandparents and great-grandparents?
10. What are the advantages of using PROLOG for AI applications?