combinatorics

How to calculate the index (lexicographical order) when the combination is given

陌路散爱 提交于 2019-11-26 10:54:42
问题 I know that there is an algorithm that permits, given a combination of number (no repetitions, no order), calculates the index of the lexicographic order. It would be very useful for my application to speedup things... For example: combination(10, 5) 1 - 1 2 3 4 5 2 - 1 2 3 4 6 3 - 1 2 3 4 7 .... 251 - 5 7 8 9 10 252 - 6 7 8 9 10 I need that the algorithm returns the index of the given combination. es: index( 2, 5, 7, 8, 10 ) --> index EDIT: actually I\'m using a java application that

How to generate all the permutations of a multiset?

独自空忆成欢 提交于 2019-11-26 09:54:58
问题 A multi-set is a set in which all the elements may not be unique.How to enumerate all the possible permutations among the set elements? 回答1: Generating all the possible permutations and then discarding the repeated ones is highly inefficient. Various algorithms exist to directly generate the permutations of a multiset in lexicographical order or other kind of ordering. Takaoka's algorithm is a good example, but probably that of Aaron Williams is better http://webhome.csc.uvic.ca/~haron

Number of combinations (N choose R) in C++

你。 提交于 2019-11-26 09:09:21
问题 Here I try to write a program in C++ to find NCR. But I\'ve got a problem in the result. It is not correct. Can you help me find what the mistake is in the program? #include <iostream> using namespace std; int fact(int n){ if(n==0) return 1; if (n>0) return n*fact(n-1); }; int NCR(int n,int r){ if(n==r) return 1; if (r==0&&n!=0) return 1; else return (n*fact(n-1))/fact(n-1)*fact(n-r); }; int main(){ int n; //cout<<\"Enter A Digit for n\"; cin>>n; int r; //cout<<\"Enter A Digit for r\"; cin>>r

Getting all possible combinations from a list of numbers

感情迁移 提交于 2019-11-26 07:38:57
问题 I\'m looking for an efficient way to achieve this: you have a list of numbers 1.....n (typically: 1..5 or 1..7 or so - reasonably small, but can vary from case to case) you need all combinations of all lengths for those numbers, e.g. all combinations of just one number ({1}, {2}, .... {n}), then all combinations of two distinct numbers ({1,2}, {1,3}, {1,4} ..... {n-1, n} ), then all combinations fo three of those numbers ({1,2,3}, {1,2,4}) and so forth Basically, within the group, the order

generate all partitions of a set [closed]

烈酒焚心 提交于 2019-11-26 07:38:34
问题 For a set of the form A = {1, 2, 3, ..., n} . It is called partition of the set A , a set of k<=n elements which respect the following theorems: a) the union of all the partitions of A is A b) the intersection of 2 partitions of A is the empty set (they can\'t share the same elements). For example. A = {1, 2,... n} We have the partitions: {1, 2, 3} {1, 2} {3} {1, 3} {2} {2, 3} {1} {1} {2} {3} These theoretical concepts are presented in my algorithms textbook (by the way, this subchapter is

Cartesian product of a dictionary of lists

拟墨画扇 提交于 2019-11-26 07:22:07
问题 I\'m trying to write some code to test out the Cartesian product of a bunch of input parameters. I\'ve looked at itertools , but its product function is not exactly what I want. Is there a simple obvious way to take a dictionary with an arbitrary number of keys and an arbitrary number of elements in each value, and then yield a dictionary with the next permutation? Input: options = {\"number\": [1,2,3], \"color\": [\"orange\",\"blue\"] } print list( my_product(options) ) Example output: [ {\

Fast permutation -> number -> permutation mapping algorithms

寵の児 提交于 2019-11-26 05:48:37
问题 I have n elements. For the sake of an example, let\'s say, 7 elements, 1234567. I know there are 7! = 5040 permutations possible of these 7 elements. I want a fast algorithm comprising two functions: f(number) maps a number between 0 and 5039 to a unique permutation, and f\'(permutation) maps the permutation back to the number that it was generated from. I don\'t care about the correspondence between number and permutation, providing each permutation has its own unique number. So, for

Combinations, Dispositions and Permutations in PHP

被刻印的时光 ゝ 提交于 2019-11-26 04:54:15
问题 What is the most efficient way to generate all the combinations, dispositions and permutations of an array in PHP? 回答1: Here is code to get all permutations: http://php.net/manual/en/function.shuffle.php#90615 With the code to get the power set, permutations are those of maximal length, the power set should be all combinations. I have no idea what dispositions are, so if you can explain them, that would help. 回答2: You can use this class: http://pear.php.net/package/Math_Combinatorics and use

Generate all permutations of a list without adjacent equal elements

帅比萌擦擦* 提交于 2019-11-26 04:11:18
问题 When we sort a list, like a = [1,2,3,3,2,2,1] sorted(a) => [1, 1, 2, 2, 2, 3, 3] equal elements are always adjacent in the resulting list. How can I achieve the opposite task - shuffle the list so that equal elements are never (or as seldom as possible) adjacent? For example, for the above list one of the possible solutions is p = [1,3,2,3,2,1,2] More formally, given a list a , generate a permutation p of it that minimizes the number of pairs p[i]==p[i+1] . Since the lists are large,

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

烈酒焚心 提交于 2019-11-26 04:09:58
问题 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. 回答1: 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