combinatorics

How to Generate all k-elements subsets from an N-elements set recursively in Java

南楼画角 提交于 2019-12-12 13:07:43
问题 So I am stuck with this problem of trying to find all k-elements subsets from a given N-elements set. I know what the total number of k-subsets is using the formula C(n,k)=C(n-1, k-1)+C(n-1, k) and I also have an idea how to do it in a iterative manner, but I get stuck when I try to think of a recursive solution. Can anyone give me a hint? Thanks! 回答1: For each element of the set, take that element, then add in turn to that all (k-1) subsets of the remaining N-1 element set. "It was a dark

Generating all Possible Combinations

二次信任 提交于 2019-12-12 12:26:21
问题 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 total number of combination = product of elements of array2 = (10 X 20 X 15 X ..X x) Similar to a Cartesian product, in which the second array defines the upper limit for each element in

Generate cartesian product in decreasing sum order

好久不见. 提交于 2019-12-12 11:07:59
问题 Given n sorted lists A1, A2, ..., An of integers in decreasing order, is there an algorithm to efficiently generate all elements of their cartesian product in decreasing tuple sum order ? For example, n=3 A1 = [9, 8, 0] A2 = [4, 2] A3 = [5, 1] The expected output would be the cartesian product of A1xA2xA3 in the following order: combination sum 9, 4, 5 18 8, 4, 5 17 9, 2, 5 16 8, 2, 5 15 9, 4, 1 14 8, 4, 1 13 9, 2, 1 12 8, 2, 1 11 0, 4, 5 9 0, 2, 5 7 0, 4, 1 5 0, 2, 1 3 回答1: If the problem

Good simple algorithm for generating necklaces in Scheme?

自闭症网瘾萝莉.ら 提交于 2019-12-12 08:48:31
问题 A k-ary necklace of length n is an ordered list of length n whose items are drawn from an alphabet of length k, which is the lexicographically first list in a sort of all lists sharing an ordering under rotation. Example: (1 2 3) and (1 3 2) are the necklaces of length 3 from the alphabet {1 2 3}. More info: http://en.wikipedia.org/wiki/Necklace_(combinatorics) I'd like to generate these in Scheme (or a Lisp of your choice). I've found some papers... Savage - A New Algorithm for Generating

Algorithm to group items in groups of 3

折月煮酒 提交于 2019-12-12 08:36:03
问题 I am trying to solve a problem where I have pairs like: A C B F A D D C F E E B A B B C E D F D and I need to group them in groups of 3 where I must have a triangule of matching from that list. Basically I need a result if its possible or not to group a collection. So the possible groups are ( ACD and BFE ), or ( ABC and DEF ) and this collection is groupable since all letters can be grouped in groups of 3 and no one is left out. I made a script where I can achieve this for small ammounts of

How to loop through all the combinations of e.g. 48 choose 5 [duplicate]

烂漫一生 提交于 2019-12-12 07:56:53
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to iteratively generate k elements subsets from a set of size n in java? I want to build my own poker hand evaluator but am having trouble with a particular part. If two players get dealt a two card hand, then there will be 48 cards left in the pack. In Texas Hold'em a further 5 possible community cards are then dealt (this is called the board). I want to enumerate / loop through all the 48 choose 5 possible

iterate matrix without nested loop

橙三吉。 提交于 2019-12-12 04:08:56
问题 I want to iterate the entries above the main diagonal of a square matrix of size n without using nested loops. For example if the matrix M is of size n=3, then there are choose(3,2)=3 entries above the diagonal. where choose is the binomial coefficient. for(i=1 to choose(n,2)) row = getRow(i) col = getCol(i) M[row,col] = some value I have not been able to come up with a formula for getting row and col based on index i. For example: for a matrix of size = 3 and indexes starting at 1, i=1

Stream all unordered/shuffled unique permutation of elements in a nested list (list of list) in Python?

五迷三道 提交于 2019-12-12 04:04:10
问题 I have two-level nested list like following. [[0, 1], [2], [3, 4], [5, 5], [6], [7], [8], [9], [10, 11], [12]] I want to generate all 8 unique permutations of this nested list, but my application absolutely needs the output to be (pseudo-)randomized and unordered. Usually, permutation strategies produce the permutations in order, but I want to be able to produce all permutations out of order. Moreover, this MUST be done through some generator, as the nested list can be very long, and number

Generating the combinations of a “choose” operation

这一生的挚爱 提交于 2019-12-12 01:43:16
问题 I want to perform a variation of a "6 choose 2" operation. I have written the following code. public void choosePatterns(){ String[] data = {"1", "2", "3", "4", "5", "6"}; String[] originalPattern = new String[15]; int index = 0; for(int i = 0; i < (6-1); i++) { for(int j = i+1; j < 6; j++) { System.out.println(i + "," +j); } } } My code so far generates all the possible combinations of "6 choose 2." However, I would like to vary this and print all the remaining numbers. So far instance, if

Loop a 2 letter range

人盡茶涼 提交于 2019-12-11 22:15:53
问题 I'm trying to achieve a loop over all possible 2 letter combinations. Something like foreach(range(aa,zz) as $i) {...} My current solution is: foreach (range(a, z) as $first) { foreach (range(a, z) as $second) { //all 2 letter combinations echo $first.$second; } } This makes me worry that if I needed all possible 10 letter combinations, there would be 10 loops involved. Is there a better way to achieve this? 回答1: You could loop over letters using a simple for loop : for ($letter = 'aa';