combinatorics

Need help in building efficient exhaustive search algorithm

狂风中的少年 提交于 2019-12-01 20:38:16
There are a 10 buttons. These buttons can unlock the lock if pressed in correct order (5 presses in sequence). Every button press triggers unlock check. Example: "password" is 123456 and I press buttons 0 1 2 3 4 5 6 I unlock the lock from 6th button press. I need to design algorithm that tries all possible combinations in the most efficient way (i.e. minimum amount of buttons should be pressed). I can interpret button number as digit and number of pressed button in sequence as digit position and then try all 99999 combinations in attempt to unlock the lock but I feel that there is a more

MATLAB: All possible combinations of binary matrices

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 19:57:35
I am looking to find all possible linear combinations of a set of matrices over GF(2) . I know the number of matrices, k , and they are all the same dimension, stored in a 3D array, C(:,:,i) for the ith matrix. Because I'm working over GF(2) , all the coefficients of the linear combination must be in {0,1} . I would like to generate each of the 2^k possible sums so that I may test the resulting matrix for a required property. There are many posts about generating all combinations of elements of matrices or vectors, but I am looking to generate all linear combinations of the matrices as a whole

comparing numbers to sort then get median value

ⅰ亾dé卋堺 提交于 2019-12-01 19:54:56
Sorting five integers using bitwise or comparison operators can be achieved by first getting the highest number then the second highest then the third and so on. Here is my code in getting the highest number: #include <stdio.h> int main() { int a, b, c, d, e; int aa, bb, cc, dd, ee; a = 4; b = 2; c = 5; d = 1; e = 3; aa = (a > b) ? ((a > c) ? ((a > d) ? ((a > e) ? a : e) : ((d > e) ? d : e)) : ((c > d) ? ((c > e) ? c : e) : ((d > e) ? d : e))) : ((b > c) ? ((b > d) ? ((b > e) ? b : e) : ((d > e) ? d : e)) : ((c > d) ? ((c > e) ? c : e) : ((d > e) ? d : e))); printf("highest: %d\n", aa); return

Subset and Set Cover

一个人想着一个人 提交于 2019-12-01 14:16:48
We are given a number of locks and to open these locks we need exactly that set of people to open that lock. Given the number of people we have and the number of locks that needs to opened, we need a specification on how to distribute the keys amongst the available people such that any number of required people to open that lock can open it, but no group less that number of required people can open it. The number of people will be in range of 1-9 and number of people required to open the lock will be in range of 0-9 Consider the following examples Number of people available = 2 Number of

Subset and Set Cover

橙三吉。 提交于 2019-12-01 13:18:13
问题 We are given a number of locks and to open these locks we need exactly that set of people to open that lock. Given the number of people we have and the number of locks that needs to opened, we need a specification on how to distribute the keys amongst the available people such that any number of required people to open that lock can open it, but no group less that number of required people can open it. The number of people will be in range of 1-9 and number of people required to open the lock

Calculate Nth multiset combination (with repetition) based only on index

断了今生、忘了曾经 提交于 2019-12-01 10:59:09
How can i calculate the Nth combo based only on it's index. There should be (n+k-1)!/(k!(n-1)!) combinations with repetitions. with n=2, k=5 you get: 0|{0,0,0,0,0} 1|{0,0,0,0,1} 2|{0,0,0,1,1} 3|{0,0,1,1,1} 4|{0,1,1,1,1} 5|{1,1,1,1,1} So black_magic_function(3) should produce {0,0,1,1,1}. This will be going into a GPU shader, so i want each work-group/thread to be able to figure out their subset of permutations without having to store the sequence globally. with n=3, k=5 you get: i=0, {0,0,0,0,0} i=1, {0,0,0,0,1} i=2, {0,0,0,0,2} i=3, {0,0,0,1,1} i=4, {0,0,0,1,2} i=5, {0,0,0,2,2} i=6, {0,0,1,1

N choose N/2 sublists of a list

半城伤御伤魂 提交于 2019-12-01 10:39:12
Is there an efficient way in Python to get all partitions of a list of size n into two subsets of size n/2 ? I want to get some iterative construct such that each iteration provides two non-overlapping subsets of the original list, each subset having size n/2 . For example: A = [1,2,3,4,5,6] # here n = 6 # some iterative construct # in each iteration, a pair of subsets of size n/2 # subsets = [[1,3,4], [2,5,6]] for example for one of the iterations # subsets = [[1,2,5],[3,4,6]] a different iteration example The subsets should be non-overlapping, e.g. [[1,2,3], [4,5,6]] is valid but [[1,2,3],

Python: find all possible word combinations with a sequence of characters (word segmentation)

时光怂恿深爱的人放手 提交于 2019-12-01 09:03:07
I'm doing some word segmentation experiments like the followings. lst is a sequence of characters, and output is all the possible words. lst = ['a', 'b', 'c', 'd'] def foo(lst): ... return output output = [['a', 'b', 'c', 'd'], ['ab', 'c', 'd'], ['a', 'bc', 'd'], ['a', 'b', 'cd'], ['ab', 'cd'], ['abc', 'd'], ['a', 'bcd'], ['abcd']] I've checked combinations and permutations in itertools library, and also tried combinatorics . However, it seems that I'm looking at the wrong side because this is not pure permutation and combinations... It seems that I can achieve this by using lots of loops, but

Calculate the index of a given number within a sorted set

对着背影说爱祢 提交于 2019-12-01 08:45:09
Not sure if this question should be on Math-Overflow or here, so will try here first: Suppose we are given a number with N 1s and M 0s. There are (M+N)!/(M!*N!) different such numbers, that can be sorted in a countable set. For example, the sorted set of all numbers with 2 ones and 3 zeros, is: 0 00011 1 00101 2 00110 3 01001 4 01010 5 01100 6 10001 7 10010 8 10100 9 11000 How can we efficiently calculate the index of a given number within the corresponding set? Note: the input to this question is only the number, and not the entire (corresponding) set. Let choose (n, k) = n! / k! / (n-k)! .

All possible combinations of a given string

萝らか妹 提交于 2019-12-01 08:03:41
I need to find all possible combinations of a given string, from a minimum length to a maximum length. interface allCombos(string: String, min: Number, max:Number): Array {} So if my input string is ‘abcde’ , and my minimum length is 3, I want the result to be: For length 3: [‘abc’, ‘abd’, ‘abe’, ‘acd’, ..., ‘bcd’, ‘bce’, ..., ‘eda’, ...] Concatenated with length 4: [‘abcd’, ‘abdc’, ‘acdb’, ‘acbd’, …etc] Concatenated with all possible combinations with length up to the max parameter. Which shouldn't be higher than the input word length. I started thinking that all possible combinations would