Ai
Ai
water_jug(3,2).
false.
Exp.no 2: Implementation of BFS for TicTacToe problem using JAVA
Aim: To implement of BFS for TicTacToe problem using JAVA
Source code:
import java.util.*;
else {
else {
System.out.println("Congratulations!" + Winner + "S have won!Thanks for playing.");
}
}
}
Output:
Welcome to 3X3 Tic Tac Toe
|___|___|___|
|1|2|3|
|______|
|4|5|6|
|______|
|7|8|9|
|______|
X will play first.Enter a slot number to place X in:
5
|___|___|___|
|1|2|3|
|______|
|4|X|6|
|______|
|7|8|9|
|______|
Osturn;Enter a slot number to placeOin
3
|___|___|___|
|1|2|O|
|______|
|4|X|6|
|______|
|7|8|9|
|______|
Xsturn;Enter a slot number to placeXin
9
|___|___|___|
|1|2|O|
|______|
|4|X|6|
|______|
|7|8|X|
|______|
Osturn;Enter a slot number to placeOin
1
|___|___|___|
|O|2|O|
|______|
|4|X|6|
|______|
|7|8|X|
|______|
Xsturn;Enter a slot number to placeXin
2
|___|___|___|
|O|X|O|
|______|
|4|X|6|
|______|
|7|8|X|
|______|
Osturn;Enter a slot number to placeOin
8
|___|___|___|
|O|X|O|
|______|
|4|X|6|
|______|
|7|O|X|
|______|
Xsturn;Enter a slot number to placeXin
7
|___|___|___|
|O|X|O|
|______|
|4|X|6|
|______|
|X|O|X|
|______|
Osturn;Enter a slot number to placeOin
6
|___|___|___|
|O|X|O|
|______|
|4|X|O|
|______|
|X|O|X|
|______|
Xsturn;Enter a slot number to placeXin
4
|___|___|___|
|O|X|O|
|______|
|X|X|O|
|______|
|X|O|X|
|______|
Its a draw!Thanks for playing.
Exp.no 3: Implementation of TSP using heuristic approach using PROLOG
Aim: To implement TSP using heuristic approach using Prolog.
Source code:
road(gnt,vij,9).
road(ama,gnt,3).
road(ama,vij,6).
road(vizag,ama,5).
road(ama,vizag,5).
road(hyd,ama,4).
road(hyd,vizag,8).
get_road(Start,End,Visited,Result):-
get_road(Start,End,[Start],0,Visited,Result).
get_road(Start,End,Waypoints,DistanceAcc,Visited,TotalDistance):-
road(Start,End,Distance),
reverse([End/Waypoints],Visited),
TotalDistance is DistanceAcc+Distance.
get_road(Start,End,Waypoints,DistanceAcc,Visited,TotalDistance):-
road(Start,Waypoint,Distance),\+member(Waypoint,waypoints),
NewDistanceAcc is DistanceAcc+Distance,
get_road(Waypoint,End,[Waypoint/Waypoints],NewDistanceAcc,Visited,TotalDistance).
Output:
get_road(hyd,vizag,Visited,Result).
Visited = [vizag/[hyd]],
Result = 8
get_road(gnt,vij,Visted,Result).
Visted = [vij/[gnt]],
Result = 9
Exp.no 4: Implementation of Towers of Hanoi problem using PROLOG
Aim: To implement of Towers of Hanoi using PROLOG
Source code:
move(1,X,Y,_):-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z):-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
Output:
move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
true
Exp.no 5: Implementation of Monkey Banana Problem using PROLOG
Aim: To implement of Monkey Banana Problem using PROLOG
Source code:
move(state(middle,onbox,middle,hasnot),
grasp,
state(middle,onbox,middle,has)).
move(ssate(P,onfloor,P,H),
climb,
state(P,onbox,P,H)).
move(state(P1,onfloor,P1,H),
drag(P1,P2),
state(P2,onfloor,P2,H)).
move(state(P1,onfloor,B,H),
walk(P1,P2),
state(P2,onfloor,B,H)).
canget(state(_,_,_,has)).
canget(state1):-
move(state1,_,state2),
canget(state2).
Output:
canget(state(atdoor,onfloor,atwindow,has)).
true.
Exp.no 6: Implementation of 8 Queens problem using Heuristic Approach
Aim: To implement of 8 Queens problem using Heuristic Approach.
Source code:
import java.util.Arrays;
class Queens{
static final int N=8;
static boolean isSafe(int[][] board,int row,int col){
for(int x=0;x<col;x++){
if(board[row][x]==1){
return false;
}
}
for(int x=row,y=col;x>=0&&y>=0;x--,y--){
if(board[x][y]==1){
return false;
}
}
for(int x=row,y=col;x<N&&y>=0;x++,y--){
if(board[x][y]==1){
return false;
}
}
return true;
}
static boolean solveNQueens(int[][] board,int col){
if(col==N){
for(int[] row:board){
System.out.println(Arrays.toString(row));
}
System.out.println();
return true;
}
for(int i=0;i<N;i++){
if(isSafe(board,i,col)){
board[i][col]=1;
board[i][col]=1;
if(solveNQueens(board,col+1)){
return true;
}
board[i][col]=0;
}
}
return false;
}
public static void main(String[] args) {
int[][] board=new int[N][N];
if(!solveNQueens(board,0)){
System.out.println("No solution found");
}
}
}
Output:
[1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
Exp.no 7: Implementation of 8 Puzzle problem using PROLOG
Aim: To implement of 8 puzzle problem using PROLOG
Source code:
test(Plan):-
write('Initial state:'),nl,
Init=[at(tile4,1),at(tile3,2),at(tile8,3),at(empty,4),at(tile2,5),at(tile6,6),at(tile5,7),at(tile1,8),at(tile7,9)],
write_sol(Init),
Goal=[at(tile1,1),at(tile2,2),at(tile3,3),at(tile4,4),at(empty,5),at(tile5,6),at(tile6,7),at(tile7,8),at(tile8,9)],nl,
write('Goal state:'),nl,
write(Goal),nl,nl,
solve(Init,Goal,Plan).
solve(State,Goal,Plan):-
solve(State,Goal,[],Plan).
is_movable(X1,Y1):-(1 is X1-Y1);(-1 is X1-Y1);(3 is X1-Y1);(-3 is X1-Y1).
solve(State,Goal,Plan,Plan):-
is_subset(Goal,State),nl,
write_sol(Plan).
solve(State,Goal,Sofar,Plan):-
act(Action,Preconditions,Delete,Add),
is_subset(Preconditions,State),
\+member(Action,Sofar),
delete_list(Delete,State,Remainder),
append(Add,Remainder,NewState),
solve(NewState,Goal,[Action|Sofar],Plan).
act(move(X,Y,Z),
[at(X,Y),at(empty,Z),is_movable(Y,Z)],
[at(X,Y),at(empty,Z)],
[at(X,Z),at(empty,Y)]).
is_subset([H|T],Set):-
member(H,Set),
is_subset(T,Set).
is_subset([],_).
delete_list([H|T],Curstate,NewState):-
remove(H,Curstate,Remainder),
delete_list(T,Remainder,NewState).
delete_list(T,Remainder,NewState).
delete_list([],Curstate,Curstate).
remove(X,[X|T],T).
remove(X,[H|T],[H|R]):-
remove(X,T,R).
write_sol([]).
write_sol([H|T]):-
write_sol(T),
write(H),nl.
append([H|T],L1,[H|L2]):-
append(T,L1,L2).
append([],L,L).
member(X,[X|_]).
member(X,[_|T]):-
member(X,T).
Output:
test(plan).
Initial state:
at(tile7,9)
at(tile1,8)
at(tile5,7)
at(tile6,6)
at(tile2,5)
at(empty,4)
at(tile8,3)
at(tile3,2)
at(tile4,1)
Goal state:
[at(tile1,1),at(tile2,2),at(tile3,3),at(tile4,4),at(empty,5),at(tile5,6),at(tile6,7),at(tile7,8),at(tile8,9)]
false.
Exp.no 8: Implementation of Hill Climbing Algorithm using LISP
Aim: To implement of Hill Climbing Algorithm using LISP
Source code:
(setf (get 's 'neighbors) '(a d)
(get 'a 'neighbors) '(s b d)
(get 'b 'neighbors) '(a c e)
(get 'c 'neighbors) '(b)
(get 'd 'neighbors) '(s a e)
(get 'e 'neighbors) '(b d f)
(get 'f 'neighbors) '(e))
Output:
snowy(rochester).
true.
snowy(seattle).
false.
Exp.no 12: Program to findout Union and Intersection of two lists
Aim: To write a program to find out Union and Intersection of two lists
Algorithm:
Algorithm to Find out Union of Two Lists
Step 1: Start
Step 2: If first list is empty return second list as the union of two lists and goto step 6.
Step 3: If head of first list is not a member of second list add this element in the union of both list produced by
calling union function with and goto step 5.
Step 4: If head of first list is a member of second list call the union function for tail of first list and second list
Step 5: Exit
Algorithm to Find out Intersection of Two Lists
Step 1: Start
Step 2: If first list is empty return empty list as intersection of both lists.
Step 3: If head of first list is a member of second list then add the head to the list obtained by calling
intersection function with tail of first list and second list and goto step 5.
Step 4: If head of first list is not a member of second list then call intersection function with tail of first list
and second list.
Step 5: Exit
Source code:
domains
X
=
s
y
m
b
o
l
*
Y
=
s
y
m
b
o
l
predicates union(X,X,X)
inters(X,X,X)member(Y,X)
clauses union([],L,L).
union([H|T],L,[H|U]):-
union(T,L,U),
not(member(H,U)),!.
union([_|T],L,U):-
union(T,L,U).
inters([],_,[]).
inters([H|T],L,[H|Lr]):-
member(H,L),
inters(T,L,Lr),!.
inters([H|T],L,Lr):-
not(member(H,L)),
inters(T,L,Lr).
member(H,[H|_]).
member(H,[_,L]):-
member(H,L).
Output:
Goal: union([a,b,c],[c,d,e],X)
X=[“a”,”b”,”c”,”d”,”e”]
1 solution
Goal:
Intersection([a,b,c],[c,d,e],X)
X=[“c”]
1 solution
Exp.no 13: Implementation of Factorial and Fibonacci of a given number
Aim: To write a program to implement the Factorial and Fibonacci of a given number
Algorithm:
Algorithm to Calculate Factorial of a given Number
Step 1: Start
Step 2: If the number is 0 then return 1 and goto step 5.
Step 3: Call the function with a number one less than the original number.
Step 4: Return product of number received in step 3 and number itself as factorial.
Step 5: Exit