combinatorics

python: Generating integer partitions

半腔热情 提交于 2019-11-30 07:14:08
I need to generate all the partitions of a given integer. I found this algorithm by Jerome Kelleher for which it is stated to be the most efficient one: def accelAsc(n): a = [0 for i in range(n + 1)] k = 1 a[0] = 0 y = n - 1 while k != 0: x = a[k - 1] + 1 k -= 1 while 2*x <= y: a[k] = x y -= x k += 1 l = k + 1 while x <= y: a[k] = x a[l] = y yield a[:k + 2] x += 1 y -= 1 a[k] = x + y y = x + y - 1 yield a[:k + 1] reference: http://homepages.ed.ac.uk/jkellehe/partitions.php By the way, it is not quite efficient. For an input like 40 it freezes nearly my whole system for few seconds before

Picking unordered combinations from pools with overlap

旧城冷巷雨未停 提交于 2019-11-30 05:57:30
I have pools of values and I would like to generate every possible unordered combination by picking from certain pools. For example, I wanted to pick from pool 0, pool 0, and pool 1: >>> pools = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] >>> part = (0, 0, 1) >>> list(product(*(pools[i] for i in part))) [(1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 2), (2, 3, 3), (2, 3, 4), (3, 1, 2), (3, 1, 3), (3, 1, 4), (3, 2, 2), (3, 2, 3), (3, 2, 4), (3, 3, 2), (3, 3, 3), (3, 3, 4)] This

How can I get a list of all possible partitions of a vector in R?

被刻印的时光 ゝ 提交于 2019-11-30 05:42:22
问题 Suppose I have an R vector of unique elements such as x <- c(1,2,3,4,5) . Is there a function to give me a list of all possible partitions of this vector x ? I guess each partition would be a list of vectors, where each element in x belongs to one of the vectors. I want all possible partitions into any number of sets of any size. (I think the number of such partitions is something like 2^n * n! , where n is the number of unique elements. I will probably not be using this function on vectors

An algorithm for randomly generating integer partitions of a particular length, in Python?

放肆的年华 提交于 2019-11-30 05:27:30
问题 I've been using the random_element() function provided by SAGE to generate random integer partitions for a given integer ( N ) that are a particular length ( S ). I'm trying to generate unbiased random samples from the set of all partitions for given values of N and S . SAGE's function quickly returns random partitions for N (i.e. Partitions(N).random_element() ). However, it slows immensely when adding S (i.e. Partitions(N,length=S).random_element() ). Likewise, filtering out random

Calculate possible “snake” passwords

强颜欢笑 提交于 2019-11-30 03:09:36
问题 We all know the new password screens on mobile devices. It consists of a matrix of dots, that are to be connected. A unique password is a vector of points. The points can be connected to themselves with the following restrictions: A point can be connected to only 1 other point A line will be forced to connect to a closer point if the destination point and the free point are on the same line. An example: Since the middle point was not connected before, There was no way to connect the top point

Finding unique permutations efficiently

十年热恋 提交于 2019-11-30 02:56:12
问题 I have the following problem. I need to compute the permutations of a set; however, the set may contain two elements which are the same and therefore cause repeated permutations. For example: Given the set [ 0 0 1 2 ] , the permutations include these possibilities: 1 2 0 0 1 2 0 0 However, I would like to avoid identical permutations such as these. In MATLAB I can simply do this: unique(perms([ 0 0 1 2 ]), 'rows') But the problem here is efficiency - I am doing this repeatedly in a huge for

Maximize sum of table where each number must come from unique row and column

喜夏-厌秋 提交于 2019-11-30 02:24:54
Suppose we have a table of numbers like this (we can assume it is a square table): 20 2 1 3 4 5 1 14 8 9 15 12 17 17 11 16 1 1 15 18 20 13 15 5 11 Your job is to calculate the maximum sum of n numbers where n is the number of rows or columns in the table. The catch is each number must come from a unique row and column. For example, selecting the numbers at (0,0), (1,1), (2,2), (3,3), and (4,4) is acceptable, but (0,0), (0,1), (2,2), (3,3), and (4,4) is not because the first two numbers were pulled from the same row. My (laughable) solution to this problem is iterating through all the possible

Algorithm to determine coin combinations

有些话、适合烂在心里 提交于 2019-11-30 02:11:12
I was recently faced with a prompt for a programming algorithm that I had no idea what to do for. I've never really written an algorithm before, so I'm kind of a newb at this. The problem said to write a program to determine all of the possible coin combinations for a cashier to give back as change based on coin values and number of coins. For example, there could be a currency with 4 coins: a 2 cent, 6 cent, 10 cent and 15 cent coins. How many combinations of this that equal 50 cents are there? The language I'm using is C++, although that doesn't really matter too much. edit: This is a more

Number of n-element permutations with exactly k inversions

别来无恙 提交于 2019-11-30 01:57:49
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 with exactly 1 inversion: (1, 2, 4, 3) (1, 3, 2, 4) (2, 1, 3, 4) In the first permutation, 4 > 3 and the

How to write combinatorics function in postgres?

纵然是瞬间 提交于 2019-11-29 20:42:45
问题 I have a PostgreSQL table of this form: base_id int | mods smallint[] 3 | {7,15,48} I need to populate a table of this form: combo_id int | base_id int | mods smallint[] 1 | 3 | 2 | 3 | {7} 3 | 3 | {7,15} 4 | 3 | {7,48} 5 | 3 | {7,15,48} 6 | 3 | {15} 7 | 3 | {15,48} 8 | 3 | {48} I think I could accomplish this using a function that does almost exactly this, iterating over the first table and writing combinations to the second table: Generate all combinations in SQL But, I'm a Postgres novice