# Compression Algorithims--I'm clearly missing something

**URL:** <https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481>\
**Category:** Factual Questions\
**Created:** [August 5, 2012, 2:38am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481 "2012-08-05T02:38:17Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [August 7, 2012, 1:21am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/21 "2012-08-07T01:21:08Z")

</div>

> [@iamthewalrus\_3](#):
>
> This example involves taking a 6-hex-character string and encoding it as a 3-hex character string. There are a lot more 6-character strings than 3-character strings.
> 
> So, either
> 
> 1. You’re only using a small subset of the possible 6-character strings (small enough that there are few enough possibilities that each one can be encoded using just 3 characters). In this case, there’s a lot of duplicated data in your set and you probably can compress it well.  
> -or-
> 2. You’re going to “overflow” your short string supply, and you’ll have to start using longer strings. Given the overhead required for compression, you eventually end up getting a larger file than you started with.
> 
> Point #2 is pretty easy to demonstrate with common compression algorithms. Take a file and zip it. Then zip that into another zip. Continue. At some point (probably around the 2nd or 3rd iteration), the files start getting larger.
> 
> If the above isn’t clear, let’s go back to binary. How would you encode all 2-character binary strings (00, 01, 10, 11) as 1-character strings (0, 1)? This is analogous to your question above. It’s pretty clear that you can’t do that, because there are 4 2-character strings and only 2 1-character strings.

However, given the correct input, and with a compression algorithm with enough catch cases, you could probably compress a file this way if it **doesn’t** contain, say, too many different 6 character strings.

Of course, this wouldn’t be very good because it would be very difficult to find a file it works with, but in theory you could make a compression algorithm that works very, very well, but only on a very, very small number of inputs.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 7, 2012, 1:50am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/22 "2012-08-07T01:50:26Z")

</div>

Well, it’s easy to make a compression algorithm that works very, very well on a very, very small number of inputs. E.g., I can pick four 10GB files of my choosing and compress them down to 2 bits each. And in counterbalance, other files will go up in size. On average, nothing will be saved.

Lossless compression is only possible to the extent that it compresses files on _weighted_ average, weighted by how common those files are. And you can only compress files on weighted average to the extent that their frequencies are not in tune with their coding; that is, to the extent that they were originally stored under some memory-inefficient coding such that some shorter files are less common than some longer files.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [August 7, 2012, 2:12am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/23 "2012-08-07T02:12:08Z")

</div>

> [@Indistinguishable](#):
>
> Well, it’s easy to make a compression algorithm that works very, very well on a very, very small number of inputs. E.g., I can pick four 10GB files of my choosing and compress them down to 2 bits each. And in counterbalance, other files will go up in size. On average, nothing will be saved.
> 
> Lossless compression is only possible to the extent that it compresses files on _weighted_ average, weighted by how common those files are. And you can only compress files on weighted average to the extent that their frequencies are not in tune with their coding; that is, to the extent that they were originally stored under some memory-inefficient coding such that some shorter files are less common than some longer files.

I never said it was useful, just that what he was saying was technically doable so long as you keep control of the space of valid inputs.

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [August 7, 2012, 2:33am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/24 "2012-08-07T02:33:43Z")

</div>

> [@Indistinguishable](#):
>
> I can pick four 10GB files of my choosing and compress them down to 2 bits each.

You’ll likely need a 40GB decompression utility in order to use them again. The combined size of the decompression utility and the 8 bits of compressed data will end up being larger than the original files.

---

<div class="post-metadata">

**Author:** ![eschereal](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/eschereal/32/18939_2.png) [@eschereal](https://boards.straightdope.com/u/eschereal)\
**Post date:** [August 7, 2012, 2:35am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/25 "2012-08-07T02:35:03Z")

</div>

> [@Jragon](#):
>
> Isn’t there a popular compression algorithm that uses something like a tree? I remember learning about something a couple years back where you’d have a tree like
> 
> ```auto
> 
> []
> / \
> 0 1
> / \
> [A] []
> /\
> 0 1
> / \
> [C] **
>         
> 
> ```
> 
> Where the most common bit-strings (or in this case, letters) were on higher leaf nodes. So then the string “bac” (011000100110000101100011) would be represented as 11100.
> 
> Or is that just a simple implementation of Huffman coding? On review they seem pretty much the same.

I think you are confusing search trees (either binary trees or balanced “B-Trees”) with data compression. Search trees tend to compress time rather than data, by requiring less effort to locate a data record.

---

<div class="post-metadata">

**Author:** ![pulykamell](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pulykamell/32/3166_2.png) [@pulykamell](https://boards.straightdope.com/u/pulykamell)\
**Post date:** [August 7, 2012, 2:37am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/26 "2012-08-07T02:37:53Z")

</div>

> [@For You](#):
>
> I think you are confusing search trees (either binary trees or balanced “B-Trees”) with data compression. Search trees tend to compress time rather than data, by requiring less effort to locate a data record.

But Huffman encoding uses a binary tree, too. I thought that’s what he was remembering.

---

<div class="post-metadata">

**Author:** ![TonySinclair](https://avatars.discourse-cdn.com/v4/letter/t/49beb7/32.png) [@TonySinclair](https://boards.straightdope.com/u/TonySinclair)\
**Post date:** [August 7, 2012, 2:38am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/27 "2012-08-07T02:38:09Z")

</div>

I’m looking for investors. I have perfected a compression algorithm that reduces any file, of any size, to ten bytes.

Still working on the decompression part.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [August 7, 2012, 2:41am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/28 "2012-08-07T02:41:57Z")

</div>

> [@pulykamell](#):
>
> But Huffman encoding uses a binary tree, too. I thought that’s what he was remembering.

Yeah, on further reading that’s what I remember. The main difference between a Huffman tree and a BST seems to be that a Huffman tree only has leaf nodes with values – for ease of parsing reasons.

> [@TonySinclair](#):
>
> I’m looking for investors. I have perfected a compression algorithm that reduces any file, of any size, to ten bytes.
> 
> Still working on the decompression part.

What a coincidence, I have a decompression utility that will decompress any 10byte file into a picture of a kitten in a teacup.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 7, 2012, 3:29am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/29 "2012-08-07T03:29:03Z")

</div>

> [@Jragon](#):
>
> I never said it was useful, just that what he was saying was technically doable so long as you keep control of the space of valid inputs.

Ah, alright.

> [@TriPolar](#):
>
> You’ll likely need a 40GB decompression utility in order to use them again. The combined size of the decompression utility and the 8 bits of compressed data will end up being larger than the original files.

Sure. Though if you let me design the machine architecture, I can get the decompression utility to be a built-in machine code of minimal length as well…

Indeed, every machine architecture defines its own notion of ultimate compression, “The smallest (or any) program that outputs this file”. But this measure will be quite architecture-dependent.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [August 7, 2012, 3:33am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/30 "2012-08-07T03:33:31Z")

</div>

There’s actually malware which is written almost entirely in meta-code. A small sized code snippet that does some often crazy stuff to write the actual malicious part on the fly. This is an example, albeit an incredibly rare and difficult to create one, of a file “compression” that produces exactly one bit-string as output.

---

<div class="post-metadata">

**Author:** ![Mangetout](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/mangetout/32/19_2.png) [@Mangetout](https://boards.straightdope.com/u/Mangetout)\
**Post date:** [August 7, 2012, 7:21am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/31 "2012-08-07T07:21:28Z")

</div>

> [@iamthewalrus\_3](#):
>
> If the above isn’t clear, let’s go back to binary. How would you encode all 2-character binary strings (00, 01, 10, 11) as 1-character strings (0, 1)? This is analogous to your question above. It’s pretty clear that you can’t do that, because there are 4 2-character strings and only 2 1-character strings.

This is the absolute nub of compression theory - and it’s the bit people often either overlook or can’t grasp because it’s wrapped up in more complex terminology. There just aren’t enough possible combinations in a small space to be able to describe every possible combination of a larger space.

---

<div class="post-metadata">

**Author:** ![Fenris](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/fenris/32/498_2.png) [@Fenris](https://boards.straightdope.com/u/Fenris)\
**Post date:** [August 7, 2012, 11:32am UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/32 "2012-08-07T11:32:30Z")

</div>

> [@iamthewalrus\_3](#):
>
> If the above isn’t clear, let’s go back to binary. How would you encode all 2-character binary strings (00, 01, 10, 11) as 1-character strings (0, 1)? This is analogous to your question above. It’s pretty clear that you can’t do that, because there are 4 2-character strings and only 2 1-character strings.

Ah-HA!!

Thank you for that! I get it now in a way I didn’t before. Really great answer.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [August 7, 2012, 2:49pm UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/33 "2012-08-07T14:49:29Z")

</div>

That’s nothing, **Indistinguishable**. I have a lossless compression algorithm that can take any file at all as input, and is guaranteed to never increase the file size, and which can be iterated such that it’ll eventually get any given input file down to an arbitrarily small size.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 7, 2012, 4:38pm UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/34 "2012-08-07T16:38:34Z")

</div>

Kid, I like your idea of storing the file info in the number of times of compressed. It shows spunk. Dammit, it’s just crazy enough to –

[But, wait, what do you do if the input is already as short as possible?](http://boards.straightdope.com/sdmb/showthread.php?p=14332561#post14332561)

If compression is reversible without increasing file size, it must be permuting the shortest possible files. Which means none are left available to send any other files to. Which means you’ll never be able to get any other files down to that size, no matter how many times you compress.

Alas, even this slight claim is still too crazy to work.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [August 7, 2012, 7:56pm UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/35 "2012-08-07T19:56:41Z")

</div>

Well, then, obviously, you compress a 1-bit file into a 0-bit file.

EDIT: I knew I’d mentioned that scheme on the SDMB before, but I’d forgotten that the previous time was also in a discussion with you. I might have guessed, though.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 7, 2012, 7:59pm UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/36 "2012-08-07T19:59:51Z")

</div>

> [@Chronos](#):
>
> Well, then, obviously, you compress a 1-bit file into a 0-bit file.

But what do you compress the 0-bit file into? You can’t send it to itself if someone else (e.g., that 1-bit file) is being sent to it. But then its file size must increase…

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 7, 2012, 8:06pm UTC](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481/37 "2012-08-07T20:06:34Z")

</div>

That is, there’s a fundamental constraint on injective (i.e., lossless) maps: if no files go up in size, then no files go down in size either. The only way to move some files lower is by displacing some lower files higher.

[Previous page](https://boards.straightdope.com/t/compression-algorithims-im-clearly-missing-something/630481.md?page=1)
