Problems? Is your data what you think it is? PerlMonks

### A Brainteaser -- Huffman with a twist

by demerphq (Chancellor)
 on Sep 05, 2001 at 02:52 UTC ( #110191=note: print w/replies, xml ) Need Help??

Hi Tilly,

Ok from the example you provided (which I converted into a funky table on a bizarre whim!) we have the tree as constructed directly by huffman encoding. The idea of this encoding as you know is to have each leaf at such a position that minimizes the overall depth x weight of the tree. We want to convert that tree into an equivelent in those terms but with the additional constraint that they are ordered left to right by weight.

The fact that a huffman tree is constructed so this branch goes left and that branch right is completely spurious so long as the relative weighting between the nodes is maintained. Also the problem is not to generate a huffman tree. You get that as input. The problem is to find its equivelent tree.

It has occurred to me that this not really golf but rather a brainteaser and for that maybe I should change the title. Also I will post my solution if you think I should.

Step Trees
0a:10b:10c:12d:12e:21f:22
1c:12d:12
 a:10 b:10
20
e:21f:22
2
 a:10 b:10
20
e:21f:22
 c:12 d:12
24
3f:22
 c:12 d:12
24
 a:10 b:10
20
e:21

41

4
 a:10 b:10
20
e:21

41

f:22
 c:12 d:12
24

46

5
 a:10 b:10
20
e:21

41

f:22
 c:12 d:12
24

46

87

Which is this tree:

```                             ():87
|
+--------------+--------+
|                       |
():41                   ():46
+-------+-------+         +-----+--------+
|               |         |              |
():20            e:21      f:22          ():24
+----+----+                              +----+----+
|         |                              |         |
a:10      b:10                           c:12      d:12
It is obvious that the above tree can be easily transformed into the below tree which satisfies all of the properties of the huffman encoding. (Isomere or Isomorph comes to mind :-)
```                             ():87
|
+--------------+--------+
|                       |
():44                   ():43
+-------+-------+         +-----+----+
|               |         |          |
():20           ():24      e:21       f:22
+----+----+     +----+----+
|         |     |         |
a:10      b:10  c:12      d:12
In other words, the two trees are identical in terms of the purpose of huffman encoding, ie the minimal paths the leafs according to the weights of the leafs. The following list maps out the paths of the two trees:

SymbolWeight D*WHuffmanFixed
a10 30000000
b10 30001001
c12 36110010
d12 36111011
e21 420110
f 22 44 10 11

Yves
--

Replies are listed 'Best First'.
Re (tilly) 5: A Brainteaser -- Huffman with a twist
by tilly (Archbishop) on Sep 05, 2001 at 19:40 UTC
Note that the property by which you are defining the Huffman encoding is not entirely obvious from the algorithm, and was not stated in your original node.

Certainly I had no idea what you meant. I had a distinct impression that we were to come up with another tree which could be produced by the same algorithm as the first. I think it would have really helped to have some complex inputs and complex outputs. Or to have a program which could be used to validate things.

Otherwise I guarantee that, for instance, someone will try to get the bonus and (for instance) use a naive recursive algorithm get the wrong result on the case where a-r are weighted 1 each, and S and T get 3. The ouput should be:

```a 1 00000
b 1 00001
c 1 00010
d 1 00011
e 1 00100
f 1 00101
g 1 00110
h 1 00111
i 1 01000
j 1 01001
k 1 01010
l 1 01011
m 1 01100
n 1 01101
o 1 01110
p 1 01111
q 1 1000
r 1 1001
S 3 101
T 3 11
However a recursive approach will have a hard time figuring out where is globally best to take the penalties of incorrect weights.

Create A New User
Node Status?
node history
Node Type: note [id://110191]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2021-04-19 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?