Professional Documents
Culture Documents
MERGE SORT
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#define max 20
using namespace std;
void mergesort(int a[],int low,int high);
void merge(int a[],int low,int mid,int high);
int main()
{
int n,i,a[max],ch=1;
clock_t start,end;
while(ch)
{
printf("\n\t enter the number of elements\n");
scanf("%d",&n);
printf("Unsorted elements are:\n");
for(i=0;i<n;i++)
{
a[i]=rand()%1000;
printf("%d\t",a[i]);
}
start= clock();
mergesort(a,0,n-1);
end=clock();
printf("\nSorted elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n\ntime taken=%lf",(end-start)/CLK_TCK);
printf("\n\ndo u wish to continue(0/1) \n");
scanf("%d",&ch);
}
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
void quicksort(int[],int,int);
int partition(int[],int,int);
int main()
{
int i,n,a[20],ch=1;
clock_t start,end;
while(ch)
{
printf("\n enter the number of elements\n");
scanf("%d",&n);
printf("Unsorted elements are:\n");
for(i=0;i<n;i++)
{
a[i]=rand()%1000;
printf("%d\t",a[i]);
}
start=clock();
quicksort(a,0,n-1);
end=clock();
printf("\nSorted elements are :\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("/t time taken=%f",(end-start)/CLK_TCK);
printf("\n\n do u wish to continue (0/1)\n");
scanf("%d",&ch);
}
return 0;
}
void quicksort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=partition(a,low,high);
quicksort(a,low,mid-1);
quicksort(a,mid+1,high);
}
}
int partition(int a[],int low,int high)
{
int key,i,j,temp,k;
key=a[low];
i=low+1;
j=high;
while(i<=j)
{
while(i<=high && key>=a[i])
i=i+1;
while(key<a[j])
j=j-1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
k=a[j];
a[j]=a[low];
a[low]=k;
}
}
return j;
}
3. TOPOLOGICAL SORTING
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define max 20
int a[max][max],n;
void topological_sort();
using namespace std;
int main()
{
int i,j;
printf("\n enter the number of vertices\n");
scanf("%d",&n);
printf("\n enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
topological_sort();
}
void topological_sort()
{
int v[max],ver[max],i,j,p=1,flag=0;
for(i=1;i<=n;i++)
v[i]=0;
while(p<=n)
{
j=1;
while(j<=n)
{
flag=0;
if(v[j]==0)
{
for(i=1;i<=n;i++)
#include<stdio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(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++]);
}
}
int main() {
int v;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
#include<stdio.h>
using namespace std;
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);
}
}
}
int main()
{
int n,i,j,cost[10][10],s[10],connected,flag;
printf("\n enter the number of nodes\n");
scanf("%d",&n);
printf("\n 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 connected\n");
}
6 . HEAP SORT
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main()
{
int i,n,a[20];
clock_t s,e;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the elements to sort\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
s=clock();
heapsort(a,n);
e=clock();
printf("The sorted elements are :\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
printf("\nTime taken = %d\n",(e-s)/CLK_TCK);
}
7. HORSPOOL
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
int table[126];
char t[100],p[25];
int i,j,k,m,n,f=0;
printf("Enter the text\n");
gets(t);
n=strlen(t);
printf("Enter the pattern\n");
gets(p);
m=strlen(p);
for(i=0;i<126;i++)
table[i]=m;
for(j=0;j<m-2;j++)
table[p[j]]=m-1-j;
i=m-1;
while(i<=n-1)
{
k=0;
while( k<=m-1 && p[m-1-k]==t[i-k])
k++;
if(k==m)
{
printf("The position of the pattern is %d\n",i-m+2);
f=1;
break;
}
else
i=i+table[t[i]];
}
if(!f)
printf("pattern is not found in the given text\n");
return 0;
}
8. WARSHALL’S ALGORITHM
#include<stdio.h>
using namespace std;
void warshall(int[10][10],int);
int main()
{
int a[10][10],i,j,n;
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("The adjacency matrix is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
warshall(a,n);
}
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int n,a[10][10],i,j;
printf("Enter the number of nodes : ");
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]);
floyd(a,n);
}
#include<stdio.h>
#define MAX 50
using namespace std;
int p[MAX],w[MAX],n;
int knapsack(int,int);
int max(int,int);
int main()
{
int m,i,optsoln;
printf("Enter no. of objects:");
scanf("%d",&n);
printf("\nEnter the weights:\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("\nEnter the profits:\n");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("\nEnter the knapsack capacity:");
scanf("%d",&m);
optsoln=knapsack(1,m);
printf("\nThe optimal solution is:%d",optsoln);
}
#include<stdio.h>
int ary[10][10],completed[10],n,cost=0;
int least(int c);
void takeInput()
{
int i,j;
printf("Enter the number of villages: ");
scanf("%d",&n);
printf("\nEnter the Cost Matrix\n");
for(i=0;i < n;i++)
{
printf("\nEnter Elements of Row: %d\n",i+1);
for( j=0;j < n;j++)
scanf("%d",&ary[i][j]);
completed[i]=0;
}
printf("\n\nThe cost list is:");
for( i=0;i < n;i++)
{
printf("\n");
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i < n;i++)
{
if((ary[c][i]!=0)&&(completed[i]==0))
if(ary[c][i]+ary[i][c] < min)
{
min=ary[i][0]+ary[c][i];
kmin=ary[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
int main()
{
takeInput();
printf("\n\nThe Path is:\n");
mincost(0); //passing 0 because starting vertex
printf("\n\nMinimum cost is %d\n ",cost);
return 0;
}
12. PRIM’S ALGORITHM
#include<stdio.h>
int ne=1,min_cost=0;
int main()
{
int n,i,j,min,cost[20][20],a,u,b,v,source,visited[20];
printf("Enter the no. of nodes:");
scanf("%d",&n);
printf("Enter the cost matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
}
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("Enter the root node:");
scanf("%d",&source);
visited[source]=1;
printf("\nMinimum cost spanning tree is\n");
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
if(visited[i]==0)
continue;
else
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
if(visited[u]==0||visited[v]==0)
{
printf("\nEdge %d\t(%d->%d)=%d\n",ne++,a,b,min);
min_cost=min_cost+min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost=%d\n",min_cost);
}
13. KRUSKAL’S ALGORITHM
#include<stdio.h>
int ne=1,min_cost=0;
int main()
{
int i,j,n,a,b,min,u,v,cost[20][20],parent[20];
printf("Enter the number of vertices : ");
scanf("%d",&n);
printf("Enter the cost adjacency matrix :\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
for(i=1;i<=n;i++)
parent[i]=0;
printf("\nThe edges of the spanning tree are\n");
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
printf("Edge %d\t(%d -> %d)=%d\n",ne++,a,b,min);
min_cost=min_cost+min;
parent[v]=u;
}
cost[a][b]=999;
}
printf("\nMin cost = %d\n",min_cost);
}
14. DIJKSTRA’S PROBLEM
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
void nqueens(int);
int place(int[],int);
void printsolution(int,int[]);
int main()
{
int n;
printf("Enter the no.of queens:");
scanf("%d",&n);
nqueens(n);
}
void nqueens(int n)
{
int x[10],count=0,k=1;
x[k]=0;
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("\nSolution %d\n",count);
printsolution(n,x);
}
else
{
k++;
x[k]=0;
}
}
else
{
k--;
}
}
return;
}
#include<stdio.h>
void subset(int,int,int);
int x[10],w[10],d,count=0;
int main()
{
int i,n,sum=0;
printf("Enter the no. of elements:"); scanf("%d",&n);
printf("\nEnter the elements in ascending order:\n");
for(i=0;i<n;i++)
scanf("%d",&w[i]);
printf("\nEnter the sum:");
scanf("%d",&d);
for(i=0;i<n;i++)
sum=sum+w[i];
if(sum<d)
{
printf("No solution\n");
return 0;
}
subset(0,0,sum);
if(count==0)
{
printf("No solution\n");
return 0;
}
}
void subset(int cs,int k,int r)
{
int i;
x[k]=1;
if(cs+w[k]==d)
{
printf("\n\nSubset %d\n",++count);
for(i=0;i<=k;i++)
if(x[i]==1)
printf("%d\t",w[i]);
}
else if(cs+w[k]+w[k+1]<=d)
subset(cs+w[k],k+1,r-w[k]);
if(cs+r-w[k]>=d && cs+w[k]<=d)
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------