combinatorics

List all k-tuples with entries summing to n, ignoring rotations

强颜欢笑 提交于 2019-12-07 08:55:24
问题 Is there an efficient algorithm for finding all sequences of k non-negative integers that sum to n , while avoiding rotations (completely, if possible)? The order matters, but rotations are redundant for the problem I'm working on. For example, with k = 3 and n = 3, I would want to get a list like the following: (3, 0, 0), (2, 1, 0), (2, 0, 1), (1, 1, 1). The tuple (0, 3, 0) should not be on the list, since it is a rotation of (3, 0, 0). However, (0, 3, 0) could be in the list instead of (3,

possible distributions and their probabilities after putting identical items into anonymous buckets

只愿长相守 提交于 2019-12-07 08:47:35
问题 Apologies if the answer to this is readily found elsewhere. My math and stats are weak and thus I don't even know the search terms for what I'm trying to do . . . I have b anonymous indistinguishable buckets into which I'm putting i identical items. I want to know all possible distributions and their probabilities. For example, if I have 3 buckets and 3 items, the answer I want is: [3,0,0] -> 1/9 [2,1,0] -> 6/9 [1,1,1] -> 2/9 Notice that the buckets are anonymous and thus I want identical

Check if two nested lists are equivalent upon substitution

这一生的挚爱 提交于 2019-12-07 07:56:53
问题 For some context, I'm trying to enumerate the number of unique situations that can occur when calculating the Banzhaf power indices for four players, when there is no dictator and there are either four or five winning coalitions. I am using the following code to generate a set of lists that I want to iterate over. from itertools import chain, combinations def powerset(iterable): s = list(iterable) return chain.from_iterable(map(list, combinations(s, r)) for r in range(2, len(s)+1)) def

How can I parallelize combn()?

别说谁变了你拦得住时间么 提交于 2019-12-07 07:01:45
问题 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

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

↘锁芯ラ 提交于 2019-12-07 02:45:03
问题 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

Generate valid combinations of numbers in array of digits

可紊 提交于 2019-12-06 22:32:33
I’m trying to generate all valid combinations of numbers from an array of digits. Let’s assume we have the following: let arr = [1, 2, 9, 4, 7]; We need to output something like this: 1 2 9 4 7 1 2 9 47 1 2 94 7 1 2 947 1 29 4 7 1 29 47 1 294 7 1 2947 12 9 4 7 12 9 47 12 94 7 12 947 129 4 7 129 47 1294 7 12947 An invalid number would be 91, 497, 72 and so on. I tried this but I’m not satisfied with the result: const combination = (arr) => { let i, j, temp; let result = []; let arrLen = arr.length; let power = Math.pow; let combinations = power(2, arrLen); for (i = 0; i < combinations; i += 1)

Rank and unrank Combination with constraints

天涯浪子 提交于 2019-12-06 13:21:18
问题 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? 回答1:

Python itertools.combinations() memory problems

喜欢而已 提交于 2019-12-06 12:03:04
I'm processing a huge number of combinations of items (from League of Legends), about 72 million, all of which are fed into a function that calculates how beneficial they are. We're trying to find the best possible combination. Ignoring the fact that there might be better ways, algorithmically speaking, to do this, can anyone tell me why I'm getting a memory error? allpossiblei = itertools.combinations(items.keys(),5) maxc = 0 i = 0 for combo in allpossiblei: icombo = [items[name] for name in combo] res, tcost = calcStats(icombo, 0.658,100,100) if res > maxc : maxc = res print str(res) + " " +

Combinatorics: Building 10 groups of 100 elements while elements remain sorted

风流意气都作罢 提交于 2019-12-06 10:59:20
问题 I've got a problem concerning combinatorics. Unfortunately, I can't describe it abstractly so I try to explain it as a story. :) Problem: There are 100 children on the schoolyard. They all have unique heights, assuming the values are 100-199cm. You want to build 10 groups, each consisting of 1-99 children. How can you build all the groups while the children must be sorted by their height? I need all possible solutions for these groups since it isn't hard to find one constellation. Short and

Creating every possible team combination — combinatorial optimization

时光总嘲笑我的痴心妄想 提交于 2019-12-06 09:46:38
问题 Apologies if the title is not an accurate description of what I'm doing. I am trying to construct every possible hypothetical team for a fantasy sports competition. This means combining all available players, each of whom has characteristics like the team they are on, their position, and their salary, which limits how many can be on a single team. The trouble I am having is finding a memory efficient way to combine them all. I made an example dataset: player_pool <- data.frame(id = seq(1,30),