combinatorics

Nth Combination

喜夏-厌秋 提交于 2019-12-17 04:00:53
问题 Is there a direct way of getting the Nth combination of an ordered set of all combinations of nCr? Example: I have four elements: [6, 4, 2, 1]. All the possible combinations by taking three at a time would be: [[6, 4, 2], [6, 4, 1], [6, 2, 1], [4, 2, 1]]. Is there an algorithm that would give me e.g. the 3rd answer, [6, 2, 1], in the ordered result set, without enumerating all the previous answers? 回答1: Note you can generate the sequence by recursively generating all combinations with the

Generating permutations lazily

試著忘記壹切 提交于 2019-12-17 02:54:53
问题 I'm looking for an algorithm to generate permutations of a set in such a way that I could make a lazy list of them in Clojure. i.e. I'd like to iterate over a list of permutations where each permutation is not calculated until I request it, and all of the permutations don't have to be stored in memory at once. Alternatively I'm looking for an algorithm where given a certain set, it will return the "next" permutation of that set, in such a way that repeatedly calling the function on its own

Generating permutations with repetitions

社会主义新天地 提交于 2019-12-17 02:13:05
问题 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 回答1: You are looking for the Cartesian Product. In mathematics, a Cartesian product (or product set) is the direct product of two sets. In your case, this would be {1,

generate a list of combinations/sets of numbers with limited supplies [closed]

允我心安 提交于 2019-12-13 18:57:51
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 8 years ago . Say I have 1 red ball, 1 blue ball, 2 yellow balls, and 3 green balls, for a total of 7 balls. I want to select 3 balls from the group of seven. How many ways can I do this? When I counted manually I got 11 combinations/sets. ie 123 124 133 134 144 233 234 244 334 344 444 What is an efficient way to generate and

All Possible combinations in C

别等时光非礼了梦想. 提交于 2019-12-13 01:15:56
问题 I'm trying to find a efficient algorithm in C, which provides me all combinations of a given charset. The algorithm should not recursive. At last the number of digits should be flexible. For example: char set[] = "a1"; -> a1 aa 1a 11 I've only found a Perl solution, but it uses substr() . I think that wasn't that fast performance-wise. For most algorithms in C, I've found were only permutations... A article in a german C++ forum claims, that C++-STL Solutions are faster than "raw" recursive

Calculate Combination based on position

杀马特。学长 韩版系。学妹 提交于 2019-12-13 00:14:31
问题 I have combinations like this: 1,2,3,4 //index 0 1,2,3,5 //index 1 1,2,3,6 //index 2 and so on until 7,8,9,10 So this will be n=10 k=4 from combinatorics How calculate combination by index For example when my index==1 myCmb = func(index) returns 1,2,3,5 this is example i need this for bigest numbers, and for more params and without (if this possible) many loops i find something like this to obtain position: http://answers.yahoo.com/question/index?qid=20110320070039AA045ib I want now reverse

Generate restricted weak integer compositions (or partitions) of an integer n into k parts in Python

送分小仙女□ 提交于 2019-12-12 19:19:47
问题 (Re-posting, as I did not get any response to my previous post) I am trying to write a Python code to generate weak integer compositions (partitions) of a number 'n' into 'k' parts but with a MINIMUM and MAXIMUM value constraint on each partition (see example given below). Also, the partitions have to be generated in lexicographic order. I have found some related posts but have not been able to implement it. Any help will be appreciated. Example: Possible integer partitions for n=5 in k=3

Finding the number of integer partitions given a total, a number of parts, and a maximum summand

你。 提交于 2019-12-12 19:15:02
问题 I'm looking for the number of integer partitions for a total N, with a number of parts S, having a maximum part that is exactly X, without enumerating all of them. For example: all partitions of 100 that have 10 parts and 42 as the largest part. I've found no theorems or partitioning identities that address this question and I suspect that is a non-trivial problem that is not easily derived from known theorems (e.g. Nijenhuis and Wilf 1978, Andrews et al. 2004, Bona 2006): For example: The

A single elimination tournament - number of possible combinations

ⅰ亾dé卋堺 提交于 2019-12-12 16:40:56
问题 What are the number of combinations in which 8 persons taking part in a single elimination tornament play? Total no of matches played would be 7 but I also need the number of combinations that can for this set 回答1: If it doesn't matter where in the tree a player starts, but only which opponents he/she fights, and how long he/she gets, we can say that the left player always wins and then just calculate the number of ways to create the bottom most row, which is 8! 40320. The first possibility:

R - generate all possible pairwise combinations of binary vectors

╄→гoц情女王★ 提交于 2019-12-12 15:11:52
问题 I am looking for a smart way to generate all pairwise combinations of two vectors of length n, where only one value is not zero. For now I am doing something quite desperate with loops through each combination with: n <- 3; z <- rep(0,n); m <- apply(combn(1:n,1),2,function(k) {z[k]=1;z}) but there must be a better way without loops? This is what I'm after for example for n=3: [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [1,] 1 0 0 [2,] 0 0 1 [1,] 0 1 0 [2,] 1 0 0 [1,] 0 1 0 [2,] 0 0 1 [1,] 0 0 1 [2,]