Total Derangement (Difficulty Level: Hard)

9

The problem: Write a function which, given a cycle length n and a number of cycles m, where 'm' is within [2, n-2], generates m random cycles, each of length n, and all of which are derangements of each other. Then n=7, for example, m could be 2, 3, 4, or 5. All possible sets of m cycles of length n that fit the criteria should have equal probability of occurring.

What is a cycle? Imagine you're hopping around a list. Each element in the list is a number corresponding to another position on the list. Start out at the first element, and update your position to be whatever index is at that element:

CycleThrough(list)
{ 
    pos = 0;
    while(true) { pos = list[pos]; }
}

If you can visit every element in the list and get back to the start using this method, it's a cycle. If you can't, it's not. Here are a few examples of cycles:

(0)
(1, 0)
(1, 2, 0)
(2, 0, 1)
(1, 2, 3, 0)
(1, 3, 0, 2)
(2, 4, 1, 0, 3)

Take the last cycle. It goes 0 -> 2 -> 1 -> 4 -> 3 -> 0. What isn't a cycle? Take (4, 0, 3, 2, 1)- this isn't a cycle because it goes 0 -> 4 -> 1 -> 0, and you won't hit 3 or 2. Or take (1, 1) - it goes 0 -> 1 -> 1 -> 1 -> 1... and it never gets back to 0, so it's not a cycle.

What does it mean for two cycles to be deranged? Lets say you have two cycles, cycle A and cycle B. If A[i] never equals B[i], they're derangements of each other. If you have more than two cycles, they're all derangements of each other if all pairs of cycles are derangements.

The fastest algorithm (in time complexity) wins.

Example outputs for n=5, m=3:

{{1, 4, 3, 0, 2}, {3, 0, 4, 2, 1}, {2, 3, 1, 4, 0}}
{{3, 0, 4, 2, 1}, {2, 4, 3, 1, 0}, {4, 3, 1, 0, 2}}
{{3, 0, 1, 4, 2}, {2, 4, 3, 1, 0}, {4, 3, 0, 2, 1}}
{{3, 4, 0, 1, 2}, {1, 2, 4, 0, 3}, {2, 3, 1, 4, 0}}
{{3, 4, 0, 1, 2}, {4, 0, 1, 2, 3}, {1, 2, 3, 4, 0}}
{{4, 2, 0, 1, 3}, {2, 3, 1, 4, 0}, {1, 4, 3, 0, 2}}
{{4, 3, 0, 2, 1}, {2, 4, 1, 0, 3}, {3, 2, 4, 1, 0}}
{{4, 0, 1, 2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
{{3, 4, 1, 5, 8, 9, 0, 10, 6, 7, 2}, {1, 2, 6, 4, 10, 8, 5, 9, 7, 3, 0}, {10, 6, 3, 1, 9, 7, 8, 4, 0, 2, 5}, {8, 3, 0, 7, 2, 6, 10, 5, 9, 1, 4}, {9, 5, 7, 6, 0, 3, 2, 8, 4, 10, 1}, {2, 0, 9, 10, 7, 1, 4, 3, 5, 6, 8}, {6, 7, 5, 8, 3, 4, 1, 2, 10, 0, 9}, {4, 8, 10, 9, 6, 0, 7, 1, 2, 5, 3}, {5, 10, 4, 0, 1, 2, 9, 6, 3, 8, 7}}
{{6, 11, 4, 5, 3, 1, 8, 2, 9, 7, 0, 10}, {1, 4, 8, 9, 10, 11, 5, 3, 7, 6, 2, 0}, {7, 9, 10, 1, 11, 3, 2, 8, 6, 0, 4, 5}, {4, 2, 5, 0, 6, 7, 9, 11, 1, 10, 8, 3}, {10, 8, 7, 6, 5, 0, 4, 1, 3, 11, 9, 2}, {9, 3, 6, 8, 7, 10, 1, 0, 5, 2, 11, 4}, {8, 0, 9, 11, 1, 4, 7, 10, 2, 3, 5, 6}, {3, 6, 0, 10, 2, 9, 11, 5, 4, 8, 1, 7}, {2, 5, 11, 7, 8, 6, 10, 9, 0, 4, 3, 1}, {11, 7, 1, 2, 0, 8, 3, 4, 10, 5, 6, 9}}

J. Antonio Perez

Posted 2017-01-23T19:01:05.050

Reputation: 1 480

2Fastest in complexity, or fastest in time taken? – Conor O'Brien – 2017-01-23T19:05:02.543

Fastest in complexity. Ideally I want an O(n * m) algorithm; not sure if that's possible. O(n * m log(n)) would be the next best bet, followed by O(n * m log(n * m)) – J. Antonio Perez – 2017-01-23T19:16:12.827

You're asking for the impossible. Consider n = 4, m = 3. There are six derangements which are single cycles, and no triple which are mutually derangements. – Peter Taylor – 2017-01-23T19:21:07.027

I should have been more specific. I mean that m can range between 2 and n-2 inclusive.. – J. Antonio Perez – 2017-01-23T21:15:54.107

I fixed the wording of the problem to remove ambiguity. – J. Antonio Perez – 2017-01-23T21:19:46.147

Please either clarify what the ambiguity is that remains or remove the hold on my question. I believe I have corrected everything you asked, and aside from that I don't believe it violates the rules in any way. – J. Antonio Perez – 2017-01-23T23:07:52.890

Can you give an example with n=9, m=7? (If you can then there's a further ambiguity, which is what the distribution of the random cycles should be: I presume it's selecting a random set of valid derangements such that each set is selected with equal probability assuming a perfect random number generator, but that's not entirely clear; however, the task being actually possible is a much bigger problem). – Peter Taylor – 2017-01-23T23:17:51.693

An answer for n=9, m=7 is {{5, 3, 4, 2, 0, 7, 8, 6, 1}, {2, 0, 8, 6, 5, 3, 1, 4, 7}, {1, 4, 6, 7, 8, 2, 3, 0, 5}, {8, 6, 0, 4, 7, 1, 2, 5, 3}, {3, 7, 5, 1, 2, 0, 4, 8, 6}, {4, 5, 7, 8, 3, 6, 0, 1, 2}, {6, 8, 3, 5, 1, 4, 7, 2, 0}} – J. Antonio Perez – 2017-01-24T01:57:52.457

I cleared up ambiguity and stated that all the different ways of doing it with m cycles of length n should have the same probability of occurring – J. Antonio Perez – 2017-01-24T02:05:17.210

If you'd like I could share all the code I've produced so far working on this problem. – J. Antonio Perez – 2017-01-24T02:17:35.963

"The fastest algorithm (in time complexity) wins." There's two parameters here, m and n, so it's not clear how to compare tradeoffs between these. If you mean for m to be linear in n, that would resolve it. – xnor – 2017-01-24T05:11:40.720

The worst case time complexity will always be when m=n-2 (since that's the maximum value m can take on for the challenge). Different algorithms will be compared for that case. – J. Antonio Perez – 2017-01-24T05:18:31.210

If n is an odd number than m can be n-1 but that isn't covered in the challenge – J. Antonio Perez – 2017-01-24T05:29:25.297

Is it fine to use built-in combinatorial functions? – Marco – 2018-02-08T10:52:49.680

No answers