combinatorics

Number of n-element permutations with exactly k inversions

浪子不回头ぞ 提交于 2019-11-30 12:03:04
问题 I am trying to efficiently solve SPOJ Problem 64: Permutations. Let A = [a1,a2,...,an] be a permutation of integers 1,2,...,n. A pair of indices (i,j), 1<=i<=j<=n, is an inversion of the permutation A if ai>aj. We are given integers n>0 and k>=0. What is the number of n-element permutations containing exactly k inversions? For instance, the number of 4-element permutations with exactly 1 inversion equals 3. To make the given example easier to see, here are the three 4-element permutations

How to get all mappings between two lists?

▼魔方 西西 提交于 2019-11-30 11:07:18
We have two lists, A and B: A = ['a','b','c'] B = [1, 2] Is there a pythonic way to build the set of all maps between A and B containing 2^n (here 2^3=8)? That is: [(a,1), (b,1), (c,1)] [(a,1), (b,1), (c,2)] [(a,1), (b,2), (c,1)] [(a,1), (b,2), (c,2)] [(a,2), (b,1), (c,1)] [(a,2), (b,1), (c,2)] [(a,2), (b,2), (c,1)] [(a,2), (b,2), (c,2)] Using itertools.product , it's possible to get all the tuples: import itertools as it P = it.product(A, B) [p for p in P] Which gives: Out[3]: [('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)] thefourtheye You can do this with itertools.product and

Find the index of a given permutation in the list of permutations in lexicographic order [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-30 10:05:32
Possible Duplicate: Given a string and permutation of the string. Find the index of this permuted string in the sorted list of the permutations of the string. This is an interview question. Let there is a list of permutations in lexicographic order. For example, 123 , 132 , 213 , 231 , 312 , 321 . Given a permutation find its index in such a list. For example, the index of permutation 213 is 2 (if we start from 0). Trivially, we can use a next_permutation algorithm to generate a next permutation in lexicographic order, but it leads to O(N!) solution, which is obviously non-feasible. Is there

How to calculate the lexicographical rank of a given permutation

僤鯓⒐⒋嵵緔 提交于 2019-11-30 09:40:23
For example there are 6 chairs in the room and there are 4 girls and 2 boys. There is 15 unique possible ways they can sit on this chairs 6!/(4!*2!)=15 . My problem is to find efficient way to calculate position of possibility they choose to sit. By position I mean following: BBGGGG - possible position #1 BGBGGG - possible position #2 BGGBGG - possible position #3 BGGGBG - possible position #4 BGGGGB - possible position #5 GBBGGG - possible position #6 GBGBGG - possible position #7 GBGGBG - possible position #8 GBGGGB - possible position #9 GGBBGG - possible position #10 GGBGBG - possible

Sum-of-Product of subsets

∥☆過路亽.° 提交于 2019-11-30 09:10:24
Is there a name for this operation? And: is there a closed-form expression? For a given set of n elements, and value k between 1 and n, Take all subsets (combinations) of k items Find the product of each subset Find the sum of all those products I can express this in Python, and do the calculation pretty easily: from operator import mul from itertools import combinations from functools import reduce def sum_of_product_of_subsets(list1, k): val = 0 for subset in combinations(list1, k): val += reduce(mul, subset) return val I'm just looking for the closed form expression, so as to avoid the loop

combination without repetition of N elements without use for..to..do

左心房为你撑大大i 提交于 2019-11-30 09:04:13
i want load in a list the combination of N number without repetition, giving to input the elements and group. For example, with 4 elements [1,2,3,4], i have for: Group 1: [1][2][3][4]; Group 2: [1,2][1,3][1,4][2,3][2,4][3,4]; Group 3: [1,2,3][1,2,4][1,3,4][2,3,4] Group 4: [1,2,3,4] Now, i have solved it using nested loop for, for example with group 2, i write: for x1 := 1 to 3 do for x2 := Succ(x1) to 4 do begin // x1, x2 // end or for group 3, i wrote: for x1 := 1 to 2 do for x2 := Succ(x1) to 3 do for x3 := Succ(x2) to 4 do begin // x1, x2, x3 // end and so for other groups. In general, if i

C# LINQ combinatorics: All Combinations of a Set without the Empty Set

痞子三分冷 提交于 2019-11-30 08:33:11
问题 I have a set of strings, and I want to find all possible combinations of the strings and add them to a list. I want to end up with a list of a list of each combination of the strings, minus the empty set. I have created a solution that does this exactly with a nested for loop. However I want to do this more elegantly, preferably with LINQ , and I am not so proficient with it because I'm still pretty new to it. The solution should have 2^n - 1 lists of combinations where n is the cardinality

R: Generating all permutations of N weights in multiples of P

偶尔善良 提交于 2019-11-30 07:42:49
问题 I need to create a function (in R) which: - given N possible variables to attribute weights to; - creates all possible permuations of weights (summing to 100%); - subject to the constraint that weights must occur in multiples of P (usually 1%) Obviously, as N and P are inversely related - i.e. I can't specify N =7, and P =0.4. However, I'd like to be able to specify integer solutions only, i.e. P =0.01. Sorry if this is a well-known problem - I'm not a math person, and I've searched using

The Assignment Problem, a NumPy function?

早过忘川 提交于 2019-11-30 07:26:09
问题 Since an assignment problem can be posed in the form of a single matrix, I am wondering if NumPy has a function to solve such a matrix. So far I have found none. Maybe one of you guys know if NumPy/SciPy has an assignment-problem-solve function? Edit: In the meanwhile I have found a Python (not NumPy/SciPy) implementation at http://software.clapper.org/munkres/. Still I suppose a NumPy/SciPy implementation could be much faster, right? 回答1: No, NumPy contains no such function. Combinatorial

Generating a very large matrix of string combinations using combn() and bigmemory package

主宰稳场 提交于 2019-11-30 07:14:55
问题 I have a vector x of 1,344 unique strings. I want to generate a matrix that gives me all possible groups of three values, regardless of order, and export that to a csv. I'm running R on EC2 on a m1.large instance w 64bit Ubuntu. When using combn(x, 3) I get an out of memory error: Error: cannot allocate vector of size 9.0 Gb The size of the resulting matrix is C1344,3 = 403,716,544 rows and three columns - which is the transpose of the result of combn() function. I thought of using the