Keep It Simple, Stupid PerlMonks

### Re^4: Challenge: Optimal Animals/Pangolins Strategy

by Limbic~Region (Chancellor)
 on May 03, 2013 at 13:45 UTC ( #1031891=note: print w/replies, xml ) Need Help??

BrowserUk,
By inversely proportional, I meant that the number of questions asked to identify an animal (Q) multiplied by how many times the animal is chosen (C) should be constant. If a goat appears a hundred times more often than a unicorn then it should take a hundred times more questions to identify the unicorn than the goat.

I apologize for not seeing it before hand, but I am pretty sure the optimal strategy for my problem is in fact Huffman Coding. To get a better idea of the fuzzy problem I am dealing with in my head, see Re^4: Challenge: Optimal Animals/Pangolins Strategy

Cheers - L~R

• Comment on Re^4: Challenge: Optimal Animals/Pangolins Strategy

Replies are listed 'Best First'.
Re^5: Challenge: Optimal Animals/Pangolins Strategy
by BrowserUk (Pope) on May 04, 2013 at 06:14 UTC
By inversely proportional, I meant that the number of questions asked to identify an animal (Q) multiplied by how many times the animal is chosen (C) should be constant. If a goat appears a hundred times more often than a unicorn then it should take a hundred times more questions to identify the unicorn than the goat.

In roboticus' example, which you seem to be endorsing, this is the tree produced:

```                                             Q
___________________/ \___________________
/                                         \
Q                         __________________Q
/ \                       /                   \
Q   fish                  Q            _________Q
/ \                       / \          /
+\
__________Q   cow                 dog   cat      Q
+ Q
/           \                                    / \
+/ \
Q             Q                                  Q   wolf sheep
+   horse
/ \           / \                                / \
walrus   badger seal   Q________________      wolverine   frog
/                 \
Q                   Q
/ \                 / \
Q   hampster   ocolot   Q
/ \                     / \
pegasus   Q              gerbil   platypus
/ \
axolotl   unicorn

The fish with a frequency of 150 requires 2 questions; the unicorn with a frequency of 1, required 9. And it puts walrus(15), badger(17), seal(18), wolverine(22) & frog(28) at the same level.

So the inverse proportionality is relative rather than mathematically absolute. It would require the insertion of 291 additional questions above the unicorn to achieve the math you describe, and in the process, throws away the "compressive" attribute that defines Huffman.

If non-compressive, relative inverse proportionality is sufficient, then my original reading of your question would be more accurate:

```     Q
/ \
fish   Q
/ \
cat   Q
/ \
dog   Q
/ \
cow   Q
/ \
horse   Q
/ \
sheep   Q
/ \
wolf   Q
/ \
frog   Q
/ \
wolverine   Q
/ \
seal   Q
/ \
/ \
walrus   Q
/ \
ocelot   Q
/ \
hamster   Q
/ \
gerbil   Q
/ \
platypus   Q
/ \
unicorn   Q
/ \
pegasus   axolotl

Which brings me back to the idea that what roboticus' use of Huffman does, is minimise the depth of the tree.

But if that were the goal, then its maximum depth of 9 is 3 more than is required:

```
__________Q__________
/                     \
fish   ____________________Q______________________
/                                           \
Q                    _________________________Q_________________
+__________
/ \                  /
+          \
cat dog      _________Q__________                                __
+___________Q_______________
/                    \                              /
+                           \
____Q____             _____Q______                 _____Q___
+___                   ______Q______
/         \           /            \               /
+   \                 /             \
Q           Q         Q              Q             Q
+    Q               Q               Q
/ \         / \       / \            / \           / \
+   / \             / \             / \
cow horse sheep wolf frog wolverine seal badger walrus ocelot ham
+ster gerbil platypus unicorn pegasus axolotl

All of which I guess means, that I have no idea what you set out to achieve :(

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
BrowserUk,
I feel like this should be an episode of Doctor Who with incongruent time lines.

At the time I responded to the clarification of what I meant by inversely proportional, I had already moved on from thinking it was an appropriate solution. I only clarified for the sake of completeness. What I should have said was something along the lines of:

Not that it matters since I now realize it does not solve my problem but there is a difference between having an inverse relationship and being inversly proportional. The idea that the more popular an animal is the fewer questions it should take to identify is an inverse relationship. When I said inversly proportional I meant that the product of popularity to questions should be a constant defining exactly how many questions should be asked. In the end, I was wrong.

As for what I am trying to achieve - I am attempting to build on top of Huffman coding. Let's say you have a file that you have done single byte frequency analysis on and generated a Huffman code tree. You notice that a few of the branches only have 1 leaf entry instead of 2. You decide you want to fill in those "holes" with with the highest frequency 2 byte pairs in the file. You fill in the first hole but before moving on to the next one, you realize a problem. The frequency analysis of the single bytes requires recalculating which means rebuilding the tree which means different holes.

I am not sure if that makes any more sense. I took the weekend off from thinking about it in hopes that I would have clarity today but it is still a jumbled pile of mud in my mind.

Cheers - L~R

Hm. No mention of Huffman or frequency analysis in the OP, but whatever.

It seems to me that you might get close to what (I think) you now want, without having to to any iterative recalculating, this way:

Calculate your single characters and bigram frequencies. Round the number of single characters up to the next power of 2, and add the N most frequent bigrams where N is the number required to make the number of singles up to that next power of two. Now when you construct your Huffman tree it will be a fully populated, balanced binary tree.

As an alternative, you might sort your singles and bigrams together by frequency and then select the top N most frequent (where N is a power of 2 that suits your requirements) from that combined set to build your fully populated, balanced binary tree.

And finally, you might consider using a heap rather than a tree as it has the same order of complexity for lookups (just as fast), but is a considerably more compact representation in memory, thus meeting your need for compact representation whilst potentially holding a greater number of items in the same space.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Create A New User
Node Status?
node history
Node Type: note [id://1031891]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2017-06-29 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (657 votes). Check out past polls.