combinatorics

Iterating over all subsets of a given size

橙三吉。 提交于 2019-12-01 07:44:01
I know that iterating over all subsets of a set of size n is a performance nightmare and will take O(2^n) time. How about iterating over all subsets of size k (for (0 <= k <= n))? Is that a performance nightmare? I know there are (n, k) = n! / k! (n - k)! possibilities. I know that if k is very close to 0 or very close to n, this is a small number. What is the worst case performance in terms of n and k? Is there a simpler way of saying it other than just O(n! / k! (n - k)!)? Is this asymptotically smaller than O(n!) or the same? You want Gosper's hack: int c = (1<<k)-1; while (c < (1<<n)) {

Permutation algorithms in C#

徘徊边缘 提交于 2019-12-01 04:53:16
问题 I'm struggling with this algorithm I need to write. I'm using C#. Say I have a List<Bag> and I have a List<Lunch> . I need to write an algorithm which will enumerate all permutations of lunches in all bags. For example, say there are 3 lunches and 2 bags: // Permutation 1 Bag 1, Lunch 1 Bag 2, Lunch 1 // Permutation 2 Bag 1, Lunch 1 Bag 2, Lunch 2 // Permutation 3 Bag 1, Lunch 1 Bag 2, Lunch 3 // Permutation 4 Bag 1, Lunch 2 Bag 2, Lunch 1 // Permutation 5 Bag 1, Lunch 2 Bag 2, Lunch 2 //

Listing combinations WITH repetitions in Scala

旧街凉风 提交于 2019-12-01 03:52:22
Trying to learn a bit of Scala and ran into this problem. I found a solution for all combinations without repetions here and I somewhat understand the idea behind it but some of the syntax is messing me up. I also don't think the solution is appropriate for a case WITH repetitions. I was wondering if anyone could suggest a bit of code that I could work from. I have plenty of material on combinatorics and understand the problem and iterative solutions to it, I am just looking for the scala-y way of doing it. Thanks I understand your question now. I think the easiest way to achieve what you want

Maximum value of postage stamps on an envelope

ぐ巨炮叔叔 提交于 2019-12-01 03:35:05
The postage stamp problem is a mathematical riddle that asks what is the smallest postage value which cannot be placed on an envelope, if the letter can hold only a limited number of stamps, and these may only have certain specified face values. For example, suppose the envelope can hold only three stamps, and the available stamp values are 1 cent, 2 cents, 5 cents, and 20 cents. Then the solution is 13 cents; since any smaller value can be obtained with at most three stamps (e.g. 4 = 2 + 2, 8 = 5 + 2 + 1, etc.), but to get 13 cents one must use at least four stamps. Is there an algorithm that

In how many ways we can pick K elements from set of n elements to form a number X?

左心房为你撑大大i 提交于 2019-12-01 01:53:55
There is one important point - We can pick any element any number of times but the total picked element should be equal to K. For example - If set of elements is 1 2 3 5 and K = 3 and X = 4. Then answer is 1 because there is only one way to pick 3 elements which adds upto 4 and those 3 elements are two 1's and one 2. (1+1+2 = 4) An algorithm can greatly help. :) Let's consider DP solution for coin change problem. Usually entries of array A with length (Sum+1) contain integers - number of ways to make the value of every cell. Simple modification - make 2D array A[Sum+1][K] , so A[M][P] will

Maximum value of postage stamps on an envelope

旧巷老猫 提交于 2019-12-01 00:14:17
问题 The postage stamp problem is a mathematical riddle that asks what is the smallest postage value which cannot be placed on an envelope, if the letter can hold only a limited number of stamps, and these may only have certain specified face values. For example, suppose the envelope can hold only three stamps, and the available stamp values are 1 cent, 2 cents, 5 cents, and 20 cents. Then the solution is 13 cents; since any smaller value can be obtained with at most three stamps (e.g. 4 = 2 + 2,

Get every combination of strings

大城市里の小女人 提交于 2019-11-30 23:10:58
I had a combinatorics assignment that involved getting every word with length less than or equal to 6 from a specific combination of strings. In this case, it was S = { 'a', 'ab', 'ba' }. The professor just started listing them off, but I thought it would be easier solved with a program. The only problem is that I can't get a good algorithm that would actually compute every possible option. If anyone could help, I'd appreciate it. I usually program in Python but really I just need help with the algorithm. You can iteratively generate all the strings made from one part, two parts, three parts

Nth combinations ({a, b} = {b, a}) without repetition (enumeration / brute force)

余生长醉 提交于 2019-11-30 21:09:20
问题 I'm looking for an algorithm that generates the nth combinations without repetition. I could find a lot on permutations where (a, b) != (b, a) , but I'm looking for combinations where {a, b} = {b, a} . Example: Set = {a, b, c} n = 2 Combinations: {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d} A generic, recursive implementation in Java that takes a Set or List would be great. I would also appreciate a link with a good explanation, pseudo or example code. 回答1: You can do this using the

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

ぐ巨炮叔叔 提交于 2019-11-30 20:32:38
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 partitions of N that are of length S is incredibly slow. However, and I hope this helps someone, I've found

Finding unique permutations efficiently

浪子不回头ぞ 提交于 2019-11-30 19:01:45
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 loop and the sorting required by unique is too slow. So my question is: can I compute unique