22
Page1 Practical Record On Artificial Intelligence Lab Submitted to Bon Maharaj Engineering College, Vrindavan (Mathura) Affiliated to Uttar Pradesh Technical University, Lucknow (UP) In partial fulfillment of the degree of Bachelor of Technology In Computer Science & Engineering (8 th SEM) Submitted By Vasundhara Ghosh Roll No: 1036710012 BON MAHARAJ ENGINEERING COLLEGE VRINDAVAN (MATHURA)

Program for Artificial intelligence (b.tech Final year) UPTU

Embed Size (px)

DESCRIPTION

This is documentation made by final year student as per completion of their A.I. lab. you can download it. and explore yourself

Citation preview

Page 1: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e1

Practical Record On

Artificial Intelligence Lab Submitted to

Bon Maharaj Engineering College, Vrindavan (Mathura) Affiliated to Uttar Pradesh Technical University, Lucknow (UP)

In partial fulfillment of the degree of

Bachelor of Technology

In

Computer Science & Engineering

(8thSEM)

Submitted By

Vasundhara Ghosh Roll No: 1036710012

BON MAHARAJ ENGINEERING COLLEGE

VRINDAVAN (MATHURA)

Page 2: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e2

INDEX

S. NO. Title Page No. Remarks

1. Program to design Tic-tac-toe game 3

2. Program for Breath first search 9

3. Program to implement Depth –first-search 10

4. Program to N-Queen’s Problem 12

5. Program to implement N-Queen problem by iterative method

14

6. WAP to solution of n-queen problem using array

16

7. WAP to implement max-min problem 18

8. WAP in c to find Hamiltonian path 20

Page 3: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e3

1. Program to design Tic -tac-toe game

#include<stdio.h>

#include<conio.h>

void Board();

voidPlayerX();

voidPlayerO();

voidPlayer_win();

void check();

int win=0,wrong_X=0,wrong_O=0,chk=0;

charname_X[30];

charname_O[30];

intpos_for_X[3][3];

intpos_for_O[3][3];

intpos_marked[3][3];

void main()

{

inti,ch,j;

charans;

/* clrscr();

printf("\n\t\t\t\tTIC TAC TOE");

printf("\n\t\t\t\t");

for(i=1;i<=11;i++)

{

delay(10000);

printf("*");

}*/

do

{

clrscr();

printf("\n\t\t\t\tTIC TAC TOE");

printf("\n\t\t\t\t");

for(i=1;i<=11;i++)

{

delay(10000);

printf("*");

}

printf("\n1.Start The Game");

printf("\n2.Quit The Game");

printf("\nEnter your choice(1-2) : ");

scanf("%d",&ch);

switch(ch)

{

case 1:

chk=0;

win=0;

Page 4: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e4

for(i=1;i<=3;i++)

{

for(j=1;j<=3;j++)

{

pos_for_X[i][j]=0;

pos_for_O[i][j]=0;

pos_marked[i][j]=0;

}

}

printf("\n\n");

clrscr();

printf("\nEnter the name of the player playing

for \'X\': ");

fflush(stdin);

gets(name_X);

printf("\nEnter the name of the player playing

for \'O\': ");

fflush(stdin);

gets(name_O);

Board();

for(;;)

{

if(win==1)

break;

check();

if(chk==9)

{

printf("\n\t\t\t MATCH DRAWS!!");

printf("\nPress any key....");

break;

}

else

chk=0;

printf("\nTURN FOR %s:",name_X);

PlayerX();

do

{

if(wrong_X!=1)

break;

wrong_X=0;

printf("\nTURN FOR %s:",name_X);

PlayerX();

}while(wrong_X==1);

check();

if(chk==9)

{

printf("\n\t\t\tMATCH DRAWS");

printf("\nPress any key....");

break;

}

else

chk=0;

printf("\nTURN FOR %s:",name_O);

PlayerO();

do

{

Page 5: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e5

if(wrong_O!=1)

break;

wrong_O=0;

printf("\nTURN FOR %s:",name_O);

PlayerO();

}while(wrong_O==1);

}

Board();

if(win!=1)

{

printf("\n\t\t\tMATCH DRAWS!!");

printf("\nPress any key.......");

}

getch();

break;

case 2:

printf("\n\n\n\t\t\tThank You For Playing The

Game.");

printf("\n\t\t\t###############################");

getch();

exit(1);

break;

}

printf("\nWant To Play(Y/N) ? ");

fflush(stdin);

scanf("%c",&ans);

}while(ans=='y' || ans=='Y');

}

void Board()

{

inti,j;

clrscr();

printf("\n\t\t\t\tTIC TAC TOE BOARD");

printf("\n\t\t\t\t*****************");

printf("\n\n\n");

printf("\n\t\t\t 1\t 2\t 3");

for(i=1;i<=3;i++)

{

printf("\n \t\t\t _____________________________");

printf("\n \t\t\tº\t º\t º\t º");

printf("\n\t\t%d\t",i);

for(j=1;j<=3;j++)

{

if(pos_for_X[i][j]==1)

{

printf(" X");

printf(" ");

}

else if(pos_for_O[i][j]==1)

{

printf(" O");

Page 6: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e6

printf(" ");

}

else

{

printf(" ");

continue;

}

}

printf("\n\t\t\tº\t º\t º\t º");

}

printf("\n\t\t\t------------------------------");

Player_win();

}

voidPlayerX()

{

introw,col;

if(win==1)

return;

printf("\nEnter the row no. : ");

fflush(stdin);

scanf("%d",&row);

printf("Enter the column no. : ");

fflush(stdin);

scanf("%d",&col);

if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 ||

col>3)

{

printf("\nWRONG POSITION!! Press any key.....");

wrong_X=1;

getch();

Board();

}

else

{

pos_for_X[row][col]=1;

pos_marked[row][col]=1;

Board();

}

}

voidPlayerO()

{

introw,col;

if(win==1)

return;

printf("\nEnter the row no. : ");

scanf("%d",&row);

printf("Enter the column no. : ");

scanf("%d",&col);

if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 ||

col>3)

{

printf("\nWRONG POSITION!! Press any key....");

wrong_O=1;

getch();

Page 7: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e7

Board();

}

else

{

pos_for_O[row][col]=1;

pos_marked[row][col]=1;

Board();

}

}

voidPlayer_win()

{

int i;

for(i=1;i<=3;i++)

{

if(pos_for_X[i][1]==1 &&pos_for_X[i][2]==1

&&pos_for_X[i][3]==1)

{

win=1;

printf("\n\nRESULT: %s wins!!",name_X);

printf("\nPress any key............");

return;

}

}

for(i=1;i<=3;i++)

{

if(pos_for_X[1][i]==1 &&pos_for_X[2][i]==1

&&pos_for_X[3][i]==1)

{

win=1;

printf("\n\nRESULT: %s wins!!",name_X);

printf("\nPress any key............");

return;

}

}

if(pos_for_X[1][1]==1 &&pos_for_X[2][2]==1

&&pos_for_X[3][3]==1)

{

win=1;

printf("\n\nRESULTL: %s wins!!",name_X);

printf("\nPress any key......");

return;

}

else if(pos_for_X[1][3]==1 &&pos_for_X[2][2]==1 &&

pos_for_X[3][1]==1)

{

win=1;

printf("\n\nRESULT: %s wins!!",name_X);

printf("\nPress any key.....");

return;

}

for(i=1;i<=3;i++)

{

if(pos_for_O[i][1]==1 &&pos_for_O[i][2]==1

&&pos_for_O[i][3]==1)

{

Page 8: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e8

win=1;

printf("\n\nRESULT: %s wins!!",name_O);

printf("\nPress any key.....");

return;

}

}

for(i=1;i<=3;i++)

{

if(pos_for_O[1][i]==1 &&pos_for_O[2][i]==1

&&pos_for_O[3][i]==1)

{

win=1;

printf("\n\nRESULT: %s wins!!",name_O);

printf("\nPress any key.....");

return;

}

}

if(pos_for_O[1][1]==1 &&pos_for_O[2][2]==1

&&pos_for_O[3][3]==1)

{

win=1;

printf("\n\nRESULT: %s wins!!",name_O);

printf("\nPress any key.....");

return;

}

else if(pos_for_O[1][3]==1 &&pos_for_O[2][2]==1 &&

pos_for_O[3][1]==1)

{

win=1;

printf("\n\nRESULT: %s wins!!",name_O);

printf("\nPress any key.....");

return;

}

}

void check()

{

inti,j;

for(i=1;i<=3;i++)

{

for(j=1;j<=3;j++)

{

if(pos_marked[i][j]==1)

chk++;

else

continue;

}

}

}

Page 9: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e9

2. Program for Breath first search

#include<stdio.h>

#include<conio.h>

int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;

voidbfs(int v)

{

for(i=1;i<=n;i++)

if(a[v][i] && !visited[i])

q[++r]=i;

if(f<=r)

{

visited[q[f]]=1;

bfs(q[f++]);

}

}

void main()

{

int v;

clrscr();

printf("\n Enter the number of vertices:");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

q[i]=0;

visited[i]=0;

}

printf("\n Enter graph data in matrix form:\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&a[i][j]);

printf("\n Enter the starting vertex:");

scanf("%d",&v);

bfs(v);

printf("\n The node which are reachable are:\n");

for(i=1;i<=n;i++)

if(visited[i])

printf("%d\t",i);

else

printf("\n Bfs is not possible");

getch();

}

Page 10: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e10

3. Program to implement Depth –first-search

#include<stdio.h>

#include<conio.h>

char stack[20];

int top=-1, n;

chararr[20];

chardfs(int );

charajMat[20][20];

char b[20];

void display();

int p=0;

int main()

{

char v;

int l=0;

printf("Enter the number of nodes in a graph");

scanf("%d",&n);

printf("Enter the value of node of graph");

for(int i=0; i<n; i++)

{

scanf("%s",&b[i]);

}

char k=b[0];

printf("Enter the value in adjancency matrix in from of 'Y' or

'N'\n");

printf("\nIf there is an edge between the two vertices then

enter 'Y' or 'N'\n");

for(int i=0; i<n; i++)

printf(" %c ",b[i]);

for(int i=0;i<n; i++)

{

printf("\n%c ",b[i]);

for(int j=0; j<n; j++)

{

printf("%c ",v=getch());

ajMat[i][j]=v;

}

printf("\n\n");

}

for(int i=0;i<n;i++)

{

l=0;

while(k!=b[l])

l++;

k=dfs(l);

}

Page 11: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e11

display();

getch();

}

void display()

{

printf(" DFS of Graph : ");

for(int i=0; i<n; i++)

printf("%c ",arr[i]);

}

void push(char val)

{

top=top+1;

stack[top]=val;

}

char pop()

{

return stack[top];

}

boolunVisit(char val)

{

for(int i=0; i<p; i++)

if(val==arr[i])

return false;

for(int i=0; i<=top; i++)

if(val==stack[top])

return false;

return true;

}

chardfs(int i)

{

int k;

char m;

if(top==-1)

{

push(b[i]);

}

m=pop();

top--;

arr[p]=m;

p++;

for(int j=0; j<n; j++)

{

if(ajMat[i][j]=='y')

{

if(unVisit(b[j]))

{

push(b[j]);

}

}

}

return stack[top];

}

Page 12: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e12

4. Program to N-Queen’s Problem

#include<stdio.h>

#include<conio.h>

#include<math.h>

char a[10][10];

int n;

voidprintmatrix()

{

inti,j;

printf("n");

for(i=0;i <n;i++)

{

for(j=0;j <n;j++)

printf("%ct",a[i][j]);

printf("nn");

}

}

//------------------------------------------

intgetmarkedcol(int row)

{

inti,j;

for(i=0;i <n;i++)

if(a[row][i]=='Q')

{

return(i);

break;

}

}

//------------------------------------------

int feasible(int row, int col)

{

inti,tcol;

for(i=0;i <n;i++)

{

tcol=getmarkedcol(i);

if(col==tcol || abs(row-i)==abs(col-tcol))

return 0;

}

return 1;

}

//------------------------------------------

Page 13: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e13

voidnqueen(int row)

{

inti,j;

if(row < n)

{

for(i=0;i <n;i++)

{

if(feasible(row,i))

{

a[row][i]='Q';

nqueen(row+1);

a[row][i]='.';

}

}

}

else

{

printf("nThe solution is:- ");

printmatrix();

}

}

//---------------------------------

void main()

{

inti,j;

clrscr();

printf("n Enter the no. of queens:- ");

scanf("%d",&n);

for(i=0;i <n;i++)

for(j=0;j <n;j++)

a[i][j]='.';

nqueen(0);

getch();

}

Page 14: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e14

5. Program to implement N-Queen problem by iterative method

#include

#include

#include

#define TRUE 1

#define FALSE 0

voidprint_solution(intn,int x[])

{

char c[10][10];

inti,j;

for(i=1;i<=n;i++)

{

for(j=1; j<=n; j++)

{

c[i][j]=’-’;

}

}

for(i=1;i<=n;i++)

{

c[i][x[i]]=’Q’;

}

for( i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

printf(“\t%c”,c[i][j]);

}

printf(“\n”);

}

}

int place(int x[],int k)

{ int i;

for(i=1;i<k;i++)

{

if(x[i]==x[k]||i-x[i]==k-x[k]||i+x[i]==k+x[k])

{

return FALSE;

}

}

return TRUE;

}

voidnqueens(int n)

{

int x[10];

int count=0;

int k=1;

x[k]=0;

Page 15: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e15

while(k!=0)

{

x[k]=x[k]+1;

while((x[k]<=n)&&(!place(x,k)))

{

x[k]=x[k]+1;

}

if(x[k]<=n)

{

if(k==n)

{

count++;

printf(“\n\tSolution %d is : \n\n\n”,count);

print_solution(n,x);

}

else

{

k++;

x[k]=0;

}

}

else

{

k–;

}

}

return;

}

void main()

{

int n;

clock_tstart,end;

clrscr();

start=clock();

printf(“\n Enter the no. of Queens : “);

scanf(“%d”,&n);

printf(“\n\n\t\t\t USING %d QUEEN’S STRATEGY \n\n”,n);

nqueens(n);

end=clock();

printf(“\n The Time Complexity is : %f msec.”,(end-

start)/CLK_TCK);

getch();

}

Page 16: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e16

6. WAP to solution of n-queen problem using array

#include<stdio.h>

#include<conio.h>

#include<math.h>

int a[30],count=0;

int place(int pos)

{

int i;

for(i=1;i<pos;i++)

{

if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))

return 0;

}

return 1;

}

void print_sol(int n)

{

int i,j;

count++;

printf("\n\nSolution #%d:\n",count);

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

if(a[i]==j)

printf("Q\t");

else

printf("*\t");

}

printf("\n");

}

}

void queen(int n)

{

int k=1;

a[k]=0;

while(k!=0)

{

a[k]=a[k]+1;

while((a[k]<=n)&&!place(k))

a[k]++;

if(a[k]<=n)

{

if(k==n)

print_sol(n);

else

{

Page 17: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e17

k++;

a[k]=0;

}

}

else

k--;

}

}

void main()

{

int i,n;

clrscr();

printf("Enter the number of Queens\n");

scanf("%d",&n);

queen(n);

printf("\nTotal solutions=%d",count);

getch();

}

Page 18: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e18

7. WAP to implement max -min problem

#include<stdio.h>

#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))

int tempmax, tempmin;

/*

* return a 2 element array

* First element is highest of list

* Second elements is lowest of list

*

*/

int* maxmin(const int list[], const int low, const int high, int

max, int min)

{

int mid,max1,min1;

static int maxAndMin[2]; // to hold the max and min value of

list

// list has only one element

// so make max and min that element

if (low == high)

{

max = list[low];

min = list[low];

}

// list has two elements then

// check for which one is greater or smaller

// and check it with temp values and update

else if (low == high-1)

{

if (list[low] < list[high])

{

tempmax = getMax(tempmax, list[high]);

tempmin = getMin(tempmin, list[low]);

}

else

{

tempmax = getMax(tempmax, list[low]);

tempmin = getMin(tempmin, list[high]);

}

}

else

{

Page 19: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e19

// if list is not small, divide list into sub-problems

// Find where to split the set

mid = (low + high) / 2;

// Solve the sub-problems

max1 = list[mid+1];

min1 = list[mid+1];

maxmin(list, low, mid, max, min);

maxmin(list, mid+1, high, max1, min1);

// Combine the solutions

tempmax = getMax(tempmax, max1);

tempmin = getMin(tempmin, min1);

}

maxAndMin[0] = tempmax;

maxAndMin[1] = tempmin;

return maxAndMin;

}

/*

* returns the highest element between first and second

*

*/

int getMax(int first, int second)

{

return first > second ? first : second;

}

/*

* returns the lowest element between first and second

*

*/

int getMin(int first, int second)

{

return first < second ? first : second;

}

int main(void)

{

int list[] = {10, 23, 24, 56, 67, 78, 90};

int *values;

int size = ARRAY_SIZE(list);

tempmax = tempmin = list[0];

values = maxmin(list, 0, size-1, list[0], list[0]);

printf("The maximum value is = %2d \n", *values);

printf("The minimum value is = %2d \n", *(values+1));

return 0;

}

Page 20: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e20

8. WAP in c to find Hamiltonian path

// Program to print Hamiltonian cycle

#include<stdio.h>

// Number of vertices in the graph

#define V 5

void printSolution(int path[]);

/* A utility function to check if the vertex v can be added at index

'pos'

in the Hamiltonian Cycle constructed so far (stored in 'path[]')

*/

bool isSafe(int v, bool graph[V][V], int path[], int pos)

{

/* Check if this vertex is an adjacent vertex of the previously

added vertex. */

if (graph [ path[pos-1] ][ v ] == 0)

return false;

/* Check if the vertex has already been included.

This step can be optimized by creating an array of size V */

for (int i = 0; i < pos; i++)

if (path[i] == v)

return false;

return true;

}

/* A recursive utility function to solve hamiltonian cycle problem

*/

bool hamCycleUtil(bool graph[V][V], int path[], int pos)

{

/* base case: If all vertices are included in Hamiltonian Cycle

*/

if (pos == V)

{

// And if there is an edge from the last included vertex to

the

// first vertex

if ( graph[ path[pos-1] ][ path[0] ] == 1 )

return true;

else

return false;

}

Page 21: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e21

// Try different vertices as a next candidate in Hamiltonian

Cycle.

// We don't try for 0 as we included 0 as starting point in in

hamCycle()

for (int v = 1; v < V; v++)

{

/* Check if this vertex can be added to Hamiltonian Cycle */

if (isSafe(v, graph, path, pos))

{

path[pos] = v;

/* recur to construct rest of the path */

if (hamCycleUtil (graph, path, pos+1) == true)

return true;

/* If adding vertex v doesn't lead to a solution,

then remove it */

path[pos] = -1;

}

}

/* If no vertex can be added to Hamiltonian Cycle constructed so

far,

then return false */

return false;

}

/* This function solves the Hamiltonian Cycle problem using

Backtracking.

It mainly uses hamCycleUtil() to solve the problem. It returns

false

if there is no Hamiltonian Cycle possible, otherwise return true

and

prints the path. Please note that there may be more than one

solutions,

this function prints one of the feasible solutions. */

bool hamCycle(bool graph[V][V])

{

int *path = new int[V];

for (int i = 0; i < V; i++)

path[i] = -1;

/* Let us put vertex 0 as the first vertex in the path. If there

is

a Hamiltonian Cycle, then the path can be started from any

point

of the cycle as the graph is undirected */

path[0] = 0;

if ( hamCycleUtil(graph, path, 1) == false )

{

printf("\nSolution does not exist");

return false;

}

printSolution(path);

return true;

Page 22: Program for Artificial intelligence (b.tech Final year) UPTU

Pag

e22

}

/* A utility function to print solution */

void printSolution(int path[])

{

printf ("Solution Exists:"

" Following is one Hamiltonian Cycle \n");

for (int i = 0; i < V; i++)

printf(" %d ", path[i]);

// Let us print the first vertex again to show the complete

cycle

printf(" %d ", path[0]);

printf("\n");

}

// driver program to test above function

int main()

{

/* Let us create the following graph

(0)--(1)--(2)

| / \ |

| / \ |

| / \ |

(3)-------(4) */

bool graph1[V][V] = {{0, 1, 0, 1, 0},

{1, 0, 1, 1, 1},

{0, 1, 0, 0, 1},

{1, 1, 0, 0, 1},

{0, 1, 1, 1, 0},

};

// Print the solution

hamCycle(graph1);

/* Let us create the following graph

(0)--(1)--(2)

| / \ |

| / \ |

| / \ |

(3) (4) */

bool graph2[V][V] = {{0, 1, 0, 1, 0},

{1, 0, 1, 1, 1},

{0, 1, 0, 0, 1},

{1, 1, 0, 0, 0},

{0, 1, 1, 0, 0},

};

// Print the solution

hamCycle(graph2);

return 0;

}