combinatorics

Algorithm for Calculating Binomial Coefficient

浪尽此生 提交于 2019-11-29 19:40:43
问题 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

Google Interview: Arrangement of Blocks

烂漫一生 提交于 2019-11-29 18:41:50
You are given N blocks of height 1…N. In how many ways can you arrange these blocks in a row such that when viewed from left you see only L blocks (rest are hidden by taller blocks) and when seen from right you see only R blocks? Example given N=3, L=2, R=1 there is only one arrangement {2, 1, 3} while for N=3, L=2, R=2 there are two ways {1, 3, 2} and {2, 3, 1} . How should we solve this problem by programming? Any efficient ways? This is a counting problem, not a construction problem, so we can approach it using recursion. Since the problem has two natural parts, looking from the left and

The Assignment Problem, a NumPy function?

痞子三分冷 提交于 2019-11-29 17:32:36
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? No, NumPy contains no such function. Combinatorial optimization is outside of NumPy's scope. It may be possible to do it with one of the optimizers in scipy

How to get all mappings between two lists?

对着背影说爱祢 提交于 2019-11-29 17:08:13
问题 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

Permutations in VBA Excel

痴心易碎 提交于 2019-11-29 17:06:35
I am trying to generate all the possible combinations of an array of characters. The input array has n characters, 5 <= n <= 7, and I would like to generate a second array A( C( n , 5 ) , 5 ) that contains all the C( n , 5 ) combinations. The order of the characters in the array isn't important. Here is an example: input array: { A, B, C, D, E, F } , so n = 6 output array should be: {A B C D E}, {A B C D F}, {A B C F E}, {A B F D E}, {A F C D E}, {F B C D E}, This is pretty simple for n=5 and n=6, but gets very complicated for n=7. Does anyone know how should I make this ? Thanks Solve it

Order bias in wrong implementation of Fisher Yates Shuffle

限于喜欢 提交于 2019-11-29 16:48:50
I implemented the shuffling algorithm as: import random a = range(1, n+1) #a containing element from 1 to n for i in range(n): j = random.randint(0, n-1) a[i], a[j] = a[j], a[i] As this algorithm is biased. I just wanted to know for any n(n ≤ 17) , is it possible to find that which permutation have the highest probablity of occuring and which permutation have least probablity out of all possible n! permutations. If yes then what is that permutation?? For example n=3 : a = [1,2,3] There are 3^3 = 27 possible shuffle No. occurence of different permutations: 1 2 3 = 4 3 1 2 = 4 3 2 1 = 4 1 3 2 =

Fastest solution to list all pairs of n integers?

旧巷老猫 提交于 2019-11-29 15:45:43
I want to list all possible pairs of the integers [1, n] with a large n . I find myself looking for the fastest option. This is what I've come up with so far. Matlab's nchoosek and combnk methods recommend n<15 for listing all possible combinations because of the explosive number of combinations. So how fast this is depends on the n. pair = nchoosek(1:n, 2); Another option would be to use a nested for loop kk =1; pair = zeros(nchoosek(n, 2), 2); for ii = 1:n for jj = ii+1:n pair(kk, :) = [ii, jj]; kk = kk + 1; end end This would be relatively slow. I also thought of using the ind2sub function

How to get all the unique n-long combinations of a set of duplicatable elements?

孤街浪徒 提交于 2019-11-29 15:31:05
I have found many solutions giving a collection elements combined in all possible orders but they all use every element just once in every result while I need them to be treated as reusable. For example if input elements are {"a", "b", "c"} and the number is 2 the output is to be {"a", "a"}, {"a", "b"}, {"a", "c"}, {"b", "a"}, {"b", "b"}, {"b", "c"}, {"c", "a"}, {"c", "b"}, {"a", "c"}. Vivek Maharajh Lets say you've got N input elements, and you want a K-long combination. All you need to do is to count in base N, scoped of course, to all numbers that have K digits. So, lets say N = {n0, n1, ..

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

不羁岁月 提交于 2019-11-29 15:09:09
问题 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

i-th element of k-th permutation

天大地大妈咪最大 提交于 2019-11-29 08:46:51
Is there a fast algorithm to compute the i-th element (0 <= i < n) of the k-th permutation (0 <= k < n!) of the sequence 0..n-1? Any order of the permutations may be chosen, it does not have to be lexicographical. There are algorithms that construct the k -th permutation in O(n) (see below). But here the complete permutation is not needed, just its i -th element. Are there algorithms that can do better than O(n) ? Is there an algorithm that has a space complexity less than O(n)? There are algorithms that construct the k -th permutation by working on an array of size n (see below), but the