Lab_AI[1]
Lab_AI[1]
LABORATORY REPORT
Artificial Intelligence Lab (CSL353)
Submitted by
YASH MODI
2022BCSE051
B.Tech. CSE (6th Semester – Section ‘A’)
Submitted to
SPRING 2025
<2022BCSE051/>
<2022BCSE051/>
CONTENTS
Page Faculty
Week # Date Experiment
No. Sign
Self-Declaration
I have done all the above programs on my own to the best of my knowledge.
Student’s Signature
<2022BCSE051/>
<2022BCSE051/>
LAB-01
Introduction to Prolog
1. Introduction
Prolog (Programming in Logic) is a declarative programming language primarily
used in Artificial Intelligence (AI), natural language processing, and symbolic
reasoning. Unlike imperative languages, Prolog is based on logic and rules rather
than sequential execution of commands.
2. Basics of Prolog
2.1 Facts, Rules, and Queries
Prolog programs consist of facts, rules, and queries.
2.1.1 Facts
Facts represent basic information known to the system. Example:
parent(john, mary).
parent(mary, alice).
This states that John is the parent of Mary and Mary is the parent of
Alice.
2.1.2 Rules
Rules define relationships between facts using logical conditions.
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
This rule states that X is a grandparent of Y if X is a parent of Z and Z
is a parent of Y.
2.1.3 Queries
Queries allow users to retrieve information from the knowledge
base.
?- grandparent(john, alice).
This query asks whether John is a grandparent of Alice.
2.2 Variables in Prolog
Variables in Prolog are written in uppercase.
<2022BCSE051/>
?- parent(X, mary).
This asks, "Who is the parent of Mary?" Prolog will return X = john.
3. Prolog Execution Mechanism
3.1 Unification
Unification is the process of matching terms to satisfy a query.
3.2 Backtracking
If Prolog encounters a failure, it backtracks to previous rules to find
alternative solutions.
3.3 Recursion
Prolog heavily relies on recursion. Example:
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
This defines an ancestor as a parent or a parent of an ancestor.
4. Lists in Prolog
Lists are fundamental in Prolog for handling collections of data.
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
This checks if X is a member of a list.
LAB-02
N-QUEEN PROBLEM
Problem Statement:
Place N queens on an N × N chessboard such that no two queens:
1. Share the same row.
2. Share the same column.
3. Lie on the same diagonal.
Objective:
Find one or all valid arrangements of N queens satisfying the above constraints.
Code:
N=8
def print_solution(board):
for row in board:
print(" ".join(str(cell) for cell in row))
return True
for i in range(N):
if is_safe(board, i, col):
board[i][col] = 1
if solve(board, col + 1):
return True
board[i][col] = 0
return False
def solve_n_queens():
board = [[0] * N for _ in range(N)]
if not solve(board, 0):
print("No solution exists")
return False
print_solution(board)
return True
solve_n_queens()
Output:
0010
1000
0001
0100
<2022BCSE051/>
LAB-03
Water Jug Problem
Problem Statement:
Place N queens on an N × N chessboard such that no two queens:
1. Share the same row.
2. Share the same column.
3. Lie on the same diagonal.
Objective:
Find one or all valid arrangements of N queens satisfying the above
constraints.
if (a, b) in visited:
return False
visited.add((a, b))
path.append((a, b))
if a == z or b == z:
print("Solution found!")
for step in path:
print(step)
return True
possible_moves = [
<2022BCSE051/>
(x, b),
(a, y),
(0, b),
(a, 0),
(a - min(a, y - b), b + min(a, y - b)),
(a + min(b, x - a), b - min(b, x - a))
]
path.pop()
return False
visited = set()
path = []
solve_water_jug_dfs(x, y, z)
LAB-04
Water Jug Problem(BFS)
Problem Statement:
while queue:
a, b = queue.popleft()
if (a, b) in visited:
continue
visited.add((a, b))
if a == z or b == z:
path = []
while (a, b) in parent:
path.append((a, b))
a, b = parent[(a, b)]
path.append((0, 0))
path.reverse()
print("Solution found!")
<2022BCSE051/>
possible_moves = [
(x, b),
(a, y),
(0, b),
(a, 0),
(a - min(a, y - b), b + min(a, y - b)),
(a + min(b, x - a), b - min(b, x - a))
]
solve_water_jug_bfs(x, y, z)