<p>Here is a suggestion, for $K = 6$ and $N = 251$. We are given a list $a_i - b_j \pmod{N}$. Start by taking one of them, without loss of generality $a_1-b_1$. Without loss of generality $b_1=0$, and we obtain the value of $a_1$. Now take another one, and hope that it is of the form $a_2-b_1$ (this happens with probability $5/35 = 1/7$), and deduce $a_2$.</p>

<p>At this stage, we know $a_1,a_2,b_1$. Our next goal is to look for $a_1-b_j$ for $j \neq 1$. For each candidate $a_i-b_j$, if $i=1$ then $(a_i-b_j)+(a_2-a_1)=a_2-b_j$ should also be on the list. If $i \neq 1$, then the probability that $(a_i-b_j)+(a_2-a_1)$ is also on the list is roughly $33/251$. So if we do find some candidate $a_i-b_j$ for which $(a_i-b_j)+(a_2-a_1)$ is also on the list, then probably $i=1$. In this way, we can recover $b_2$ with some certainty.</p>

<p>At this stage, we know $a_1,a_2,b_1,b_2$. In the same way that we recovered $b_2$, we can recover $a_3$ with reasonable certainty. We can then recover $b_3$ by looking for a candidate $a_i-b_j$ for which $(a_i-b_j)+(a_2-a_1)$ and $(a_i-b_j)+(a_3-a_1)$ are both on the list. Because we have more $a$s, our failure probability goes appreciably down. We continue and find $b_3,a_4,b_4,a_5,b_6,a_6,b_6$.</p>

<p>At any point in this algorithm, we might have guessed something wrong, and this will eventually result in a contradiction (say at some point, there is no good candidate $a_i-b_j$). We then backtrack and try another possibility; if we exhaust all possibilities, we backtrack again, and try another possibility (for a different stage of the algorithm); and so on.</p>

<p>It is a good exercise to actually program this algorithm - that's probably the only way to understand how to implement the backtracking correctly. That's also the only way to tell whether this algorithm works in practice.</p>

CC BY-SA 3.0
2013-04-25T14:10:46.300Z
Thank you and I'll also code this backtracking to get it more understood. Maybe the author of that original paper used a similar method because he also mentioned "backtrack".
a guest
CC BY-SA 3.0
2013-05-05T07:16:43.983Z
Sorry for forgetting to post my comment to your answer! I also implemented the method you suggested (in C++). The conclusion is that your algorithm works quite well and one of the solutions can be then found very fast (in less than a second on my PC). And this time, I can understand the backtrack procedures better. Thank you very much!
a guest
CC BY-SA 3.0
2013-05-05T07:23:43.940Z
Why can't I "@Yuval" in my last comment?! Sorry, but I've tried several times.
a guest
CC BY-SA 3.0
2013-05-05T15:14:23.613Z
Perhaps you could share the code online, so that other people reading the paper will have access to it.
Yuval Filmus
CC BY-SA 3.0
2013-04-18T21:31:39.057Z
2013-04-18T21:31:39.057Z
Yuval Filmus
<p><strong>Update</strong>: <em>The description below is for a different problem (in which you have all pairwise distances in a set rather than pairwise distances between two distinct sets). I'll leave it up anyway since it is closely related.</em></p>

<p>This problem is called the beltway problem, and is a special case of the general $d$-torus embedding problem. It is also closely related to the turnpike problem, in which the distances differences are absolute (not modulo some number). </p>

<p>It is not known whether the beltway problem admits a poly-time algorithm. There are various pseudo-poly-time algorithms for related questions. The best reference (alas an old one) is the paper by <a href="http://www.cs.sunysb.edu/~skiena/papers/turnpike.ps">Lemke, Skiena and Smith</a>. </p>

CC BY-SA 3.0
2013-04-18T17:24:32.667Z
I think this problem is different. In the beltway problem we know all pairwise distances, here we only know it between two points that are in different groups. While this seems like less information, it can in fact help to solve the problem.
domotorp
CC BY-SA 3.0
2013-04-18T17:43:10.390Z
Ah yes. it's a bipartite graph. good point.
Suresh Venkat
CC BY-SA 3.0
2013-04-19T03:03:41.403Z
Bipartite graph? Something like. Maybe I should try the problem this way, but I don't have the concrete train of thought now.
a guest
CC BY-SA 3.0
2013-04-18T16:46:21.543Z
2013-04-18T17:44:19.770Z
2013-04-18T17:44:19.770Z
Suresh Venkat
<p>Here's a different approach, based upon iteratively finding numbers that cannot appear among $\{a_1,\dots,a_6\}$.  Call a set $A$ an over-approximation of the $a$'s if we know that $\{a_1,\dots,a_6\} \subseteq A$.  Similarly, $B$ is an overapproximation of the $b$'s if we know that $\{b_1,\dots,b_6\} \subseteq B$.  Obviously, the smaller $A$ is, the more useful this over-approximation is, and the same goes for $B$.  My approach is based upon iteratively refining these over-approximations, i.e., iteratively reducing the size of these sets (as we rule out more and more values as impossible).</p>

<p>The core of this approach is a method for <em>refinement</em>: given an over-approximation $A$ for the $a$'s and an over-approximation $B$ for the $b$'s, find a new over-approximation $A^*$ for the $a$'s such that $A^* \subsetneq A$.  In particular, normally $A^*$ will be smaller than $A$, so this lets us refine the over-approximation for the $a$'s.</p>

<p>By symmetry, essentially the same trick will let us refine our over-approximation for the $b$'s: given an over-approximation $A$ for the $a$'s and an over-approximation $B$ for the $b$'s, it will produce a new over-approximation $B^*$ for the $b$'s.</p>

<p>So, let me tell you how do refinement, then I'll put everything together to get a full algorithm for this problem.  In what follows, let $D$ denote the multi-set of differences, i.e., $D=\{a_i-b_j:1 \le i,j \le 6\}$; we'll focus on finding a refined over-approximation $A^*$, given $A,B$.</p>

<p><strong>How to compute a refinement.</strong>  Consider a single difference $d \in D$.  Consider the set $d+B=\{d+y : y \in B\}$.  Based on our knowledge that $B$ is an over-approximation of the $b$'s, we know that at least one element of $d+B$ must be an element of $\{a_1,\dots,a_6\}$.  Therefore, we can treat each of the elements in $d+B$ as a "suggestion" for a number to possibly include in $A$.  So, let's sweep over all differences $d \in D$ and, for each, identify which numbers are "suggested" by $d$.</p>

<p>Now I'm going to observe that the number $a_1$ is sure to be suggested at least 6 times during this process.  Why?  Because the difference $a_1-b_1$ is in $D$, and when we process it, $a_1$ will be one of the numbers it suggests (since we're guaranteed that $b_1 \in B$, $(a_1-b_1)+B$ will surely include $a_1$).  Similarly, the difference $a_1-b_2$ appears somewhere in $D$, and it'll cause $a_1$ to be suggested again.  In this way, we see that the correct value of $a_1$ will be suggested at least 6 times.  The same holds for $a_2$, and $a_3$, and so on.</p>

<p>So, let $A^*$ be the set of numbers $a^*$ that have been suggested at least 6 times.  This is sure to be an over-approximation of the $a$'s, by the above comments.</p>

<p>As an optimization, we can filter out all suggestions that are not present in $A$ immediately: in other words, we can treat the difference $d$ as suggesting all of the values $(d+B)\cap A$.  This ensures that we will have $A^* \subseteq A$.  We are hoping that $A^*$ is strictly smaller than $A$; no guarantees, but if all goes well, maybe it will be.</p>

<p>Putting this together, the algorithm to refine $A,B$ to yield $A^*$ is as follows:</p>

<ol>
<li><p>Let $S = \cup_{d \in D} (d+B)\cap A$.  This is the multi-set of suggestions.</p></li>
<li><p>Count how many times each value appears in $S$.  Let $A^*$ be the set of values that appear at least 6 times in $S$.  (This can be implemented efficiently by building an array $a$ of 251 initially, initially all zero, and each time the number $s$ is suggested, you increment $a[s]$; at the end you sweep through $a$ looking for elements whose value is 6 or larger)</p></li>
</ol>

<p>A similar method can be built to refine $A,B$ to get $B^*$.  You basically reverse things above and flip some signs: e.g., instead of $d+B$, you look at $-d+A$.</p>

<p><strong>How to compute an initial over-approximation.</strong> To get our initial over-approximation, one idea is to assume (wlog) that $b_1=0$.  It follows that each value $a_i$ must appear somewhere among $D$, thus the list of differences $D$ can be used as our initial over-approximation for the $a$'s.  Unfortunately, this doesn't give us a very useful over-approximation for the $b$'s.</p>

<p>A better approach is to additionally guess the value of one of the $a$'s.  In other words, we assume (wlog) that $b_1=0$, and use $A=D$ as our initial over-approximation of the $a$'s.  Then, we guess which one of these 36 values is indeed one of the $a$'s, say $a_1$.  That then gives us an over-approximation $B=a_1-D$ for the $b$'s.  We use this initial over-approximation $A,B$, then iteratively refine it until convergence, and test whether the result is correct.  We repeat up to 36 times, with 36 different guesses at $a_1$ (on average 6 guesses should be enough) till we find one that works.</p>

<p><strong>A full algorithm.</strong>  Now we can have a full algorithm to compute $a_1,\dots,a_6,b_1,\dots,b_6$.  Basically, we derive an initial over-approximation for $A$ and $B$, then iteratively refine.</p>

<ol>
<li><p><em>Make a guess:</em> For each $z \in D$, guess that $a_1=z$.  Do the following:</p>

<ol>
<li><p><em>Initial over-approximation:</em> Define $A=D$ and $B=z-D$.</p></li>
<li><p><em>Iterative refinement:</em> Repeatedly apply the following until convergence:</p>

<ul>
<li>Refine $A,B$ to get a new over-approximation $B^*$ of the $b$'s.</li>
<li>Refine $A,B^*$ to get a new over-approximation $A^*$ of the $a$'s.</li>
<li>Let $A:= A^*$ and $B:= B^*$.</li>
</ul></li>
<li><p><em>Check for success:</em> If the resulting sets $A,B$ each have size 6, test whether they are a valid solution to the problem.  If they are, stop.  If not, continue with the loop over candidate values of $z$.</p></li>
</ol></li>
</ol>

<p><strong>Analysis.</strong>
Will this work?  Will it eventually converge on $A=\{a_1,\dots,a_6\}$ and $B=\{b_1,\dots,b_6\}$, or will it get stuck without completely solving the problem?  The best way to find out is probably to test it.  However, for your parameters, yes, I expect it will be effective.</p>

<p>If we use method #1, as long as $|A|,|B|$ are not too large, heuristically I expect the sizes of the sets to monotonically shrink.  Consider deriving $A^*$ from $A,B$.  Each difference $d$ suggests $|B|$ values; one of them correct, and the other $|B|-1$ can be treated (heuristically) as random numbers.  If $x$ is a number that does not appear among the $a$'s, what is the probability that it survives the filtering and is added to $A^*$?  Well, we expect $a$ to be suggested about $(|B|-1) \times 36/251$ times in total (on average, with standard deviation about the square root of that).  If $|B|\le 36$, the probability that a wrong $x$ survives the filtering should be about $p=0.4$ or so (using the normal approximation for the binomial, with continuity correction).  (The probability is smaller if $|B|$ is smaller; e.g., for $|B|=30$, I expect $p\approx 0.25$.)  I expect the size of $A^*$ to be about $p (|A|-6) + 6$, which will strictly improve the over-approximation since it is strictly smaller than $|A|$.  For instance, if $|A|=|B|=36$, then based upon these heuristics I expect $|A^*|\approx 18$, which is a big improvement over $|A|$.</p>

<p>Therefore, I predict that the running time will be very fast.  I expect about 3-5 iterations of refinement to be enough for convergence, typically, and about 6 guesses at $z$ should probably be enough.  Each refinement operation involves maybe a few thousand memory reads/writes, and we do that maybe 20-30 times.  So, I expect this to be very fast, for the parameters you specified.  However, the only way to find out for sure is to try it and see if it works well or not.</p>

CC BY-SA 3.0
2013-04-25T13:52:34.313Z
@DW: Thank you very much for your long answer and the effort you took to type so many words!!! According to your description, your algorithm here is quite correct. And I’m going to code it to test the efficiency right now.
a guest
CC BY-SA 3.0
2013-04-27T08:38:31.960Z
@DW: Hi, I’ve implemented your description in C++. The algorithm runs fast and the refinement step does reduce the sizes of original sets $A$ and $B$. However, the convergence seems to be not so perfect. In fact, for each guess $z\in D$, the final sizes of $A^*$ and $B^*$ are still more than 10 according to my record output by the program. The most frequent number of existing elements when $A^*$ (and $B^*$) can not be improved by further repetitions of refinement is 11, but I can hardly see a number below 10. However, this has made the problem solvable by trying each 6-elements chosen from
a guest
CC BY-SA 3.0
2013-04-27T08:39:07.213Z
@DW: (Cotinued)final $A^*$ and $B^*$ for each guess $z$ (although I didn’t implement the last step on my PC). The total amount computation will be about $2^{20}$, I estimate. Thank you very much!
a guest
CC BY-SA 3.0
2013-04-27T08:49:02.843Z
Sorry, but my last comment is too long, and I have to split it into two.
a guest
CC BY-SA 3.0
2013-04-20T05:19:41.773Z
2013-04-20T07:24:13.613Z
2013-04-20T07:24:13.613Z
D.W.
<p>Here's an observation that I think gives you a foothold, possibly enough of one to solve the problem.</p>

<p>Suppose we have four differences $a_1-b_1$, $a_1-b_2$, $a_2-b_1$, $a_2-b_2$ that arise as the pairwise differences between two $a$'s and two $b$'s.  Call this a <em>quartet</em> of differences.  Notice that we have a non-trivial relationship:</p>

<p>$$(a_1-b_1)-(a_1-b_2) = (a_2-b_1)-(a_2-b_2) \pmod N.$$</p>

<p>You can try to use this relationship to identify potential quartets out of the list of $K^2$.  For instance, pick four differences out of the list; if they don't satisfy the above relationship, then they definitely don't arise from a quartet structure; if they do satisfy the relationship, they might arise from a quartet.</p>

<p>There are many ways you can take things from here, but I suspect this is going to be enough.</p>

<p>I especially suspect that, for your example parameter settings, the problem is going to be pretty easy, because the above test for recognizing a quartet probably won't have too many false positives.  Our of all ${K^2 \choose 4}$ ways of choosing 4 differences from the list, there will be ${K \choose 2}^2$ quartets (which will all satisfy the relationship) and the remainder are non-quartets (which satisfy the relationship with probability $1/N$, heuristically).  Therefore we expect to see about $({K^2 \choose 4}-{K \choose 2}^2)/N$ false positives, i.e., 4-tuples that pass the test even though they aren't quartets.  For your parameters, this means we have 225 quartets and $(58905-225)/251 \approx 234$ other false positives; so about half of the 4-tuples that pass the test are actually quartets.  This means that the above test is a pretty good way to recognize quartets.  Once you can recognize quartets, you can really go to town on recovering the structure of the list of differences.</p>

CC BY-SA 3.0
2013-04-25T14:04:55.607Z
@DW: Thank you, but I'm now wondering the next step after all the possible quartets (totally 225+234=459 ones) are found. Should it be searching for 3 non-overlapping quartets and testing whether they can constitute a possible solution? How to accomplish this efficiently? Maybe not so difficult because there will not be many overlappings.
a guest
CC BY-SA 3.0
2013-04-25T22:07:25.173Z
@aguest, good question!  I can't remember what I was thinking at the time.  I think I recall thinking one approach could be to start with one quartet, then look for all others that overlap it in 2 differences (e.g., arising from $a_1,a_j,b_1,b_2$ where $j\ne 2$), but I don't know where to go from there (how to filter out false positives).
D.W.
CC BY-SA 3.0
2013-04-19T16:29:35.333Z
2013-04-19T16:29:35.333Z
D.W.
<p>Can anyone help me with the following problem?</p>

<p>I want to find some values $a_i,b_j$ (mod $N$) where $i=1,2,…,K, j=1,2,…,K $ (for example $K=6$), given a list of $K^2$ values that correspond to the differences $a_i-b_j\pmod N$ (for example $N=251$), without knowing the concrete corresponding relation. Since the values $a_i,b_j\pmod N$ are not uniquely defined given the differences $a_i-b_j\pmod N$, we look for <em>any</em>  valid assignment of values.</p>

<p>Definitely, trying each permutation of the $K^2$ numbers in the list (totally $K^2!$ possible cases) and then solving the modular equations with $a_i,b_j$ as the variables is infeasible.</p>

<p>In fact, this problem arises in a paper on the cryptanalysis to an early version of NTRU signature scheme (<a href="http://eprint.iacr.org/2001/005" rel="noreferrer">http://eprint.iacr.org/2001/005</a>). However, the author wrote only one sentence “A simple backtrack algorithm ﬁnds one solution…” (in Section 3.3) and so can anyone give more explanations? What’s more, the author also mentioned that “every circular shift $\{((a_i+M)\mod N,(b_i+M)\mod N\}_{i=1}^K$ or a swap $(\{(N-1-b_i,N-1-a_i)\}_{i=1}^K)$ results in the same pattern of $a_i-b_j\mod N$” and is this statement helpful?</p>

CC BY-SA 3.0
2013-04-18T14:10:03.810Z
Notice that it's impossible to recover $a_i,b_j$, since if you add some constant $C$ to all the numbers, then the differences remain the same.
Yuval Filmus
CC BY-SA 3.0
2013-04-18T17:25:36.877Z
@Yuval: This is already included in the last sentence of the description. I think only one solution is needed, as several might exist.
domotorp
CC BY-SA 3.0
2013-04-19T02:44:32.057Z
@Yuval Sorry for not pointing out that the $a_i,b_j$’s should also be taken modular $N$. So there are not infinite solutions.
a guest
CC BY-SA 3.0
2013-04-19T02:44:51.057Z
@domotorp Yes, finding any one of the solutions is OK.
a guest
CC BY-SA 3.0
2013-04-20T17:53:07.797Z
Maybe the OP could clarify that the $a_i$, $b_j$ are taken modulo $N$ earlier in the post: perhaps in the title or in the first paragraph. The issue with the constant $C$ is also worth mentioning. Both things confused me when I started to read.
Juan Bermejo Vega
CC BY-SA 3.0
2013-04-24T13:55:23.493Z
@Juan: You are right and thanks a lot for helping me with the edit.
a guest
<ds.algorithms><linear-algebra><nt.number-theory><cryptographic-attack><comp-number-theory>
How to obtain the unknown values $a_i,b_j$ given an unordered list of $a_i-b_j\mod N$?
a guest
# How to obtain the unknown values $a_i,b_j$ given an unordered list of $a_i-b_j\mod N$?

(Asked by: username_0 on 2013-04-18)

Can anyone help me with the following problem?

I want to find some values $a\_i,b\_j$ (mod $N$) where $i=1,2,…,K, j=1,2,…,K $ (for example $K=6$), given a list of $K^2$ values that correspond to the differences $a\_i-b\_j\pmod N$ (for example $N=251$), without knowing the concrete corresponding relation. Since the values $a\_i,b\_j\pmod N$ are not uniquely defined given the differences $a\_i-b\_j\pmod N$, we look for *any* valid assignment of values.

Definitely, trying each permutation of the $K^2$ numbers in the list (totally $K^2!$ possible cases) and then solving the modular equations with $a\_i,b\_j$ as the variables is infeasible.

In fact, this problem arises in a paper on the cryptanalysis to an early version of NTRU signature scheme (<http://eprint.iacr.org/2001/005>). However, the author wrote only one sentence “A simple backtrack algorithm ﬁnds one solution…” (in Section 3.3) and so can anyone give more explanations? What’s more, the author also mentioned that “every circular shift $\{((a\_i+M)\mod N,(b\_i+M)\mod N\}\_{i=1}^K$ or a swap $(\{(N-1-b\_i,N-1-a\_i)\}\_{i=1}^K)$ results in the same pattern of $a\_i-b\_j\mod N$” and is this statement helpful?

**Tags:** ds.algorithms, linear-algebra, nt.number-theory, cryptographic-attack, comp-number-theory

> **Comments:**
> **username_1**: Notice that it's impossible to recover $a_i,b_j$, since if you add some constant $C$ to all the numbers, then the differences remain the same.  
> **username_2**: @Yuval: This is already included in the last sentence of the description. I think only one solution is needed, as several might exist.  
> **username_0**: @Yuval Sorry for not pointing out that the $a_i,b_j$’s should also be taken modular $N$. So there are not infinite solutions.  
> **username_0**: @username_2 Yes, finding any one of the solutions is OK.  
> **username_3**: Maybe the OP could clarify that the $a_i$, $b_j$ are taken modulo $N$ earlier in the post: perhaps in the title or in the first paragraph. The issue with the constant $C$ is also worth mentioning. Both things confused me when I started to read.  
> **username_0**: @Juan: You are right and thanks a lot for helping me with the edit.  

---

## 4 answers

### Answer by username_1 on 2013-04-18. Score: 4 | Accepted answer

Here is a suggestion, for $K = 6$ and $N = 251$. We are given a list $a\_i - b\_j \pmod{N}$. Start by taking one of them, without loss of generality $a\_1-b\_1$. Without loss of generality $b\_1=0$, and we obtain the value of $a\_1$. Now take another one, and hope that it is of the form $a\_2-b\_1$ (this happens with probability $5/35 = 1/7$), and deduce $a\_2$.

At this stage, we know $a\_1,a\_2,b\_1$. Our next goal is to look for $a\_1-b\_j$ for $j \neq 1$. For each candidate $a\_i-b\_j$, if $i=1$ then $(a\_i-b\_j)+(a\_2-a\_1)=a\_2-b\_j$ should also be on the list. If $i \neq 1$, then the probability that $(a\_i-b\_j)+(a\_2-a\_1)$ is also on the list is roughly $33/251$. So if we do find some candidate $a\_i-b\_j$ for which $(a\_i-b\_j)+(a\_2-a\_1)$ is also on the list, then probably $i=1$. In this way, we can recover $b\_2$ with some certainty.

At this stage, we know $a\_1,a\_2,b\_1,b\_2$. In the same way that we recovered $b\_2$, we can recover $a\_3$ with reasonable certainty. We can then recover $b\_3$ by looking for a candidate $a\_i-b\_j$ for which $(a\_i-b\_j)+(a\_2-a\_1)$ and $(a\_i-b\_j)+(a\_3-a\_1)$ are both on the list. Because we have more $a$s, our failure probability goes appreciably down. We continue and find $b\_3,a\_4,b\_4,a\_5,b\_6,a\_6,b\_6$.

At any point in this algorithm, we might have guessed something wrong, and this will eventually result in a contradiction (say at some point, there is no good candidate $a\_i-b\_j$). We then backtrack and try another possibility; if we exhaust all possibilities, we backtrack again, and try another possibility (for a different stage of the algorithm); and so on.

It is a good exercise to actually program this algorithm - that's probably the only way to understand how to implement the backtracking correctly. That's also the only way to tell whether this algorithm works in practice.

> **Comments:**
> **username_0**: Thank you and I'll also code this backtracking to get it more understood. Maybe the author of that original paper used a similar method because he also mentioned "backtrack".  
> **username_0**: Sorry for forgetting to post my comment to your answer! I also implemented the method you suggested (in C++). The conclusion is that your algorithm works quite well and one of the solutions can be then found very fast (in less than a second on my PC). And this time, I can understand the backtrack procedures better. Thank you very much!  
> **username_0**: Why can't I "@Yuval" in my last comment?! Sorry, but I've tried several times.  
> **username_1**: Perhaps you could share the code online, so that other people reading the paper will have access to it.  

---

### Answer by username_4 on 2013-04-18. Score: 5

**Update**: *The description below is for a different problem (in which you have all pairwise distances in a set rather than pairwise distances between two distinct sets). I'll leave it up anyway since it is closely related.*

This problem is called the beltway problem, and is a special case of the general $d$-torus embedding problem. It is also closely related to the turnpike problem, in which the distances differences are absolute (not modulo some number).

It is not known whether the beltway problem admits a poly-time algorithm. There are various pseudo-poly-time algorithms for related questions. The best reference (alas an old one) is the paper by [Lemke, Skiena and Smith](http://www.cs.sunysb.edu/~skiena/papers/turnpike.ps).

> **Comments:**
> **username_2**: I think this problem is different. In the beltway problem we know all pairwise distances, here we only know it between two points that are in different groups. While this seems like less information, it can in fact help to solve the problem.  
> **username_4**: Ah yes. it's a bipartite graph. good point.  
> **username_0**: Bipartite graph? Something like. Maybe I should try the problem this way, but I don't have the concrete train of thought now.  

---

### Answer by username_5 on 2013-04-20. Score: 3

Here's a different approach, based upon iteratively finding numbers that cannot appear among $\{a\_1,\dots,a\_6\}$. Call a set $A$ an over-approximation of the $a$'s if we know that $\{a\_1,\dots,a\_6\} \subseteq A$. Similarly, $B$ is an overapproximation of the $b$'s if we know that $\{b\_1,\dots,b\_6\} \subseteq B$. Obviously, the smaller $A$ is, the more useful this over-approximation is, and the same goes for $B$. My approach is based upon iteratively refining these over-approximations, i.e., iteratively reducing the size of these sets (as we rule out more and more values as impossible).

The core of this approach is a method for *refinement*: given an over-approximation $A$ for the $a$'s and an over-approximation $B$ for the $b$'s, find a new over-approximation $A^\*$ for the $a$'s such that $A^\* \subsetneq A$. In particular, normally $A^\*$ will be smaller than $A$, so this lets us refine the over-approximation for the $a$'s.

By symmetry, essentially the same trick will let us refine our over-approximation for the $b$'s: given an over-approximation $A$ for the $a$'s and an over-approximation $B$ for the $b$'s, it will produce a new over-approximation $B^\*$ for the $b$'s.

So, let me tell you how do refinement, then I'll put everything together to get a full algorithm for this problem. In what follows, let $D$ denote the multi-set of differences, i.e., $D=\{a\_i-b\_j:1 \le i,j \le 6\}$; we'll focus on finding a refined over-approximation $A^\*$, given $A,B$.

**How to compute a refinement.** Consider a single difference $d \in D$. Consider the set $d+B=\{d+y : y \in B\}$. Based on our knowledge that $B$ is an over-approximation of the $b$'s, we know that at least one element of $d+B$ must be an element of $\{a\_1,\dots,a\_6\}$. Therefore, we can treat each of the elements in $d+B$ as a "suggestion" for a number to possibly include in $A$. So, let's sweep over all differences $d \in D$ and, for each, identify which numbers are "suggested" by $d$.

Now I'm going to observe that the number $a\_1$ is sure to be suggested at least 6 times during this process. Why? Because the difference $a\_1-b\_1$ is in $D$, and when we process it, $a\_1$ will be one of the numbers it suggests (since we're guaranteed that $b\_1 \in B$, $(a\_1-b\_1)+B$ will surely include $a\_1$). Similarly, the difference $a\_1-b\_2$ appears somewhere in $D$, and it'll cause $a\_1$ to be suggested again. In this way, we see that the correct value of $a\_1$ will be suggested at least 6 times. The same holds for $a\_2$, and $a\_3$, and so on.

So, let $A^\*$ be the set of numbers $a^\*$ that have been suggested at least 6 times. This is sure to be an over-approximation of the $a$'s, by the above comments.

As an optimization, we can filter out all suggestions that are not present in $A$ immediately: in other words, we can treat the difference $d$ as suggesting all of the values $(d+B)\cap A$. This ensures that we will have $A^\* \subseteq A$. We are hoping that $A^\*$ is strictly smaller than $A$; no guarantees, but if all goes well, maybe it will be.

Putting this together, the algorithm to refine $A,B$ to yield $A^\*$ is as follows:

1. Let $S = \cup\_{d \in D} (d+B)\cap A$. This is the multi-set of suggestions.
2. Count how many times each value appears in $S$. Let $A^\*$ be the set of values that appear at least 6 times in $S$. (This can be implemented efficiently by building an array $a$ of 251 initially, initially all zero, and each time the number $s$ is suggested, you increment $a[s]$; at the end you sweep through $a$ looking for elements whose value is 6 or larger)

A similar method can be built to refine $A,B$ to get $B^\*$. You basically reverse things above and flip some signs: e.g., instead of $d+B$, you look at $-d+A$.

**How to compute an initial over-approximation.** To get our initial over-approximation, one idea is to assume (wlog) that $b\_1=0$. It follows that each value $a\_i$ must appear somewhere among $D$, thus the list of differences $D$ can be used as our initial over-approximation for the $a$'s. Unfortunately, this doesn't give us a very useful over-approximation for the $b$'s.

A better approach is to additionally guess the value of one of the $a$'s. In other words, we assume (wlog) that $b\_1=0$, and use $A=D$ as our initial over-approximation of the $a$'s. Then, we guess which one of these 36 values is indeed one of the $a$'s, say $a\_1$. That then gives us an over-approximation $B=a\_1-D$ for the $b$'s. We use this initial over-approximation $A,B$, then iteratively refine it until convergence, and test whether the result is correct. We repeat up to 36 times, with 36 different guesses at $a\_1$ (on average 6 guesses should be enough) till we find one that works.

**A full algorithm.** Now we can have a full algorithm to compute $a\_1,\dots,a\_6,b\_1,\dots,b\_6$. Basically, we derive an initial over-approximation for $A$ and $B$, then iteratively refine.

1. *Make a guess:* For each $z \in D$, guess that $a\_1=z$. Do the following:

   1. *Initial over-approximation:* Define $A=D$ and $B=z-D$.
   2. *Iterative refinement:* Repeatedly apply the following until convergence:

      * Refine $A,B$ to get a new over-approximation $B^\*$ of the $b$'s.
      * Refine $A,B^\*$ to get a new over-approximation $A^\*$ of the $a$'s.
      * Let $A:= A^\*$ and $B:= B^\*$.
   3. *Check for success:* If the resulting sets $A,B$ each have size 6, test whether they are a valid solution to the problem. If they are, stop. If not, continue with the loop over candidate values of $z$.

**Analysis.**
Will this work? Will it eventually converge on $A=\{a\_1,\dots,a\_6\}$ and $B=\{b\_1,\dots,b\_6\}$, or will it get stuck without completely solving the problem? The best way to find out is probably to test it. However, for your parameters, yes, I expect it will be effective.

If we use method #1, as long as $|A|,|B|$ are not too large, heuristically I expect the sizes of the sets to monotonically shrink. Consider deriving $A^\*$ from $A,B$. Each difference $d$ suggests $|B|$ values; one of them correct, and the other $|B|-1$ can be treated (heuristically) as random numbers. If $x$ is a number that does not appear among the $a$'s, what is the probability that it survives the filtering and is added to $A^\*$? Well, we expect $a$ to be suggested about $(|B|-1) \times 36/251$ times in total (on average, with standard deviation about the square root of that). If $|B|\le 36$, the probability that a wrong $x$ survives the filtering should be about $p=0.4$ or so (using the normal approximation for the binomial, with continuity correction). (The probability is smaller if $|B|$ is smaller; e.g., for $|B|=30$, I expect $p\approx 0.25$.) I expect the size of $A^\*$ to be about $p (|A|-6) + 6$, which will strictly improve the over-approximation since it is strictly smaller than $|A|$. For instance, if $|A|=|B|=36$, then based upon these heuristics I expect $|A^\*|\approx 18$, which is a big improvement over $|A|$.

Therefore, I predict that the running time will be very fast. I expect about 3-5 iterations of refinement to be enough for convergence, typically, and about 6 guesses at $z$ should probably be enough. Each refinement operation involves maybe a few thousand memory reads/writes, and we do that maybe 20-30 times. So, I expect this to be very fast, for the parameters you specified. However, the only way to find out for sure is to try it and see if it works well or not.

> **Comments:**
> **username_0**: @DW: Thank you very much for your long answer and the effort you took to type so many words!!! According to your description, your algorithm here is quite correct. And I’m going to code it to test the efficiency right now.  
> **username_0**: @DW: Hi, I’ve implemented your description in C++. The algorithm runs fast and the refinement step does reduce the sizes of original sets $A$ and $B$. However, the convergence seems to be not so perfect. In fact, for each guess $z\in D$, the final sizes of $A^*$ and $B^*$ are still more than 10 according to my record output by the program. The most frequent number of existing elements when $A^*$ (and $B^*$) can not be improved by further repetitions of refinement is 11, but I can hardly see a number below 10. However, this has made the problem solvable by trying each 6-elements chosen from  
> **username_0**: @DW: (Cotinued)final $A^*$ and $B^*$ for each guess $z$ (although I didn’t implement the last step on my PC). The total amount computation will be about $2^{20}$, I estimate. Thank you very much!  
> **username_0**: Sorry, but my last comment is too long, and I have to split it into two.  

---

### Answer by username_5 on 2013-04-19. Score: 3

Here's an observation that I think gives you a foothold, possibly enough of one to solve the problem.

Suppose we have four differences $a\_1-b\_1$, $a\_1-b\_2$, $a\_2-b\_1$, $a\_2-b\_2$ that arise as the pairwise differences between two $a$'s and two $b$'s. Call this a *quartet* of differences. Notice that we have a non-trivial relationship:

$$(a\_1-b\_1)-(a\_1-b\_2) = (a\_2-b\_1)-(a\_2-b\_2) \pmod N.$$

You can try to use this relationship to identify potential quartets out of the list of $K^2$. For instance, pick four differences out of the list; if they don't satisfy the above relationship, then they definitely don't arise from a quartet structure; if they do satisfy the relationship, they might arise from a quartet.

There are many ways you can take things from here, but I suspect this is going to be enough.

I especially suspect that, for your example parameter settings, the problem is going to be pretty easy, because the above test for recognizing a quartet probably won't have too many false positives. Our of all ${K^2 \choose 4}$ ways of choosing 4 differences from the list, there will be ${K \choose 2}^2$ quartets (which will all satisfy the relationship) and the remainder are non-quartets (which satisfy the relationship with probability $1/N$, heuristically). Therefore we expect to see about $({K^2 \choose 4}-{K \choose 2}^2)/N$ false positives, i.e., 4-tuples that pass the test even though they aren't quartets. For your parameters, this means we have 225 quartets and $(58905-225)/251 \approx 234$ other false positives; so about half of the 4-tuples that pass the test are actually quartets. This means that the above test is a pretty good way to recognize quartets. Once you can recognize quartets, you can really go to town on recovering the structure of the list of differences.

> **Comments:**
> **username_0**: @DW: Thank you, but I'm now wondering the next step after all the possible quartets (totally 225+234=459 ones) are found. Should it be searching for 3 non-overlapping quartets and testing whether they can constitute a possible solution? How to accomplish this efficiently? Maybe not so difficult because there will not be many overlappings.  
> **username_5**: @aguest, good question!  I can't remember what I was thinking at the time.  I think I recall thinking one approach could be to start with one quartet, then look for all others that overlap it in 2 differences (e.g., arising from $a_1,a_j,b_1,b_2$ where $j\ne 2$), but I don't know where to go from there (how to filter out false positives).  

---

