In this blog, I want to show you a cool result that I found. It is about the following data structure problem. You have an array of size $$$2^n$$$. There are q = $$$\Omega(2^n)$$$ queries. Each query either:
- add some number $$$x$$$ to $$$a_i$$$
- query the current sum of $$$a_j$$$ over all $$$j$$$ that are a submask of $$$i$$$. In other words, for which
(j&i)=j.
I found this problem in an old adamant blog, where he explains a trick to handle the queries in $$$\mathcal{O}\left(2^{\frac{n}{2}}\right)$$$ time per query. Interestingly, in the comments under the blog, turmax found a way to handle the queries in $$$\mathcal{O}\left(\left(\frac{7}{4}\right)^{\frac{n}{2}}\right)$$$ time, albeit using a way more complex algorithm. In a reply, hos.lyric noted that his algorithm can be simplified a lot, resulting in the slightly worse complexity of $$$\mathcal{O}\left(\left(\frac{4}{3}\right)^n\right)$$$.
Lastly, turmax noted that it is probably possible to improve the complexity by handling more bits at once. Even though the problem of finding an improvement by handling more than 2 bits at once is too computationally hard for my laptop, I was able to find the following two nice results:
- hos.lyric's method is optimal among the algorithms handling a single bit at once.
- turmax's method is not optimal when handling 2 bits at once.
In fact, you can prove that the optimal complexity achieved by handling 2 bits at once is $$$\mathcal{O}\left(\left(\frac{22}{13}\right)^{\frac{n}{2}}\right)$$$, using a whopping 13 different cases. In the remainder of this blog, I will explain a bit about how these methods work, and how we can prove that they are optimal. It turns out that the problem is in a way very similar to finding explicit methods for lowering the matrix multiplication constant $$$\omega$$$. Inspired by OpenAI's recent result, proving that $$$\omega = \frac{9}{4}$$$, I conjecture that for the problem of online submask queries, there is also some constant $$$\omega_{\text{sub}} \gt 1$$$ so that the optimal complexity of handling queries, among all algorithms that only use linear transforms and comparisons is $$$\mathcal{O}\left(\omega_{\text{sub}}^n\right)$$$.
Queries and updates as tensor decompositions
Let us first consider the most naïve algorithm for solving the problem: for each update, we loop over all the submasks of $$$i$$$, adding the value $$$x$$$ to each $$$b_j$$$ such that (j&i)=j. In a query, the required number is stored in the corresponding index of $$$b$$$, and can be queried in $$$\mathcal{O}(1)$$$. Unfortunately, there can be $$$\mathcal{O}\left(2^n\right)$$$ $$$j$$$'s that are a submask of $$$i$$$ during an update, making this solution quite slow. Regardless, let us analyze the method a bit. Define the matrix $$$M$$$ as
Using this notation, our naïve algorithm comes down to adding the $$$i$$$-th column of $$$M$$$ to the vector $$$b$$$ during an update, and retrieving the $$$i$$$-th value of $$$b$$$ during a query. It turns out, that by dividing the work over the updates and queries more evenly, we can improve the complexity. If we have $$$M = VU$$$, for two matrices $$$V$$$ and $$$U$$$, then the work can be split between the updates and queries by adding the $$$i$$$-th column of $$$U$$$ to a vector $$$c$$$ during an update, and computing the inner product of the $$$i$$$-th row of $$$U$$$ and $$$c$$$ during a query. The neat part is that similarly to our naïve algorithm, we only need to add values for each nonzero entry in the $$$i$$$-th column of $$$U$$$ / row of $$$V$$$ during updates / queries. Both turmax's and hos.lyric's determine such $$$U$$$ and $$$V$$$, recursively, so that over all updates and queries, the average amount of nonzero entries are small.
Let us, for now, consider only a single bit ($$$n = 1$$$). There are multiple ways of choosing $$$U$$$ and $$$V$$$. For example, there are the following three factorizations of $$$M$$$:
If we count in advance how many updates there are in index $$$u0$$$ and $$$u1$$$, and how many queries there are at index $$$v0$$$ and $$$v1$$$, we can choose a decomposition out of the thee decompositions above that minimizes the total amount of nonzero entries seen throughout the handling of all queries. It turns out, that regardless of how the updates and queries are distributed over the indices $$$0$$$ and $$$1$$$, we can always choose one out of the three decompositions of $$$M$$$ so that on average, at most $$$\frac{4}{3}$$$ nonzero matrix entries were seen, averaged over all updates and queries.
This method can be made into a recursive algorithm. At a certain recursive step of the algorithm, a prefix of the bits of the indices of the vector $$$c$$$ that can still be affected is known, as well as a set of updates / queries that fall into this category, along with scalar multipliers for the updates and queries, that tell us what value to add. Initially, at the start of the algorithm, all the scalars are $$$1$$$, but due to a $$$-1$$$ existing in one of the three decompositions, the scalar might become $$$-1$$$ during some recursive step of the algorithm. The optimal choice of decomposition of $$$M$$$ to use for distributing the values of updates and queries among the two ranges obtained by setting the next bit to either $$$0$$$ or $$$1$$$ is guaranteed to make the average number of nonzero entries seen among all these updates and queries at most $$$\frac{4}{3}$$$. Hence, the branching factor of the recursive algorithm is bounded by $$$\frac{4}{3}$$$, giving us the complexity $$$\mathcal{O}\left(\left(\frac{4}{3}\right)^n\right)$$$. This way of recursively doing updates and queries, bit by bit, is equivalent to the method from hos.lyric's comment.
Interestingly, in turmax's original comment, he gives a similar set of $$$4$$$ decompositions, where two bits are handled at once, resulting in matrices of size $$$4$$$. It can be shown that regardless of how often each of the combinations of two bits $$$00$$$, $$$01$$$, $$$10$$$ and $$$11$$$ occurred during updates and queries, you can always choose one of these four decompositions, so that the average number of nonzero entries over all updates and queries is at most $$$\frac{7}{4}$$$. Since we handle $$$2$$$ bits at once, we only need to recurse $$$\frac{n}{2}$$$ times, giving us the complexity $$$\mathcal{O}\left(\left(\frac{7}{4}\right)^{\frac{n}{2}}\right)$$$. This turns out to be slightly better.
My (optimal) decomposition
It can be shown (by means of linear programming), that regardless of how the updates and queries for an instance of $$$n=2$$$ bits are distributed, one of the following $$$13$$$ cases can always be chosen, so that the average number of nonzero entries over all updates and queries is at most $$$\frac{22}{13}$$$:
Case # 1: $$$(u00 + u01 + u10 + u11) \otimes v00 + (u01 + u11) \otimes v01 + (u10 + u11) \otimes v10 + u11 \otimes v11$$$
Case # 2: $$$(u00 + u01 + u10 + u11) \otimes (v00 + v01) + (u00 + u10) \otimes - v01 + (u10 + u11) \otimes (v10 + v11) + u10 \otimes - v11$$$
Case # 3: $$$(u00 + u10) \otimes v00 + (u01 + u11) \otimes (v00 + v01) + u10 \otimes v10 + u11 \otimes (v10 + v11)$$$
Case # 4: $$$(u00 + u01 + u10 + u11) \otimes (v00 + v10) + (u00 + u01) \otimes - v10 + (u01 + u11) \otimes (v01 + v11) + u01 \otimes - v11$$$
Case # 5: $$$(u00 + u01 + u10 + u11) \otimes (v00 + v01 + v10 + v11) + (u00 + u01) \otimes (- v10 - v11) + (u00 + u10) \otimes (- v01 - v11) + u00 \otimes v11$$$
Case # 6: $$$(u00 + u10) \otimes (v00 + v10) + u00 \otimes - v10 + (u01 + u11) \otimes (v00 + v01 + v10 + v11) + u01 \otimes (- v10 - v11)$$$
Case # 7: $$$(u00 + u01) \otimes v00 + u01 \otimes v01 + (u10 + u11) \otimes (v00 + v10) + u11 \otimes (v01 + v11)$$$
Case # 8: $$$(u00 + u01) \otimes (v00 + v01) + u00 \otimes - v01 + (u10 + u11) \otimes (v00 + v01 + v10 + v11) + u10 \otimes (- v01 - v11)$$$
Case # 9: $$$u00 \otimes v00 + u01 \otimes (v00 + v01) + u10 \otimes (v00 + v10) + u11 \otimes (v00 + v01 + v10 + v11)$$$
Case # 10: $$$u00 \otimes v00 + (u01 + u10) \otimes (v00 + v01) + u10 \otimes (- v01 + v10) + u11 \otimes (v00 + v01 + v10 + v11)$$$
Case # 11: $$$(u00 + u01 + u10 + u11) \otimes v00 + u01 \otimes v01 + u10 \otimes v10 + u11 \otimes (v01 + v10 + v11)$$$
Case # 12: $$$(u00 + u01) \otimes v00 + (u01 - u10) \otimes v01 + (u10 + u11) \otimes (v00 + v01 + v10 + v11) + u10 \otimes - v11$$$
Case # 13: $$$(u00 + u01 + u10) \otimes v00 + u01 \otimes v01 + u10 \otimes v10 + u11 \otimes (v00 + v01 + v10 + v11)$$$
Interestingly, it seems like the constant $$$\frac{22}{13}$$$ cannot be achieved with less than $$$13$$$ cases, although I do not have a rigorous proof of this yet.
Finding and proving optimal decompositions
Let's say we have some list of decompositions already (initially containing some arbitrary decompositions). How do we find out what the worst possible distribution of updates and queries is, so that the lowest possible branching factor among all the decompositions is maximized? Since we know for each bit combination of the updates and queries how many nonzero entries there are in each of the decompositions, this task turns out to be a (relatively simple) linear program.
Therefore, to both find and prove the optimal decomposition, we can use the following, iterative technique. As long as the algorithm has not terminated yet, we compute a worst possible distribution of bit combinations among updates and queries using simplex with rational values. Given this vector, we then use a solver to find the best possible decomposition for this specific distribution. If the solver terminates, and the branching factor for this distribution did not improve, then we have found a distribution for which there is not decomposition that works better than the decompositions we have currently in our list of cases. This proves the optimality of our list of cases, and we can terminate. Otherwise, simply add the newly found decomposition to the list of cases.
By means of method, along with some intermediate pruning of cases that never get reached and heuristics, we can show that the branching factor of $$$\frac{4}{3}$$$ is indeed optimal when handling one bit in each recursive step, and the branching factor of $$$\frac{22}{13}$$$ is optimal when handling two bits at once.
As a last remark, I want to note that any algorithm that uses only linear combinations of the input values, and conditional statements can be turned into a tensor decomposition like the ones we covered in this blog. This is a reason for me to believe there is a universal constant $$$\omega_{\text{sub}}$$$ so that any algorithm solving this problem runs in $$$\Omega\left(\omega_{\text{sub}}^n\right)$$$ time per update / query. With my decomposition, the upper bound on $$$\omega_{\text{sub}}$$$ is currently $$$\omega_{\text{sub}} \leq \left(\frac{22}{13}\right)^{\frac{1}{2}} \approx 1.300887$$$. My challenge to the reader is to try to lower the bound on $$$\omega_{\text{sub}}$$$. Maybe someone will use AI to kill this problem, too...
Edit: It turns out this problem has been studied before, with slightly better bounds in literature, albeit even more galactic: https://arxiv.org/html/2603.15565v2.








Auto comment: topic has been updated by BLOBVISGOD (previous revision, new revision, compare).
Auto comment: topic has been updated by BLOBVISGOD (previous revision, new revision, compare).
Auto comment: topic has been updated by BLOBVISGOD (previous revision, new revision, compare).
Auto comment: topic has been updated by BLOBVISGOD (previous revision, new revision, compare).
Auto comment: topic has been updated by BLOBVISGOD (previous revision, new revision, compare).
Auto comment: topic has been updated by BLOBVISGOD (previous revision, new revision, compare).