Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re (tilly) 1: A Golf question maybe? Huffman with a twist

by tilly (Archbishop)
on Sep 04, 2001 at 20:30 UTC ( [id://110080]=note: print w/replies, xml ) Need Help??


in reply to A Golf question maybe? Huffman with a twist

I don't see how your specification is solvable.

What output would you want on an input file of:

10 a 10 b 11 c 11 d 18 e 19 f
A valid Huffman output has to look something like:
a 10 000 b 10 001 e 18 01 c 11 100 d 11 101 f 19 11
There is simply no way to write a valid Huffman encoding of this example with the letters appearing sorted by weight. (In this representation e appears before c and d.)

Replies are listed 'Best First'.
Re: Re (tilly) 1: A Golf question maybe? Huffman with a twist
by demerphq (Chancellor) on Sep 04, 2001 at 20:51 UTC
    Hmm. Well, im *pretty* sure that this is solvable :-), either that or the solution I came up with is bogus, and I tested it thoroughly.

    I think the following does the trick....

    b 10 000 a 10 001 d 11 010 c 11 011 e 18 10 f 19 11
    Displayed in depthfirst left format.
    So i think youll have to revisit the problem, or should i take a bit of time to clarify it? Perhaps I was not sufficiently clear?? /msg me and let me know tilly.

    Cheers,
    Yves
    --
    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

      Oops, I got my weights wrong. Try 10, 10, 12, 12, 21, 22.

      The spec you give for a Huffman encoder means that you have to generate the tree by combining the lightest two subtrees at each step. Therefore your structure must evolve as follows (using parens and weights because my ascii art sucks):

      (a:10 b:10 c:12 d:12 e:21 f:22) ((a b):20 c:12 d:12 e:21 f:22) ((a b):20 (c d):24 e:21 f:22) (((a b) e):41 (c d):24 f:22) (((a b) e):41 ((c d) f):46)
      That grouping is forced from your description of the Huffman spec, and conflicts with arranging the elements in order of ascending weight.
        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
        --
        You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://110080]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-03-28 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found