-1

we are looking for an algorithm that could help us creating subgroups of people. I'll explain it better with an example:

We have a group of 5 students, let's call them A, B, C, D, E. Given a number (call it n) smaller than the number of students (<5), we would like to create a subgroup of n elements per each student.

For example, given n = 3 we need to create 3 subgroups (SG1, SG2 and SG3) and a possible result would be

STUDENT -  SG1 - SG2 - SG3

A           B  -  C  -  D
B           C  -  D  -  E
C           A  -  E  -  B   
D           E  -  B  -  A
E           D  -  A  -  C

Each column must have all elements and they cannot repeat in the same row.

Many thanks in advance!

Jordi Corominas
  • 1,244
  • 10
  • 15

1 Answers1

0

Why don't you just always do the following order, MUCH easier to generate:

a - b,c,d
b - c,d,e
c - d,e,a
d - e,a,b
e - a,b,c

//aka

(  0 - 1,2,3  )  %5
(  1 - 2,3,4  )  %5
(  2 - 3,4,5  )  %5
(  3 - 4,5,6  )  %5
(  4 - 5,6,7  )  %5

I've created a generating form similar to the above but utilizing modulo in C. You can try it online here: http://ideone.com/n6qBzN

Code for reference:

main() {

    char *myString = "ABCDEFGHIJKLMNOP4583";
    int characters = mystrlen(myString);
    int i = 0;
    while (i < characters) {

        printf("%c", myString[i]);
        printf(" - ");

        int a = 1;
        while (a <= characters-2) {
            printf("%c, ", myString[(i+a)%characters]);
            a++;
        }

        printf("\n");

        i++;
    }

}

int mystrlen(char *s) {//arbitrary function for determining number of characters in a string (your initial group set)
    int i =0;
    while (*s++) i++;
    return i;
}

So you can see in the above code that the initial list of people groups is ABCDEFGHIJKLMNOP4583 and it generates the following table:

A - B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, 4, 5, 8, 
B - C, D, E, F, G, H, I, J, K, L, M, N, O, P, 4, 5, 8, 3, 
C - D, E, F, G, H, I, J, K, L, M, N, O, P, 4, 5, 8, 3, A, 
D - E, F, G, H, I, J, K, L, M, N, O, P, 4, 5, 8, 3, A, B, 
E - F, G, H, I, J, K, L, M, N, O, P, 4, 5, 8, 3, A, B, C, 
F - G, H, I, J, K, L, M, N, O, P, 4, 5, 8, 3, A, B, C, D, 
G - H, I, J, K, L, M, N, O, P, 4, 5, 8, 3, A, B, C, D, E, 
H - I, J, K, L, M, N, O, P, 4, 5, 8, 3, A, B, C, D, E, F, 
I - J, K, L, M, N, O, P, 4, 5, 8, 3, A, B, C, D, E, F, G, 
J - K, L, M, N, O, P, 4, 5, 8, 3, A, B, C, D, E, F, G, H, 
K - L, M, N, O, P, 4, 5, 8, 3, A, B, C, D, E, F, G, H, I, 
L - M, N, O, P, 4, 5, 8, 3, A, B, C, D, E, F, G, H, I, J, 
M - N, O, P, 4, 5, 8, 3, A, B, C, D, E, F, G, H, I, J, K, 
N - O, P, 4, 5, 8, 3, A, B, C, D, E, F, G, H, I, J, K, L, 
O - P, 4, 5, 8, 3, A, B, C, D, E, F, G, H, I, J, K, L, M, 
P - 4, 5, 8, 3, A, B, C, D, E, F, G, H, I, J, K, L, M, N, 
4 - 5, 8, 3, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, 
5 - 8, 3, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, 
8 - 3, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, 4, 
3 - A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, 4, 5, 
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195