combinatorics

Find all possible partitions of n elements with k-sized subsets, where two elements share same set only once

I have n elements that need to be partitioned into x sets, each set has to hold exactly k=4 elements. I need to find all possible partitions with the constraint that each pair of elements only shares the same set once. So if I start with [1 2 3 4] [5 6 7 8] [...], all consecutive partitions cannot hold e.g. [1 2 X X] or [X X 1 3]. sets...

Generating Balls in Boxes

Given two sorted vectors a and b, find all vectors which are sums of a and some permutation of b, and which are unique once sorted. You can create one of the sought vectors in the following way: Take vector a and a permutation of vector b. Sum them together so c[i]=a[i]+b[i]. Sort c. I'm interested in finding the set of b-permutatio...

Generating all Possible Combinations

Hi everyone, Given 2 arrays Array1 = {a,b,c...n} and Array2 = {10,20,15....x} .. how can i generate all possible combination as Strings a(i) b(j) c(k) n(p) where 1 <= i <= 10, 1 <= j <= 20 , 1 <= k <= 15, .... 1 <= p <= x such as a1 b1 c1 .... n1 a1 b1 c1..... n2 ...... ...... a10 b20 c15 nx (last combination) So in all t...

generating permutations with repetitions in python

I know about itertools, but it seems it can only generate permutations without repetitions. for example, I'd like to generate all possible dice rolls for 2 dice. So I need all permutations of size 2 of [1, 2, 3, 4, 5, 6] including repetitions: (1, 1), (1, 2), (2, 1)... etc If possible I don't want to implement this from scratch ...

Help modifying recursive function

Given a canvas, let's say 10x10, and given 3 rectangles/squares. Canvas = 10x10 Rectangle 1 = 2x2 Rectangle 2 = 3x3 Rectangle 3 = 2x4 I've created a recursive function that loops every position of every rectangle on the canvas, and it works fine. (I've included the function below incase anyone wants to see it but I don't think it's n...

Compute rank of a combination?

I want to pre-compute some values for each combination in a set of combinations. For example, when choosing 3 numbers from 0 to 12, I'll compute some value for each one: >>> for n in choose(range(13), 3): print n, foo(n) (0, 1, 2) 78 (0, 1, 3) 4 (0, 1, 4) 64 (0, 1, 5) 33 (0, 1, 6) 20 (0, 1, 7) 64 (0, 1, 8) 13 (0, 1, 9) 24 (0, 1, 10...

Counting Treaps

Consider the problem of counting the number of structurally distinct binary search trees: Given N, find the number of structurally distinct binary search trees containing the values 1 .. N It's pretty easy to give an algorithm that solves this: fix every possible number in the root, then recursively solve the problem for the left a...

combinations/permutations with no repeats across groupings

Hi all - I'm looking for C or Python code to implement either of the two pseudocode functions: function 1: list1 = [0,1,2] #any list of single-integer elements list2 = [0,3,4] list3 = [0,2,4] function1(list1, list2, list3) >>> (0,3,2),(0,3,4),(0,4,2),(1,0,2),(1,0,4),(1,3,0),(1,3,2),(1,3,4), (1,4,0),(1,4,2),(2,0,4),(2,3,0),(2,3,4...

Algorithm to find all permutations of n characters

Possible Duplicates: Generating permutations lazily How to generate all permutations of a list in Python Algorithm to generate all possible permutations of a list? I didn't think this could be tough, but I'm having problem to come up with an algorithm to find all variations of n characters. We can assume each character onl...

How many combinations of k neighboring pixels are there in an image?

I suck at math, so I can't figure this out: how many combinations of k neighboring pixels are there in an image? Combinations of k pixels out of n * n total pixels in the image, but with the restriction that they must be neighbors, for each k from 2 to n * n. I need the sum for all values of k for a program that must take into account th...

Algorithm to find the simplest combination of integers that has not yet been used

I am looking for an algorithm for finding the simplest combination of integers from 0 to 5 (that is the one that consists of the fewest number of integers) that has not yet been used (the used combinations are in a list). The order does matter and the combinations should be returned in a list. For example, the list with the used number...

Getting all possible permutations from a list of numbers

I'm looking for an efficient way to achieve this: you have a list of numbers 1.....n (typically: 1..5 or 1..7 or so - reasonably small, but can vary from case to case) you need all combinations of all lengths for those numbers, e.g. all combinations of just one number ({1}, {2}, .... {n}), then all combinations of two distinct numbers ...

Generating tuples modulo index

I am looking for an algorithm (or a C-like implementation, no itertools available) which generates all tuples [a_0 a_1 ... a_(n-1)] such that 0 <= a_i <= i + 1. Pointers to literature are also welcome. ...

Calculate n-ary Cartesian Product

Given two lists, I can produce a list of all permutations the Cartesian Product of these two lists: permute :: [a] -> [a] -> [[a]] permute xs ys = [ [x, y] | x <- xs, y <- ys ] Example> permute [1,2] [3,4] == [ [1,3], [1,4], [2,3], [2,4] ] How do I extend permute so that instead of taking two lists, it takes a list (length n) of list...

Number of all possible groupings of a set of values?

I want to find a combinatorial formula that given a certain number of integers, I can find the number of all possible groupings of these integers (such that all values belong to a single group) Say I have 3 integers, 1, 2, 3 There would be 5 groupings: 1 2 3 1|2|3| 1 2|3 1|2 3 2|1 3 I have calculated these computationally for N = 3 t...

Counting placing items in bins according to boolean restrictions

We have a pool of items. Each item has a set of characteristics. Each characteristic is a string, integer, or floating-point number. Each item is also of a particular type. Types are hierarchical. We also have a set of bins. Each bin has a requirement, which is a boolean expression comprised of atomic expressions regarding item cha...

Modelling combinatorial optimization? problem

I've been unable to match this problem into some canonical one, and I would like some guides to build/use an algorithm and solve it. Description is as follows: We have some people who want breakfast. Each one may order any number of coffee, juice and toast. We accumulate the order for all the group. InitialOrder = { C1, J1, T1 } wit...

Calculating Binomial Coefficient (nCk) for large n & k

Hi everyone! I just saw this question and have no idea how to solve it. can you please provide me with algorithms , C++ codes or ideas? This is a very simple problem. Given the value of N and K, you need to tell us the value of the binomial coefficient C(N,K). You may rest assured that K <= N and the maximum value of N is 1,000,000,...

Generate all possible combinations of several columns in .NET

I have like 4 columns in a file, each column contains a number of values, more or less. I need to get all possible combinations, while order of columns and number of sections in resulting strings should remain the same. E.g. first column contains numbers ranging 1-100, second: letters a-z, and the third one is also numeric, I would get ...

Choosing subsets of a set such that the subsets satisfy a global constraint

We have a set of items I = {i_1, i_2, ..., i_n}. Each of these items has what we call a p value, which is some real number. We want to choose a subset of I, call it I', of size m (for some m with 1 <= m <= n) such that the average of the p values of the items in I' falls within some specified range, [p_l, p_u]. (For example, we might ...