combinatorics

How can I parallelize combn()?

夙愿已清 提交于 2019-12-05 11:37:27
The function combn() generates all combinations of the elements of x taken m at a time. It is very fast and efficient for nCm small (where n is the number of elements of x) but it quickly runs out of memory. For example: > combn(c(1:50), 12, simplify = TRUE) Error in matrix(r, nrow = len.r, ncol = count) : invalid 'ncol' value (too large or NA) I would like to know if the function combn() can be modified such that it generates only k chosen combinations. Let's call this new function chosencombn(). Then we would have: > combn(c("a", "b", "c", "d"), m=2) [,1] [,2] [,3] [,4] [,5] [,6] [1,] "a" "a

Generating a list of repetitions regardless of the order

寵の児 提交于 2019-12-05 08:50:50
I want to generate combinations that associate indices in a list with "slots". For instance, (0, 0, 1) means that 0 and 1 belong to the same slot while 2 belongs to an other. (0, 1, 1, 1) means that 1, 2, 3 belong to the same slot while 0 is by itself. In this example, 0 and 1 are just ways of identifying these slots but do not carry information for my usage. Consequently, (0, 0, 0) is absolutely identical to (1, 1, 1) for my purposes, and (0, 0, 1) is equivalent to (1, 1, 0) . The classical cartesian product generates a lot of these repetitions I'd like to get rid of. This is what I obtain

Find all subsets of size N in an array using Ruby

╄→尐↘猪︶ㄣ 提交于 2019-12-05 08:47:19
Given an array ['a', 'b', 'c', 'd', 'e', 'f'] , how would I get a list of all subsets containing two, three, and four elements? I'm quite new to Ruby (moving from C#) and am not sure what the 'Ruby Way' would be. basicxman Check out Array#combination Then something like this: 2.upto(4) { |n| array.combination(n) } Tweaking basicxman's a little bit: 2.upto(4).flat_map { |n| array.combination(n).to_a } #=> [["a", "b"], ["a", "c"], ["a", "d"], ..., ["c", "d", "e", "f"]] 来源: https://stackoverflow.com/questions/7356030/find-all-subsets-of-size-n-in-an-array-using-ruby

Distribution of balls into 'bins with given capacities' using Dynamic Programming

痴心易碎 提交于 2019-12-05 06:40:39
I was wondering how to solve such a problem using DP. Given n balls and m bins, each bin having max. capacity c1, c2,...cm. What is the total number of ways of distributing these n balls into these m bins. The problem I face is How to find a recurrence relation (I could when the capacities were all a single constant c). There will be several test cases, each having its own set of c1,c2....cm. So how would the DP actually apply for all these test cases because the answer explicitly depends on present c1,c2....cm, and I can't store (or pre-compute) the answer for all combinations of c1,c2....cm.

Generating all possible combinations

烈酒焚心 提交于 2019-12-05 06:31:26
问题 I'm writing some code and ended up with this problem. I have N products and I have to form all possible combinations of these products, form a product catalog and find some properties, like price. In order to do this I have to form the catalog of products from the given products (exhaustively, but no duplicates allowed). Is there a standardized algorithm for doing this? Note that the catalogs can contain any positive number of products. 回答1: The first few sections of The Art of Computer

Every possible combination of X split into N stacks

蓝咒 提交于 2019-12-05 04:40:42
I am sure this problem has a formal name, and knowing that name would probably help me find the solution, but I don't know it, and wording the problem for Google keeps pointing me to the Knapsack Problem , which isn't the same thing. I want to take some value X and find every possible combination of splitting that value into N stacks of whole integers. In case my wording is confusing, here is an example of X = 4, N = 3 Stack -> 1 | 2 | 3 | ---------------------- #1-----> 4 | 0 | 0 | ---------------------- #2-----> 3 | 1 | 0 | ---------------------- #3-----> 2 | 1 | 1 | ----------------------

Number of combinations in configurator

穿精又带淫゛_ 提交于 2019-12-05 01:59:26
问题 I have been asked to program a routine to decide the number of possible combinations in a product configurator. The configurator is really simple. Even though it has more features than this, it can be modeled as several "radio groups" (like the UI control) where one of n options has to be selected. The only kind of constraints that can be used is rules that says if one option is selected another option can not be selected. So what I want to do is to calculate the number of different products

random and unique subsets generation

↘锁芯ラ 提交于 2019-12-05 01:38:03
问题 Lets say we have numbers from 1 to 25 and we have to choose sets of 15 numbers. The possible sets are, if i'm right 3268760. Of those 3268760 options, you have to generate say 100000 What would be the best way to generate 100000 unique and random of that subsets? Is there a way, an algorithm to do that? If not, what would be the best option to detect duplicates? I'm planning to do this on PHP but a general solution would be enough, and any reference not to much 'academic' (more practical)

Determine list of all possible products from a list of integers in Python

橙三吉。 提交于 2019-12-04 22:08:43
In Python 2.7 I need a method that returns all possible products of a list or tuple of int . Ie. if input is (2, 2, 3, 4) , then I'd want a output like (3, 4, 4) , 2 * 2 = 4 (2, 4, 6) , 2 * 3 = 6 (2, 3, 8) , 2 * 4 = 8 (3, 4, 4) , 2 * 2 = 4 (2, 2, 12) , 3 * 4 = 12 (2, 24) , 2 * 3 * 4 = 24 (3, 16) , 2 * 2 * 4 = 16 (4, 12) , 2 * 2 * 3 = 12 (48) , 2 * 2 * 3 * 4 = 48 wrapped up in a list or tuple . I figure that a nice implementation is probably possible using combinations from itertools , but I'd appreciate any help. Note that I am only interested in distinct lists, where order of int plays no

Rank and unrank Combination with constraints

隐身守侯 提交于 2019-12-04 19:20:40
I want to rank and unrank combinations with an Element distance constraint. Selected elements cannot repeated. For example: n := 10 elements choose from k := 5 elements being choosen d := 3 max distance between 2 choosen elements 1,3,5,8,9 matches the constraint 1,5,6,7,8 dont matches the constraint How can ranking the combination with given distance constraint, where 1,2,3,4,5 is smaller than 1,2,3,4,6 ? Is there a way do the ranking without compute the combinations with smaller Rank? You can do this by first creating and populating a two-dimensional array, which I will call NVT for "number