r/AskComputerScience • u/beatbrot • Mar 11 '21
Is there a theoretical upper limit for compression algorithms?
I understand that this question is tricky as the input of the algorithm greatly influences the compression ratio.
So my actual question rather is: Is there a way to measure the "average" compression ratio of an algorithm and if yes, is there an upper limit to it.
11
Mar 11 '21
So my actual question rather is: Is there a way to measure the "average" compression ratio of an algorithm and if yes, is there an upper limit to it.
The average compression ratio of a lossless compression (ie you can exactly reconstruct the input from the output) across all inputs is well know. It is zero. The optimal compression algorithm for all possible inputs is to do nothing. Every other lossless compression algorithm will sometimes make things worse and will also have some overhead.
Advantages of this method: compression and decompress can be done instantly
Disadvantage of this method: no compression is provided on any input
64
u/teraflop Mar 11 '21 edited Mar 12 '21
For lossless compression, the maximum compression is determined by the entropy of a sequence. A purely random binary sequence of 0s and 1s has 1 bit of entropy per bit, so you can't (on average) compress it at all. A sequence with many 0s and few 1s, or vice versa, has less entropy and is more compressible.
In the real world, we have to deal with inputs for which we don't have a complete mathematical model of the process that generated them. It turns out that estimating the entropy of a sequence is basically the same thing as building a good compressor. For English text, the best available compressors can get it down to a little bit under 1 bit per character (as measured by a sample of text from Wikipedia).
Also, bear in mind that by the pigeonhole principle, no lossless compression can compress all files by even a single bit. Any compression algorithm that shrinks some files must expand others. The trick is to find a technique that maps the kinds of inputs that are likely to occur to outputs that are as short as possible.
Finally, you can define the theoretical maximum compression of a particular input sequence as the size of the shortest possible program (for example, a decompressor accompanied by compressed data) that could generate it. This is known as the Kolmogorov complexity of a string. Unfortunately, although we can define it in theory, there's no general way to actually calculate it (it's equivalent to solving the halting problem).
See also: https://en.wikipedia.org/wiki/Lossless_compression#Limitations