0% found this document useful (0 votes)
74 views40 pages

AI and DAA Practical Record

The document provides a certificate for lab work completed by Ms. Shweta kailash jaiswal for the subjects Lab V [ 4MCS1 Artificial Intelligence and Expert systemsand 4MCS2 Design and Analysis of Algorithms ] during the academic year 2020-21. It contains the signatures of the guide, head of department and an index of programs completed for each subject.

Uploaded by

prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views40 pages

AI and DAA Practical Record

The document provides a certificate for lab work completed by Ms. Shweta kailash jaiswal for the subjects Lab V [ 4MCS1 Artificial Intelligence and Expert systemsand 4MCS2 Design and Analysis of Algorithms ] during the academic year 2020-21. It contains the signatures of the guide, head of department and an index of programs completed for each subject.

Uploaded by

prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

G. S. Science,Arts & Commerce College, Khamgaon.

Department of Computer Science & Application

certificate
This is to certify that this book contain the bonafied record
of Mr./Ms. Shweta kailash jaiswal for Lab V[ 4MCS1 Artificial
Intelligence and Expert systemsand 4MCS2 Design and
Analysis of Algorithms ] for M.Sc. II (Computer Science) Sem
IV - during academic year 2020-21.

Date:

Guide Head Of Departenant


1) Ms. Rupali Jadhav Dr. P. E. Ajmire
2) Ms. Priti Gaur

Department of Computer Science & Application


G. S. Science, Arts & Commerce College, Khamgaon
INDEX

Practical
Sr. No. Artificial Intelligence Date
1 Program in prolog for simple LIKE.
2 Program in prolog for simple queries.
3 Program in prolog for Cut Predicate.
4 Program in prolog for fail Predicate.
5 Program in prolog for Cut-Fail.
6 Program in prolog for relation.
7 Program in prolog for list.
8 Program in prolog for MinMax.

Practical
Sr. No. Design And Analysis Date
1 Travelling Salesman C Program.
2 Quick Sort C Program.
3 Merge Sort C Program.
4 Knapsack C Program.
5 Prims Algorithm - C program.
6 Dijikstra Algorithm - C Program.
7 Kruskals C Program.
8 Stack Using Array.
1.Simple Prolog Programs.

Input:
like(riya,piya).

like(piya,riya).

like(priya,piya).

Output:
consult('C:/GNU-Prolog/bin/First.pl').

compiling C:/GNU-Prolog/bin/First.pl for byte code...

C:/GNU-Prolog/bin/First.pl compiled, 7 lines read - 427 bytes written, 16 ms

(16 ms) yes

| ?- like(riya,ram).

no

| ?- like(riya,piya).

yes

| ?- like(priya,piya).

yes

| ?- like(piya,riya).

yes

| ?- like(piya,jiya).

no

| ?- like(tom,jerry).

no
2.Program in prolog for simple queries.

Input:
tv(lg).

laptop(lenovo).

freeze(whirlpool).

Output:

| ?- consult('C:/GNU-Prolog/bin/prolog1.pl').
compiling C:/GNU-Prolog/bin/prolog1.pl for byte code...
C:/GNU-Prolog/bin/prolog1.pl compiled, 5 lines read - 794 bytes written, 4 ms
Yes

| ?- tv(lg).
Yes

| ?- laptop(philips).
No

| ?- laptop(lenovo).
Yes

| ?- freeze(whirlpool).
Yes

| ?- freeze(lg).
no
| ?-
3.Program for The Cut Predicate in Prolog.

Example 1

Input:

sumto(1, 1).

sumto(N, S) :- N1 is N-1, sumto(N1, S1),

S is S1+N.

Output:

consult('C:/GNU-Prolog/bin/sumTo.pl').

compiling C:/GNU-Prolog/bin/sumTo.pl for byte code...

C:/GNU-Prolog/bin/sumTo.pl compiled, 3 lines read - 732 bytes written, 11 ms

yes

| ?- sumto(3, S).

S=6?

yes

| ?- sumto(6, S).

S = 21 ?

(31 ms) yes

Example 2

Input:
larger(X, Y, X) :- X>Y.

larger(X, Y, Y).

Output:
consult('C:/GNU-Prolog/bin/Larger.pl').
compiling C:/GNU-Prolog/bin/Larger.pl for byte code...

C:/GNU-Prolog/bin/Larger.pl:2: warning: singleton variables [X] for larger/3

C:/GNU-Prolog/bin/Larger.pl compiled, 2 lines read - 338 bytes written, 26 ms

(16 ms) yes

| ?- larger(7, 5, A).

A=7?;

A=5

Yes
4.Program in prolog for fail Predicate

Inputs: -
bird(sparrow).
bird(parrot).
bird(dove).
bird(turkey).
bird(crow).
bird(penguins).
can_fly(penguins):-!,fail.
can_fly(A):-bird(A).

Outputs: -
| ?- consult('C:/GNU-Prolog/bin/prolog3.pl').
compiling C:/GNU-Prolog/bin/prolog3.pl for byte code...
C:/GNU-Prolog/bin/prolog3.pl compiled, 7 lines read - 763 bytes written, 3 ms

yes
| ?- can_fly(sparrow).

yes
| ?- can_fly(parrot).

yes
| ?- can_fly(dove).

yes
| ?- can_fly(turkey).

yes
| ?- can_fly(crow).

yes
| ?- can_fly(penguins).

no
| ?-
5.Program For cut and fail in prolog.

Input:
can_fly(penguins) :- fail.

can_fly(A) :- bird(A).

can_fly(penguins) :- !, fail.

can_fly(A) :- bird(A).

bird(parrot).

bird(penguins).

Output:
consult('C:/GNU-Prolog/bin/CutFail.pl').

compiling C:/GNU-Prolog/bin/CutFail.pl for byte code...

C:/GNU-Prolog/bin/CutFail.pl compiled, 9 lines read - 652 bytes written, 12 ms

yes

| ?- can_fly(parrot).

true ?

yes

| ?- can_fly(penguins).

true ? ;

no
6.Program for the relations in Prolog.

Input :
female(pam).

female(liz).

female(pat).

female(ann).

male(jim).

male(bob).

male(tom).

male(peter).

parent(pam,bob).

parent(tom,bob).

parent(tom,liz).

parent(bob,ann).

parent(bob,pat).

parent(pat,jim).

parent(bob,peter).

parent(peter,jim).

mother(X,Y):- parent(X,Y),female(X).

father(X,Y):- parent(X,Y),male(X).

sister(X,Y):- parent(Z,X),parent(Z,Y),female(X),X\==Y.

brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y.

Output:
consult('C:/GNU-Prolog/bin/Family.pl').

compiling C:/GNU-Prolog/bin/Family.pl for byte code...


C:/GNU-Prolog/bin/Family.pl compiled, 21 lines read - 3071 bytes written, 14 ms

yes

| ?- parent(X,jim).

X = pat ? ;

X = peter

yes

| ?- mother(X,Y).

X = pam

Y = bob ? ;

X = pat

Y = jim ? ;

(31 ms) no

| ?- sister(X,Y).

X = liz

Y = bob ?

(15 ms) yes

| ?- brother(X,Y).

X = bob

Y = liz ?

Yes
7.Program for the Linked Lists in Prolog.

Input:
add_front(L,E,NList) :- NList = node(E,L).

add_back(nil, E, NList) :-

NList = node(E,nil).

add_back(node(Head,Tail), E, NList) :-

add_back(Tail, E, NewTail),

NList = node(Head,NewTail).

Output:
consult('C:/GNU-Prolog/bin/LinkedList.pl').

compiling C:/GNU-Prolog/bin/LinkedList.pl for byte code...

C:/GNU-Prolog/bin/LinkedList.pl compiled, 8 lines read - 948 bytes written, 11 ms

(15 ms) yes

| ?- add_front(nil, 6, L1), add_front(L1, 5, L2), add_front(L2, 2, L3).

L1 = node(6,nil)

L2 = node(5,node(6,nil))

L3 = node(2,node(5,node(6,nil)))

(16 ms) yes

| ?- add_back(nil, 6, L1), add_back(L1, 5, L2), add_back(L2, 2, L3).

L1 = node(6,nil)

L2 = node(6,node(5,nil))

L3 = node(6,node(5,node(2,nil)))

yes

| ?- add_front(nil, 6, L1), add_front(L1, 5, L2), add_back(L2, 2, L3).

L1 = node(6,nil)
L2 = node(5,node(6,nil))

L3 = node(5,node(6,node(2,nil)))

Yes
8.Program for the Max and Min of two numbers in Prolog.

Input:
find_max(X, Y, X) :- X >= Y, !.

find_max(X, Y, Y) :- X < Y.

find_min(X, Y, X) :- X =< Y, !.

find_min(X, Y, Y) :- X > Y.

Ouput:
consult('C:/GNU-Prolog/bin/MinMax.pl').

compiling C:/GNU-Prolog/bin/MinMax.pl for byte code...

C:/GNU-Prolog/bin/MinMax.pl compiled, 5 lines read - 843 bytes written, 14 ms

yes

| ?- find_max(100,200,Max).

Max = 200

yes

| ?- find_max(40,10,Max).

Max = 40

yes

| ?- find_min(100,200,Min).

Min = 100

yes

| ?- find_min(100,200,Min).

Min = 100

Yes
1.Travelling Salesman C Program

#include<stdio.h>
#include<stdlib.h>

int smallest(int);

void minimumcost(int);

int a[10][10], visited[10];

int n, cost=0;

void main()

{
int i, j;
printf("enter the no. of cities\n");

scanf("%d",&n);

printf("enter matrix\n");

for(i=0;i<n;i++)
{
for(j=0;j>n;j++)
{
scanf("%d",&a[i][j]);
}
visited[i] = 0;
}
printf("the path is\n");

minimumcost(0);

printf("minimum cost = %d",cost);


}
void minimumcost(int city)

{
int i, nextcity;
visited[city] = 1;

printf("%d-->",city+1);

nextcity = smallest(city);

if(nextcity == 999)
{
nextcity = 0;

printf("%d",nextcity+1);

cost = cost + a[city][nextcity];


return;
}
minimumcost(nextcity);
}

int smallest(int c)

{
int i, nc=999;

int minimum = 999, dmin;

for(i=0;i<n;i++)
{
if(a[c][i] != 0 && visited[i] == 0)
{
if(a[c][i] + a[i][c] < minimum)
{
minimum = a[i][0] + a[c][i];

dmin = a[c][i];

nc = i;
}
}
}

if(minimum != 999)
{
cost += dmin;
}
return nc;
}

Output Travelling Salesman C Program


enter the no. of cities
4

enter matrix
0
1
5
7
4
0
6
5
7
8
0
1
7
6
3
0

the path is
1-->2-->4-->3-->1

minimum cost = 16
2.Quick Sort C Program
#include<stdio.h>
#include<stdlib.h>

void main()

{
int n, i, a[10];

printf("enter the size of array\n");

scanf("%d",&n);

printf("enter the array elements\n");

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

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

quicksort(a, 0, n-1);

printf("array after sorting is");

for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}

void quicksort(int a[], int first, int last)

{
int j;

if(first >= last)


{
return;
}
else
{
j = partition(a, first, last);

quicksort(a, first, j-1);

quicksort(a, j+1, last);


}
}
int partition(int a[], int first, int last)

{
int i, j, pivot, temp;

i = first;

j = last;

pivot = first;

while(i < j)
{
while(a[i] < a[pivot])
{
i++;
}

while(a[j] > a[pivot])


{
j--;
}

if(a[i] > a[j])


{
temp = a[i];

a[i] = a[j];

a[j] = temp;
}
}

temp = a[pivot];
a[pivot] = a[j];

a[j] = temp;

return j;
}

Output quick sort


enter the size of the array
5
enter the array elements
1
4
2
8
7
array after sorting is
1
2
4
7
8
3.Merge Sort C Program

#include<stdio.h>
#include<stdlib.h>

void mergesort(int[], int, int);

void merge(int[], int, int);

void main()

{
int n, a[10], i, j;

printf("enter the size of the array\n");

scanf("%d",&n);

printf("enter the array elements\n");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

mergesort(a,0,n-1);

printf("array after sorting is \n");

for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}

void mergesort(int a[], int low, int high)

{
int mid;

if(low>=high)
{
return;
}
else
{
mid = (low + high)/2;

mergesort(a, low, mid);

mergesort(a, mid+1, high);

merge(a, low, high);

}
}

void merge(int a[], int low, int high)

{
int mid = (low + high)/2;

int k,i, j, temp[10];

i = low;

j = mid+1;

k = low;

while(i<=mid && j<= high)


{
if(a[i] < a[j])
{
temp[k++] = a[i++];
}

else
{
temp[k++] = a[j++];
}
}

while(i<=mid)
{
temp[k++] = a[i++];
}
while(j<=high)
{
temp[k++] = a[j++];
}

for(i=low;i<=high;i++)
{
a[i] = temp[i];
}
}

Output merge sort

enter the size of the array


5
enter the array elements
1
4
2
8
7
array after sorting is
1
2
4
7
8
4.Knapsack C Program

#include<stdio.h>

#include<stdlib.h>

int knapsack(int, int[], int[], int);

int max(int, int);

void main()

int n, W, i;

int weight[10], value[10];

printf("enter the no. of items\n");

scanf("%d",&n);

printf("enter the value and weight\n");

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

scanf("%d%d",&value[i],&weight[i]);

printf("enter the capacity of knapsack\n");

scanf("%d",&W);

printf("%d",knapsack(W, weight, value, n));

int knapsack(int W, int weight[], int value[], int n)

if(n==0 || W==0)

return 0;

else if(weight[n-1] > W)

return knapsack(W, weight, value, n-1);

else
{

return max(value[n-1] + knapsack(W-weight[n-1], weight, value, n-1), knapsack(W,


weight, value, n-1));

int max(int a, int b)

if(a > b)

return a;

else

return b;

Output knapscak

enter the no. of items


3
enter the value and weight
100 20
50 20
180 5

enter the capacity of knapsack : 70


330
5.Prims Algorithm - C program
#include<stdio.h>
#include<stdlib.h>

int main()

{
int n, ne = 1, i, j;

int c[10][10], traversed[10] = {0};

int minimum, minc = 0, a, b, x, y;

printf("enter no. of vertices\n");

scanf("%d",&n);

printf("enter matrix\n");

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&c[i][j]);

if(c[i][j] == 0)
{
c[i][j] = 999;
}
}
}

traversed[1] = 1;

while(ne < n)
{
minimum = 999;

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(c[i][j] < minimum)
{
if(traversed[i] != 0)
{
minimum = c[i][j];

a = x = i;

b = y = j;
}
}
}
}

if(traversed[x] == 0 || traversed[y] == 0)
{
printf("%d edge(%d, %d) = %d\n",ne++, a, b, minimum);

minc = minc + minimum;

traversed[b] = 1;
}

c[a][b] = c[b][a] = 999;

printf("\n total cost = %d",minc);

return 0;
}

Output prims algorithm


enter the no. of vertices
4
enter the matrix
0
10
0
2
10
0
6
0
0
6
0
8
2
0
8
0

1 edge(1, 4) : 2
2 edge(4, 3) : 8
3 edge(3, 2) : 6

total cost = 16
6.Dijikstra Algorithm - C Program
#include<stdio.h>
#include<stdlib.h>

void main()

{
int i, j, n, startn, nextn, count, minc, mindistance;

int c[10][10], dist[10], visit[10], pre[10];

printf("enter the no. of vertices\n");

scanf("%d",&n);

printf("enter the matrix\n");

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

{
for(j=0;j<n;j++)
{
scanf("%d",&c[i][j]);

if(c[i][j] == 0)
{
c[i][j] = 9999;
}

}
}

printf("enter the startnode\n");

scanf("%d",&startn);

for(i=0;i<n;i++)
{
dist[i] = c[startn][i];

visit[i] = 0;
pre[i] = startn;
}

count = 1;

dist[startn] = 0;

visit[startn] = 1;

while(count < n)

{
mindistance = 9999;

for(i=0;i<n;i++)
{
if(dist[i] < mindistance && !visit[i])
{
mindistance = dist[i];

nextn = i;
}
}

visit[nextn] = 1;

for(i=0;i<n;i++)
{
if(!visit[i])
{
if(mindistance + c[nextn][i] < dist[i])
{
dist[i] = mindistance + c[nextn][i];

pre[i] = nextn;
}
}
}

count++;
}

for(i=0;i<n;i++)
{
if(i!=startn)
{
printf("\ndistance of %d is %d\n", i, dist[i]);

printf("path = %d",i);

j = i;

do
{
j = pre[j];

printf("<-- %d",j);
}
while(j!=startn);
}
}
}

Output Dijikstra Algorithm

enter the no. of vertices


4

enter the matrix


0
10
0
2
10
0
6
0
0
6
0
8
2
0
8
0

enter the startnode


0

distance of 1 is 10
path = 1<-- 0

distance of 2 is 10

path = 2<-- 3<-- 0

distance of 3 is 2

path = 3<-- 0
7.Kruskals C Program
#include<stdio.h>
#include<stdlib.h>

int p[10], c[10][10];

int search(int);

int u(int, int);

int main()

{
int i, j, n, count = 1, x, y, a, b, minimum, minc;

printf("Kruskals algorithm \n");

printf("enter size of matrix:\t");

scanf("%d",&n);

printf("\n enter the elements of matrix \n");

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

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

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

if(c[i][j] == 0)

{
c[i][j] = 9999;
}
}
}

while(count<n)
{
minimum = 9999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(c[i][j] < minimum)
{
minimum = c[i][j];

a = x = i;

b = y = j;
}
}
}

x = search(x);

y = search(y);

if(u(x, y))

{
printf("\n%d edge(%d, %d) = %d",count++, a, b, minimum);

minc = minc + minimum;


}

c[a][b] = c[b][a] = 9999;

printf("\ntotal cost = %d",minc);

return 0;

int search(int i)

{
while(p[i])
{
i = p[i];
}
return i;
}

int u(int i, int j)

{
if(i != j)
{
p[j] = i;

return 1;
}

return 0;
}

Output Kruskals

enter the no. of vertices


4
enter the matrix
0
10
0
2
10
0
6
0
0
6
0
8
2
0
8
0

1 edge(1, 4) : 2
2 edge(4, 3) : 8
3 edge(3, 2) : 6

total cost = 16
8. Stack Using Array

#include<stdio.h>
#include<stdlib.h>
#define SIZE 10

int stack[SIZE];

int top = -1, i, ele;

void main()

{
int choice;

printf("stack implementation\n");

while(1)
{

printf("1. push \n 2. pop \n 3. display \n 4. exit \n");

printf("enter your choice : ");

scanf("%d",&choice);

switch(choice)

{
case 1: push();

break;

case 2: pop();

break;

case 3: display();

break;

case 4: exit(0);
break;

default: printf("\nInvalid Choice\n");

}
}
}

void push()

{
if(top >= SIZE - 1)

{
printf("stack is full\n");

else

{
printf("enter the element to be pushed : ");

scanf("%d",&ele);

top++;

stack[top] = ele;
}

void pop()

{
if(top <= -1)

{
printf("stack is underflow \n");

else

{
printf("The popped element is %d\n",stack[top]);
top--;
}

void display()

{
if(top <= -1)

{
printf("stack is empty\n");

else

{
for(i = top; i >= 0; i--)

printf("\n %d\n",stack[i]);

Output Stack Using Array

stack implementation
1. push
2. pop
3. display
4. exit
enter your choice : 1

enter the element to be pushed : 10


1. push
2. pop
3. display
4. exit

enter your choice : 1

enter the element to be pushed : 20


1. push
2. pop
3. display
4. exit

enter your choice : 1

enter the element to be pushed : 30


1. push
2. pop
3. display
4. exit

enter your choice : 1

enter the element to be pushed : 40


1. push
2. pop
3. display
4. exit

enter your choice : 3

40

30

20

10

1. push
2. pop
3. display
4. exit

enter your choice : 2

The popped element is 40

1. push
2. pop
3. display
4. exit

enter your choice : 2


The popped element is 30

1. push
2. pop
3. display
4. exit

enter your choice : 3

20

10

1. push
2. pop
3. display
4. exit

You might also like