combinatorics

Depth-first combination algorithm

隐身守侯 提交于 2019-12-25 05:18:23
问题 Let's say I have the following array of arrays: A = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h'], ['i'], ['j', 'k', 'l'] ] I want to find all possible combinations of the elements of each array with the elements of the other arrays (i.e. 'adgij' is one possibility but not 'abcde'). I can brute force it and just loop everything like this (javascript): var A = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h'], ['i'], ['j', 'k', 'l'] ], combinations, newCombinations = []; A.forEach(function(a,

Arranging group of numbers to fit Specific size total

 ̄綄美尐妖づ 提交于 2019-12-25 05:08:03
问题 I have several different numbers in a group that range in sizes and would like to calculate which group the numbers should go in based on the max size the group can be. Example of the numbers: 10,20,30,40,50,60 Example of conditions: the maximum total the numbers can add up to in a group is 60 So from the example above the answer would be: group 1 would have the numbers 10,20,30 group 2 would have the number 40 group 3 would have the number 50 group 4 would have the number 60 Is there a way

Function to generate the unique combinations of a list in Haskell

房东的猫 提交于 2019-12-25 01:04:12
问题 Is there a Haskell function that generates all the unique combinations of a given length from a list? Source = [1,2,3] uniqueCombos 2 Source = [[1,2],[1,3],[2,3]] I tried looking in Hoogle but could not find a function that did this specifically. Permutations does not give the desired result. Has anybody used a similar function before? 回答1: I don't know a predefined function either, but it's pretty easy to write yourself: -- Every set contains a unique empty subset. subsets 0 _ = [[]] --

Static sized permutations from Dynamic sized set

霸气de小男生 提交于 2019-12-25 00:22:22
问题 How can I obtain all combinations for a list where combination size need only be static eg. if the list has 4 elements then it will only need permutations of length 4 not 3, 2 and 1. I'm guessing this will need recursion. Unique combinations would be helpful but I'd like to see it in simplest (no uniqueness?) form for my puppy power. 回答1: set s = { x1, x2, x3, x4 }; array solution; permute( i ) => if( i == 0 ) => print and return; while unused elements in set => take element from set which is

How to find all possible combinations of balls? [duplicate]

瘦欲@ 提交于 2019-12-24 22:45:09
问题 This question already has answers here : Generate a matrix containing all combinations of elements taken from n vectors (4 answers) Closed last month . I need to find all possible ways to combine k amount of balls from n types of balls. Let's say if there are 3 types of balls and I want to take 2, result I want is: 1 1 1 2 1 3 2 2 2 3 3 3 I am trying to do it with the following line: unique(nchoosek(repmat(1:n, 1, n), k), 'rows') I get: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 How to do find all

Generate all lists of size n, such that each element is between 0 and m (inclusive)

依然范特西╮ 提交于 2019-12-24 16:53:23
问题 Generate all lists of size n , such that each element is between 0 and m (inclusive). There are (m+1)^n such lists. 回答1: There are two easy ways of writing the general case. One is described in the existing answer from @didierc. The alternative is recursion. For example, think about a method that takes a String as an argument: if(input string is long enough) print or store it else iterate over digit range recursive call with the digit appended to the string 回答2: This is just like enumerating

Partition and Composition (combinatorics) implementation in C++

亡梦爱人 提交于 2019-12-24 16:29:09
问题 Given a matrix of size M and N , we want to fill in each row with integer value (>=0) so that it sums up to certain value. Note that the dimension of M and N are pre-computed using certain formula, so that it is guaranteed to match the fill given the desired condition (i.e. sum_val below). This is implemented in R under Partition library. library(partitions) # In this example, we impose condition # that each rows must sum up to 2 in total # And each row has 5 columns sum_val <- 2 n <- 5 #The

Loop i and j, where i != j

流过昼夜 提交于 2019-12-24 12:15:08
问题 For example I need to get all combinations of 2-values array with numbers {0, 1, 2} where this two numbers are not the same. I get 0 1 0 2 1 0 1 2 2 0 2 1 and I ignore 0 0 1 1 2 2 Now I use for (int i= 0; i < L ; i++) { for (int j = 0; j < L; j++) { if (i!= j) { but it is very slow? Any solution for this? L will be > 4000. What am I doing is finding every combinations of splitting matrix to 4 sub-matrices example: 3 | 0 2 -8 -8 5 | 3 2 2 3 2 | 5 2 1 4 ------------------- 3 4 -1 | 4 2 -3 6 2 |

Grundy's game extended to more than two heaps

倾然丶 夕夏残阳落幕 提交于 2019-12-24 08:27:08
问题 How can In break a heap into two heaps in the Grundy's game? What about breaking a heap into any number of heaps (no two of them being equal)? 回答1: Games of this type are analyzed in great detail in the book series "Winning Ways for your Mathematical Plays". Most of the things you are looking for are probably in volume 1. You can also take a look at these links: Nimbers (Wikipedia), Sprague-Grundy theorem (Wikipedia) or do a search for "combinatorial game theory". My knowledge on this is

String permutations in lexigraphic order without inversions/reversions

▼魔方 西西 提交于 2019-12-24 07:49:35
问题 The problem: I would like to generate a list of permutations of strings in lexigraphical but excluding string inversions. For instance, if I have the following string: abc, I would like to generate the following list abc acb bac instead of the typical abc acb bac bca cab cba An alternative example would look something like this: 100 010 instead of 100 010 001 Currently, I can generate the permutations using perl, but I am not sure on how to best remove the reverse duplicates. I had thought of