b-m算法

b-m算法原理及实现

b-m算法常用来研究某一的01比特串的线性复杂度,换句话说通过BM算法可以得到01序列的最小线性多项式。

在介绍b-m算法之前,我先列出基本的公式


从公式就可以看出b-m算法是一个迭代算法,它的基本思想是利用前n-1个01序列所求出的最小线性多项式来求前n个01 序列的最小线性多项式,初始值容易明确,其重点在于整个算法如何如何迭代。

首先要明确的是公式中的加法并不是‘或’ 的意思,而是‘异或’运算,参数c,x和a之间是‘与’运算,x的n次方是指,第n号线性移位寄存器的值。

迭代有三种情况:

  • 第一种情况比较特殊,就是前n个01序列全为0的情况,这时的L0,L1,...,Ln全为0,很容易得到fn(x)=1;

  • 第二种情况就是当dn==0时, 这时应当认为fn-1(x)是可以推到出前n个01 序列的,即fn-1(x)==fn(x)

  • 第三种情况则是dn==1,这时fn-1(x)不适用前n个01 序列,如何进行调整?先把问题放在这里,我们先讨论一下L,L是指fn(x)所能取到的最大的次数,当然由于fn(x) 可能会退化,fn(x)的次数可能会小于L。明白了这一点,就很容易知道L是单调不减的,同时它增长的时候必然是dn==1的。同样是两种情况,一种是前n-1个01序列全为0,第n个序列为1的情况,易得Ln=n,当然这是一种特殊情况; 同样Ln可以看成生成前n个序列所需要的最大寄存器数,那么这第二种情况就可以看成当fn(x)不适用前n+1个01 序列时,比较ln与n+1-ln,如果n+1-ln比较大,那就说明原来的那ln个寄存器已经不够用了,这时需要n+1-ln个寄存器。 ok,当我们在回来看m的取值时,就很容易的理解,m就是记录最新的一次L的更新,值得注意的是fm所求出的dn==1。回到最开始的问题,我们需要考虑两个方面,一个是如何把dn调整到0,另一个是如何不会破化前面已经修正的n-1序列。方法就是fn-1 + x^(n-m)*fm

    个人能力有限,无法给出严格的数学证明,只能做一些直观的理解,如果有错误的地方,欢迎指正

附上c语言代码,采用c99标准,在命令行下运行,用了几个01序列验证,尚未发现bug

  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
  #include <stdbool.h>
  
  typedef unsigned int bin;
  
  //将接受的01字符串转化为一个无符号整形
  bin strToBinary(char* str)
  {
     bin result=0;
     int len=strlen(str);
     for(int i=0;i<len;i++)
     {
          if(str[i]=='1')
          {
              result=(result<<1)+1;
          }
          else
          {
              result<<=1;
          }
     }
     return result;
  }
  
  //利用list序列,cn参数,以及ln+1长度求出dn(dn为0或1)
  bool getbn(bin list,bin cn,int seek)
  {
      bool bn=((cn>>seek) & (list>>seek)) & 1;
      while((--seek)>-1)
      {
          bn^=((cn>>seek) & (list>>seek)) & 1;
      }
      return bn;
  }
  
  
  //通过命令行接收01序列
  int main(int argc,char* argv[])
  {
      //简单处理得到的参数
      if(2!=argc)
      {
          perror("error:parameter is not correct\n");
          exit(22);
      }
      else if(strlen(argv[1])>32)
      {
          perror("error:parameter is too big\n");
          exit(22);
      }
  
      //序列的长度
      int N=strlen(argv[1]);
  
      //list为格式化的序列
      bin list=strToBinary(argv[1]);
  
      int n=0,ln=0;
      bool bn;
      int m;
      //初始化fm,每一位分别是cln,cln-1,...,c1,c0
      bin fm=0;
      //初始化f(x),每一位分别是cln,cln-1,...c1,c0
      bin f=1;
      //判断l0,l1,...,ln是否全为0
      bool flagIs0=true;
      //临时保存fn-1
      bin fn_1;
  
      do
      {
          bn=getbn(list>>(N-n-1),f,ln);
          if(bn)
          {
              if(flagIs0)
              {
                  m=n;
                  fm=f;
                  f=(1<<(n+1)) | 1;
                  ln=n+1;
              }
              else
              {
                  fn_1=f;
                  f^=fm<<(n-m);
                  if(ln<n+1-ln)
                  {
                      m=n;
                      fm=fn_1;
                      ln=n+1-ln;
                  }
              }
              if(0!=ln)
                  flagIs0=false;
          }
          n++;
      }while(n<N);
  
      //打印f
      printf("f(x)=1");
      for(int i=1;i<N;i++)
      {
          if((f>>i)&1)
          {
              printf("+x^%d",i);
          }
      }
      printf("\n");
      
  
      return 0;
  }


  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以处理无限数据,求其线性复杂度 //by史瑞 //LFSR线性移位寄存器以及求异或运算OK unsigned char LFSRB_M(unsigned char *fun,unsigned char *seq,unsigned long Cont){ unsigned long x; unsigned char ch=0x00,t=0x00,*array; array=(unsigned char *)malloc(Cont*sizeof(unsigned char)); for(x=0;x<Cont;x++) *(array+x)=(*(seq+x))&(*(fun+x)); for(x=0;x<Cont*8;x++){ ch=(*array)&0x80; LeftShift(array,Cont); t^=ch; } free(array); return t; } #define word(ln) ((ln-1)/8+1) #define place(ln) ((ln-1)%8) //Berlek_Massey求生成任意序列的联接多项式OK unsigned char *Berlek_Massey(unsigned char *seq,unsigned long *Rank,unsigned long Cont,FILE *fmm){ unsigned long n=0,m,lm,ln=0; int d; unsigned long lfm; unsigned char *fun,*fm,*array,ch,t=0x00; unsigned char *func,*fmc; unsigned long x,y,k; fun=(unsigned char *)malloc(sizeof(unsigned char)); *(fun)=0x00; for(x=0;x<Cont*8;x++){ if(ln!=0){ array=(unsigned char *)malloc(word(ln)); InitialDSR(array,word(ln)); for(y=0;y<ln;y++){ ch=((*(seq+(n-(y+1))/8))<<((n-(y+1))%8))&0x80; if(ch) *(array+y/8)^=(ch>>(y%8)); } t=LFSRB_M(fun,array,word(ln)); d=((((*(seq+n/8))<<(n%8))&0x80;)^t)?1:0; free(array); } else d=(((*(seq+n/8))<<(n%8))&0x80;)?1:0; if(d){ if(ln!=0){ lm=ln; func=(unsigned char *)malloc(word(ln)*sizeof(unsigned char)); memcpy(func,fun,word(ln)); if(ln<(n+1-ln)){ ln=n+1-ln; } fmc=(unsigned char *)malloc(word(ln)*sizeof(unsigned char)); InitialDSR(fmc,word(ln)); memcpy(fmc,fm,word(lfm)); for(k=0;k<n-m;k++){ RightShift(fmc,word(ln)); } ch=0x80; *(fmc+(n-m-1)/8)^=(ch>>((n-m-1)%8)); fun=(unsigned char *)realloc(fun,word(ln)*sizeof(unsigned char)); for(k=word(lm);k<word(ln);k++) *(fun+k)=0x00; XorDSR(fun,fmc,word(ln)); free(fmc); if(lm<(n+1-lm)){ m=n; lfm=lm; fm=(unsigned char *)realloc(fm,word(lm)); memcpy(fm,func,word(lfm)); } free(func); } else{ ln=n+1; m=n; if(m!=0){ fm=(unsigned char *)malloc(word(m)*sizeof(unsigned char)); InitialDSR(fm,word(m)); lfm=m; } else{ fm=(unsigned char *)malloc(sizeof(unsigned char)); *fm=0x00; lfm=1; } fun=(unsigned char *)realloc(fun,word(ln)); InitialDSR(fun,word(ln)); ch=0x80; ch>>=(place(ln)); *(fun+(word(ln)-1))^=ch; } } n++; printf("\t<%d,%d>",n,ln); fprintf(fmm,"\t<n,d,ln>=<%d,%d,%d>",n,d,ln); if(n%3==0){ printf("\n"); fprintf(fmm,"\n"); } } printf("\nFn="); // for(k=0;k<word(ln);k++) // FromBytetoBit(*(fun+k)); *Rank=ln; return fun; }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值