Professional Documents
Culture Documents
#include<stdio.h>
#include<conio.h>
int w[10],p[10],n;
int max(int a,int b)
{
return a>b?a:b;
}
int knap(int i,int m)
{
if(i==n) return w[i]>m?0:p[i];
if(w[i]>m) return knap(i+1,m);
returnmax(knap(i+1,m),knap
(i+1,m-w[i])+p[i]);
}
void main()
{
int m,i,max_profit;
clrscr();
printf("Enter the number of objects:\n");
scanf("%d",&n);
printf("Enter knapsack capacity:\n");
scanf("%d",&m);
printf("Enter the profit(value) followed by
weight: \n");
for(i=1;i<=n;i++)
scanf("%d %d",&p[i],&w[i]);
max_profit=knap(1,m);
printf("Max_profit=%d\n",max_profit);
getch();
}
Output:
Enter the number of object:
4
Enter knapsack capacity:
50
Enter the profit (value) followed by weight:
10 10
12 20
20 30
15 20
Max_profit=37
5. Print all the nodes reachable from for(j=1;j<=n;j++)
a given starting node in a digraph scanf("%d",&a[i][j]);
using BFS method. printf("Enter the source vertex:\
#include<stdio.h> n");
#include<conio.h> scanf("%d",&source);
int visited[10]; for(i=1;i<=n;i++)
void bfs(int n,int a[10][10],int source)
{ visited[i]=0;
int i,q[10],u; bfs(n,a,source)
int front=1,real=1; for(i=1;i<=n;i++)
visited[source]=1; {
q[real]=source;
while(front<=real) if(visited[i]==0)
{ printf("The node %d is not reachable\
u=q[front]; n",i);
front=front+1; else
for(i=1;i<=n;i++) printf("The node %d is rechable\n",i);
if(a[u][i]==1 && }
visited[i]==0) getch();
{ }
real=real+1; OUTPUT:
q[real]=i; Enter the number of nodes
visited[i]=1; 5
} Enter the adjacency matrix
} 0 0 1 0 0
} 1 0 0 0 0
void main() 0 1 0 1 0
{ 1 0 0 0 0
int i,j,source; 0 0 0 0 0
int n,a[10][10]; Enter the Source node
clrscr(); 2
printf("Enter the number of node:\ The node 1 is reachable
n"); The node 2 is reachable
scanf("%d",&n); The node 3 is reachable
printf("Enter the adjacncy The node 4 is reachable
matrix:\n"); The node 5 is not reachable
for(i=1;i<=n;i++)
6. Check whether a given is visited[i]=0;
connected or not using DFS dfs(n,a,source);
Method. for(i=1;i<=n;i++)
{
#include<stdio.h> if(visited[i]==0)
#include<conio.h> printf("The node %d is not
int visited[10]; reachable\n",i);
void dfs(int n,int a[10][10],int else
source) printf("The node %d is reachable\n",i);
{ }
int i; getch();
visited[source]=1; }
for(i=1;i<=n;i++)
if(a[source][i]==1 && Output:
visited[i]==0) Enter the number of nodes
dfs(n,a,i); 4
}
Enter the path(0-no edge &1=if preset)
void main()
{ 0 1 1 1
int i,j,source; 0 1 1 1
int n,a[10][10];
0 1 0 1
clrscr();
printf("Enter the number of nodes:\ 0 0 0 0
n"); Enter the Source node
scanf("%d",&n);
2
printf("Enter the path(0-no edge
&1=if preset)\n"); The node 1 is not reachable
for(i=1;i<=n;i++) The node 2 is reachable
for(j=1;j<=n;j++) The node 3 is reachable
scanf("%d",&a[i][j]);
printf("Enter the source node\n"); The node 4 is reachable
scanf("%d",&source);
for(i=1;i<=n;i++)
7. Write a program to implement if(f==1)
Binary Search using divide and printf("\n the
conquer technique. element is found\n");
else
#include<stdio.h> printf("\n the
#include<conio.h> element is not
void main() found\n");
{ getch();
int a[10],i,n,x,f,low,high,mid; }
clrscr(); Output:
printf("Enter the size of the Enter the size of the array
array:\n"); 5
scanf("%d",&n); Enter 5 elements
printf("\n Enter the %d 20
element:\n",n); 25
for(i=0;i<n;i++) 30
scanf("%d",&a[i]); 40
printf("\n Enter the element 50
to be searched:\n"); Enter the elements to be searched
scanf("%d",&x); 40
low=0; The element is found
high=n-1;
for(i=0;i<n;i++)
{
mid=(low+high)/2;
if(x==a[mid])
f=1;
else
if(x<a[mid])
high=mid-1;
else
low=mid+1;
8. Write a program to implement Insertion {
Sort using decrease and conquer technique.
scanf("%d",&a[i]);
#include<stdio.h> }
#include<conio.h> in_sort(n,a);
#include<time.h> e=clock();
void in_sort(int n,int a[]) printf("sorted element are\n");
{ for(i=0;i<n;i++)
int i,j,item; printf("%d\n",a[i]);
for(i=0;i<n;i++) printf("time taken:%f
{ sec",(e-s)/CLK_TCK);
item=a[i]; getch();
j=i-1; }
while(j>=0 && item<=a[i])
{ Output:
a[j+1]=a[j]; Enter the number of elements to sort
j=j-1; 5
} Enter the elements
a[j+1]=item;
}
} Sorted elements are
void main()
{
int n,i,j,a[200]; Time taken: 0.109890 sec
clock_t s,e;
clrscr();
printf("Enter the no. of element to
sort\n");
scanf("%d",&n);
s=clock();
printf("Enter the element\n");
for(i=0;i<n;i++)
1)#include<stdio.h>
#include<conio.h>
void quik_sort (int a[10], int f,int p=f,i=f;j=k;
k); while(i<j)
void main() {
while((a[i]<=a[p])&&i<k)
{
i++;
int i,n,a[10];
while(a[j]>a[p])
clrscr();
\ j--;
printf("enter the element\n"); if(i<j)
{
scanf("%d",&n);
t=a[i];
printf("enter the array a[i]=a[j];
elements\n");
a[j]=t;
for(i=0;i<n;i++) }
}
scanf("%d",&a[i]);
t=a[p];
quik_sort(a,0,n-1);
a[p]=a[j];
printf("the sorted array is\n");
a[j]=t;
for(i=0;i<n;i++)
quik_sort(a,f,j-1);
printf("%d\n",a[i]);
quik_sort(a,j+1,k);
getch();
}
}
void quik_sort(int a[10],int f,int k) }
{
2)#include<stdio.h>
int i,j,p,t;
if(f<k) #include<conio.h>
{
#include<time.h> j=j+1;
voidsimple_merge(int a[], k=k+1;
intlow,intmid,int high)
}
{
}
inti=low;
while(i<=mid)
int j=mid+1;
{
int k=low;
c[k++]=a[i++];
int c[10];
}
while(i<=mid && j<=high)
while(j<=high)
{
{
if(a[i]<a[j])
c[k++]=a[j++];
{
}
c[k]=a[i];
for(i=low;i<=high;i++)
i=i+1;
{
k=k+1;
a[i]=c[i];
}
}
else
}
{
void merge_sort(int a[],int low,
c[k]=a[j]; int high)
{ scanf("%d",&n);
int mid; printf("enter the element\n");
if(low<high) for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
mid=(low+high)/2; st=clock();
merge_sort(a,low,mid); merge_sort(a,0,n-1);
merge_sort(a,mid+1,high); delay(100);
simple_merge(a,low,mid,high); e=clock();
} diff=(e-st)/CLK_TCK;
} printf("sorted elements are\
n");
void main()
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
int a[10],n,i;
printf("time required for
clock_t st,e;
merge sort is:%f sec",diff);
float diff;
getch();
clrscr();
}
printf("enter thenumber of
element to sort\n");