combinatorics

Variable amount of nested for loops

安稳与你 提交于 2019-11-26 19:10:11
问题 Edit: I'm sorry, but I forgot to mention that I'll need the values of the counter variables. So making one loop isn't a solution I'm afraid. I'm not sure if this is possible at all, but I would like to do the following. To a function, an array of numbers is passed. Each number is the upper limit of a for loop, for example, if the array is [2, 3, 5] , the following code should be executed: for(var a = 0; a < 2; a++) { for(var b = 0; b < 3; b++) { for(var c = 0; c < 5; c++) { doSomething([a, b,

Scheduling algorithm for a round-robin tournament?

爷,独闯天下 提交于 2019-11-26 19:07:04
问题 I recently did studying stuff and meet up with Donald Knuth. But i didn't found the right algorithm to my problem. The Problem We have a league with n players. every week they have a match with one other. in n-1 weeks every team fought against each other. there are n/2 matches a day. but one team can only fight once in a week. if we generate an (n/k) combination we get all of the combinations... (assuming k = 2) but i need to bring them in the right order. My first suggestion was... not the

Cartesian product of a dictionary of lists

感情迁移 提交于 2019-11-26 18:30:35
I'm trying to write some code to test out the Cartesian product of a bunch of input parameters. I've looked at itertools , but its product function is not exactly what I want. Is there a simple obvious way to take a dictionary with an arbitrary number of keys and an arbitrary number of elements in each value, and then yield a dictionary with the next permutation? Input: options = {"number": [1,2,3], "color": ["orange","blue"] } print list( my_product(options) ) Example output: [ {"number": 1, "color": "orange"}, {"number": 1, "color": "blue"}, {"number": 2, "color": "orange"}, {"number": 2,

How to (cheaply) calculate all possible length-r combinations of n possible elements

倾然丶 夕夏残阳落幕 提交于 2019-11-26 18:27:34
问题 What is the fastest way to calculate all possible length-r combinations of n possible elements without resorting to brute force techniques or anything that requires STL? While working on an Apriori algorithm for my final project in my data structures class, I developed an interesting solution that uses bit-shifting and recursion, which i will share in an answer below for anyone who is interested. However, is this the fastest way of achieving this (without using any common libraries)? I ask

Nth Combination

放肆的年华 提交于 2019-11-26 17:43:50
Is there a direct way of getting the Nth combination of an ordered set of all combinations of nCr? Example: I have four elements: [6, 4, 2, 1]. All the possible combinations by taking three at a time would be: [[6, 4, 2], [6, 4, 1], [6, 2, 1], [4, 2, 1]]. Is there an algorithm that would give me e.g. the 3rd answer, [6, 2, 1], in the ordered result set, without enumerating all the previous answers? Note you can generate the sequence by recursively generating all combinations with the first element, then all combinations without. In both recursive cases, you drop the first element to get all

Combinations, Dispositions and Permutations in PHP

落花浮王杯 提交于 2019-11-26 16:42:32
What is the most efficient way to generate all the combinations, dispositions and permutations of an array in PHP? Victor Liu Here is code to get all permutations: http://php.net/manual/en/function.shuffle.php#90615 With the code to get the power set, permutations are those of maximal length, the power set should be all combinations. I have no idea what dispositions are, so if you can explain them, that would help. You can use this class: http://pear.php.net/package/Math_Combinatorics and use it like: $combinatorics = new Math_Combinatorics; $words_arr = array( 'one' => 'a', 'two' => 'b',

N-D version of itertools.combinations in numpy

久未见 提交于 2019-11-26 16:26:33
问题 I would like to implement itertools.combinations for numpy. Based on this discussion, I have a function that works for 1D input: def combs(a, r): """ Return successive r-length combinations of elements in the array a. Should produce the same output as array(list(combinations(a, r))), but faster. """ a = asarray(a) dt = dtype([('', a.dtype)]*r) b = fromiter(combinations(a, r), dt) return b.view(a.dtype).reshape(-1, r) and the output makes sense: In [1]: list(combinations([1,2,3], 2)) Out[1]: [

Creating combinations that have no more one intersecting element

删除回忆录丶 提交于 2019-11-26 16:01:06
问题 I am looking to create a special type of combination in which no two sets have more than one intersecting element. Let me explain with an example: Let us say we have 9 letter set that contains A, B, C, D, E, F, G, H, and I If you create the standard non-repeating combinations of three letters you will have 9C3 sets. These will contain sets like ABC, ABD, BCD, etc. I am looking to create sets that have at the most only 1 common letter. So in this example, we will get following sets: ABC, ADG,

Generate all permutations of a list without adjacent equal elements

做~自己de王妃 提交于 2019-11-26 15:49:41
When we sort a list, like a = [1,2,3,3,2,2,1] sorted(a) => [1, 1, 2, 2, 2, 3, 3] equal elements are always adjacent in the resulting list. How can I achieve the opposite task - shuffle the list so that equal elements are never (or as seldom as possible) adjacent? For example, for the above list one of the possible solutions is p = [1,3,2,3,2,1,2] More formally, given a list a , generate a permutation p of it that minimizes the number of pairs p[i]==p[i+1] . Since the lists are large, generating and filtering all permutations is not an option. Bonus question: how to generate all such

How to design an algorithm to calculate countdown style maths number puzzle

£可爱£侵袭症+ 提交于 2019-11-26 15:38:38
问题 I have always wanted to do this but every time I start thinking about the problem it blows my mind because of its exponential nature. The problem solver I want to be able to understand and code is for the countdown maths problem: Given set of number X1 to X5 calculate how they can be combined using mathematical operations to make Y. You can apply multiplication, division, addition and subtraction. So how does 1,3,7,6,8,3 make 348 ? Answer: (((8 * 7) + 3) -1) *6 = 348 . How to write an