combinatorics

Algorithm to get all the combinations of size n from an array (Java)? [closed]

六月ゝ 毕业季﹏ 提交于 2019-11-26 15:28:02
问题 Right now I'm trying to write a function that takes an array and an integer n, and gives a list of each size n combination (so a list of int arrays). I am able to write it using n nested loops, but this only works for a specific size of subset. I can't figure out how to generalize it to work for any size of combination. I think I need to use recursion? This is the code for all combinations of 3 elements, and I need an algorithm for any number of elements. import java.util.List; import java

generating permutations with repetitions in python

假如想象 提交于 2019-11-26 15:23:36
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 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, 2, 3, 4, 5, 6} x {1, 2, 3, 4, 5, 6} . itertools can help you there: import itertools x = [1, 2, 3, 4, 5, 6

Creating all possible k combinations of n items in C++

只谈情不闲聊 提交于 2019-11-26 15:05:31
There are n people numbered from 1 to n . I have to write a code which produces and print all different combinations of k people from these n . Please explain the algorithm used for that. I assume you're asking about combinations in combinatorial sense (that is, order of elements doesn't matter, so [1 2 3] is the same as [2 1 3] ). The idea is pretty simple then, if you understand induction / recursion: to get all K -element combinations, you first pick initial element of a combination out of existing set of people, and then you "concatenate" this initial element with all possible combinations

Generating permutations lazily

家住魔仙堡 提交于 2019-11-26 14:53:37
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 output will cycle through all permutations of the original set, in some order (what the order is doesn't

Concatenate values of n arrays in php

痞子三分冷 提交于 2019-11-26 12:47:42
问题 I have an unknown number of arrays, each containing an unknown number of words. I want to concatenate the values from each list so that all possible variations of the words are stored to a final array. For example, if array 1 contains: dog cat and array 2 contains: food tooth and array 3 contains: car bike I\'d like the output to be: dog food car dog food bike dog tooth car dog tooth bike cat food car cat food bike cat tooth car cat tooth bike There could be more than 3 lists, and each list

Generating all combinations with repetition using MATLAB

 ̄綄美尐妖づ 提交于 2019-11-26 12:46:34
问题 How do I create all k-combinations with repetitions of a given set (also called k-multicombinations or multisubsets ) using MATLAB? This is similar to the cartesian product, but two rows that only differ by their sorting should be considered the same (e.g. the vectors [1,1,2]=~=[1,2,1] are considered to be the same), so generating the cartesian product and then applying unique(sort(cartesianProduct,2),\'rows\') should yield the same results. Example: The call nmultichoosek(1:n,k) should

Permute all unique enumerations of a vector in R

只愿长相守 提交于 2019-11-26 12:27:45
问题 I\'m trying to find a function that will permute all the unique permutations of a vector, while not counting juxtapositions within subsets of the same element type. For example: dat <- c(1,0,3,4,1,0,0,3,0,4) has factorial(10) > 3628800 possible permutations, but only 10!/(2!*2!*4!*2!) factorial(10)/(factorial(2)*factorial(2)*factorial(2)*factorial(4)) > 18900 unique permutations when ignoring juxtapositions within subsets of the same element type. I can get this by using unique() and the

Cartesian product of 2 lists in Haskell

纵然是瞬间 提交于 2019-11-26 12:19:55
I wish to produce the Cartesian product of 2 lists in Haskell, but I cannot work out how to do it. The cartesian product gives all combinations of the list elements: xs = [1,2,3] ys = [4,5,6] cartProd :: [a] -> [b] -> [(a,b)] cartProd xs ys ==> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)] This is not an actual homework question and is not related to any such question, but the way in which this problem is solved may help with one I am stuck on. This is very easy with list comprehensions. To get the cartesian product of the lists xs and ys , we just need to take the tuple (x,y) for

all combinations of k elements out of n

随声附和 提交于 2019-11-26 12:14:06
问题 Can somebody provide me a link or pseudocode of a function for finding all combinations of k elements out of n? possibly in STL. I don\'t need to compute n choose k, I need to list all vectors of numbers of size k. Thanks 回答1: In C++ given the following routine: template <typename Iterator> inline bool next_combination(const Iterator first, Iterator k, const Iterator last) { /* Credits: Thomas Draper */ if ((first == last) || (first == k) || (last == k)) return false; Iterator itr1 = first;

all permutations of a binary sequence x bits long

点点圈 提交于 2019-11-26 11:02:00
问题 I would like to find a clean and clever way (in python) to find all permutations of strings of 1s and 0s x chars long. Ideally this would be fast and not require doing too many iterations... So, for x = 1 I want: [\'0\',\'1\'] x =2 [\'00\',\'01\',\'10\',\'11\'] etc.. Right now I have this, which is slow and seems inelegant: self.nbits = n items = [] for x in xrange(n+1): ones = x zeros = n-x item = [] for i in xrange(ones): item.append(1) for i in xrange(zeros): item.append(0) items.append