The Berlekamp–Massey algorithm is an algorithm for finding the shortest linear feedback shift register (LFSR) for a given output sequence. Equivalently, it is an algorithm for finding the minimal polynomial of a linearly recurrent sequence.
Do not confuse it with Berlekamp's algorithm for factoring polynomials over finite fields.
The algorithm was invented by Elwyn Berlekamp in 1968. Its connection to linear codes was observed by James Massey the following year. It became the key to practical application of the now ubiquitous Reed–Solomon code.
The algorithm
- Let the stream be
- Initialise three arrays , and of the length of the stream to be zeroes, except ; initialise
- While is less than :
- Let be
- If is zero, then is already a polynomial which annihilates the portion of the stream from to ; increase by 1 and continue.
- If is 1, then:
- let be a copy of
- Set up to (where is the Exclusive or operator)
- If , set , set , and let ; otherwise leave , and alone
- Increment and continue
- At the end of the algorithm, is the length of the minimal LFSR for the stream, and we have for all .