You are on page 1of 18

1.

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

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


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

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


{
int i,j,k,t[max];
i=low;
j=mid+1;
k=low;
while((i<=mid) && (j<=high))
{
if(a[i]<=a[j])
t[k++]=a[i++];
else
t[k++]=a[j++];
}
while(i<=mid)
t[k++]=a[i++];
while(j<=high)
t[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=t[i];
}
2. QUICK SORT

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

if((a[i][j]!=0) && (v[i]==0))


{
flag=1;
break;
}
if(flag==0)
{
v[j]=1;
ver[p++]=j;
break;
}
}
j++;
if(j>n)
{
printf("topo order is nt posible");
getch();
exit(0);
}
}
}
printf("\n topological order obtained is...\n");
for(i=1;i<p;i++)
printf("\t%d",ver[i]);
}
4. BFS

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

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


q[i] = 0;
visited[i] = 0;
}

printf("\n Enter graph data in matrix form:\n");


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

printf("\n Enter the starting vertex:");


scanf("%d", &v);
bfs(v);
printf("\n The node which are reachable are:\n");

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


if(visited[i])
printf("%d\t", i);
else {
printf("\n Bfs is not possible. Not all nodes are reachable");
break;
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
5. DFS

#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;

void heapcom(int a[],int n)


{
int i,j,k,item;
for(i=1;i<=n;i++)
{
item=a[i];
j=i;
k=j/2;
while(k!=0 && item>a[k])
{
a[j]=a[k];
j=k;
k=j/2;
}
a[j]=item;
}
}

void adjust(int a[],int n)


{
int item,i,j;
j=1;
item=a[j];
i=j*2;
while(i<n)
{
if((i+1)<n)
{
if(a[i]<a[i+1])
i++;
}
if(item<a[i])
{
a[j]=a[i];
j=i;
i=2*j;
}
else
break;
}
a[j]=item;
}

void heapsort(int a[],int n)


{
int i,temp;
heapcom(a,n);
for(i=n;i>=1;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
adjust(a,i);
}
}

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

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


{
int i,j,k;
for(k=1;k<=n;k++)
{
for(j=1;j<=n;j++)
{
for(i=1;i<=n;i++)
{
if((p[i][j]==0) && (p[i][k]==1) && (p[k][j]==1))
{
p[i][j]=1;
}
}
}
}
printf("\nThe path matrix is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",p[i][j]);
}
printf("\n");
}
}
9. FLOYD’S ALGORITHM

#include<stdio.h>
#include<stdlib.h>
using namespace std;

void floyd(int [10][10],int);


int min(int,int);

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

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


{
int d[10][10],i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
d[i][j]=a[i][j];
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
printf("The distance matrix is :\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",d[i][j]);
printf("\n");
}
}

int min(int a,int b)


{
return (a<b)? a:b;
}
10. KNAPSACK PROBLEM

#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);
}

int knapsack(int i,int m)


{
if(i==n)
return (w[n]>m) ? 0 : p[n];
if(w[i]>m)
return knapsack(i+1,m);
return max(knapsack(i+1,m),knapsack(i+1,m-w[i])+p[i]);
}

int max(int a,int b)


{
return (a>b)?a:b;
}
11. TSP PROBLEM

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

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


printf("\t%d",ary[i][j]);
}
}

void mincost(int city)


{
int i,ncity;
completed[city]=1;
printf("%d--->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}

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>

void dij(int,int [20][20],int [20],int [20],int);


int main()
{
int i,j,n,visited[20],source,cost[20][20],d[20];
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",&cost[i][j]);
printf("Enter the source : ");
scanf("%d",&source);
dij(source,cost,visited,d,n);
for(i=1;i<=n;i++)
if(i!=source)
printf("\nShortest path from %d to %d is %d",source,i,d[i]);
}

void dij(int source,int cost[20][20],int visited[20],int d[20],int n)


{
int i,j,min,u,w;
for(i=1;i<=n;i++)
{
visited[i]=0;
d[i]=cost[source][i];
}
visited[source]=0;
d[source]=0;
for(j=2;j<=n;j++)
{
min=999;
for(i=1;i<n;i++)
{
if(!visited[i])
{
if(d[i]<min)
{
min=d[i];
u=i;
}
}
}
visited[u]=1;
for(w=1;w<=n;w++)
{
if(cost[u][w]!=999 && visited[w]==0)
{
if(d[w]>cost[u][w]+d[u])
d[w]=cost[u][w]+d[u];
}
}
}
}
15. NQUEENS PROBLEM

#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;
}

int place(int x[],int k)


{
int i;
for(i=1;i<k;i++)
if(x[i]==x[k]||(abs(x[i]-x[k]))==abs(i-k))
return 0;
return 1;
}

void printsolution(int n,int x[])


{
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';
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%c\t",c[i][j]);
}
printf("\n");
}
}
16. SUBSET PROBLEMS

#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]);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------

You might also like