combinatorics

Generate all possible outcomes of k balls in n bins (sum of multinomial / categorical outcomes)

久未见 提交于 2019-11-30 18:13:19
Suppose we have n bins in which we are throwing k balls. What is a fast (i.e. using numpy/scipy instead of python code) way to generate all possible outcomes as a matrix? For example, if n = 4 and k = 3 , we'd want the following numpy.array : 3 0 0 0 2 1 0 0 2 0 1 0 2 0 0 1 1 2 0 0 1 1 1 0 1 1 0 1 1 0 2 0 1 0 1 1 1 0 0 2 0 3 0 0 0 2 1 0 0 2 0 1 0 1 2 0 0 1 1 1 0 1 0 2 0 0 3 0 0 0 2 1 0 0 1 2 0 0 0 3 Apologies if any permutation was missed, but this is the general idea. The generated permutations don't have to be in any particular order, but the above list was convenient for categorically

Replace duplicate values in array with new randomly generated values

℡╲_俬逩灬. 提交于 2019-11-30 15:27:30
I have below a function (from a previous question that went unanswered) that creates an array with n amount of values. The sum of the array is equal to $max. function randomDistinctPartition($n, $max) { $partition= array(); for ($i = 1; $i < $n; $i++) { $maxSingleNumber = $max - $n; $partition[] = $number = rand(1, $maxSingleNumber); $max -= $number; } $partition[] = $max; return $partition; } For example: If I set $n = 4 and $max = 30. Then I should get the following. array(5, 7, 10, 8); However, this function does not take into account duplicates and 0s. What I would like - and have been

how to get all possible combination of items from 2-dimensional list in python?

落花浮王杯 提交于 2019-11-30 15:12:13
I didn't find a better way to phrase this question in the title. If you can, please edit. I have a list of lists like this: a = [['a','b'],[1,2]] now, I'd like a function that spits out all possible combination like this: [['a',1],['a',2],['b',1],['b',2]] where nor the number of lists in a is known in advance, nor is the length of each of the sub lists known in advance, but all the combinations that come out should contain 1 item from every sublist. You need itertools.product() : >>> list(itertools.product(*a)) [('a', 1), ('a', 2), ('b', 1), ('b', 2)] This may be what itertools.product()

Langford sequence implementation Haskell or C

戏子无情 提交于 2019-11-30 14:53:23
问题 In combinatorial mathematics, a Langford pairing, also called a Langford sequence, is a permutation of the sequence of 2n numbers 1, 1, 2, 2, ..., n, n in which the two ones are one unit apart, the two twos are two units apart, and more generally the two copies of each number k are k units apart. For example: Langford pairing for n = 3 is given by the sequence 2,3,1,2,1,3. What is a good method to solve this in haskell or C Can you suggest an algorithm to solve it (Do not want to use brute

The pythonic way to generate pairs

僤鯓⒐⒋嵵緔 提交于 2019-11-30 14:28:43
问题 I want something like code below, but "pythonic" style or using standard library: def combinations(a,b): for i in a: for j in b: yield(i,j) 回答1: These are not really "combinations" in the sense of combinatorics, these are rather elements from the cartesian product of a and b . The function in the standard library to generate these pairs is itertools.product(): for i, j in itertools.product(a, b): # whatever 回答2: As @Sven said, your code is attempting to get all ordered pairs of elements of

How to write combinatorics function in postgres?

三世轮回 提交于 2019-11-30 14:18:09
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 and cannot for the life of me figure out how to do this using plpgsql. It doesn't need to be

Algorithm for Calculating Binomial Coefficient

最后都变了- 提交于 2019-11-30 13:28:20
I need a way of calculating combinations without running out of memory. Here's what i have so far. public static long combination(long n, long k) // nCk { return (divideFactorials(factorial(n), ((factorial(k) * factorial((n - k)))))); } public static long factorial(long n) { long result; if (n <= 1) return 1; result = factorial(n - 1) * n; return result; } public static long divideFactorials(long numerator, long denominator) { return factorial(Math.Abs((numerator - denominator))); } I have tagged it as C#, but the solution should ideally be language independent. public static long combination

Hungarian algorithm in Python

僤鯓⒐⒋嵵緔 提交于 2019-11-30 12:15:34
Is there good implementation of Hungarian algorithm in standard python libraries? jcomeau_ictx I just tried: pip install munkres and it worked. Here you can find a short explanation on how to use it. I got an error trying to install "hungarian". Check this munkres out There are multiple Options: pip install munkres Documentation here pip install hungarian Documentation here pip install scipy scipy.optimize.linear_sum_assignment Documentation here 来源: https://stackoverflow.com/questions/4075669/hungarian-algorithm-in-python

Algorithm to determine coin combinations

一个人想着一个人 提交于 2019-11-30 12:14:05
问题 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

Langford sequence implementation Haskell or C

廉价感情. 提交于 2019-11-30 12:07:41
In combinatorial mathematics, a Langford pairing , also called a Langford sequence, is a permutation of the sequence of 2n numbers 1, 1, 2, 2, ..., n, n in which the two ones are one unit apart, the two twos are two units apart, and more generally the two copies of each number k are k units apart. For example: Langford pairing for n = 3 is given by the sequence 2,3,1,2,1,3. What is a good method to solve this in haskell or C Can you suggest an algorithm to solve it (Do not want to use brute force)? --------------------------EDIT---------------------- How could we define the mathematical rules