Algorithm to return all combinations of k from n and the corresponding list of complements


Egvin Seday

This is related to this question , but I also want the corresponding one's complement sorted list with elements that are not efficiently selected using python or c++ . For example, for an ordered list, list=(0,1,1,2,3,3)when we select 3 elements, one possible return pair would be (0,1,3)sum (1,2,3). Also, I want the function to return a total C(N,K) of terms such that a pair of sums (0,1,3)should (1,2,3)be repeated 4 times in the returned result.

Full example for short input: define a function foo(list,k)which foo([0,1,1],1)should then return a rlist of length C(3,1)=3,

r(0)=[[0],[1,1]] (choose 0, complement list is [1,1])
r(1)=[[1],[0,1]] (choose first 1, complement list is [0,1])
r(2)=[[1],[0,1]] (choose second 1, complement list is [0,1])
tobias_k

You can use the basic composition algorithm, but it returns a tuple: an "in" element and an "out" element. Then just recursively generate combinations for the rest of the list and add the first element to the "in" or "out" list respectively.

Here is some Python code:

def comb_and_comp(lst, n):
    # no combinations
    if len(lst) < n:
        return
    # trivial 'empty' combination
    if n == 0 or lst == []:
        yield [], lst
    else:
        first, rest = lst[0], lst[1:]
        # combinations that contain the first element
        for in_, out in comb_and_comp(rest, n - 1):
            yield [first] + in_, out
        # combinations that do not contain the first element
        for in_, out in comb_and_comp(rest, n):
            yield in_, [first] + out

This will create the "in" and "out" lists in one pass, rather than creating the complement in a second pass.

Related


Algorithm to return all combinations of k elements from n

Frederick I want to write a function that takes an array of letters as a parameter and selects multiple letters. Suppose you provide an array of 8 letters and want to select 3 letters from it. Then you will get: 8! / ((8 - 3)! * 3!) = 56 Returns an array (or

Algorithm to return all combinations of k elements from n

Frederick I want to write a function that takes an array of letters as a parameter and selects multiple letters. Suppose you provide an array of 8 letters and want to select 3 letters from it. Then you will get: 8! / ((8 - 3)! * 3!) = 56 Returns an array (or

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as itertools.combinations(iterable, r) in python So far I came up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combinatio

Get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as in pythonitertools.combinations(iterable, r) So far I have come up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combin

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now I'm trying to write a function that takes an array and an integer n and gives a list of each combination of size n (i.e. a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I don'

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now, I'm trying to write a function that takes an array and an integer n, and gives a list of each combination of size n (hence a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I c

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now, I'm trying to write a function that takes an array and an integer n, and gives a list of each combination of size n (hence a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I c

Algorithm (Java) to get all combinations of size n from an array?

Esostack Right now I'm trying to write a function that takes an array and an integer n and gives a list of each combination of size n (i.e. a list of int arrays). I could write it using n nested loops, but that only works for a subset of a certain size. I don'

make all combinations of size k from 1 to number n

Ananya Given two numbers n and k, you have to find all possible combinations of k numbers in 1...n. I am using DFS algorithm to achieve this. But my ansarray returns None, whereas if I try to print temp, the combination is generated correctly. What am I doing

make all combinations of size k from 1 to number n

Ananya Given two numbers n and k, you have to find all possible combinations of k numbers in 1...n. I am using DFS algorithm to achieve this. But my ansarray returns None, whereas if I try to print temp, the combination is generated correctly. What am I doing

Algorithm to generate n combinations of size k characters

alex_and_ra I need an algorithm that generates all combinations of size n of k characters. For example, if I have n=1 and k={a,b}, the result should be: a b If n=3 and k={a,b}, the result should be: a a a a a b a b a a b b b a a b a b b b a b b b Can someone

Algorithm to generate n combinations of size k characters

alex_and_ra I need an algorithm that generates all combinations of size n of k characters. For example, if I have n=1 and k={a,b}, the result should be: a b If n=3 and k={a,b}, the result should be: a a a a a b a b a a b b b a a b a b b b a b b b Can someone

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

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated