

 This page uses content from Wikipedia. The original article was at Burrows–Wheeler_transform. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



The Burrows–Wheeler transform (BWT, also called block-sorting compression) rearranges a character string into runs of similar characters.

This is useful for compression, since it tends to be easy to compress a string that has runs of repeated characters by techniques such as move-to-front transform and run-length encoding.

More importantly, the transformation is reversible, without needing to store any additional data.

The BWT is thus a "free" method of improving the efficiency of text compression algorithms, costing only some extra computation.



Source: Burrows–Wheeler transform

ClearAll[BurrowWheeler, InverseBurrowWheeler]
BurrowWheeler[sin_String, {bdelim_, edelim_}] := Module[{s},
  s = Characters[bdelim <> sin <> edelim];
  s = RotateLeft[s, #] & /@ Range[Length[s]];
  StringJoin[LexicographicSort[s][[All, -1]]]
  ]
InverseBurrowWheeler[sin_String, {bdelim_, edelim_}] := Module[{s, chars},
  chars = Characters[sin];
  s = ConstantArray[{}, Length[chars]];
  Do[
   s = LexicographicSort[MapThread[Prepend, {s, chars}]];
   ,
   {Length[chars]}
   ];
  StringTake[StringJoin[SelectFirst[s, Last /* EqualTo[edelim]]], {2, -2}]
  ]
BurrowWheeler["BANANA", {"^", "|"}]
InverseBurrowWheeler[%, {"^", "|"}]
BurrowWheeler["appellee", {"^", "|"}]
InverseBurrowWheeler[%, {"^", "|"}]
BurrowWheeler["dogwood", {"^", "|"}]
InverseBurrowWheeler[%, {"^", "|"}]