ai record 2012

44
DEPARTMENT OF INFORMATION TECHNOLOGY IC RECORD Page 1 WATER JUG PROBLEM AIM: To write a C++ program to illustrate Water Jug Problem. ALGORITHM: 1. Start the program 2. Create class water and declare variables and necessary methods. 3. Using getdata() function, get the jug size, final stages and initialize initial stage as 0 and call jug-fill() function by passing necessary variables, Initialize l=j2,h,a=0. 4. In jug-fill function(),create an object 01,chck if(j1>=h), then assign h=h+l,l=0,a=h. 4.1. If(a==f1) then print next state as h and l and print “final state achieved”. 4.2. Else if(b>j1)then assign l=b-j1,b=j1 print next state as b&l,assign b=0,a=h.print next state b&l and call the jug-fill() function by passing variables. 5. Else assign l=h and print the next state b and l and call the jug-fill() function. 6. In main() function, create an object for class water and invoke the function getdata() using that object. 7. Stop the program. PROGRAM CODE: #include<iostream.h> #include<conio.h> #include<stdio.h> class water { public: int j1,j2,f1,f2,h,l,a; void getdata(); void jug_fill(int,int,int,int,int,int); }; void water::getdata() { water o; cout<<"water jug problem \n"; cout<<"enter the jug size:"; cin>>j1; cin>>j2; cout<<"enter the final stage of the jug:\n"; cin>>f1;

Upload: kowsalya-sankar

Post on 23-Oct-2014

346 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 1

WATER JUG PROBLEM

AIM: To write a C++ program to illustrate Water Jug Problem. ALGORITHM: 1. Start the program

2. Create class water and declare variables and necessary methods.

3. Using getdata() function, get the jug size, final stages and initialize initial stage as 0 and

call jug-fill() function by passing necessary variables, Initialize l=j2,h,a=0.

4. In jug-fill function(),create an object 01,chck if(j1>=h), then assign h=h+l,l=0,a=h.

4.1. If(a==f1) then print next state as h and l and print “final state achieved”.

4.2. Else if(b>j1)then assign l=b-j1,b=j1 print next state as b&l,assign b=0,a=h.print

next state b&l and call the jug-fill() function by passing variables.

5. Else assign l=h and print the next state b and l and call the jug-fill() function.

6. In main() function, create an object for class water and invoke the function getdata() using

that object.

7. Stop the program.

PROGRAM CODE: #include<iostream.h> #include<conio.h> #include<stdio.h> class water { public: int j1,j2,f1,f2,h,l,a; void getdata(); void jug_fill(int,int,int,int,int,int); }; void water::getdata() { water o; cout<<"water jug problem \n"; cout<<"enter the jug size:"; cin>>j1; cin>>j2; cout<<"enter the final stage of the jug:\n"; cin>>f1;

Page 2: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 2

cin>>f2; cout<<"initial stage:0 0\n"; l=j2; h=0; a=0; cout<<"next stage"<<h<<" "<<l<<endl; o.jug_fill(j1,j2,f1,h,l,a); } void water::jug_fill(int j1,int j2,int f1,int h,int l,int a) { water o1; if(j1>=h) { h=h+l; l=0; a=h;

if(a==f1) { cout<<"next state"<<h<<" "<<l<<endl; cout<<"final state achieved!!"<<endl; } else if(h>j1) { l=h-j1; h=j1; cout<<"next state:"<<h<<" "<<l<<endl; h=0; a=h; cout<<"next state:"<<h<<" "<<l<<endl; o1.jug_fill(j1,j2,f1,h,l,a); } else { l=h; cout<<"next state:"<<h<<" "<<l<<endl; o1.jug_fill(j1,j2,f1,h,l,a); } } } void main() { clrscr(); water o; o.getdata(); getch(); }

Page 3: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 3

SAMPLE INPUT AND OUTPUT: WATER JUG PROBLEM Enter the jug size: 4 3 Enter the final state of the jugs: 2 0 Initial state:0 0 Next state:0 3 Next state:3 3 Next state:4 2 Next state:0 2 Next state:2 0 Final state is achieved! RESULT: The above C++ program was executed and the output was verified.

Page 4: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 4

SINGLE PLAYER GAME AIM: To write a Single Player Game using C++ . ALGORITHM: 1. Create a class single and declare the variables and function under public.

2. In function get(), get the starting and ending arrary.

3. Call h.fn using goal object.

4. Check if the input array is not equal goal array in function h_fn(),then assign temp h.

5. In function copy(),check if array[i][j] is equal to zero then assign si=I; ,sj=j.

6. Check if si>3,assign temp_temp+1; call the function copy(),assign

temp[temp].arr[si][sj]=aRr[si+1][sj] assign arr[si][sj-1]=0.

7. Check if si>1, call the function copy() then assign the array value as the value above it.

8. Check if si>3, call function copy(), then assign the value below it.

9. In function print(), print the array values..

10. In function Find- min(),print the array values are checked for minimum.

11. In path() function, visited array values are obtained.

12. In function find_sol(),increasing value of the minimum array values are obtained.

13. In the main() function, the function get() is called.

14. Stop the program.

PROGRAM CODE: #include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> int tmp,no=0; class single { public: int i,j; int arr[10][10]; int h_no; void h_fn(); void get(); void print(); void copy(int); void find_sol();

Page 5: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 5

int find_min(); void path(); }temp[100],start,goal,visited[30]; void single::get() { cout<<"Enter the starting array:\n"; for(i=1;i<=3;i++) for(j=1;j<=3;j++) cin>>start.arr[i][j]; start.h_fn(); cout<<"Enter the ending array:\n"; for(i=1;i<=3;i++) for(j=1;j<=3;j++) cin>>goal.arr[i][j]; goal.h_fn(); } void single::h_fn() { int temp=0; for(i=1;i<=3;i++) for(j=1;j<=3;j++) if(arr[i][j]!=goal.arr[i][j]) ++temp; h_no=temp; } void single::copy(int t) { for(i=1;i<=3;i++) for(j=1;j<=3;j++) temp[t].arr[i][j]=arr[i][j]; } void single::find_sol() { tmp=0; int si,sj; for(i=1;i<=3;i++) for(j=1;j<=3;j++) if(arr[i][j]==0) { si=i; sj=j; break; } if(sj<3) { ++tmp; copy(tmp); temp[tmp].arr[si][sj]=arr[si][sj+1]; temp[tmp].arr[si][sj+1]=0;

Page 6: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 6

temp[tmp].h_fn(); } if(sj>1) { ++tmp; copy(tmp); temp[tmp].arr[si][sj]=arr[si][sj-1]; temp[tmp].arr[si][sj-1]=0; temp[tmp].h_fn(); } if(si>1) { ++tmp; copy(tmp); temp[tmp].arr[si][sj]=arr[si-1][sj]; temp[tmp].arr[si-1][sj]=0; temp[tmp].h_fn(); } if(si<3) { ++tmp; copy(tmp); temp[tmp].arr[si][sj]=arr[si+1][sj]; temp[tmp].arr[si+1][sj]=0; temp[tmp].h_fn(); } } void single::print() { for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { if(arr[i][j]==0) cout<<"*"<<" "; else cout<<arr[i][j]<<" "; } cout<<"\n"; } h_fn(); getch(); cout<<endl<<endl; } int single::find_min() { int min=1; for(i=1;i<tmp;i++) if(temp[i].h_no>temp[i+1].h_no) min=i+1;

Page 7: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 7

return min; } void single::path() { ++no; for(i=1;i<=3;i++) for(j=1;j<=3;j++) visited[no].arr[i][j]=arr[i][j]; visited[no].print(); if(visited[no].h_no==0) { printf("Solution found!"); getch(); exit(0); } visited[no].find_sol(); next_min:int min_h=0; min_h=find_min(); int flag=0; for(i=1;i<=no;i++) { flag=0; if(visited[i].h_no==temp[min_h].h_no) { for(j=1;j<=3;j++) for(int k=1;k<=3;k++) if(visited[i].arr[j][k]!=temp[min_h].arr[j][k]) { flag=1; } if(flag==0) { flag=2; break; } } } if(flag==0||flag==1) { temp[min_h].path(); } else if(flag==2) { temp[min_h].h_no=99; goto next_min; } } void main() { single p[20];

Page 8: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 8

clrscr(); start.get(); cout<<"\n Solution:\n"; start.path(); } SAMPLE INPUT AND OUTPUT: SINGLE PLAYER GAME Enter the starting array: 2 8 3 1 6 4 7 0 5 Enter the ending array: 1 2 3 8 0 4 7 6 5 Solution: 2 8 3 1 6 4 7 * 5 2 8 3 1 * 4 7 6 5 2 8 3 * 1 4 7 6 5 8 * 3 2 1 4 7 6 5 8 * 3 2 1 4 7 6 5 8 1 3 2 * 4 7 6 5 8 1 3 * 2 3 7 6 5

Page 9: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 9

* 1 3 8 2 4 7 6 5 1 2 3 8 * 4 7 6 5 Solution Found! RESULT: The above C++ program was executed and the output was verified.

Page 10: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 10

TWO PLAYER GAME

AIM: To develop a C++ program to solve the Two Player Game. ALGORITHM: 1. Start the program.

2. Entries are made depending on the three constraints,

0 indicates free cell space.

3 indicates the move of user(max).

5 indicates the move of computer(min).

3. Max indicates the first move.

4. Depending upon max’s move, min makes its move.

5. The play alternates until the leaf nodes corresponding to the terminal state such that one

player as 3 entries in a row, column or diagonal.

6. After each move, the corresponding results are displayed.

7. Stop the program.

PROGRAM CODE: #include<iostream.h> #include<stdio.h> #include<conio.h> void printstate(int s[3][3]) { cout<<"\n\n\t\t\t"; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<<s[i][j]<<"\t"; } cout<<endl<<"\t\t\t"; } } void makemove(int arr[][3],int i) { int j,k; switch(i) {

Page 11: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 11

case 0: for(int j=0;j<3;j++) { if(arr[0][j]==0) { arr[0][j]=5; break; } } break; case 1: for(j=0;j<3;j++) { if(arr[1][j]==0) { arr[1][j]=5; break; } } break;

case 2: for(j=0;j<3;j++) { if(arr[2][j]==0) { arr[2][j]=5; break; } } break; case 3: for(j=0;j<3;j++) { if(arr[j][0]==0) { arr[j][0]=5; break; } } break; case 4: for(j=0;j<3;j++) { if(arr[j][1]==0) { arr[j][1]=5; break;

Page 12: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 12

} } break; case 5: for(j=0;j<3;j++) { if(arr[j][2]==0) { arr[j][2]=5; break; } } break; case 6: for(j=0;j<3;j++) { if(arr[j][j]==0) { arr[j][j]=5; break; } } break; case 7: for(j=0,k=2;j<3&&k>0;j++,k--) { if(arr[j][k]==0) { arr[j][k]=5; break; } } break; } } void main() { clrscr(); int arr[3][3],a[8],x,y,over=0,movemade=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) arr[i][j]=0; cout<<"Instructions:"<<endl; cout<<"---------------:"<<endl; cout<<"\t 0 indicates that the cell is free"<<endl; cout<<"\t 3 indicates the move of the user"<<endl; cout<<"\t 5 indicates the move of computer"<<endl;

Page 13: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 13

while(1) { movemade=0; printstate(arr); cout<<"make your move"<<endl; cout<<"-----------"<<endl; cout<<"Enter the row number:"; cin>>x; cout<<"Enter the column number:"; cin>>y; if((arr[x-1][y-1]==3)||arr[x-1][y-1]==5) { cout<<"\n\n\t\t\t The cell is not free\a\n"; cout<<"\n\t\t Make another choice\n"; continue; } else arr[x-1][y-1]=3; a[0]=arr[0][0]+arr[0][1]+arr[0][2]; a[1]=arr[1][0]+arr[1][1]+arr[1][2]; a[2]=arr[2][0]+arr[2][1]+arr[2][2]; a[3]=arr[0][0]+arr[1][0]+arr[2][0]; a[4]=arr[0][1]+arr[1][1]+arr[2][1]; a[5]=arr[0][2]+arr[1][2]+arr[2][2]; a[6]=arr[0][0]+arr[1][1]+arr[2][2]; a[7]=arr[0][2]+arr[1][1]+arr[2][0]; for(int i=0;i<8;i++) { if(a[i]==15) { printstate(arr); cout<<"Computer won.Better luck next time"; over=1; break; }

if(a[i]==9) { printstate(arr); cout<<"You won.Well done"; over=1; break; } } if(over==1) break; for(i=0;i<8;i++) {

Page 14: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 14

if(a[i]==10) { makemove(arr,i); printstate(arr); cout<<"\n\nComputer won.Better luck next time"; movemade=1; break; } } if(movemade==1) break; for(i=0;i<8;i++) { if(a[i]==6) { makemove(arr,i); movemade=1; break; } } if(movemade==1) continue; for(i=0;i<8;i++) { if(a[i]==8) { makemove(arr,i); movemade=1; break; } } if(movemade==1) continue; for(i=0;i<8;i++) { if(a[i]==3) { makemove(arr,i); movemade=1; break; } }

if(movemade==1) continue; printstate(arr); cout<<"\n\n Game is draw.Better luck next time"; break; } getch();

Page 15: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 15

} SAMPLE INPUT AND OUTPUT: TWO PLAYER GAME Instructions: ------------- 0 indicates that the cell is free 3 indicates the move of the user 5 indicates the move of the computer 0 0 0 0 0 0 0 0 0 Make your move -------------------- Enter the row number:2 Enter the column number:2 0 0 0 5 3 0 0 0 0 Make your move -------------------- Enter the row number:3 Enter the column number:3 5 0 0 5 3 0 0 0 3 Make your move -------------------- Enter the row number:3 Enter the column number:1 5 0 0 5 3 0 3 5 3

Page 16: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 16

Make your move -------------------- Enter the row number:1 Enter the column number:3 5 0 3 5 3 0 3 5 3 You won. Well done! RESULT: The above C++ program was executed and the output was verified.

Page 17: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 17

A* ALGORITHM AIM: To develop a C program to derive A* algorithm. ALGORITHM: 1. Start the program.

2. Place the starting node s on respectively and s on open.

3. If open is empty, stop and return Failure.

4. Remove from open node that has the smallest value f*(n). If the node is a goal node, return

success and stop.

5. Otherwise expand n, generating all of its successors ‘n’ and place n on closed. For every

successor ’n’, if n is not already open or closed attach a back pointer to n.Compute f*(n) and

place it an open.

6. Return to step2.

7. Stop the program.

PROGRAM CODE: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { char name; int g; int h; int f; char parent; }open[20],closed[20],temp[10]; static int open_front,open_rear; static int closed_front,closed_rear; int current; int n,pos; int check(char); void astar(); void next() { int i,j; printf("\n\n\t Enter the number of childs:");

Page 18: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 18

scanf("%d",&n); pos=0; for(i=0;i<n;i++) { printf("\n\n\t Enter the next node of %c:",closed[current]); temp[pos].name=getche(); printf("\n\n\t Enter its heuristic value:"); scanf("%d",&temp[pos].h); temp[pos].g=closed[current].g+1; temp[pos].f=temp[pos].g+temp[pos].h; temp[pos].parent=closed[current].name; pos++; } } int check(char temp) { int i; for(i=0;i<open_rear;i++) { if(open[open_rear].name==temp) { return 0; } } for(i=0;i<closed_rear;i++) { if(closed[closed_rear].name==temp) { return 0; } } return 1; } void add_open() { int i; char temp1; for(i=0;i<n;i++) { temp1=temp[i].name; if(check(temp1)) { open[open_rear].name=temp[i].name; open[open_rear].g=temp[i].g; open[open_rear].h=temp[i].h; open[open_rear].f=temp[i].f; open[open_rear].parent=temp[i].parent; open_rear++; } }

Page 19: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 19

} void swap(int i,int j) { node t; t.name=open[i].name; t.h=open[i].h; t.g=open[j].g; t.f=open[i].f; t.parent=open[i].parent; open[i].name=open[j].name; open[i].h=open[j].h; open[i].g=open[j].g; open[i].f=open[j].f; open[i].parent=open[j].parent; open[j].name=t.name; open[j].h=t.h; open[j].g=t.g; open[j].f=t.f; open[j].parent=t.parent; } void find_min() { int i,j,min; for(i=open_front;i<open_rear-1;i++) { for(j=i+1;j<open_rear;j++) { if(open[i].f>open[i].f) swap(i,j); } } } void goal() { int i,dist=0; clrscr(); printf("\n\n\tthe optimal path is :"); for(i=closed_front;i<closed_rear;i++) { printf("\t%c->",closed[i].name); dist+=closed[i].h; } printf("\n\n\tThe optimal path is %d",dist); printf("\t%c",closed[i].name); getch(); exit(0); } void add_closed() { char temp;

Page 20: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 20

closed[closed_rear].name=open[open_front].name; closed[closed_rear].h=open[open_front].h; closed[closed_rear].g=open[open_front].g; closed[closed_rear].f=open[open_front].f; closed[closed_rear].parent=open[open_front].parent; open_front++; current=closed_front; closed_rear++; s1:printf("\n\n\t do you want to continue(y/n)?"); temp=getche(); if(temp=='y'||temp=='Y') { astar(); } else if(temp=='n'||temp=='N') goal(); else goto s1; } void astar() { next(); add_open(); find_min(); add_closed(); } void main() { int i,j,temp; char name; clrscr(); printf("\n\n\t A* algorithm"); printf("\n\n\t enter the root node"); scanf("%c",&name); printf("\n\n\tenter its heuristic value:"); scanf("%d",&temp); getch(); closed[closed_rear].name=name; closed[closed_rear].h=temp; closed[closed_rear].parent='0'; closed[closed_rear].f=temp; closed_rear++; current=closed_front; astar(); }

Page 21: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 21

SAMPLE INPUT AND OUTPUT: A* ALGORITHM Enter the root node: A Enter its heuristic value: 10 Enter the number of childs: 3 Enter the next node of A: B Enter its heuristic value: 8 Enter the next node of A: C Enter its heuristic value: 6 Enter the next node of A: D Enter its heuristic value: 4 Do you want to continue(y/n)?: y Enter the number of childs: 2 Enter the next node of A: E Enter its heuristic value: 1 Enter the next node of A: F Enter its heuristic value: 2 Do you want to continue(y/n)?: n The optimal path is: A -> D -> E -> Distance of the optimal path is: 15 RESULT: The above C program was executed and the output was verified.

Page 22: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 22

UNIFICATION ALGORITHM AIM: To write a C program to implement Unification algorithm. ALGORITHM: 1. Start the program.

2. Declare the variables.

3. In main get the first and second literals and call the unify(l1,l2).

4. In Unify(), assign a=chkfun(l1,l2),if(a=0) then print function not match.

Else check a=chkarg(l1,l2),if(a=0) then print the number of argument does not match.else

compare arg1&arg2, if it is ‘0’, then print Unification not needed.

Else call chksub(l1,l2).

5. In chkfun(), use for(i=0 to strlen(l1), check if(l1[i]!=’(’), then strcat(new1,x),

Perform the same for second literal l2. Compare new(1&2) if it is equal return 1, else o.

6. In chkarg(), use for(i=0 to strlen(l1), check if(l1[i]==’(’, then execute while until l1[i]=’)’.

Concatenate(arg & x). perform the same for l2 also. If length of arg1 and arg2 are equal then

return 1 else return 0.

7. In chksub(), check l1[i]&l2[i], if it is not equal print the result.

8. Stop the program.

PROGRAM CODE: #include<conio.h> #include<string.h> int s1=0,s2=0; char new1[50],new2[50],arg1[25],arg2[25]; main() { char l1[50],l2[15]; clrscr(); printf("\nUNIFICATION ALGORITHM"); printf("\n____________________\n"); printf("\nEnter the first literal:"); scanf("%s",l1); printf("Enter the second literal:"); scanf("%s",l2);

Page 23: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 23

unify(l1,l2); getch(); } unify(char l1[50],char l2[50]) { int a,b; a=chkfun(l1,l2); if(a==0) { printf("\n The function name does not match!”);

printf(“\n Unification cannot be done!"); return; } else { a=chkarg(l1,l2); if(a==0) { printf("\nThe no.of arg do not match!”);

printf(“\n Unification cannot be done!"); return; }

else { if(strcmp(arg1,arg2)==0) printf("\nUnification not needed!”);

printf(“\n Both the predicate and literals are same!"); else { printf("Unification possible"); chksub(l1,l2); } return; } } } chkfun(char l1[50],char l2[50]) { int i,c=0; char x[1]; for(i=0;i<strlen(l1);i++) { if(l1[i]!='(') { c++; x[0]=l1[s1]; x[1]='\0';

Page 24: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 24

strcat(new1,x); s1++; } if(l1[i]=='(') break; }

for(i=0;i<strlen(l2);i++) { if(l2[i]!='(') { c++; x[0]=l2[s2]; x[1]='\0'; strcat(new2,x); s2++; } if(l2[i]=='(') break; } if(strcmp(new1,new2)==0) return 1; else return 0; } chkarg(char l1[50],char l2[50]) { int l; int i; char x[1]; for(i=0;i<strlen(l1);i++) { if(l1[i]=='(') { i++; while(l1[i]!=')') { if(l1[i]!=')') { x[0]=l1[i]; x[1]='\0'; } strcat(arg1,x); i++; } break; } else continue;

Page 25: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 25

} for(i=0;i<strlen(l2);i++) { if(l2[i]=='(') { i++; while(l2[i]!=')') { if(l2[i]!=')') { x[0]=l2[i]; x[1]='\0'; strcat(arg2,x); } i++; } break; } else continue; } if(strlen(arg1)==strlen(arg2)) return 1; else return 0; } chksub(char l1[50],char l2[50]) { int i; for(i=0;i<strlen(l1);i++) { if(l1[i]!=l2[i]) printf("\nSubstitution:%c/%c",l2[i],l1[i]); } return; }

Page 26: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 26

SAMPLE INPUT AND OUTPUT: UNIFICATION ALGORITHM -------------------------------------- Enter the first literal: abc(p,q) Enter the second literal: abc(x,y) Unification possible! Substitution: x / p Substitution: y / q UNIFICATION ALGORITHM -------------------------------------- Enter the first literal: abc(p,q) Enter the second literal: abcd(x,y) The function name does not match. Unification cannot be done! UNIFICATION ALGORITHM -------------------------------------- Enter the first literal: abc(p,q) Enter the second literal: abc(x) The number of arguments does not match. Unification cannot be done! UNIFICATION ALGORITHM -------------------------------------- Enter the first literal: abc(p,q) Enter the second literal: abc(p,q) Unification not needed. Both the predicate and literals are same! RESULT:

Page 27: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 27

The above C program was executed and the output was verified.

N-QUEENS PROBLEM AIM: To develop a C++ program to solve N-Queens Problem. Algorithm:

1. Start the program.

2. Declare int,char.

3. Print n-queens Problem.

4. Print Enter the no. of Queens.

5. Get the value of no. of queens.

6. Get the solution.

7. A[i][x[i]] �Q.

8. nqueens(k+1,n).

9. Stop the program.

PROGRAM CODE: #include<iostream.h> #include<stdio.h> #include<conio.h> #include<math.h> int x[10],n,count=0; unsigned char a[10][10]; void nqueens(int,int); void main() { int i,j,k,n; clrscr(); cout<<"\n\n N-QUEENS PROBLEM"; cout<<"\n\t Enter the number of queens:"; cin>>n; nqueens(1,n); cout<<"\t The number of solutions are:"<<count; getch(); } int place(int k,int i) {

Page 28: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 28

int j; for(j=1;j<=k-1;j++) { if((x[j]==i)||abs(x[j]-i)==abs(j-k)) { return(0); } } return(1); } void nqueens(int k,int n) { int i,flag,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) a[i][j]='-'; for(i=1;i<=n;i++) { flag=place(k,i); if(flag==1) { x[k]=i; if(k==n) { count++; cout<<"\n\n"; cout<<"Solution"<<count<<":\n"; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { a[i][x[i]]='Q'; cout<<"\t"<<a[i][j]; } cout<<"\n"; } cout<<"\n"; } else nqueens(k+1,n); } } }

Page 29: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 29

SAMPLE INPUT AND OUTPUT: N-QUEENS PROBLEM Enter the number of queens:4 Solution1: - Q - - - - - Q Q - - - - - Q - Solution2: - - Q - Q - - - - - - Q - Q - - The number of solutions are:2 RESULT: The above C program was executed and the output was verified.

Page 30: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 30

RULE BASED SYSTEMS

AIM: To write a C++ program to implement a Rule Based Systems for medical diagnosis.

ALGORITHM:

1. Start the program.

2. Create a structure and declare necessary methods.

3. Set the rules for seven disease in binary form. Example: For Measles – it is 1101110.

4. In function getinput(), get the symptoms from the patient.

5. Call comp() function.

6. Compare the reply,y if it is yes , then set ans[tcmp]=1, else set ans[tcmp]=0.

7. In comp(), check if (p[j].bit[k]=ans[k]) then set flag , else unset flag and break.

8. If flag =1, then print the disease name.

9. Stop the program.

PROGRAM CODE: #include<iostream.h> #include<stdio.h> #include<conio.h> #include<string.h> struct expert { char *name; int bit[17]; }p[17]; void comp(); void check(int); int ques,ans[17]; char *rep; void setrules() { p[1].name="Measles"; p[1].bit[1]=1; p[1].bit[2]=1;

Page 31: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 31

p[1].bit[3]=0; p[1].bit[4]=1; p[1].bit[5]=1; p[1].bit[6]=1; p[1].bit[7]=0; p[2].name="German Measles"; p[2].bit[1]=1; p[2].bit[2]=1; p[2].bit[3]=1; p[2].bit[4]=1; p[2].bit[5]=0; p[2].bit[6]=0; p[2].bit[7]=0;

p[3].name="Flu";

p[3].bit[1]=1; p[3].bit[2]=0; p[3].bit[3]=1; p[3].bit[4]=1; p[3].bit[5]=1; p[3].bit[6]=1; p[3].bit[7]=1;

p[4].name="Mumps";

p[4].bit[1]=1; p[4].bit[2]=0; p[4].bit[3]=0; p[4].bit[4]=0; p[4].bit[5]=0; p[4].bit[6]=0; p[4].bit[7]=0; p[5].name="Chicken pox"; p[5].bit[1]=1; p[5].bit[2]=1; p[5].bit[3]=0; p[5].bit[4]=0; p[5].bit[5]=0; p[5].bit[6]=0; p[5].bit[7]=1; p[6].name="Typhoid"; p[6].bit[1]=1; p[6].bit[2]=0; p[6].bit[3]=0; p[6].bit[4]=0; p[6].bit[5]=0; p[6].bit[6]=1;

Page 32: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 32

p[6].bit[7]=0;

p[7].name="Cholera"; p[7].bit[1]=1; p[7].bit[2]=0; p[7].bit[3]=0; p[7].bit[4]=0; p[7].bit[5]=1; p[7].bit[6]=1; p[7].bit[7]=0; } void getinput() { cout<<"\nGuess the Disease!!!\n"; ques=1; cout<<"Does the person have fever?"; cin>>rep; check(ques); ques++; cout<<"Does the person have rash?"; cin>>rep; check(ques); ques++; cout<<"Does the person have headache?"; cin>>rep; check(ques); ques++; cout<<"Does the person have running nose?"; cin>>rep; check(ques); ques++; cout<<"Does the person have conjunctivitis?"; cin>>rep; check(ques); ques++;

cout<<"Does the person have cough?"; cin>>rep; check(ques); ques++; cout<<"Does the person have bodyache?"; cin>>rep; check(ques); ques++; comp(); } void check(int temp) { if(strcmp(rep,"y")==0) ans[temp]=1;

Page 33: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 33

else ans[temp]=0; return; } void comp() { int j,k,flag=0,flag1=0; for(j=1;j<=7;j++) { for(k=1;k<=7;k++) { if(p[j].bit[k]==ans[k]) flag=1; else { flag=0; break; } }

if(flag==1) { flag1=1; cout<<"The person is suffering from "<<p[j].name<< " disease!"; continue; } } if(flag1==0) cout<<"None of the symptoms matches the given condition"; } void display() { cout<<"\n Expert sstem for medical diagnosis"; cout<<"\n The diseases are:"; cout<<"\nMeasles \n German measles \n Flu \n Mumps \n Chicken pox \n Typhoid \n Cholera \n"; } void main() { clrscr(); setrules(); display(); getinput(); getch(); }

Page 34: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 34

SAMPLE INPUT AND OUTPUT: RULE BASED SYSTEMS FOR MEDICAL DIAGNOSIS The diseases are: Measles Germen measles Flu Mumps Chicken Pox Typhoid Cholera Guess the Disease!!! Does the person have fever? y Does the person have rash? y Does the person have headache? n Does the person have running nose? n Does the person have conjunctivitis? n Does the person have cough? n Does the person have body ache? y The person is suffering from Chicken Pox disease! RESULT: The above C++ program was executed and the output was verified.

Page 35: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 35

PREDICATE TO PROPOSITIONAL LOGIC

AIM: To develop a C program to convert the given facts in the propositional logic. ALGORITHM: 1. Start the program.

2. Get the fact.

3. Analyze the fact word by word.

4. If the first word is “if” then break the fact into two parts by using the following rule:

a) The “if” clause is the first part.

b) The “then” clause is the second part.

5. Do the following steps for both the parts independently.

a) Analyze the sentence word by word.

b) Check if it is a pronoun, if so, neglect and proceed to the next word.

c) Check if it is an auxillary verb, if so neglect and proceed to next word.

6. Print the word and stop.

PROGRAM CODE: #include<graphics.h> #include<stdio.h> #include<conio.h> #include<string.h> int conj(char *); int noun(char *); int article(char *); int prep(char *); int aux(char *); void process(char *); int length=0; char ans[100]; void main() { char a[90],s[30]; int i,j; clrscr(); printf("\nEnter the fact:");

Page 36: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 36

gets(a); i=0; while((a[i]!='\n')&&(a[i]!='\0')) { j=0; while((a[i]!=' ')&&(a[i]!='\0')&&(a[i]!='\n')) { s[j]=a[i]; i++; j++; } while(a[i]==' ') i++; s[j]='\0'; printf("\n %s",s); process(s); } printf("\n prepositional logic:%s",ans); getch(); } void process(char *s) { int i; if(noun(s)||aux(s)||article(s)||prep(s)||(!strcmpi("if",s))||(!strcmpi("all",s))||

(!strcmpi("every",s))) return; else if(!strcmpi(s,"then")) { ans[length]='-'; length++; } else if(conj(s)) { i=conj(s); if(i==1)ans[length]='&'; if(i==2)ans[length]='|'; if(i==3)ans[length]='~'; length++; } else { i=0; while(s[i]!='\0') { ans[length]=s[i]; length++; i++; }

Page 37: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 37

ans[length]='\0'; } } int noun(char *s) { char pron[10][20]={"he","she","it","they"}; int i=0; for(i=0;i<4;i++) if(!strcmpi(pron[i],s)) return(1); return(0); } int article(char *s) { char article[10][10]={"the","a","an"}; int i=0; for(i=0;i<3;i++) if(!strcmpi(article[i],s)) return(1); return(0); } int prep(char *s) { char prep[10][10]={"to","on","in","at","by","from"}; int i=0; for(i=0;i<6;i++) if(!strcmpi(prep[i],s)) return(1); return(0); } int aux(char *s) {

char aux[20][20] = {"is","was","has","have","had","am","were","are", "will","would","should","may","might"};

int i=0; for(i=0;i<14;i++) if(!strcmpi(aux[i],s)) return(1); return(0); } int conj(char *s) { char log[4][20]={"the","a","an"}; int i=0; for(i=0;i<4;i++)

Page 38: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 38

if(!strcmpi(log[i],s)) return(1); return(0); } SAMPLE INPUT AND OUTPUT: PREDICATE TO PROPOSITION LOGIC Enter the input in predicate form: if it is raining then it is not sunny if it not raining then it is not sunny The prepositional logic is: raining->not sunny RESULT: The above C program was executed and the output was verified.

Page 39: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 39

BREADTH FIRST SEARCH. AIM To develop a C++ program to implement breadth first search. ALGORITHM

1. Initialize the node and place the root node in queue and mark as visited. 2. Remove the first node from the queue and find its successors and mark as visited. 3. Place the visited node in the queue. 4. Store the node that is removed from the queue in the array. 5. If queue is not empty, then go to step 2. 6. If queue is empty, the return. 7. Display the result.

PROGRAM CODE #include<conio.h> #include<iostream.h> #define MAX 10 using namespace std; class BFS { private : int n; int adj[MAX][MAX]; int visited[MAX]; public : void bfs(int); void readmatrix(); }; void BFS :: readmatrix() { int i,j; cout<<"\nenter the number of vertices in the graph : "; cin>>n; cout<<"\nenter the adjacency matrix\n"; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>adj[i][j]; for(i=1;i<=n;i++) visited[i]=0; } void BFS :: bfs(int source)

Page 40: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 40

{ int queue[MAX]; int i,front,rear,root; front=rear=0; visited[source]=1; queue[rear++]=source; cout<<source<<""; while(front!=rear) { root=queue[front]; for(i=1;i<=n;i++) if(adj[root][i]&&!visited[i]) { visited[i]=1; queue[rear++]=i; cout<<i<<""; } front++; } } int main() { clrscr(); int source; BFS breadth; breadth.readmatrix(); cout<<"enter the source : "; cin>>source; cout<<"\nthe nodes visited in bfs order is "; breadth.bfs(source); return 0; } OUTPUT enter the number of vertices in the graph : 8 enter the adjacency matrix 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 1 1 1 0 enter the source : 1

Page 41: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 41

the nodes visited in bfs order is 1 2 3 4 5 6 7 8 RESULT Thus, the above C++ Program for breadth first search was executed and verified.

Page 42: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 42

DEPTH FIRST SEARCH AIM To develop a C++ program to implement depth first search.

ALGORITHM

1. Make all the node in visited and output array as zero initially. 2. Visit next node which is present below the initial node. 3. Now mark this as a visited node and add to the stack. 4. The node that are visited in the depth wise that is all the left successors area traversed

first. 5. Backtracking is performed for the non-visited nodes. 6. Display the result.

PROGRAM CODE #include<conio.h> #include<iostream.h> #define MAX 10 class DFS { private : int n; int adj[MAX][MAX]; int visited[MAX]; public : void dfs(int); void readmatrix(); }; void DFS :: readmatrix() { int i,j; cout<<"\nenter the number of vertices in the graph : "; cin>>n; cout<<"\nenter the adjacency matrix\n"; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>adj[i][j]; for(i=1;i<=n;i++) visited[i]=0; } void DFS :: dfs(int source)

Page 43: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 43

{ int i; visited[source]=1; cout<<source<<" "; for(i=1;i<=n;i++) { if(adj[source][i]&&!visited[i]) { dfs(i); } } } int main() { clrscr(); int source; DFS depth; depth.readmatrix(); cout<<"\nenter the source : "; cin>>source; cout<<"\nthe nodes visited in dfs order is "; depth.dfs(source); return 0; } OUTPUT enter the number of vertices in the graph : 8 enter the adjacency matrix 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 1 1 1 0 enter the source : 1 the nodes visited in dfs order is 1 2 4 8 5 6 3 7 RESULT

Thus, the above C++ Program for depth first search was executed and verified.

Page 44: AI Record 2012

DEPARTMENT OF INFORMATION TECHNOLOGY

IC RECORD Page 44