Tuesday 6 May 2014

Program In C To Implement Prim's Algorithm

#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
 for(j=1;j<=n;j++)
 {
  scanf("%d",&cost[i][j]);
  if(cost[i][j]==0)
   cost[i][j]=999;
 }
visited[1]=1;
printf("\n");
while(ne<n)
{
 for(i=1,min=999;i<=n;i++)
  for(j=1;j<=n;j++)
   if(cost[i][j]<min)
    if(visited[i]!=0)
    {
     min=cost[i][j];
     a=u=i;
     b=v=j;
    }
 if(visited[u]==0 || visited[v]==0)
 {
  printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
  mincost+=min;
  visited[b]=1;
 }
 cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}

Friday 11 April 2014

Program In C++ TO Implement Rabin karp String Matching Algorithm

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ int T[30],P[10],i,n,m,p,to,s,flag=0,j,d=10,q=11,h;
clrscr();
cout<<"Enter The Length Of Text: ";
cin>>n;
cout<<"\nEnter Text: ";
for(i=1;i<=n;i++)
cin>>T[i];
cout<<"\nEnter The Length Of Pattern: ";
cin>>m;
cout<<"\nEnter Pattern: ";
for(i=1;i<=m;i++)
cin>>P[i];
h=pow(d,m-1);
h=h%q;
p=to=0;
for(i=1;i<=m;i++)
{ p=((d*p)+P[i])%q;
to=((d*to)+T[i])%q;
for(s=0;s<=n-m;s++)
if(p==to)
{
for(i=s+1;i<=s+m;i++)
{
 for(j=1;j<=m;j++)
 {
  if(T[s+j]==P[j])
   {
    flag=1;
    continue ;
   }
  else
  {
   flag=0;
   break;
  }
 }
}
if(flag==1)
 {
  cout<<"\nPattern Occurs With Shift: "<<s<<"\n";
 }
}
if(s<n-m)
{ to=(d*(to-(T[s+1]*h)))+(T[s+m+1]%q);}
}
getch();
}

Tuesday 18 March 2014

Program In C++ To Implement Naive String Matching Algorithm

#include<iostream.h>
#include<conio.h>
void naive(int t[],int p[],int n,int m)
{
 int s,flag=0,i,j;
 for(s=0;s<=(n-m);s++)
{
 for(i=s+1;i<=s+m;i++)
 {
  for(j=1;j<=m;j++)
  {
   if(t[s+j]==p[j])
    {
     flag=1;
     continue ;
    }
   else
   {
    flag=0;
    break;
   }
  }
 }
 if(flag==1)
  {
   cout<<"\nPattern Occurs With Shift: "<<s<<"\n";
  }
 }
}

void main()
{ clrscr();
int p[100],t[20];
int m,n,i;
cout<<"Enter Length Of Text : ";
cin>>n;
cout<<"\nEnter Text : ";
for(i=1;i<=n;i++)
{ cin>>t[i]; }
cout<<"\nEnter Length Of Pattern: ";
cin>>m;
cout<<"\nEnter Pattern: ";
for(i=1;i<=m;i++)
{ cin>>p[i]; }
naive(t,p,n,m);
getch();
}

Tuesday 11 March 2014

Program In C++ To Find Maximum And Minimum In 2D Array

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[20][20],m,n,i,j,max,min;
cout<<"Enter Number Of Rows In Array: ";
cin>>m;
cout<<"\nEnter Number Of Columns In Array: ";
cin>>n;
cout<<"\nEnter Array: ";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ cin>>a[i][j]; }
}
min=max=a[0][0];
cout<<"\nMatrix Entered Is: \n";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ cout<<a[i][j]<<"  ";
if(a[i][j]<min)
min=a[i][j];
if(a[i][j]>max)
max=a[i][j];
}
cout<<"\n"; }
cout<<"\nMinimum In The Array Is: "<<min;
cout<<"\nMaximum In The Array Is: "<<max;
getch();
}

Program In C++ To Find Maximum And Minimum In 1D Array

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[20],n,i,max,min;
cout<<"\nEnter Size Of Array: ";
cin>>n;
cout<<"\nEnter Array: ";
for(i=0;i<n;i++)
cin>>a[i];
max=min=a[0];
for(i=0;i<n;i++)
{ if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
cout<<"\nMinimum In Array Is: "<<min;
cout<<"\nMaximum In Array Is: "<<max;
getch();
}

Monday 10 March 2014

Program In C++ for quicksort sort and its time complexity

#include<iostream.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
int Partition(int a[],int p,int r)
{ int tmp,x=a[r];
int i=p-1;
for(int j=p;j<r;j++)
if(a[j]<x)
{ i++;
tmp=a[i];
a[i]=a[j];
a[j]=tmp; }
tmp=a[i+1];
a[i+1]=a[r];
a[r]=tmp;
return i+1; }
void quicksort(int a[],int p,int r)
{ int q;
if(p<r)
 { q=Partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r); }}

int main()
{ clrscr();
int a[10],n,i,j,tmp;
clock_t start,end;
cout<<"Enter Array Size: ";
cin>>n;
cout<<"\nEnter Array: ";
for(i=0;i<n;i++)
{ cin>>a[i]; }
start=clock();
quicksort(a,0,n-1);
delay(100);
end=clock();
cout<<"\nArray After Sorting Is: ";
for(i=0;i<n;i++)
{ cout<<a[i]<<"\t"; }
cout<<"\nTime Taken Is: "<<(end-start)/CLK_TCK<<" seconds";
getch();
return 0;
}

Program In C++ for merge sort and its time complexity

#include<iostream.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
#include<limits.h>
void merge(int a[],int p,int q,int r)
{int i,j;
int n1=q-p+1;
int n2=r-q;
int L[6];int R[6];
for(i=0;i<n1;i++)
  L[i]=a[p+i];
for(j=0;j<n2;j++)
  R[j]=a[q+j+1];
 L[n1]=INT_MAX;
 R[n2]=INT_MAX;
 i=0;j=0;
 for(int k=p;k<=r;k++)
 { if(L[i]<R[j])
   { a[k]=L[i];
     i++; }
   else
   { a[k]=R[j];
     j++;
   }
 }
}
void mergesort(int a[],int p,int r)
{ int q;
if(p<r)
{ q=(p+r)/2;
mergesort(a,p,q);
mergesort(a,q+1,r);
merge(a,p,q,r);
 }
}
void main()
{ clrscr();
int a[10],n,i;
clock_t start, end;
cout<<"\nEnter The Size Of Array: ";
cin>>n;
cout<<"\nEnter The Array: ";
for(i=0;i<n;i++)
cin>>a[i];
start=clock();
mergesort(a,0,n-1);
delay(200);
end=clock();
cout<<"\nArray After Sorting Is: ";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<"\nTime Taken Is: "<<(end-start)/CLK_TCK;
getch();
}