Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeatedly generate non-repeating smaller groups from a larger set

Tags:

r

permutation

I have a large set of size M (let's say 10), and I want to, repeatedly for a certain number of occasions (let's say 13), randomly split it into M/N smaller groups of size N (let's say 2). I'd like no element in the large set to be in a repeating group until they have been in a small group with every one else. (The actual problem here: I have a class of 10 people and I want to split them into 5 pairs for a duration of 13 weeks, but I don't want anyone to be in a repeat pairing until they have been in a pairing with everyone in the class.)

How can I do this? I started by [generating non-repeating permutations from my larger group][1], but the trouble I am having is that these unique permutations don't necessarily yield unique groups. (Someone seems to have posed this same question, but [it was resolved in Python][2]. I don't understand Python, and so I'm looking for an easy R solution.)

Any help much appreciated.

Edit: Thanks to all for suggestions. I realize my original question wasn't exactly clear. The solutions suggested below work well when I only want to split the set into a single subset of size N, each time. But my problem is actually that I want to split the set into M/N subsets of size N. For example, in the case of my class, I want to split the 10 students into 5 pairs of 2 on 13 different occasions, and I want pairs to be unique until they no longer can be (i.e., after 9 occasions have passed). Unless I'm failing to see how they can be applied, I don't think any of these solutions quite solves this problem.

like image 684
daanoo Avatar asked Sep 05 '20 19:09

daanoo


People also ask

How do you generate a non repeating random number in C++?

First create an array of numbers 0-9, then shuffle it and take the first 5 (or any size <= 10) numbers. That will be your non-repeating numbers.


Video Answer


4 Answers

I see that the OP has provided a solution from the linked math.so solution, but I would like to provide a working solution of the other answer on that page that gets to the heart of this problem. That solution mentions Round-robin tournament. From the wikipedia page, the algorithm is straightforward.

One simply fixes a position in a matrix and rotates the other indices clockwise. Given M initial players, there are M - 1 unique rounds. Thus, for our given situation, we can only obtain 9 unique sets of groups.

Below, is a very straightforward base R implementation:

roll <- function( x , n ){
    if( n == 0 )
        return(x)
    c(tail(x,n), head(x,-n))
}

RoundRobin <- function(m, n) {
    m <- as.integer(m)
    n <- as.integer(n)
    
    if (m %% 2L != 0L) {
        m <- m + 1L
    }
    
    myRounds <- list(n)
    myRounds[[1]] <- 1:m
    
    for (i in 2:n) {
        myRounds[[i]] <- myRounds[[i - 1L]]
        myRounds[[i]][2:m] <- roll(myRounds[[i]][-1], 1)
    }
    
    lapply(myRounds, matrix, nrow = 2)
}

The roll function was obtained from this answer.

Here is sample output for 10 students and 4 weeks:

RoundRobin(10, 4)
[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    4    6    8
[2,]   10    3    5    7    9

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1   10    3    5    7
[2,]    9    2    4    6    8

[[4]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    9    2    4    6
[2,]    8   10    3    5    7

When we hit the 10th week, we see our first repeat "round".

RoundRobin(10, 13)[c(1, 2, 9, 10, 11)]
[[1]]
     [,1] [,2] [,3] [,4] [,5]   ## <- first week
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

[[2]]
     [,1] [,2] [,3] [,4] [,5]   ## <- second week
[1,]    1    2    4    6    8
[2,]   10    3    5    7    9

[[3]]
     [,1] [,2] [,3] [,4] [,5]   ## <- ninth week
[1,]    1    4    6    8   10
[2,]    3    5    7    9    2

[[4]]
     [,1] [,2] [,3] [,4] [,5]   ## <- tenth week
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

[[5]]
     [,1] [,2] [,3] [,4] [,5]   ## <- eleventh week
[1,]    1    2    4    6    8
[2,]   10    3    5    7    9

Note, this is a deterministic algorithm and given the simplicity, it is pretty efficient. E.g. if you have 1000 students and want to find all 999 unique pairings, you can run this function without fear:

system.time(RoundRobin(1000, 999))
   user  system elapsed 
  0.038   0.001   0.039
like image 60
Joseph Wood Avatar answered Oct 21 '22 10:10

Joseph Wood


I think you maybe want something like this. It will produce a data frame with the unique combinations in rows. These are sampled randomly until all unique combinations are exhausted. Thereafter, if more samples are required it will sample randomly with replacement from unique combinations:

create_groups <- function(M, N, samples)
{
  df <- seq(N) %>%
          lapply(function(x) M) %>%
          do.call(expand.grid, .) %>%
          apply(1, sort) %>%
          t() %>%
          as.data.frame() %>%
          unique()
  
  df <- df[apply(df, 1, function(x) !any(duplicated(x))), ]
  
  df <- df[sample(nrow(df)), ]
  
  if(samples <= nrow(df)) return(df[seq(samples), ])
  
  rbind(df, df[sample(seq(nrow(df)), samples - nrow(df), TRUE), ])
}

It's easy to see how it works if we want groups of 4 elements from 5 objects (there are only 5 possible combinations):

create_groups(letters[1:5], 4, 5)
#>   V1 V2 V3 V4
#> 1  a  b  d  e
#> 2  a  b  c  d
#> 3  a  c  d  e
#> 4  b  c  d  e
#> 5  a  b  c  e

We have a randomly-ordered sample of 4 objects drawn from the set, but no repeats. (the elements within each sample are ordered alphabetically however)

If we want more than 5 samples, the algorithm ensures that all unique combinations are exhausted before resampling:

create_groups(letters[1:5], 4, 6)
#>   V1 V2 V3 V4
#> 1  a  b  c  e
#> 2  a  c  d  e
#> 3  a  b  d  e
#> 4  b  c  d  e
#> 5  a  b  c  d
#> 6  a  b  d  e

Here we see there are no repeated rows until row 6, which is a repeat of row 3.

For the example in your question, there are 45 unique combinations of 2 elements drawn from 10 objects, so we get no repeats in our 13 samples:

create_groups(1:10, 2, 13)
#>    V1 V2
#> 1   7  8
#> 2   4 10
#> 3   2  8
#> 4   3 10
#> 5   3  9
#> 6   1  8
#> 7   4  9
#> 8   8  9
#> 9   7  9
#> 10  4  6
#> 11  5  7
#> 12  9 10
#> 13  4  7
like image 32
Allan Cameron Avatar answered Oct 21 '22 10:10

Allan Cameron


I am not sure combn + sample can work for your goal

as.data.frame(t(combn(M, N))[sample(K <- choose(length(M), N), i, replace = K < i), ])

which gives

   V1 V2
1   4  9
2   4  8
3   1  9
4   6 10
5   5  9
6   2 10
7   3  7
8   7  8
9   6  7
10  1  7
11  6  8
12  5  6
13  3  8
like image 25
ThomasIsCoding Avatar answered Oct 21 '22 09:10

ThomasIsCoding


With apologies to all for not writing a clear question, here is a solution based on the solution suggested in this post. (Depending on the seed, it can get stuck, and if weeks are larger, the code to recycle old groups has to be adjusted a little.)

set.seed(1)

m<-10
n<-2
weeks<-13

groupmat<-combn(m,n)
students <- c(1:m)

pickedpairs <- matrix(
  data=NA,
  nrow=n,
  ncol=0
)

while( ncol(pickedpairs) < ((m-1)*(m/n)) ) {
  
  thisweekspairs <- matrix(sample(students),nrow=n,ncol=m/n)
  
  #check if this weeks pairs
  #are already in pickedpairs
  #if so, skip iteration
  pairsprez <- lapply(1:ncol(thisweekspairs),function(j) {
    #j<-1
    apply(pickedpairs,2,function(x) sum(x%in%thisweekspairs[,j])==n)
  }) %>% Reduce(f="|") %>% sum
  if(pairsprez>=1) {
    pickedpairs<<-pickedpairs
  } else {
    pickedpairs<<-cbind(pickedpairs,thisweekspairs)
  }
  print(ncol(pickedpairs))
  
}

uniquepairs <- lapply(1:(ncol(pickedpairs)/(m/n)),function(i) {
  pickedpairs[,(1 + (m/n)*(i-1)):((m/n)*i)]
})

#generate weeks' number of unique pairs
combine(
    uniquepairs,
    uniquepairs[sample(1:length(uniquepairs),weeks-length(uniquepairs))]
)
like image 29
daanoo Avatar answered Oct 21 '22 09:10

daanoo