Marzouk's blog

By Marzouk, 3 days ago, In English

This blog is heavily inspired by Monogon's blog on the best branching factor for segment trees which I really recommend reading as it doesn't feature hard math and is really interesting :)

So as you might have guessed I wanted to get the optimal lifting power for binary lifting :)

the answer

so at first I thought that having a higher power would get us fewer levels which is actually true, but does it mean that high power is more optimal? I didn't know so I went through analyzing it mathematically

So in binary lifting we just loop at each "digit" in base-2 and do the jumps accordingly, so having $$$d_i$$$ in the $$$i$$$-th level would mean that we have to do $$$d_i$$$ jumps each with distance $$$2^i$$$ now if we are doing k-lifting we would loop at the "digits" in base-k and do the same thing

we currently have 2 factors affecting the jumps:- 1. the number of levels we have 2. the number of jumps per level

1. the number of levels we have

this is really easy to calculate, number N has about $$$\log_k(N)$$$ digits in base-k

2. the number of jumps per level

this is also really easy, each digit must be less than k that is $$$d_i \in \left[ 0, k \right)$$$ in base-k

The analysis

ok so as we have calculated, if we have a tree with $$$N$$$ nodes, in the worst case we would have to do approximately $$$(k-1)\cdot \log_k(N)$$$ jumps in total, to simplify the math we can replace $$$\log_k(N)$$$ with $$$\frac{\log(N)}{\log(k)}$$$ (where $$$\log(x)$$$ is the natural logarithm of $$$x$$$), so now we are doing $$$(k-1) \cdot \frac{\log(N)}{\log(k)}$$$, now since $$$N$$$ is independent from the base $$$k$$$ we can neglect it yielding $$$f(k) = \frac{k-1}{\log(k)}$$$ and we need to find the $$$k$$$ that minimizes this function so we will take its derivative, $$$f'(k) = \frac{\log(k) - \frac{k-1}{k}}{\log^2(k)} = \frac{\log(k) + \frac{1}{k} - 1}{\log^2(k)}$$$

from here we can try to find the value of k where it will make the function equal to 0 and we would be done however other than it being really annoying,

the math

we would get $$$k = 1$$$ which is not even in the domain of the function, therefore we need to get the tendency of the function in another way

we can see that the denominator is always positive so we only care about the sign of the numerator so lets say

$$$ \begin{aligned} &g(k) = \log(k) + \frac{1}{k} - 1\\ &g'(k) = \frac{1}{k} - \frac{1}{k^2} = \frac{k-1}{k^2} \end{aligned} $$$

we can see the $$$g'(k)$$$ is always positive for all $$$ k \gt 1$$$ then we can say that $$$g(k) \gt 0$$$ for all $$$k \gt 1$$$ then $$$f'(k) \gt 0$$$ for all $$$k \gt 1$$$ then $$$f(k)$$$ always increases as we increase $$$k$$$ so the optimal value for $$$k$$$ is the smallest integer number that is greater than $$$1$$$ which turns out to be $$$2$$$ so the answer to the question I asked at the start is yes binary lifting is optimal and it turns out that I have achieved absolutely nothing new :)

it was really fun tho and I hope you like it too, even if it's completely useless :)

  • Vote: I like it
  • +60
  • Vote: I do not like it

»
32 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Do this for binary search

  • »
    »
    25 hours ago, hide # ^ |
    ← Rev. 2  
    Vote: I like it +3 Vote: I do not like it

    For binary search it's actually pretty trivial that division in half is optimal for the worst case. Any unequal division may make the number to be in the larger group, leading you with a search on s larger set of elements than you'd be guaranteed to get in an equal division.

  • »
    »
    21 hour(s) ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I would assume it's the same analysis as spliting on base $$$k$$$ would give us $$$k-1$$$ different points to check the answer at each of them giving us $$$k-1$$$ checks per level and having $$$\log_k(N)$$$ levels leading to the same analysis

»
31 hour(s) ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I wonder if trying to minimize the average yields the same result.

  • »
    »
    21 hour(s) ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    can be interesting to look into as in the blog I mentioned there was indeed a better answer for some distribution of updates and queries, so maybe some quirky case would have a different result, highly doubt it tho

»
29 hours ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

Bruh we want to train for ACPC teens not to proof is binary lifting optimal, go upsolve EV problems

  • »
    »
    21 hour(s) ago, hide # ^ |
     
    Vote: I like it -7 Vote: I do not like it

    go get us some good gyms to practice on instead of criticizing my blogs -_-

»
25 hours ago, hide # |
← Rev. 3  
Vote: I like it +3 Vote: I do not like it

I think that there is a missing component in the analysis: how would we find which pointer to use? When I'm implementing binary lifting, I usually just go from LOGN down to 0, and if the jump size is at most the desired jump, I make it, otherwise I skip it.

In this implementation, the cost function is $$$(k-1) \log_k (N)+\log_k(N)$$$, with the additional summand coming from having to loop over all the levels to check whether to actually execute the jump, as we don't know which pointer to use in O(1) from a given node. Maybe some bit magic could help here, but I don't usually waste time on thinking about this. Without binary magic (as it's the case for arbitrary base), the most immediate way to find the next jump that comes to mind is binary search, which would change the function to $$$\log_k N (k - 1 + \log_2 (\log_k N))$$$, so actually a slow down.

The solution you obtained (k=1) is sort of a unary solution, which would correspond to having precomputed all pointers ahead of time, and only doing ans=pointer[v][jump_size] — really fast, but requires $$$N^2$$$ precompute and memory. If we used the general method of finding the pointer, it would take $$$N$$$ steps to go from LOGN down to 0 (it makes sense to assume $$$\log_1 N = N$$$ here)

For the bases smaller than 2, you can try to look into Zeckendorf numeric system (uses Fibonacci numbers as digit values), and is roughly equivalent to k=1.61.

  • »
    »
    21 hour(s) ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    that's actually a really good point, I thought thought that we would also loop from $$$\log(N)$$$ to $$$0$$$ and at level $$$i$$$ repeat the jump $$$d_i$$$ times where $$$d_i$$$ is the $$$i$$$-th digit.

    thx for pointing that out and I will check this "Zeckendorf numeric system" it looks quite interesting :)

»
22 hours ago, hide # |
 
Vote: I like it -6 Vote: I do not like it

Nice blog. Do not listen to haters (i.e., Baraa-Ahmed).