🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #62459

Re: Ode to the xBin (xMas?) trees

#62459
From: Rene
Date: Fri, 27 Dec 2002 18:40
407 lines
17251 bytes
I seem to be unable to post this long message plus an attachment, so I try
to post the message part here only...

curt@kcwc.com (Curt Welch) wrote:
> > Now this doesn't work for non po2 trees, but I've found an easy
> > mapping. I'll test it and will post the code/algorithm if it turns out
> > correct.
> Cool.

Ok finally through with that. My test-code was faulty and I didn't find the
off-by-one error, went to bed and found my mistake shortly after getting
up.

So first, what was my idea to map skewed trees to full ones? Consider the
following full 8-Tree.
      root
     /     \
    e       f
  /  \    /  \
 a   b   c   d
/ \ / \ / \ / \
1 2 3 4 5 6 7 8

Now if we calculate all possible sequences over this tree, we get the
follwing table. Seq means sequence, L is the number of leaves to the left
of the sequence and R is the number to the right of the sequence. Res is
the result (number of hashes needed, that's the small h to not confuse the
number with something else)

So for the 8-Tree we get the following table:
Seq L R Res
 1  0 7  3h
1-2 0 6  2h
1-3 0 5  2h
1-4 0 4  1h
1-5 0 3  2h
1-6 0 2  1h
1-7 0 1  1h
1-8 0 0  0h
 2  1 6  3h
2-3 1 5  3h
2-4 1 4  2h
2-5 1 3  3h
2-6 1 2  2h
2-7 1 1  2h
2-8 1 0  1h
 3  2 5  3h
3-4 2 4  2h
3-5 2 3  3h
3-6 2 2  2h
3-7 2 1  2h
3-8 2 0  1h
 4  3 4  3h
4-5 3 3  4h
4-6 3 2  3h
4-7 3 1  3h
4-8 3 0  2h
 5  4 3  3h
5-6 4 2  2h
5-7 4 1  2h
5-8 4 0  1h
 6  5 2  3h
6-7 5 1  3h
6-8 5 0  2h
 7  6 1  3h
7-8 6 0  2h
 8  7 0  3h

(Just in case you wonder, the hash count is simply number of 1 bits in L
plus number of 1 bits in R)

So now I "sew" the 8th node away to create a 7-Tree. What happens with the
hash values? Not much, all sequences with 8 are no longer possible, and
neighbouring nodes may change value. But the most part remains the same. So
I write the 7-Tree in table-form right next to the 8-Tree, which gives the
following:

8-Tree         7-Tree (d=1)
Seq L R Res    L R Res Cor
 1  0 7  3h    0 6  3h  -
1-2 0 6  2h    0 5  2h  -
1-3 0 5  2h    0 4  2h  -
1-4 0 4  1h    0 3  1h  -
1-5 0 3  2h    0 2  2h  -
1-6 0 2  1h    0 1  1h  -
1-7 0 1  1h    0 0  0h  X
1-8 0 0  0h    -----------
 2  1 6  3h    1 5  3h  -
2-3 1 5  3h    1 4  3h  -
2-4 1 4  2h    1 3  2h  -
2-5 1 3  3h    1 2  3h  -
2-6 1 2  2h    1 1  2h  -
2-7 1 1  2h    1 0  1h  X
2-8 1 0  1h    -----------
 3  2 5  3h    2 4  3h  -
3-4 2 4  2h    2 3  2h  -
3-5 2 3  3h    2 2  3h  -
3-6 2 2  2h    2 1  2h  -
3-7 2 1  2h    2 0  1h  X
3-8 2 0  1h    -----------
 4  3 4  3h    3 3  3h  -
4-5 3 3  4h    3 2  4h  -
4-6 3 2  3h    3 1  3h  -
4-7 3 1  3h    3 0  2h  X
4-8 3 0  2h    -----------
 5  4 3  3h    4 2  3h  -
5-6 4 2  2h    4 1  2h  -
5-7 4 1  2h    4 0  1h  X
5-8 4 0  1h    -----------
 6  5 2  3h    5 1  3h  -
6-7 5 1  3h    5 0  2h  X
6-8 5 0  2h    -----------
 7  6 1  3h    6 1  2h  X
7-8 6 0  2h    -----------
 8  7 0  3h    -----------

You see that only the values with an "X" sign in the Cor column are
different. The others are exactly like the ones in the left column. Let's
have a close look at the sequence "6". In the 8-Tree that a L=5, R=2 in the
7-Tree that's a L=5, R=1. Now the distance of the 7-Tree from the 8-Tree is
(8-7) = 1 node, so in the 7-Tree we simply add distance d(1) to R, making
that a L=5, R=2 and look that up in the 8-tree. The only time we DONT do
that is, when R in the 7-Tree is smaller than d.

Since d here is 1, that means all R's < d = 0, all R values of 0 get a
special treatment. So lets look at the row of sequence 6-7. The values for
the 7-Tree are L=5 R=0 so where do we get that value? Simple! Just look it
up on the 8-Tree, this time without adding distance to R.

That's it !

Really? Well actually not, there's a bit more to it. If the skewed tree
you're looking at is even, then any R smaller than distance is to be left
as it is and add only distance d to R if R's >= distance. If the tree is
odd, add d to R if R > 1.

So let me add the 6- and 5-Trees to the table, it becomes more clear then
(I hope) :

8-Tree         7-Tree (d=1)   6-Tree (d=2)   5-Tree (d=3)
Seq L R Res    L R Res Cor    L R Res Cor    L R Res Cor
 1  0 7  3h    0 6  3h  -     0 5  3h  -     0 4  3h  -
1-2 0 6  2h    0 5  2h  -     0 4  2h  -     0 3  2h  -
1-3 0 5  2h    0 4  2h  -     0 3  2h  -     0 2  2h  -
1-4 0 4  1h    0 3  1h  -     0 2  1h  -     0 1  1h  -
1-5 0 3  2h    0 2  2h  -     0 1  1h  X     0 0  0h  X
1-6 0 2  1h    0 1  1h  -     0 0  0h  X     -----------
1-7 0 1  1h    0 0  0h  X     -----------    -----------
1-8 0 0  0h    -----------    -----------    -----------
 2  1 6  3h    1 5  3h  -     1 4  3h  -     1 3  3h  -
2-3 1 5  3h    1 4  3h  -     1 3  3h  -     1 2  3h  -
2-4 1 4  2h    1 3  2h  -     1 2  2h  -     1 1  2h  -
2-5 1 3  3h    1 2  3h  -     1 1  2h  X     1 0  1h  X
2-6 1 2  2h    1 1  2h  -     1 0  1h  X     -----------
2-7 1 1  2h    1 0  1h  X     -----------    -----------
2-8 1 0  1h    -----------    -----------    -----------
 3  2 5  3h    2 4  3h  -     2 3  3h  -     2 2  3h  -
3-4 2 4  2h    2 3  2h  -     2 2  2h  -     2 1  2h  -
3-5 2 3  3h    2 2  3h  -     2 1  2h  X     2 0  1h  X
3-6 2 2  2h    2 1  2h  -     2 0  1h  X     -----------
3-7 2 1  2h    2 0  1h  X     -----------    -----------
3-8 2 0  1h    -----------    -----------    -----------
 4  3 4  3h    3 3  3h  -     3 2  3h  -     3 1  3h  -
4-5 3 3  4h    3 2  4h  -     3 1  3h  X     3 0  2h  X
4-6 3 2  3h    3 1  3h  -     3 0  2h  X     -----------
4-7 3 1  3h    3 0  2h  X     -----------    -----------
4-8 3 0  2h    -----------    -----------    -----------
 5  4 3  3h    4 2  3h  -     4 1  2h  X     4 0  1h  X
5-6 4 2  2h    4 1  2h  -     4 0  1h  X     -----------
5-7 4 1  2h    4 0  1h  X     -----------    -----------
5-8 4 0  1h    -----------    -----------    -----------
 6  5 2  3h    5 1  3h  -     5 0  2h  X     -----------
6-7 5 1  3h    5 0  2h  X     -----------    -----------
6-8 5 0  2h    -----------    -----------    -----------
 7  6 1  3h    6 1  2h  X     -----------    -----------
7-8 6 0  2h    -----------    -----------    -----------
 8  7 0  3h    -----------    -----------    -----------

So, whenever you have an "-" in the Cor column, you can just lookup the
hash value in the clolumn(s) to the left, ie the higher tree one's. Or add
distance to R and look the value up in the "base-tree". Whenever you have
an "X" you need to look up the L,R-value in the base-tree unchanged.

That's it now? Unfortunately no. The method outlined does work perfectly
for (right-sub)trees of a size you can still paint on paper and verify
easily. However for large trees, the algorithm sometimes fails.

For example, I could use this algorithm calculating all possible sequences
in trees up and including N=25. The N=26 and N=27 trees fail, then it works
again up to N=thirthy-something.

Those problem-trees are always easy to fix. For example, consider a
27-Tree, thats a noncomplete 32-Tree. It has full 16 nodes in the left
subtree but only 11 in the right subtree.

So here's a part of the table of the 16- and 11-Tree
16-Tree         11-Tree (d=5)
Seq  L R Res    L R Res Cor
1-5  0 11 3h    0 6  3h  -
1-6  0 10 2h    0 5  2h  -
1-7  0 9  2h    0 4  2h  -
1-8  0 8  1h    0 3  1h  -
1-9  0 7  3h    0 2  2h  !!!
1-10 0 6  2h    0 1  1h  X
1-11 0 5  2h    0 0  0h  X
1-12 0 4  1h    -----------
1-13 0 3  2h    -----------
1-14 0 2  1h    -----------
1-15 0 1  1h    -----------
1-16 0 0  0h    -----------

It works nice for all values up to the 11-Tree's 0 2. If added distance 5,
0 7 in the 16-Tree has one hash too many. If nothing is added, 0 2 in the
16-Tree has one hash too few. Unsolvable? No, if you read the sequence of
hashes from the 11-Tree down (3,2,2,1,2,1), you might notice, that the very
same sequence is in the 16-Tree from 1-9 to 1-14. So if I trim the 11-Tree
and simply change the distance from 5 to 1, it works again.

HOWEVER, I must do this manually, for I haven't found the pattern yet, when
I need to "trim" the trees and by which values. It seems always possible,
but I simply don't see the pattern here. Add that drawing the large trees
becomes increasingly impossible. I gave up on that idea.

Maybe someone finds the pattern and I can implement it straight forward. As
for now I simply gave up on that. I know though that I can use the
algorithm for some limited size of N. So I simply use recursion: If the
right subtree is N<10 I use this method, if not, I recursivly solve that
N=>10 tree.

It would have been very nifty without the recursion, because I wanted to
avoid recursion. However it turns out, that this is still extremely fast,
even for trees with an N>100Mio.

So it is not 100% what I wanted, but it is fast, correct (proofed with a
Tree structure and traversing it) and it is the base for the worst case
calculation, which actually is what we really want (knowing the number of
needed hashes fast is nice, but the encoder actually needs to collect those
hashes and thus needs to traverse the tree structure)

So I guess it's not all that bad.

> > As a sideeffect, I now have found the worst case of hashes to
> > transport. worst case is = sequence contains only rightmost hash of the
> > *left* subtree IF the right subtree has only one hash/node, else the
> > sequence spanning the rightmost node of the left subtree and the
> > leftmost node of the right subtree (ie nodes in the middle).
> so that's either a 1 or 2 hash-block part which starts with the rightmost
> block of the left subtree?  That sounds like it could be right.  The 2
> block post centered in the file is the obvious worst case for a po2 tree
> and for a non po2 tree, the right tree might be worse if you don't take
> the block out so for that, the 1 block post would be the worse.

Actually that was wrong. I now have the final solution to the worst case
problem. po2-Tree's worst case *is* in the middle, but for non po2, it is
usually the sequence spanning the two nodes in the left-subtree.

Look at this 9-Tree:
         /----root----\
        g             x
     /     \         /
    e       f       x
  /  \    /  \     /
 a   b   c   d    x
/ \ / \ / \ / \  /
1 2 3 4 5 6 7 8 9

x are actually hash 9.

I said having either [8] or [8,9] is the worst case. But [8] needs 4 hashes
(7, c, e, 9) [8,9] needs 3 hashes (7, c, e). The worst case is however
spanning the larger subtree of the two subtrees. Worst case is [4,5] with 5
hashes (3,6,a,d,9)

I have written a worstCase routine, it looks like this (in pseudocode):
worstCase(N)  // for a N >= 4 !
 next = nextPowerOfTwo(N) // get next po2 if N is not a po2
 next = next / 2 // next is now the middle element
 one = calculateTree(N, next, next+1); // calculate sequence spanning
 middle two = calculateTree(N, next/2, next/2+1); // calculate sequence in
 left subtree
                                           // which spans middle in left
 subtree return(max(one,two))  // return the larger of the two values
END

Now a call to calculateTree even for huge N is very cheap, so this routine
is very fast.

To be sure of my observation, I also wrote a worstCaseBruteForce, which for
a given Tree N simply calculates all sequences and gets the largest value
back. It was up to N=1000 always the same value as worstCase gave. But
worstCaseBruteForce gets very slow after around N=200 where it calculates
all possible sequences in that trees... But I needed that only to verify
the result of worstCase.

> So, this points out that for file which are po2 in size, it's easy to
> calcuate the worst case, so we could look at a 2GB file and know the
> worst case.

2GB = 2*10^9 Bytes = 2000000000 Bytes / blocksize(assuming 4500) =
444444.444 = 444445 blocks.

WorstCase of 444445 = 36.

20GB = 2*10^10 = 4444445 blocks. Worstcase = 43.
200GB = 44444445 blocks. Worstcase = 49.

You say a hash takes 27 Bytes. A 20GB file therefor needs 27*43 = 1161
Bytes. A 2GB 27*36 Bytes = 972, would just fit.

Please note that all my number of hashes are *always* without the root
node. I never count the root, so add 27 if you add the root node.

> Ah, but, is it possible for a smaller file (2B - 1 block) to have a worse
> case signature size which has more hash values than the po2 tree?

No, smaller trees have smaller worst cases. When you remove blocks
(leaf-nodes) the tree shrinks. And a smaller tree can't have a larger worst
case.

> Or, for any file size, will the worse case get better, if you add more
> blocks?

You mean, the worst case number gets smaller with more blocks? No. When you
add blocks you add leaf-nodes. Thus the tree is getting bigger. For two
trees, a, b if b is a bigger tree than a then the worstcase of b >= than
the worstcase of a.

b can have the same worstcase value as a, even when b is a bigger tree. But
b cannot be smaller.

> Or, asking it another way, does the worse case function when ploted
> against file size, always increase or stay the same as the file size
> increases?

Here's some values (my program allows you to plot an arbitrary sequence of
N's for worstcase calcualtion):

Worstcase
 N    = 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
wc(N) = 2, 3, 3, 4, 4, 5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7

 N    = 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
wc(N) =  7,  7,  7,  7,  8,  8,  8,  8,  8,  8,  8,  8

It continues that way. So you see a bigger tree can have the same value,
but not smaller. Also the worst case number can appear more than once in a
tree ! Not just at the "typical worstcase position". But it is always
there.

> > Ok so much for now. Rest should follow as soon as I'm certain it holds
> Sounds good.

Ok code follows (finally):

This is valid and compileable java code. Conversion to C should be quite
easy. You can cut & paste the code into a file. Then enter "javac <file>"
to compile the file. You'll get HT2.class, TreeHash.class and
TreeNode.class. Type "java HT2" to start the main program. Should you have
no java, you can download it at http://java.sun.com/j2se/downloads.html for
almost any OS.

Depending on the parameter you add to "HT2" it will do something
differently. If you add no parameter, HT2 will calculate the worstCase
Number of a N=100000 Tree. Twice, one time it calculates it using my
algorithm, the other time it will find it via bruteforce (but that takes
some time...) Output looks like this:
drax@underworld:~> java HT2
No parameter given, doing a worstcase compare with 100000 (houndred
thousend) Worst case compare. 100000-Tree worstcase is:
WorstCase = 32
WorstCaseBruteSearch (testing all sequences) = 32
done

If you give ONE integer as parameter, it will do the same worstCase compare
with that number:
drax@underworld:~> java HT2 1234
One parameter given, doing worstcase compare with 1234
Worst case compare. 1234-Tree worstcase is:
WorstCase = 19
WorstCaseBruteSearch (testing all sequences) = 19
done

If you give it TWO integers as parameter, it will plot the worstCases for
the Trees between those two numbers. It will no longer use the slow
WorstCaseBruteSearch, so you can use that to find wc-values fast:
drax@underworld:~> java HT2 4 9 Two parameters given, plotting worstcase
hash number from 4-Tree to 9-Tree 4-Tree has a worstcase of 2
5-Tree has a worstcase of 3
6-Tree has a worstcase of 3
7-Tree has a worstcase of 4
8-Tree has a worstcase of 4
9-Tree has a worstcase of 5

Lastly, if you give it THREE integers as parameters (n, a, b), it will
calculate the number of hashes needed in an N-Tree with a sequence of (a,
b). This is very fast, too:
drax@underworld:~> java HT2 1000000000 3437889
635893683 Three parameters given, calculating number of hashes required for
a sequence from 3437889 to 635893683 in a 1000000000-Tree Result is: 20

Wow you read all this down to here? :) Good, then here is the code. It is
commented so you souldn't have much problems. If there are any, simply ask.

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service              New Rate! $9.95/Month 50GB
Technical Details
Message-ID: <20021227134052.621$xV@newsreader.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news.maxwell.syr.edu!feed2.newsreader.com!newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mail
References: <20021226102324.117$5X@newsreader.com> <20021226142834.692$qr@newsreader.com>

Show Full Headers
Complete Article Headers
Message-ID: <20021227134052.621$xV@newsreader.com>
Subject: Re: Ode to the xBin (xMas?) trees
Date: Fri, 27 Dec 2002 18:40:52 +0000
From: Rene <invalid@email.addr>
References: <20021226102324.117$5X@newsreader.com> <20021226142834.692$qr@newsreader.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news.maxwell.syr.edu!feed2.newsreader.com!newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mail
Organization: NewsReader.Com Subscriber
Newsgroups: news.software.nntp
X-Trace: NewsReader.Com KwVwxXZLOM6zXxOkzknx8rHkGxt8ARQAO.lA7_mroFmgO@_
Lines: 406