Create all possible subsets of k and m combinations of n terms in C


Famedoro

I'm looking for a solution to my problem: I have to write a code that computes the combinations of unique elements, that is, all the different combinations of n elements selected as k elements, and recomputes the new combinations of the remaining subsets without duplication. Given S (the set of all possible unique elements), I have to compute a subset T of unique combinations of elements of S, and now I have to recompute a new subset of combinations of T -V- and all subsets T and V must to be unique:

For example I have this set S: {0, 1, 2, 3, 4}

i have to get

a {0, 1} {2, 3} { 4} 
b {0, 1} {2, 4} { 3} 
c {0, 1} {3, 4} { 2} 
d {0, 2} {1, 3} { 4} 
e {0, 2} {1, 4} { 3} 
f {0, 2} {3, 4} { 1} 
g {0, 3} {1, 2} { 4} 
h {0, 3} {1, 4} { 2} 
i {0, 3} {2, 4} { 1} 
j {0, 4} {1, 2} { 3} 
k {0, 4} {1, 3} { 2} 
l {0, 4} {2, 3} { 1} 
discarded as the same as g -> {1, 2} {0, 3} { 4} 
discarded as the same as j -> {1, 2} {0, 4} { 3} 
m {1, 2} {3, 4} {0} 
discarded as the same as d -> {1, 3} {0, 2} { 4} 
discarded as the same as k -> {1, 3} {0, 4} { 2} 
n {1, 3} {2, 4}{ 0} 
discarded as the same as e -> {1, 4} {0, 2} { 3} 
discarded as the same as h -> {1, 4} {0, 3} { 2} 
o {1, 4} {2, 3}{0} 
discarded as the same as a -> {2, 3} {0, 1} { 4} 
discarded as the same as l -> {2, 3} {0, 4} { 1} 
discarded as the same as o -> {2, 3} {1, 4} { 0} 
discarded as the same as b -> {2, 4} {0, 1} { 3} 
discarded as the same as i -> {2, 4} {0, 3} { 1} 
discarded as the same as n -> {2, 4} {1, 3} { 0} 
discarded as the same as c -> {3, 4} {0, 1} { 2} 
discarded as the same as f -> {3, 4} {0, 2} { 1} 
discarded as the same as m -> {3, 4} {1, 2} { 0} 

{1, 2} {0, 3} {4} is the same combination (for this question) as {0, 3} {1, 2} {4}, which must then be discarded, the same as {1, 2} {0, 4 } {3} and {0, 4} {1, 2} {3}.

Is it possible to achieve the goal without using a data structure (as a list) that considers composition?

I need something like this: generate combinations: 1

It is not a duplicate of the previous question, because the study deals with partitions that must be considered unambiguous, i.e. the elements contained in a partition (regardless of their order) must not have been agreed upon in the previous subdivision, so e.g. {1, 2 } {0,4}{3} and {0,4}{1,2}{3} must be considered non-unique, so there are only valid combinations: {0,4}{1,2}{3}

Tom's

This is a lot harder than I initially thought.

Ok, let's say you have "n" uniq elements. The sum of the uniq possibilities is "n!". (so for 5 elements you have 120 possibilities).

Say you want to "group" "k" numbers.

So if n = 5 and k = 2, you will get the example:

{0,1},{2,3},{4}。

Now, here's where the fun begins: in order to know if the current proposition is not a duplicate, you can discard every proposition that doesn't sort the first number in each complete group .

E.g:

{0,1},{2,3},{4}。

Here, 1 and 3 are useless because it is not the first value of a complete group, and 4 is part of an incomplete group. So it's interesting

{ 0,?},{ 2,?},{?}。

Is it sorted by 0 and 2? Yes, so you can keep this claim. This means that if you have

{2,3},{0,1},{4}。

bad because

{ 2,?},{ 0,?},{?}。

2, 0 are not sorted.


If n = 6 and k = 2, then

{0,2},{3,4},{1,5}

Does it work? No, because 0 3 1 is not sorted. you can see

{0,2},{1,5},{3,4}

is a valid ordering proposition.


Now, if we know n and k, how many valid propositions is it possible to calculate?

Yes.

Maybe. I think...I'll update if I can find it.

edit:

Aaaaaannnd, here is an implementation. Do something fun. It's based on the previous algorithm, so if my algorithm is false, this code is false.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



#define N 5
#define K 2


void Comb_Display(int proposition[N])
{
    printf("{");
    for (int i = 0; i < N; ++i) {
        if (i && i % K == 0) {
            printf("} {");
        }
        printf("%d%s", proposition[i], (i && (i + 1) % K == 0) || i + 1 >= N ? "" : ", ");
    }
    printf("}\n");
}

bool Comb_Valid(int proposition[N])
{
    int nbGroup = N / K;

    if (nbGroup == 1) {
        return (true);
    }
    for (int i = 0; i < nbGroup; i += K) {
        if (proposition[i] > proposition[i + K]) {
            return (false);
        }
    }
    return (true);
}

void Comb_MakeMagicPlease(int numberAvailable[N], int proposition[N], int ind)
{
    // We are at the end of the array, so we have a proposition
  if (ind >= N) {
    printf("%s : ", Comb_Valid(proposition) ? "OK" : "  "); // O = Valide, ' ' = invalide
    Comb_Display(proposition);
    return;
  }

  // We scan "numberAvailable" in order to find the first number available
  for (int i = 0; i < N; i++) {
    if (numberAvailable[i] != -1) {
        int number = numberAvailable[i];

        numberAvailable[i] = -1; // We mark the number as not available

      proposition[ind] =  number;
      Comb_MakeMagicPlease(numberAvailable, proposition, ind + 1);
      numberAvailable[i] = number; // we mark the number as available
    }
  }
}

int main(void)
{
  int numberAvailable[N];
  int proposition[N];

  for (int i = 0; i < N; ++i) {
    numberAvailable[i] = i + 1;
  }

  Comb_MakeMagicPlease(numberAvailable, proposition, 0);
  return 0;
}

Related


all N combinations of all subsets

Mel Given a vector of elements, I want to get a list of all possible length combinations for a nsubset of elements . For example, given the (simplest) sequence 1:2, I want to get a list object of the form { {{1},{1}}, {{1},{2}}, {{2},{2}}, {{1},{1,2}}, {{2},{1

all N combinations of all subsets

Mel Given a vector of elements, I want to get a list of all possible length combinations of a nsubset of elements . For example, given the (simplest) sequence 1:2, I want to get a list object of the form { {{1},{1}}, {{1},{2}}, {{2},{2}}, {{1},{1,2}}, {{2},{1,

all N combinations of all subsets

Mel Given a vector of elements, I want to get a list of all possible length combinations for a nsubset of elements . For example, given the (simplest) sequence 1:2, I want to get a list object of the form { {{1},{1}}, {{1},{2}}, {{2},{2}}, {{1},{1,2}}, {{2},{1

all N combinations of all subsets

Mel Given a vector of elements, I want to get a list of all possible length combinations of a nsubset of elements . For example, given the (simplest) sequence 1:2, I want to get a list object of the form { {{1},{1}}, {{1},{2}}, {{2},{2}}, {{1},{1,2}}, {{2},{1,

all N combinations of all subsets

Mel Given a vector of elements, I want to get a list of all possible length combinations for a nsubset of elements . For example, given the (simplest) sequence 1:2, I want to get a list object of the form { {{1},{1}}, {{1},{2}}, {{2},{2}}, {{1},{1,2}}, {{2},{1

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

All possible combinations of N objects in K buckets

Aliresa Norri Say I have 3 boxes labeled A, B, C and I have 2 balls, B1 and B2. I want to get all possible combinations of these balls in a box. Note that it is important to know the balls in each box, which means that B1 and B2 are not the same. A B

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

All possible combinations and subsets of arrays in Java

username Given an array of variable dimensions....for example, array = {1,2,4,5} I need a way to generalize all possible combinations and subsets of arrays. Given an array of n elements, I need to have all subsets of all possible subsets (all subsets of 1 elem

All possible combinations and subsets of arrays in Java

username Given an array of variable dimensions....for example, array = {1,2,4,5} I need a way to generalize all possible combinations and subsets of arrays. Given an array of n elements, I need to have all subsets of every possible subset (all subsets of 1 ele

All possible combinations and subsets of arrays in Java

username Given an array of variable dimensions....for example, array = {1,2,4,5} I need a way to generalize all possible combinations and subsets of arrays. Given an array of n elements, I need to have all subsets of every possible subset (all subsets of 1 ele

C# create string with all possible combinations

username So I have a method that takes a string. The string is made up of a constant and 2 booleans, 2 constant ints and an int which can be 10, 20 or 30. This is all a string with parameters separated by underscores. example: string value = "horse" string com

C# create string with all possible combinations

username So I have a method that takes a string. The string is made up of a constant and 2 booleans, 2 constant ints and an int which can be 10, 20 or 30. This is all a string with parameters separated by underscores. example: string value = "horse" string com

C# create string with all possible combinations

username So I have a method that takes a string. The string is made up of a constant and 2 booleans, 2 constant ints and an int which can be 10, 20 or 30. This is all a string with parameters separated by underscores. example: string value = "horse" string com

C# create string with all possible combinations

username So I have a method that takes a string. The string is made up of a constant and 2 booleans, 2 constant ints and an int which can be 10, 20 or 30. This is all a string with parameters separated by underscores. example: string value = "horse" string com

C# create string with all possible combinations

username So I have a method that takes a string. The string is made up of a constant and 2 booleans, 2 constant ints and an int which can be 10, 20 or 30. This is all a string with parameters separated by underscores. example: string value = "horse" string com

C# create string with all possible combinations

username So I have a method that takes a string. The string is made up of a constant and 2 booleans, 2 constant ints and an int which can be 10, 20 or 30. This is all a string with parameters separated by underscores. example: string value = "horse" string com

C# create string with all possible combinations

username So I have a method that takes a string. The string is made up of a constant and 2 booleans, 2 constant ints and an int which can be 10, 20 or 30. This is all a string with parameters separated by underscores. example: string value = "horse" string com

C# create string with all possible combinations

username So I have a method that takes a string. The string is made up of a constant and 2 booleans, 2 constant ints and an int which can be 10, 20 or 30. This is all a string with parameters separated by underscores. example: string value = "horse" string com