You are on page 1of 11

3.

Write a program to obtain the for(i=0;i<n;i++)


topological ordering of vertices in a given for(j=0;j<n;j++)
digraph. in[i]=in[i]+a[j][i];
#include<stdio.h> printf("\n\n the topological order
#include<conio.h> is:");
#define TRUE 1 while(count<n)
#define FALSE 0 {
int main() for(k=0;k<n;k++)
{ {
int i,j,k; if((in[k]==0)&&(visit[k]==0))
int n,a[10][10],in[10],visit[10]; {
int count=0;
clrscr(); printf("%d",k);
visit[k]=TRUE;
printf("\n\t\t topological sorting");
n=4; }
for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
for(j=0;j<n;j++) if(a[i][k]==1)
in[k]--;
{
}
a[i][j]=0; }
} count++;
} }
printf("\n graph is created as follows-"); getch();
printf("\n the node '0' and '1' are return 0;
}
connected");
a[0][1]=1; Output:
printf("\n the node '0' and '2' are
connected"); Topological Sorting
a[0][2]=1; Graph is created as follows-
The node ‘0’ and ‘1’ are connected
printf("\n the node '3' and '2' are
The node ‘0’ and ‘2’ are connected
connected"); The node ‘3’ and ‘2’ are connected
a[3][2]=1;
for(i=0;i<n;i++) The topological order is: 0 3 1 2
{
in[i]=0;
visit[i]=FALSE;
}
4. Implement the knapsack problem(0/1).

#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++)

9. Find minimum cost Spanning tree of a }


given undirected path using a Prim's void main()
algorithm. {
int a[10][10],i,n,j,m,source;
#include<stdio.h> clrscr();
#include<conio.h> printf("Enter the number of vertexs\
#define INFINITY 999 n");
int prim(int cost[10][10],int source,int n) scanf("%d",&n);
{ printf("Enter the cost matrix:0-self
int loop & 999 for no edge\n");
i,j,sum=0,visited[10],cmp[10],vertex[10]; for(i=1;i<=n;i++)
int min,u,v; for(j=1;j<=n;j++)
for(i=1;i<=n;i++) scanf("%d",&a[i][j]);
{ printf("Enter the source\n");
vertex[i]=source; scanf("%d",&source);
visited0[i]=0; m=prim(a,source,n);
cmp[i]=cost[source][i]; printf("Total cost=%d",m);
} getch();
visited[source]=1; }
for(i=1;i<=n-1;i++)
{ Output:
min=INFINITY; Enter the number of vertices
for(j=1;j<=n;j++) 4
if(!visited[j]&&cmp[j]<min) Enter the cost matrix: 0-self loop &
{ 999 for no edge
min=cmp[j]; 0 1 5 2
u=j; 1 0 999 999
} 5 999 0 3
visited[u]=1; 2 999 3 0
sum=sum+cmp[u]; Enter the source
printf("%d->%d sum=%d\ 1
n",vertex[u],u,sum);
for(v=1;v<=n;v++) 1à2 sum=1
if(!visited[v]&&cost[u] 1à4 sum=3
[v]<cmp[v]) 4à3 sum=6
{
cmp[v]=cost[u][v]; Total cost=6
vertex[v]=u;
}
}
return sum;

10.From a given vertex in a weighted min=999;


connected graph, find shortest paths for(i=1;i<=n;i++)
to other vertices using dijkstra's algorithm. {
#include<stdio.h> if(!visited[i])
void dij(int,int [20][20],int [20],int [20],int); {
void main() if(d[i]<min)
{ {
int i,j,n,visited[20],source,cost[20] min=d[
[20],d[20]; i];
clrscr(); u=i;
printf("Enter no. of vertices:\n"); }
scanf("%d",&n); }
printf("Enter the cost adjacency matrix\ }
n"); visited[u]=1;
for(i=1;i<=n;i++) for(w=1;w<=n;w++)
{ {
for(j=1;j<=n;j++) if(cost[u][w]!=999 && visited[w]==0)
{ {
scanf("%d",&cost[i][j]); if(d[w]>cost[u][w]+d[u])
} d[w]=cost[u][w]+d[u];
} }
printf("\n Enter the source node:\n"); }
scanf("%d",&source); }
dij(source,cost,visited,d,n); }
for(i=1;i<=n;i++)
if(i!=source)
printf("\n Shortest path from %d to %d is Output:
%d",source,i,d[i]);
getch(); Enter no. of vertices:
} 5
void dij(int source,int cost[20][20],int Enter the cost adjacency matrix
visited[20],int d[20],int n) 0 3 999 7 999
{ 3 0 4 2 999
int i,j,min,u,w; 999 4 0 5 6
for(i=1;i<=n;i++) 7 2 5 0 4
{ 999 999 6 4 0
visited[i]=0;
d[i]=cost[source][i]; Enter the source node:
} 1

d[source]=0; Shortest path from 1 to 2 is 3


for(j=2;j<=n;j++) Shortest path from 1 to 3 is 7
{ Shortest path from 1 to 4 is 5
Shortest path from 1 to 5 is 9

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");

You might also like