0

The practical application of this algorithm is to assign each person in a group of 2..N people a different target person from the same group of people for as many consecutive rounds as possible.

I'm representing the group of people as an array. The array holds the person identifiers as numbers, for example, 5 people will create an array [1, 2, 3, 4, 5]. The targets for each people can then be derived as:

  • Person 1 will have Person 2 as target
  • Person 2 will have Person 3
  • 3 => 4
  • 4 => 5
  • 5 => 1 (array will loop)

Array [1, 2, 3, 4, 5] can then yield the following targets in an abbreviated form: 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 = 1

I need to find an algorithm that finds out all the permutations (different orders that the array elements can be sorted in) that have different consecutive elements.

For example, for the given array, result [2, 4, 3, 1, 5] is one, because you can't find 2 before 4, 4 before 3, 3 before 1, 1 before 5 and 5 before 2 (notice that I handle the array as looping) from the original array [1, 2, 3, 4, 5].

An array with 2 elements (or persons) will always have each other as targets. This means that algorithm with an input [1, 2] will only have one result: [1, 2]. It doesn't matter if you reorder the array elements as [2, 1] since we consider the array looping: 2 -> 1, 1 -> 2.

For [1, 2, 3] it's possible to find two permutations:
[1, 2, 3] (1 -> 2, 2 -> 3, 3 -> 1) and [2, 1, 3] (2 -> 1, 1 -> 3, 3 -> 2)

The algorithm should return all the permutations of the array as follows:

algorithm([1, 2, 3, 4, 5]) => [[1, 2, 3, 4, 5], [2, 4, 3, 1, 5], ...]
Markus Rautopuro
  • 7,997
  • 6
  • 47
  • 60
  • What do you mean by "the same consecutive elements"? I haven't come up with any interpretation of that phrase that fits your examples. – user2357112 Apr 25 '16 at 18:05
  • What is the practical application of this? – Giorgi Nakeuri Apr 25 '16 at 18:06
  • (1) You seem to write permutation as a single cycle, which is confusing, since there are permutations consisting of more than one cycle. Whether or not you want to consider only cyclic permutations remains unclear. Please consult Wikipedia or other source for understandable terminology. – Gassa Apr 25 '16 at 18:19
  • (2) You may be talking about derangements here (permutations where no elements stays in place). However, your terminology is unclear, so please update the question using more standard names for stuff. – Gassa Apr 25 '16 at 18:21
  • Let me rephrase the terminology in the question. – Markus Rautopuro Apr 25 '16 at 18:45
  • It's still not clear what you mean. Any interpretation I can come up with is contradicted by your examples. The fact that `[1, 2]`, `[1, 2, 3]`, and `[1, 2, 3, 4, 5]` are present in the output is particularly baffling. – user2357112 Apr 25 '16 at 18:59
  • 1
    Your new edit is much better. Can we have outputs where 1 and 2 target each other and 3 and 4 target each other, or does it have to be like a game of [Assassin](https://en.wikipedia.org/wiki/Assassin_(game)) where all the target assignments form one big murder ring? – user2357112 Apr 25 '16 at 19:04
  • I've now rephrased the question. There was also an error in the examples I provided, which should be corrected now. 1 and 2 can target each other as you said. Please note that it might not be a best way to represent this problem as an array. It's just currently the input data model for the "person list". – Markus Rautopuro Apr 25 '16 at 19:06

1 Answers1

1

According to your comments, we can have outputs where 1 and 2 target each other and 3 and 4 target each other. (These outputs aren't representable in the output format you've chosen.)

In that case, on every round i from 1 to N-1, just have everyone target the person i spots ahead of them.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • This seems to work, which leads to the fact that the maximum number of different rounds (rounds with different targets) is `N-1`. This also means that [1, 2, 3, 4] has at least two solutions: – Markus Rautopuro Apr 25 '16 at 19:28
  • **1)** `1 => 2, 2 => 1, 3 => 4, 4 => 3` and `1 => 3, 2 => 4, 3 => 1, 4 => 2` **2)** `1 => 3, 2 => 4, 3 => 1, 4 => 2` and `1 => 4, 2 => 1, 3 => 2, 4 => 3` – Markus Rautopuro Apr 25 '16 at 19:34
  • @גלעדברקן: No, but now that the question has been clarified, it doesn't look like that's what the question is asking for. – user2357112 Apr 25 '16 at 22:56