combinatorics

Given an array, how to generate all combinations of subset size k?

江枫思渺然 提交于 2019-12-06 08:04:20
问题 So given input = [1, 2, 3] and k=2 this would return: 1 2 1 3 2 1 2 3 3 1 3 2 This is the closest to what I am looking for, but not quite: http://algorithms.tutorialhorizon.com/print-all-combinations-of-subset-of-size-k-from-given-array/ function subsetsOfSize(a, used, startIndex, currentSize, k) { if (currentSize === k) { for (var i = 0; i < a.length; i++) { if (used[i]) console.log(a[i]); } console.log('-'); return; } if (startIndex === a.length) return; used[startIndex] = true;

Seeking a solution or a heursitic approxmation for the 3-partition combinatorial situation

陌路散爱 提交于 2019-12-06 07:28:34
How do I distribute 48 items each with its own dollar value to each of 3 inheritors so that the value given to each is equal or nearly equal? This is a form of partitioning problem with is NP-complete (or some such) and therefore impossible to perfectly answer with 48 items. I'm looking for a practical and generally acknowledged approximate algorithm to do this. It's a problem faced by many in resolving wills and estates. Answer must be out there somewhere! The answer could be a computer script or just a manual method. A heuristic that is "Generally Accepted" would suffice. With my programmer

recursion instead of multi-loops

可紊 提交于 2019-12-06 07:24:16
问题 I want this method to work for any given number of arguments, i can do that with code generation(with a lot of ugly code), can it be done with recursion? if so how? I understand recursion, but i dont know how to write this. private static void allCombinations(List<String>... lists) { if (lists.length == 3) { for (String s3 : lists[0]) { for (String s1 : lists[1]) { for (String s2 : lists[2]) { System.out.println(s1 + "-" + s2 + "-" + s3); } } } } if (lists.length == 2) { for (String s3 :

Number of all possible groupings of a set of values?

痴心易碎 提交于 2019-12-06 06:32:30
I want to find a combinatorial formula that given a certain number of integers, I can find the number of all possible groupings of these integers (such that all values belong to a single group) Say I have 3 integers, 1, 2, 3 There would be 5 groupings: 1 2 3 1|2|3| 1 2|3 1|2 3 2|1 3 I have calculated these computationally for N = 3 to 11, but I am trying to theoretically assertain. These values are: (I believe they are correct) num_integers num_groupings 3 5 4 15 5 52 6 203 7 877 8 4140 9 21147 10 115975 11 678570 The reason for doing this is to find the total number of partitionings of a

Write a faster combinatorics algorithm

落爺英雄遲暮 提交于 2019-12-06 05:57:37
问题 I'm trying to write a combinatorics algorithm to get all the possible combinations of k out of n without repetitions. The formula is: n!/(k!(n-k)!)); The results end up in an array. What I've actually written is this: function Factorial($x) { if ($x < 1) { echo "Factorial() Error: Number too small!"; ) $ans = 1; for ($xx = 2; $xx >= $x; $xx++) { $ans = $ans * $xx; } return($ans); } function Combination($selectcount,$availablecount) { $ans = Factorial($availablecount) / ( Factorial(

Custom permutation, Equal distribution of pairs

狂风中的少年 提交于 2019-12-06 04:38:56
问题 I've been playing with a strange problem for a few weeks and can't seem to get the results I want. I'd like to take a permutation of a list of objects to get unique pairs. Then order them in a particular way to maximize equal distribution of the objects at any point in the list. This also means that if an object is at the beginning of a pair if should also be at the end of a pair soon after. No pairs can repeat. To clarify, here is an example. list (A,B,C,D) might result in the following: (A

What category of combinatorial problems appear on the logic games section of the LSAT?

有些话、适合烂在心里 提交于 2019-12-06 04:38:09
EDIT : See Solving "Who owns the Zebra" programmatically? for a similar class of problem There's a category of logic problem on the LSAT that goes like this: Seven consecutive time slots for a broadcast, numbered in chronological order I through 7, will be filled by six song tapes-G, H, L, O, P, S-and exactly one news tape. Each tape is to be assigned to a different time slot, and no tape is longer than any other tape. The broadcast is subject to the following restrictions: L must be played immediately before O. The news tape must be played at some time after L. There must be exactly two time

Algorithm for generating “anti-Gray” on-demand combinations of k elements from n

只谈情不闲聊 提交于 2019-12-06 04:28:11
问题 I'm trying to implement an algorithm to get all combinations of k elements out of a set of n elements where the difference between two consecutive combinations are maximized (so kind of reverse Gray codes). In other words, the combinations should be ordered to avoid elements from appearing twice in a row, and so that no element is unnecessarily discriminated. Ideally, the algorithm would also NOT pre-calculate all combinations and store them into memory, but rather deliver combinations on

Word ranking efficiency

房东的猫 提交于 2019-12-06 03:50:09
I am not sure how to solve this problem within the constraints. Consider a "word" as any sequence of capital letters A-Z (not limited to just "dictionary words"). For any word with at least two different letters, there are other words composed of the same letters but in a different order (for instance, STATIONARILY/ANTIROYALIST, which happen to both be dictionary words; for our purposes "AAIILNORSTTY" is also a "word" composed of the same letters as these two). We can then assign a number to every word, based on where it falls in an alphabetically sorted list of all words made up of the same

Computing the nth 6 character permutation of an alphabet

前提是你 提交于 2019-12-06 03:49:39
问题 I have been researching for days trying to figure out a solution to this problem. I would be happy to pay someone for consulting time to solve this if need be. I am currently using Python itertools to generate 6 character permutations of a 32 character alphabet. Via the following command: gen = itertools.permutations('ABCDEFGHJKLMNPQRSTUVWXYZ23456789',6) From the documentation, this function produces "r-length tuples, all possible orderings, no repeated elements". You can use the library to