combinatorics

An efficient method to generate all possible ways to pair up items in a data set

Deadly 提交于 2020-01-01 18:56:07
问题 This is somewhat of a combinatorial problem; I'm trying to figure out an efficient way to pair up all items in a data set. For example, I have an array of length 6: [1,2,3,4,5,6], and I want to make all possible pairings of the contents in the array as so: [1,2],[3,4],[5,6] [1,2],[3,5],[4,6] [1,2],[3,6],[4,5] [1,3],[2,4],[5,6] [1,3],[2,5],[4,6] ... and so on. In this example, there would be 15 combinations total. In general, the number of possibilities in this solution should be (N-1)!! where

Prolog: how to do “check(a++b++c++d equals d++a++c++b) -> yes”

拟墨画扇 提交于 2020-01-01 12:05:29
问题 Let's define custom operators - let it be ++ , equals :- op(900, yfx, equals). :- op(800, xfy, ++). And fact: check(A equals A). I try to make predicate, let it be check/1 , that will return true in all following situations: check( a ++ b ++ c ++ d equals c ++ d ++ b ++ a ), check( a ++ b ++ c ++ d equals d ++ a ++ c ++ b), check( a ++ b ++ c ++ d equals d ++ b ++ c ++ a ), % and all permutations... of any amount of atoms check( a ++ ( b ++ c ) equals (c ++ a) ++ b), % be resistant to any

Python Numpy vectorize nested for-loops for combinatorics

笑着哭i 提交于 2020-01-01 10:07:26
问题 Given an nxn array A of real positive numbers, I'm trying to find the minimum of the maximum of the element-wise minimum of all combinations of three rows of the 2-d array. Using for-loops, that comes out to something like this: import numpy as np n = 100 np.random.seed(2) A = np.random.rand(n,n) global_best = np.inf for i in range(n-2): for j in range(i+1, n-1): for k in range(j+1, n): # find the maximum of the element-wise minimum of the three vectors local_best = np.amax(np.array([A[i,:],

Generate All Unique Combinations of Elements of a IEnumerable(Of T)

旧街凉风 提交于 2020-01-01 05:48:11
问题 This question is virtually the same as this SO post, only I'm looking for a VB.NET (.NET 4) solution. I've spun my wheels long enough trying to come up with a generic solution to solving this "power set" problem. Given: Dim choices As IEnumerable(Of String) = {"Coffee", "Tea", "Milk", "Cookies"} Dim choiceSets = choices.CombineAll() I'm looking for choiceSets to be an IEnumerable(Of IEnumerable(Of T)) so that I can do something like: For each choiceSet in choiceSets Console.WriteLine(String

Removing items from unevenly distributed set

匆匆过客 提交于 2020-01-01 04:44:06
问题 I have a website where users submit questions (zero, one or multiple per day), vote on them and answer one question per day (more details here). A user can see the question only once either by submitting, voting or answering it. I have a pool of questions that players have already seen. I need to remove 30 questions from the pool each month. I need to pick questions to remove in such way that I maximize the number of available questions left in the pool for player with least available

Cartesian Product of Sets where No Elements are Identical under Permutations in Python

删除回忆录丶 提交于 2019-12-31 06:43:25
问题 I have some sets I would like to take the Cartesian product of, which is working well. However, I want to remove all elements of this new set which are identical under a permutation of the elements. For example, take the following code: import itertools as ittools x = 2 y = 3 z = 5 flist = list(ittools.product([x,y,z],repeat=3)) for f in flist: print reduce(lambda a,b: a*b, f) This code find the Cartesian product of the set {2,3,5} and returns the product of all three components of each

Iterating over all subsets of a given size

怎甘沉沦 提交于 2019-12-30 10:14:31
问题 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

Get every combination of strings

假装没事ソ 提交于 2019-12-30 07:12:44
问题 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

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

点点圈 提交于 2019-12-30 06:07:23
问题 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

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

心已入冬 提交于 2019-12-30 06:02:05
问题 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