1

An array of int (between 0 and 30) has N rows and 4 columns.

It has a head and a tail, a start and an end.

All the rows but two have to be permutated between the head and the tail. The rows identified as the head and the tail can be moved, but the distance between the two must, from top to bottom, remain the same.

Example:

enter image description here

In this picture, the sequence { 2, 4, 7, 9 } is the head, { 7, 1, 9, 4 } is the tail. Thus, only the elements between these two can be shuffled row by row (not column by column).

(It must be noted that "Row#" refers to the row which contains the respective sequences, not the absolute rows from our perspective. Then "Row3" in this picture is situated at the second row).

Theoretically, if I am right, there are 3! possible permutations.

But these elements can not be randomly shuffled. A specific configuration is sought.

First contraint: Each int of the second column (here: ColB) must be identical to the int situated at x mod N rows below it in the first column (ColA). (x is arbitrary: we know that there is a solution for a given x).

Second constraint: Each int of the third column (ColC) must be identical to the int just below it, mod N (then, NC = 1D), in the fourth column (ColD).

An example of good configuration (x = 3 here): enter image description here

Take the number 4, row1, colB. It is identical to the number situated at ((row where is 4)+3) % 5, colA (in blue). And this is also the case for all numbers of ColB.

Take the number 7, row1, colC. It is identical to the number situated at ((row where is 7)+1) % 5, colD (in yellow).

This is a good configuration too (x = 3 too):

enter image description here

Head and tail have been moved, but the distance from top to bottom (we start from head of course) between the two remains the same.

Take the number 2, row3 (the fourth row then), colB. It is identical to the number situated at ((row where is 2)+3) % 5 = second row, colA.

As you see, it is very simple, trivial to solve in this situation.

Now imagine that N = 300. 300-2 rows must be permutated in order to meet the constraints. Then 298! permutations possible. It seems unsolvable.

Do you know if an algorithm can help to solve this problem with N = 300 rows with repetitive ints in a reasonable time (that is, < age of the Universe)?

Thanks!

  • 1
    Rather than trying every combination, have you tried doing an iterative algorithm? For example, you could take out all of the rows and then recursively put them back in, using your second constraint to see which ones would fit there. The first constraint would be a little trickier, that might just have to be validated at the end. The worst case would likely still be very bad, but for most cases this would eliminate most of the computation. Also, how did you get the value of x=3 in all of your examples? Is that just the number of rows to permute? I don't think you introduced it anywhere. – sabreitweiser Apr 29 '15 at 00:31
  • 1
    Also, regarding your two solutions given in the example, aren't they just ring permutations?http://mathworld.wolfram.com/CircularPermutation.html Since everything is mod N, it seems that all members of a ring permutations will either be good or bad, unless I'm missing something. – sabreitweiser Apr 29 '15 at 00:35
  • 1/ The (major) problem with an iterative algorithm, as you pointed out, is the first constraint. It seems to me that it won't be efficient because of this issue. 2/ x=3 is arbitrary: I know that there is a solution with x=3. But it can be anything (< N of course). –  Apr 29 '15 at 00:35
  • This seems indeed to be the case of circular permutations (cf. my tags), but I do not know how to find that each element is good in a reasonable time... –  Apr 29 '15 at 00:38
  • 1
    Does the second constraint "wrap around"? That is NC = 1D? – Beta Apr 29 '15 at 00:51
  • 1
    I think this is NP-complete. Working out details. How does this problem arise, by the way? – BadZen Apr 29 '15 at 02:04
  • I think too, but I think there is a way to optimize the search for an optimal solution. I tried with a genetic algorithm, but I failed miserably. Regarding the origins of this problem, let's say I love working on permutations and double bijections of data in arrays (see my other questions). ;) –  Apr 29 '15 at 07:18

0 Answers0