views:

581

answers:

4

I have a set of N^2 numbers and N bins. Each bin is supposed to have N numbers from the set assigned to it. The problem I am facing is finding a set of distributions that map the numbers to the bins, satisfying the constraint, that each pair of numbers can share the same bin only once.

A distribution can nicely be represented by an NxN matrix, in which each row represents a bin. Then the problem is finding a set of permutations of the matrix' elements, in which each pair of numbers shares the same row only once. It's irrelevant which row it is, only that two numbers were both assigned to the same one.

Example set of 3 permutations satisfying the constraint for N=8:

 0  1  2  3  4  5  6  7
 8  9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
 0  8 16 24 32 40 48 56
 1  9 17 25 33 41 49 57
 2 10 18 26 34 42 50 58
 3 11 19 27 35 43 51 59
 4 12 20 28 36 44 52 60
 5 13 21 29 37 45 53 61
 6 14 22 30 38 46 54 62
 7 15 23 31 39 47 55 63
 0  9 18 27 36 45 54 63
 1 10 19 28 37 46 55 56
 2 11 20 29 38 47 48 57
 3 12 21 30 39 40 49 58
 4 13 22 31 32 41 50 59
 5 14 23 24 33 42 51 60
 6 15 16 25 34 43 52 61
 7  8 17 26 35 44 53 62

A permutation that doesn't belong in the above set:

 0 10 20 30 32 42 52 62
 1 11 21 31 33 43 53 63
 2 12 22 24 34 44 54 56
 3 13 23 25 35 45 55 57
 4 14 16 26 36 46 48 58
 5 15 17 27 37 47 49 59
 6  8 18 28 38 40 50 60
 7  9 19 29 39 41 51 61

Because of multiple collisions with the second permutation, since, for example they're both pairing the numbers 0 and 32 in one row.


Enumerating three is easy, it consists of 1 arbitrary permutation, its transposition and a matrix where the rows are made of the previous matrix' diagonals.

I can't find a way to produce a set consisting of more though. It seems to be either a very complex problem, or a simple problem with an unobvious solution. Either way I'd be thankful if somebody had any ideas how to solve it in reasonable time for the N=8 case, or identified the proper, academic name of the problem, so I could google for it.

In case you were wondering what is it useful for, I'm looking for a scheduling algorithm for a crossbar switch with 8 buffers, which serves traffic to 64 destinations. This part of the scheduling algorithm is input traffic agnostic, and switches cyclically between a number of hardwired destination-buffer mappings. The goal is to have each pair of destination addresses compete for the same buffer only once in the cycling period, and to maximize that period's length. In other words, so that each pair of addresses was competing for the same buffer as seldom as possible.


EDIT:

Here's some code I have. CODE

It's greedy, it usually terminates after finding the third permutation. But there should exist a set of at least N permutations satisfying the problem.

The alternative would require that choosing permutation I involved looking for permutations (I+1..N), to check if permutation I is part of the solution consisting of the maximal number of permutations. That'd require enumerating all permutations to check at each step, which is prohibitively expensive.

+3  A: 

Make a 64 x 64 x 8 array: bool forbidden[i][j][k] which indicates whether the pair (i,j) has appeared in row k. Each time you use the pair (i, j) in the row k, you will set the associated value in this array to one. Note that you will only use the half of this array for which i < j.

To construct a new permutation, start by trying the member 0, and verify that at least seven of forbidden[0][j][0] that are unset. If there are not seven left, increment and try again. Repeat to fill out the rest of the row. Repeat this whole process to fill the entire NxN permutation.

There are probably optimizations you should be able to come up with as you implement this, but this should do pretty well.

Brian
+1, but I think the constraint is stronger: once a pair has appeared in the same row, they can't appear together in *any* row in another permutation. So maybe the "forbidden" array should be 64x64, without the final dimension?
Jim Lewis
A greedy approach like this one yields only a small number of permutations before terminating. It was the first thing I tried.
Vlad
+1  A: 

Possibly you could reformulate your problem into graph theory. For example, you start with the complete graph with N×N vertices. At each step, you partition the graph into N N-cliques, and then remove all edges used.

For this N=8 case, K64 has 64×63/2 = 2016 edges, and sixty-four lots of K8 have 1792 edges, so your problem may not be impossible :-)

John Fouhy
That sounds correct! And insightful -- because the clique finding problem is known to be NP-complete in general. What I suspect is that the first few iterations (while the NxN graph is still relatively dense) are probably easy to find by brute force, but as edges are removed, the cliques take longer and longer to find.
Jim Lewis
Well, finding _maximal_ cliques is NP-complete. I'm not sure about this problem. I think the cliques would be easy to find if they exist, since every vertex has to be a member of one, and you're only interested in size N. The problem is that a greedy algorithm will probably pick the wrong cliques and be unable to find all N, assuming N exist (well, that's my gut feeling anyway).
John Fouhy
Yes, essentially what I have implemented is finding all 8-cliques. This is fast having 2 starting permutations (40320 8-cliques found), and doable having 1 starting permutation (16 million 8-cliques found). The problem though:1. Enumerating all legal permutations, that is sets of 8 8-cliques, is a 40320^8 or (16 million)^8 affair.2. The number of 8-cliques and possible permutations in the next step depends on the choice of permutation in this step, greedy indeed doesn't work.A complete search would need to, while looking for permutation I, evaluate all later permutations (I+1..N) :/
Vlad
+2  A: 

What you want is a combinatorial block design. Using the nomenclature on the linked page, you want designs of size (n^2, n, 1) for maximum k. This will give you n(n+1) permutations, using your nomenclature. This is the maximum theoretically possible by a counting argument (see the explanation in the article for the derivation of b from v, k, and lambda). Such designs exist for n = p^k for some prime p and integer k, using an affine plane. It is conjectured that the only affine planes that exist are of this size. Therefore, if you can select n, maybe this answer will suffice.

However, if instead of the maximum theoretically possible number of permutations, you just want to find a large number (the most you can for a given n^2), I am not sure what the study of these objects is called.

Martin Hock
Many, many thanks!On the wikipedia page for block designs I found a link to a database containing the (64, 8, 1) solution I was interested in.
Vlad
A: 

Right, the greedy style doesn't work because you run out of numbers.

It's easy to see that there can't be more than 63 permutations before you violate the constraint. On the 64th, you'll have to pair at least one of the numbers with another its already been paired with. The pigeonhole principle.

In fact, if you use the table of forbidden pairs I suggested earlier, you find that there are a maximum of only N+1 = 9 permutations possible before you run out. The table has N^2 x (N^2-1)/2 = 2016 non-redundant constraints, and each new permutation will create N x (N choose 2) = 28 new pairings. So all the pairings will be used up after 2016/28 = 9 permutations. It seems like realizing that there are so few permutations is the key to solving the problem.

You can generate a list of N permutations numbered n = 0 ... N-1 as

A_ij = (i * N + j + j * n * N) mod N^2

which generates a new permutation by shifting the columns in each permutation. The top row of the nth permutation are the diagonals of the n-1th permutation. EDIT: Oops... this only appears to work when N is prime.

This misses one last permutation, which you can get by transposing the matrix:

A_ij = j * N + i
Brian
You also get the `N+1` upper limit by examining the N-1 neighbors of a given value, e.g. 1. Each of the remaining N^2-1 numbers can appear only once in a row with 1, meaning there are at most (N^2-1)/(N-1)=N+1 unique rows, hence matrices, containing 1.
outis