Tuesday, October 13, 2015

program to find the linearly dependent columns in multidimensional array

#include<stdio.h>
#include<stdlib.h>
int main()
{

int i,j,k,m,n,len,col,a[9][9]={{2,3,6,5,1,2,3,6,9},{2,3,6,5,4,2,3,6,2},{2,3,6,5,4,2,3,1,9},{2,3,6,5,4,2,3,6,9},{2,3,6,5,4,2,3,6,9},
       {2,3,6,5,4,2,3,6,9},{2,3,6,5,4,3,3,6,9},{2,3,6,5,4,2,4,6,9},{2,3,6,5,8,2,3,6,9}
        };

for(i=0;i<8;i++)
    for(j=i+1;j<9;j++)
{ printf("here");
     if(a[0][i]%a[0][j]==0)
     {  printf("here1");
           len=1;
         col=i;
         for(k=1;k<9;k++)
         {
             if(a[k][i]%a[k][j]!=0)
                 len=0;
         }
       printf("here2");
     }
     else if(a[0][j]%a[0][i]==0)
     {   len=1;printf("here3");
         col=j;
         for(k=1;k<9;k++)
         {   len=1;printf("in55");
             if(a[k][j]%a[k][i]!=0)
                 len=0;
                 printf("in56");
         }
         printf("here4");
     }
     if(len==1)printf("\ncolumn : %d is linearly dependent",col);
}
return 0;
}

Saturday, October 10, 2015

Implementation of longest common sub-sequence in c

#include<stdio.h>
#include<stdlib.h>
int max(int a,int b){return (a>b)?a:b;}
char *x, *y;
int lcs(int i,int j)
 {
  if(i==0||j==0)return 0;
  else if(i>0&&j>0&&x[i]==y[j]) return lcs(i-1,j-1)+1;
  else if(i>0&&j>0&&x[i]!=y[j]) return max(lcs(i-1,j),lcs(i,j-1));
}
int main()
{int n=10,m=5;

 x=(char *)malloc(n*sizeof(char ));
 y=(char *)malloc(m*sizeof(char ));
 gets(x);
 gets(y);
 //sizeof longest common subsequence among first n letters of x and first m letters of y
 printf("%d",lcs(n,m));
return 0;
}

Thursday, October 8, 2015

Represent graph with adjacency list : C program

#include <stdio.h>
#include <stdlib.h>
typedef struct node{int key;struct node * next;}node;
node * init_node(node * n,int b){n=(node *)malloc(sizeof(node));n->key=b;n->next=NULL;return n;}
node **list,*front=NULL,*rear=NULL;
void enqueue(node * n)
{ if(rear==NULL)front=rear=n;
  else{ rear->next=n;rear=n; } }

void add_edge(int a,int b)
{  node * ptr=NULL,*n=NULL;
   n=init_node(n,b);
    if(list[a]->next==NULL){list[a]->next=n;}
  else{  ptr=list[a]; while(ptr->next!=NULL)ptr=ptr->next;ptr->next=n;}
}
int main()
{int n=4,i,j;node * ptr=NULL;
 list=(node **)malloc(n*(sizeof(node *)));

for(i=0;i<n;i++)
{  list[i]=(node *)malloc(n*(sizeof(node *)));list[i]->next=NULL;list[i]->key=i;}

add_edge(0,3);add_edge(0,1);add_edge(1,3);
add_edge(2,0);add_edge(3,2);add_edge(2,1);
for(j=0;j<n;j++)
    if(list[j]->next!=NULL)
         { printf("[ %d ]",j);ptr=list[j]->next;
           while(ptr!=NULL){printf("--> %d ",ptr->key);ptr=ptr->next;}
          printf("\n");
         }

printf("IN THE END......");
return 0;
}

Saturday, October 3, 2015

Program for String Matching with Finite Automata in C

#include <stdio.h>
#include <stdlib.h>
char * prefix(char * c,int start, int end,int size)
{   int i;
    char *temp=(char *)malloc(size*sizeof(char));
    for(i=0;i<size;i++)
       temp[i]=c[start++];
    return temp;
}
char * suffix(char * c,int start, int end,int size)
{   int i,j;
    char *temp=(char *)malloc(size*sizeof(char));
    for(j=0,i=end-size+1;i<=end;i++,j++)
       temp[j]=c[i];
    return temp;
}
int match(char * a, char *b)
{  int i=0;
    while(a[i]!='\0')
     {
if(a[i]!=b[i])
 return 0;
i++;
     }
    return 1;
}
int sigma(char * t , char * p ,int i)
{
  int j,v=0;
  for(j=0;p[j]!='\0';j++)
     if(match(suffix(t,0,i,j+1),prefix(p,0,j,j+1)))
v=j+1;
  return v;
}
int main()
{  char a[]="dfhghjgfjschhscgchgfbgfsgfjh";
   char b[]="jhgfj";
   printf("%d\n",sigma(a,b,8));
   
 
}

Friday, October 2, 2015

Implmentation of Rabin-Karp Algorithm in c

#include <stdio.h>
#include<stdlib.h>
int hash (char * a,int s,int t)
{ int h=0;
    for(;s<=t;s++)
        h+=a[s];
       return h;
}
int match(char * a,int a1,int a2,char * b, int b1,int b2)
{  
    for(;a1<=a2,b1<=b2;a1++,b1++)
       { if(a[a1]!=b[b1])
           return 0;
       }
       return 1;
}   
   

void rk(char*a,char *b,int as,int bs)

   int ph=hash(b,0,bs-1),i;
   for(i=0;i<(as-bs+1);i++)
    { if(hash(a,i,i+bs-1)==ph)
        if(match(a,i,i+bs-1,b,0,bs-1))
      { printf("  match for < %s > found at position : %d ",b,i+1);}
    }
        
    
}
void main()
{  
 char t[]="abcdefghijklmnopqrstuvwxyz";
 char p[]="klmnop";
    int st=sizeof(t)-1;
    int  sp=sizeof(p)-1;
    rk(t,p,st,sp);
    
    
}