Prolog Code
Prolog Code
a. Water jug,
b. Chess,
c. Tic tac toe
d. 7 bridge problem and
e. 8/15 queen problem
PROLOG CODE:-
% State: jug1_amount, jug2_amount
% Initial state
Initial_state(0, 0).
Goal_state(2, _).
Goal_state(Jug1, Jug2),
Write(Solution).
Write([Operation | Solution]).
CHESS
% Position: (X, Y), Direction: ‘up’ or ‘down’
Win(Board, Player) :-
Draw(Board) :-
\+ member(_, Board).
% Example usage
7 BRIDGES PROBLEM
% Representing the graph: Bridge(a, b) means there's a bridge between a and b.
bridge(a, b).
bridge(b, c).
bridge(c, d).
bridge(d, a).
bridge(a, e).
bridge(e, c).
bridge(c, a).
% Eulerian path exists if each vertex has an even degree, or exactly two vertices have an
odd degree
degree(Vertex, Degree) :- findall(_, bridge(Vertex, _), L), length(L, Degree).
eulerian_path :-
length(OddDegreeVertices, Count),
8 QUEENS PROBLEM
% The 8 queens problem
Place_queens(Queens) :-
Q1 > 0, Q1 =< 8,
Safe_queens(Queens).
Safe(Q1, Q5, 4), safe(Q1, Q6, 5), safe(Q1, Q7, 6), safe(Q1, Q8, 7),
% Add similar checks for other pairs...
Safe(Qx, Qy, D) :- abs(Qx – Qy) =\= D. % Queens cannot be in the same diagonal.
a. Define
b. Algorithm
c. Example with diagram
d. Space and time complexity
e. Prolog code
f. Dfs(Start, Goal) :- dfs_helper([Start], Goal).
PROLOG CODE FOR DEPTH FIRST SEARCH
Dfs_helper([Goal|_], Goal).
Dfs_helper([Start|Rest], Goal) :-
Dfs_helper(NewStack, Goal).
% Example edges: edge(From, To).
Edge(a, b).
Edge(a, c).
Edge(b, d).
Edge(c, e).
Bfs([Start|Rest], Goal) :-
Start \= Goal,
Bfs(NewQueue, Goal).
Edge(a, c).
Edge(b, d).
Edge(c, e).
PROLOG CODE :-
% Action rules
Action(temperature(too_cold), wear_coat).
Action(temperature(too_hot), remove_coat).
PROLOG CODE :-
Action(state(freezing), wear_coat).
Action(state(warm), remove_coat).
PROLOG CODE :-
% Goal definition
Goal(need_to_warm).
% Action rules
Action(temperature(too_cold), wear_coat).
PROLOG CODE :-
Utility(temperature(too_cold), 5).
Utility(temperature(too_hot), 1).
LEARNING AGENT
PROLOG CODE :-