0% found this document useful (0 votes)
113 views33 pages

'Pgm:1 (A) BINARY SEARCH

The document contains programs for various sorting and searching algorithms: 1. Binary search and linear search programs that search for a key in a sorted array and output whether it was found and the time taken. 2. Implementations of sorting algorithms like heap sort, merge sort, selection sort, and quicksort that take an array as input, sort it, and output the sorted array and time taken. 3. Other algorithms like knapsack problem, Dijkstra's algorithm, and Kruskal's algorithm for minimum spanning trees are also included with inputs, outputs and explanations of the algorithms.

Uploaded by

Puneetha Bm
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views33 pages

'Pgm:1 (A) BINARY SEARCH

The document contains programs for various sorting and searching algorithms: 1. Binary search and linear search programs that search for a key in a sorted array and output whether it was found and the time taken. 2. Implementations of sorting algorithms like heap sort, merge sort, selection sort, and quicksort that take an array as input, sort it, and output the sorted array and time taken. 3. Other algorithms like knapsack problem, Dijkstra's algorithm, and Kruskal's algorithm for minimum spanning trees are also included with inputs, outputs and explanations of the algorithms.

Uploaded by

Puneetha Bm
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

`Pgm:1(a) BINARY SEARCH

#include<stdio.h>
#include<conio.h>
#include<time.h>
int bin_search(int[],int,int);
int key,a[20],n;

void main()
{
int i,ans,low,high;
clock_t st,end;
clrscr();
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("Enter the elements in the ascending order\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the key element\n");
scanf("%d",&key);
low=0, high=n-1;
st=clock();
//delay(1000);
ans=bin_search(a,low,high);
end=clock();
if(ans==-1)
{ printf("key not found\n"); }
Else
{ printf("Key found at position %d\n",ans+1);
printf("Time taken to search is %f\n",(end-st)/CLK_TCK); }
getch();
}

int bin_search(int a[10],int low,int high)


{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
return mid;
}
if(key<a[mid])
{
return bin_search(a,low,mid-1);
}
if(key>a[mid])
{
return bin_search(a,mid+1,high);
}
}
return -1;
}

OUTPUT

Enter the no.of elements


5
Enter the elements in ascending order
11 22 33 44 55
Enter the key element
22
Key found at position 2
Time taken to search is 0.000000
Pgm:1(b) LINEAR SEARCH

#include<stdio.h>
#include<conio.h>
#include<time.h>
int key;
int lin_search(int a[],int n)
{
if(n<0)
return -1;
if(key==a[n])
return n;
else
return lin_search(a,n-1);
}

void main()
{
int a[100],n,i,res;
clock_t st,end;
clrscr();
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("Enter the numbers in ascending order\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("Enter the key to be searched\n");
scanf("%d",&key);
st=clock();
res=lin_search(a,n-1);
end=clock();
if(res==-1)
printf("Key element not found\n");
else
printf("Key element found at position %d\n",res+1);
printf("Time taken to search is %f\n",(end-st)/CLK_TCK);
getch();
}

OUTPUT
Enter the no.of elements
5
Enter the numbers in ascending order
11 22 33 44 55
Enter the key to be searched
44
Key element found at position 4
Time taken to search is 0.000000
Pgm:2 HEAP SORT
#include<stdio.h>
#include<conio.h>
#include<time.h>
void heap(int a[],int);
void heap_sort(int a[],int n);

void main()
{
int i,n,a[20];
clock_t st,end;
clrscr();
printf("Enter no.of elements\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=1; i<=n; i++)
scanf("%d",&a[i]);
printf("Sorted list\n");
st=clock();
heap(a,n);
heap_sort(a,n);
end=clock();
printf("\nTime require for sorting is %f\n",(end-st)/CLK_TCK);
getch();
}

void heap(int a[],int n)


{
int i,j,k,v,heap;
for(i=n/2; i>=1; i--)
{
k=i, v=a[k], heap=0;
while(!heap && ((2*k)<=n))
{
j=2*k;
if(j<n)
if(a[j]>a[j+1])
j++;
if(v<=a[j])
heap=1;
else
{
a[k]=a[j];
k=j;
}
}
a[k]=v;
}
}

void heap_sort(int a[],int n)


{
int w,temp,last=n;
for(w=1; w<=n; w++)
{
temp=a[last];
a[last]=a[1];
a[1]=temp;
printf("%5d",a[last]);
last--;
heap(a,last);
}
}

OUTPUT

Enter no.of elements


7
Enter array elements
30 25 45 5 50 100 75
Sorted list
5 25 30 45 50 75 100
Time require for sorting is 0.000000
Pgm:3 MERGE SORT
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<dos.h>

void simplemerge(int a[],int low,int mid,int high)


{
int i,j,c[100],k;
i=low, j=mid+1, k=low;
while(i<=mid && j<=high)
{
if(a[i]<a[j])
{
c[k]=a[i], i=i+1, k=k+1;
}
else
{
c[k]=a[j], j=j+1, k=k+1;
}
}
while(i<=mid)
{
c[k]=a[i], i=i+1, k=k+1;
}
while(j<=high)
{
c[k]=a[j], j=j+1, k=k+1;
}
for(i=low; i<=high; i++)
{
a[i]=c[i];
}
}

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


{
int mid;
delay(1000);
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
simplemerge(a,low,mid,high);
}
}

void main()
{
int a[100],i,n,low,high;
clock_t st,end;
clrscr();
printf("Enter the no,of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
st=clock();
low=0;
high=n-1;
mergesort(a,low,high);
end=clock();
printf("The sorted array is\n");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
printf("Time taken required is %f\n",(end-st)/CLK_TCK);
getch();
}

OUTPUT
1. Enter the no,of elements
10
Enter the array elements
98 23 76 7 34 45 22 11 1 12
The sorted array is
1 7 11 12 22 23 34 45 76 98
Time taken required is 18.901099

2. Enter the no,of elements


6
Enter the array elements
98 23 76 7 34 45
The sorted array is
7 23 34 45 76 98
Time taken required is 10.934066

3. Enter the no,of elements


4
Enter the array elements
98 23 76 7
The sorted array is
7 23 76 98
Time taken required is 6.978022
Pgm:4 SELECTION SORT
#include<stdio.h>
#include<conio.h>
#include<time.h>

void sel(int a[],int n)


{
int pos,temp,i,j;
for(i=0; i<n; i++)
{
pos=i;
for(j=i+1; j<n; j++)
{
if(a[j]<a[pos])
pos=j;
}
temp=a[pos];
a[pos]=a[i];
a[i]=temp;
}
}

void main()
{
int a[100],n,i;
clock_t st,end;
clrscr();
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
st=clock();
sel(a,n);
end=clock();
printf("The sorted array is\n");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
printf("time taken to sort is %f\n",(end-st)/CLK_TCK);
getch();
}

OUTPUT
Enter the no.of elements
5
Enter the array elements
12 7 20 50 35
The sorted array is
7 12 20 35 50
Time taken to sort is 0.000000
Pgm:5 KNAPSACK PROBLEM
#include<stdio.h>
#include<conio.h>

int max(int a,int b)


{
return a>b?a:b;
}

void knapsack(int n,int w[],int m,int p[])


{
int i,j,v[10][10],x[10];
for(i=0; i<=n; i++)
{
for(j=0; j<=m; j++)
{
if(i==0 || j==0)
v[i][j]=0;
else if(j<w[i])
v[i][j]=v[i-1][j];
else
v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
}
}
printf("The output is\n");
for(i=0; i<=n; i++)
{
for(j=0; j<=m; j++)
printf("%d\t",v[i][j]);
printf("\n");
}
printf("The optimal solution is %d\n",v[n][m]);
for(i=0; i<=n; i++)
{
x[i]=0, i=n, j=m;
while(i!=0 && j!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1, j=j-w[i];
}
i=i-1;
}
for(i=1; i<=n; i++)
printf("x[%d] ",i);
printf("=");
for(i=1; i<=n; i++)
printf("%d ",x[i]);
}}

void main()
{
int m,i,j,n,w[10],p[10],v[10][10];
clrscr();
printf("Enter the no.of objects\n");
scanf("%d",&n);
printf("Enter the weights of n objects\n");
for(i=1; i<=n; i++)
scanf("%d",&w[i]);
printf("Enter the profit of n objects\n");
for(i=1; i<=n; i++)
scanf("%d",&p[i]);
printf("Enter the capacity of knapsack\n");
scanf("%d",&m);
knapsack(n,w,m,p);
getch();
}

OUTPUT
Enter the no.of objects
5
Enter the weights of n objects
32145
Enter the profit of n objects
25 20 15 40 50
Enter the capacity of knapsack
6
The output is
0 0 0 0 0 0 0
0 0 0 25 25 25 25
0 0 20 25 25 45 45
0 15 20 35 40 45 60
0 15 20 35 40 55 60
0 15 20 35 40 55 65
The optimal solution is 65
x[1] x[2] x[3] x[4] x[5] =0 0 1 0 1
Pgm:6 DIJKSTRA'S ALGORITHM

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,a[10][10],s[10],d[10],k,min,u,v,n;
clrscr();
printf("Enter the no of vertices\n");
scanf("%d",&n);
printf("Enter the cost adjacency matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
scanf("%d",&a[i][j]);
}
printf("Enter the source matrix\n");
scanf("%d",&v);
for(i=1; i<=n; i++)
{
s[i]=0, d[i]=a[v][i];
}
d[v]=0, s[v]=1;
for(k=2; k<=n; k++)
{
min=999;
for(i=1; i<=n; i++)
if(s[i]==0 && d[i]<min)
{
min=d[i], u=i;
}
s[u]=1;
for(i=1; i<=n; i++)
if(s[i]==0)
{
if(d[i]>(d[u]+a[u][i]))
d[i]=d[u]+a[u][i];
}
}
printf("The shortest distance from %d is \n",v);
for(i=1; i<=n; i++)
printf("%d--->%d=%d\n",v,i,d[i]);
getch();
}
OUTPUT
Enter the no of vertices
5
Enter the cost adjacency matrix
999 3 999 7 999
3 999 999 5 6
7 2 5 999 4
999 999 6 4 999
999 7 999 3 999
Enter the source matrix
1
The shortest distance from 1 is
1--->1=0
1--->2=3
1--->3=13
1--->4=7
1--->5=9
Pgm:7 QUICKSORT
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<dos.h>

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


{
int s;
if(low<high)
{
s=partition(a,low,high);
quicksort(a,low,s-1);
quicksort(a,s+1,high);
}
}

int partition(int a[],int low,int high)


{
int pivot,i,j,temp;
pivot=a[low];
i=low;
j=high+1;
delay(1000);
while(i<j)
{
do
{
i++;
}
while(pivot>=a[i]);
do
{
j--;
}
while(pivot<a[j]);
if(i<j)
{
temp=a[j];
a[i]=a[j];
a[j]=temp;
}
}
a[low]=a[j];
a[j]=pivot;
return j;
}

void main()
{
int i,a[100],n,low,high;clock_t st,end;
clrscr();
printf("Enter no.of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
delay(1000);
st=clock();
quicksort(a,low,high);
end=clock();
printf("Sorted array is\n");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
printf("The time required is %f\n",(end-st)/CLK_TCK);
getch();
}
OUTPUT
Enter no.of elements
5
Enter the array elements
33 11 22 55 44
Sorted array is
11
22
33
44
55
The time required is 2.967033
Pgm:8 KRUSKAL
#include<stdio.h>
#include<conio.h>
#define INF 9999
struct edge
{
int u,v,cost;
};
typedef struct edge EDGE;

int find(int v,int parent[])


{
while(parent[v]!=v)
v=parent[v];
return v;
}

void union_ij(int i,int j,int parent[])


{
if(i<j)
parent[j]=i;
else
parent[i]=j;
}

void create_edge(int m,EDGE e[])


{
int i,j,k,cost;
printf("Enter the edge & cost in form of u,v,w\n");
for(k=1; k<=m; k++)
{
scanf("%d%d%d",&i,&j,&cost);
e[k].u=i;
e[k].v=j;
e[k].cost=cost;
}
}

int min(EDGE e[],int n)


{
int i,small,pos;
small=INF;
pos=-1;
for(i=1; i<=n; i++)
{
if(e[i].cost<small)
{
small=e[i].cost;
pos=i;
}
}
return pos;
}

void kruskal(int n,EDGE e[],int m)


{
int i,j,u,v,pos,k,sum,count,parent[10],t[10][10];
count=0, k=0, sum=0;
for(i=1; i<=n; i++)
parent[i]=i;
while(count!=n-1)
{
pos=min(e,m);
if(pos==-1) break;
u=e[pos].u;
v=e[pos].v;
i=find(u,parent);
j=find(v,parent);
if(i!=j)
{
t[k][0]=u;
t[k][1]=v;
k++;
count++;
sum+=e[pos].cost;
union_ij(i,j,parent);
}
e[pos].cost=INF;
}
if(count==n-1)
{
printf("\nspanning tree exists\n");
for(i=0; i<k; i++)
printf("%d\t%d\n",t[i][0],t[i][1]);
printf("\ncost of mwst=%d\n",sum);
}
else
printf("spanning tree doesn't exist\n");
}
void main()
{
int n,m;
EDGE e[20];
clrscr();
printf("Enter no of nodes\n");
scanf("%d",&n);
printf("Enter no of edges\n");
scanf("%d",&m);
printf("Enter the edge list \n");
create_edge(m,e);
kruskal(n,e,m);
getch();
}
OUTPUT
Enter no of nodes: 8
Enter no of edges: 11
Enter the edge list
Enter the edge & cost in form of u,v,w
255
6 7 10
5 7 15
4 5 20
1 3 25
4 8 30
5 6 35
3 5 40
1 4 45
7 8 50
1 2 55
spanning tree exists
2 5
6 7
5 7
4 5
1 3
4 8
3 5
cost of mwst=145
Pgm:9(a) BFS METHOD
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<time.h>
#include<dos.h>

void bfs(int a[20][20],int n,int source,int t[20][20],int s[])


{
int f,i,k=0,r,q[20],u,v;
for(i=0; i<n; i++)
s[i]=0, f=k=r=0;
q[r]=source, s[source]=1;
while(f<=r)
{
u=q[f++];
for(v=0; v<=n; v++)
{
if(a[u][v]==1 && s[v]==0)
{
s[v]=1, q[++r]=v;
t[k][0]=u, t[k][1]=v;
k++;
}
}
}
}

void main()
{
int i,j,flag,n,source;
int a[20][20],s[10],t[20][20];
clrscr();
printf("Enter the no.of nodes\n");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter rhe source\n");
scanf("%d",&source);
bfs(a,n,source,t,s);
flag=0;
for(i=0; i<n;i++)
{
if(s[i]==0)
{
printf("The vertex %d is not reachable\n",i);
flag=1;
}
else
printf("The vertex %d is reachable\n",i);
}
if(flag==1)
printf("Some nodes are not visible\n");
else
printf("BFS traversal is\n");
for(i=0; i<n; i++)
printf("%d---->%d\n",t[i][0],t[i][1]);
getch();
}
OUTPUT
Enter the no.of nodes
4
Enter the adjacency matrix
0110
0000
0001
0100
Enter rhe source
0
The vertex 0 is reachable
The vertex 1 is reachable
The vertex 2 is reachable
The vertex 3 is reachable
BFS traversal is
0---->1
0---->2
2---->3
0---->0

Pgm:9(b) DFS ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dos.h>

void dfs(int n,int cost[10][10],int u,int s[])


{
int v;
s[u]=1;
for(v=0; v<n; v++)
{
if(cost[u][v]==1 && s[v]==0)
{
dfs(n,cost,v,s);
}
}
}

void main()
{
int n,i,j,cost[10][10],s[10],connected,flag;
clrscr();
printf("Enter the no.of nodes\n");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&cost[i][j]);
}
}
connected=0;
for(j=0; j<n; j++)
{
for(i=0; i<n; i++)
s[i]=0;
dfs(n,cost,j,s);
flag=0;
for(i=0; i<n; i++)
{
if(s[i]==0) flag=1;
}
if(flag==0) connected=1;
}
if(connected==1)
printf("Graph is connected\n");
else
printf("Graph is not connecte\n");
getch();
}

OUTPUT
Enter the no.of nodes
4
Enter the adjacency matrix
0110
0000
0100
0000
Graph is not connected
Pgm:10 SUBSET PROBLEM
#include<stdio.h>
#include<conio.h>

void subset(int n,int d,int w[])


{
int i,k,s,x[10];
for(i=1; i<=n; i++)
x[i]=0, s=0, k=1, x[k]=1;
while(1)
{
if(k<=n && x[k]==1)
{
if(s+w[k]==d)
{
printf("Solution is\n");
for(i=1; i<=n; i++)
{
if(x[i]==1)
printf("%d",w[i]);
}
printf("\n");
x[k]=0;
}
else if(s+w[k]<d)
{
s+=w[k];
}
else
{
x[k]=0;
}}
else
{
k--;
while(k>0 && x[k]==0)
{
k--;
}
if(k==0) break;
x[k]=0;
s=s-w[k];
}
k=k+1;
x[k]=1;
}
}
void main()
{
int d,i,n,w[10];
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the set in increasing order\n");
for(i=1; i<=n; i++)
scanf("%d",&w[i]);
printf("Enter the maximum subset value of d\n");
scanf("%d",&d);
subset(n,d,w);
getch();
}
OUTPUT
Enter the value of n
5
Enter the set in increasing order
12345
Enter the maximum subset value of d
9
Solution is
135
Solution is
234
Solution is
45
Pgm:11(a) HORSPOOL
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>

void shift_table(char p[],int t[])


{
int i,m;
m=strlen(p);
for(i=0; i<128; i++)
t[i]=m;
for(i=0; i<=m-2; i++)
t[p[i]]=m-1-i;
}

int horspool(char p[],char t[])


{
int i,k,m,n,s[256];
shift_table(p,s);
m=strlen(p);
n=strlen(t);
i=m-1;
while(i<=n-1)
{
k=0;
while(k<=m-1 && t[i-k]==p[m-1-k])
{
k=k+1;
}
if(k==m)
return i-m+1;
i=i+s[t[i]];
}
return -1;
}

void main()
{
char p[20],t[40];
int pos;
clrscr();
printf("Enter the text string\n");
scanf("%s",&t);
printf("Enter the pattern string\n");
scanf("%s",&p);
pos=horspool(p,t);
if(pos==-1)
printf("Pattern string not found\n");
else
printf("Pattern string found at %dth position\n",pos+1);
getch();
}
OUTPUT

1. Enter the text string


raghuvansham
Enter the pattern string
vansham
Pattern string found at 6th position

2. Enter the text string


raghuvanham
Enter the pattern string
vasham
Pattern string not found
Pgm:11(b) BIONOMIAL COEFFICIENT
#include<stdio.h>
int binomial(int n,int k);
void main()
{
int n,k;
clrscr();
printf("Enter the value of n & k such that n>k\n");
scanf("%d%d",&n,&k);
printf("(%d,%d)=%d\n",n,k,binomial(n,k));
getch();
}

int binomial(int n,int k)


{
int i,j,c[100][100];
for(i=0; i<=n; i++)
for(j=0; j<=n; j++)
if(j==0 || i==j)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
return c[n][k];
}
OUTPUT
Enter the value of n & k such that n>k
4
6
(4,6)=2448
Pgm:12 PRIM’S ALGORITHM
#include<stdio.h>
#include<conio.h>
int i,j,n,c[20][20],visited[20];
void prims();

void main()
{ clrscr();
printf("Enter no.of vertices\n");
scanf("%d",&n);
printf("Enter the cost of matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
scanf("%d",&c[i][j]);
visited[i]=0;
}
prims();
getch(); }

void prims()
{
int a,b,k,min=1000,cost=0,count=0;
visited[1]=1;
while(count<n-1)
{
min=999;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
if(visited[i] && !visited[j] && min>c[i][j])
{
min=c[i][j], a=i, b=j;
}
}
printf("%d--->%d\n",a,b,c[a][b]);
cost+=c[a][b];
visited[b]=1;
count++;
}
printf("Total cost of spanning tree is %d",cost);
}
OUTPUT
Enter no.of vertices
6
Enter the cost of matrix
999 3 999 999 6 5
3 999 1 6 999 4
999 1 999 6 999 4
999 6 6 999 8 5
6 999 999 8 999 2
5 4 4 5 2 999
1--->2=3
2--->3=1
2--->6=4
6--->5=2
6--->4=5
Total cost of spanning tree is 15
Pgm:13 Floyd’s Algorithm
#include<stdio.h>
#include<conio.h>
int min(int a,int b)
{
if(a<b)
return a;
return b;
}

void floyds(int n,int a[10][10],int p[10][10])


{
int i,j,k;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
p[i][j]=a[i][j];
}
for(k=0; k<n; k++)
{
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(p[i][k]<999 &&p[k][j]<999)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
}
}
}

void read_matrix(int n,int a[10][10])


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

void write_matrix(int n,int a[10][10])


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

void main()
{
int n,a[10][10],p[10][10];
clrscr();
printf("Enter the no.of vertices\n");
scanf("%d",&n);
printf("Enter the cost of adjacency matrix\n");
read_matrix(n,a);
floyds(n,a,p);
printf("The transition closure is as shown\n");
write_matrix(n,p);
getch();
}
OUTPUT
Enter the no.of vertices
4
Enter the cost of adjacency matrix
0 9999 3 9999
2 0 9999 9999
9999 7 0 1
6 9999 9999 0
The transition closure is as shown
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
Pgm:14 Warshall’s Algorithm
#include<stdio.h>
#include<conio.h>

int warshall(int n,int a[10][10],int p[10][10])


{
int i,j,k;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
p[i][j]=a[i][j];
for(k=0; k<n; k++)
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(p[i][k]==1 && p[k][j]==1)
p[i][j]=1;
}

void read_matrix(int n,int a[10][10])


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

void write_matrix(int n,int a[10][10])


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

void main()
{
int n,a[10][10],p[10][10];
clrscr();
printf("Enter the no.of nodes\n");
scanf("%d",&n);
printf("Enter the cost of adjacency matrix\n");
read_matrix(n,a);
warshall(n,a,p);
printf("The transitive closure of (path) matrix is as shown\n");
write_matrix(n,p);
getch();
}

OUTPUT
Enter the no.of nodes
4
Enter the cost of adjacency matrix
0100
0001
0000
1010
The transitive closure of (path) matrix is as shown

1 1 1 1
1 1 1 1
0 0 0 0
1 1 1 1
Pgm:15 N-Queen’s Problem
#include<stdio.h>
#include<conio.h>
#include<math.h>
int count,x[10];
void nqueens(int,int);
void display(int,int x[]);
int place(int,int);

void main()
{
int n;
clrscr();
printf("Enter the no.of queens\n");
scanf("%d",&n);
nqueens(1,n);
if(count==0)
printf("No possible solution\n");
else
printf("No.of solutions are %d ",count);
getch();
}

void nqueens(int k,int n)


{
int i,j;
for(i=1; i<=n; i++)
{
if(place(k,i))
{
x[k]=i;
if(k==n)
{
count++;
display(n,x);
}
else
nqueens(k+1,n);
}
}
}

void display(int n,int x[10])


{
int i,j;
char c[10][10];
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
c[i][j]='x';
for(i=1; i<=n; i++)
c[i][x[i]]='Q';
printf("The solution is: %d\n",count);
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
printf("%c\t",c[i][j]);
printf("\n");
}
printf("\n");
}

int place(int k,int i)


{
int j;
for(j=1; j<k; j++)
{
if(x[j]==i||(j-x[j])==k-i||(j+x[j])==k+i)
return 0;
}
return 1;
}
OUTPUT
Enter the no.of queens: 4
The solution is: 1
x Q x x
x x x Q
Q x x x
x x Q x
The solution is: 2
x x Q x
Q x x x
x x x Q
x Q x x
No.of solutions are 2

You might also like