r/dailyprogrammer • u/Cosmologicon 2 3 • Jul 14 '17
[2017-07-14] Challenge #323 [Hard] Difference coverage
Description
Given a positive integer N, return a set of integers between 0 and N such that every integer is equal to difference of two in the set, modulo N. Make the set as small as you can.
For example, given N = 26, you might return the set { 0, 3, 9, 11, 21, 22 }, (which has a size of 6). Every integer is the difference between two (not necessarily unique) integers in this set, modulo 26. For instance, 7 = 3 - 22 (mod 26).
It's not good enough to write a program that will eventually complete. You must be able to actually run your program to completion for five-digit values of N. Post (or link to) your output for N = 12345 along with your solution.
As far as I know, the size of the optimal (smallest) set is an open question, so your program does not have to be optimal. But it needs to be pretty close. The best I've found for N = 12345 is a set of size 179, so aim for that.
Challenge
When this post is seven days old, +1 gold medal flair will be awarded to the submitter who posts the smallest valid output for N = 123456. Smallest here refers to the size of the set of integers in the output.
If this turns out to be easier than I anticipate, there may be a tie. In that case, as a tiebreaker, also post your output for N = 1234567. If there's still a tie, post for 12345678, 123456789, 1234567890, 12345678901, etc. I'll also look at time of posting to break a tie if necessary.
Inspiration
This problem was inspired by me trying to make a minimal set of rows for a Caesar shift cipher key. The set { 0, 3, 9, 11, 21, 22 } corresponds to the rows:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
XYZABCDEFGHIJKLMNOPQRSTUVW
RSTUVWXYZABCDEFGHIJKLMNOPQ
PQRSTUVWXYZABCDEFGHIJKLMNO
FGHIJKLMNOPQRSTUVWXYZABCDE
EFGHIJKLMNOPQRSTUVWXYZABCD
Now I have a key for any Caesar cipher by comparing two rows. For instance, the shift A->H (shifting by 7 places) corresponds to mapping the 2nd row to the 6th row, because 7 = 3 - 22 (mod 26). You didn't need to know this in order to solve the problem, but I thought I'd mention it.
18
u/pastmidnight14 Jul 14 '17
For clarification, is this the question?
Where set A is all integers in {0,1,...,N-1}, find the minimal set B which satisfies:
B is a subset of A. Each element of A can be derived as the difference(modN) of two elements of B.
12
u/___def 1 0 Jul 15 '17
C. I implemented a greedy algorithm first with O(N2.5) running time, but somebody beat me to it. So I found another algorithm in a research paper (looked in the citations of the paper /u/ttmp3 linked, then downloaded it from sci-hub). It has a running time of O(sqrt(N)) and solution set size of less than sqrt(1.5*N)+6. 12345 has a set of size 136, 123456 has a set of size 430, and 1234567 has a set of size 1366.
6
u/CrazyMerlyn Jul 14 '17
Unless I am misunderstanding the question, your example seems to be incorrect. It can't represent 8, 12, 14 and 18.
8
u/skeeto -9 8 Jul 14 '17 edited Jul 14 '17
C, randomly searching the solution space with a greedy algorithm. When selecting the next number for the set, it chooses the value that has the largest effect, breaking ties with
rand(). It uses a bitset to keep track of which values are covered.So far the best it can do with 12345 is a set of
180179, and for 123456 it has found656655.6
u/[deleted] Jul 14 '17 edited Jul 15 '17
You might want to read this paper from sciencedirect, "The complexity of minimum difference cover".
Edit: This paper suggest a upper limit of cardinality of minimum difference cover. Citing it,
That means, for 12345, we could find a set of cardinality of
sqrt(1.5 * 12345) + 6 = 143that fulfills the requirement of this challenge. So 179 isn't best solution here?Please correct me if I misunderstand the paper.
3
u/popillol Jul 14 '17 edited Jul 14 '17
Made a quick (brute force) way to check if a set works. I'm sure everyone will have their own/faster implementation but I'll share it in case anyone wants to use it.
Go Playground link
2
u/MattieShoes Jul 14 '17 edited Jul 14 '17
Note that
%is not really modulus in most languages.(3 - 22) % 26will generally yield-19, not7.(26 + 22 - 3) % 26--> 19(26 + 3 - 22) % 26--> 72
u/Godspiral 3 3 Jul 15 '17 edited Jul 15 '17
there's 1872 n=26, 6 number sorted "combinations". 56 for n=28. 0 for n =30 or 29
for 5 numbers, there's 324 for 18, 342 for 19, 42 for 21
solution for 12345, should be not much higher than 112,
largest n=26 solution, in J
12 15 19 23 24 25
even a 6 number solution for n=31 (max possible distinct differences of 6 numbers + 0 from self). In fact, there are 310 solutions, the last is:
13 18 20 26 29 30
2
u/gabyjunior 1 2 Jul 19 '17 edited Jul 19 '17
Made some research and the Singer difference sets look promising for this challenge, at least for some particular values of N.
For any q being a power of prime, the size of the set will be q+1 for N = q2 + q + 1, each difference appearing only once in the set.
Example with q = 113, size of the set = 114 for N = 12883.
But I will not have time to code a solution at least for the end of this week, the algorithm is using rather complex finite field arithmetic.
This document is discussing different types of sets and provides an example to generate a Singer difference set.