combinatorics

Old Top Coder riddle: Making a number by inserting +

限于喜欢 提交于 2019-12-04 09:29:55
问题 I am thinking about this topcoder problem. Given a string of digits, find the minimum number of additions required for the string to equal some target number. Each addition is the equivalent of inserting a plus sign somewhere into the string of digits. After all plus signs are inserted, evaluate the sum as usual. For example, consider "303" and a target sum of 6. The best strategy is "3+03". I would solve it with brute force as follows: for each i in 0 to 9 // i -- number of plus signs to

Generating permutations with a sum constraint

拥有回忆 提交于 2019-12-04 08:21:05
I have n sets of variable length and would like to get all permutations of items from each set where the sum is within a certain range. For example in R we can do: set1 <- c(10, 15, 20) set2 <- c(8, 9) set3 <- c(1, 2, 3, 4) permutations <- expand.grid(set1, set2, set3) permutations$sum <- rowSums(permutations) final <- permutations[permutations$sum >= 25 & permutations$sum <= 29, ] # final: # Var1 Var2 Var3 sum # 3 20 8 1 29 # 5 15 9 1 25 # 8 15 8 2 25 # 11 15 9 2 26 # 14 15 8 3 26 # 17 15 9 3 27 # 20 15 8 4 27 # 23 15 9 4 28 This is fine for a small number of sets, however quickly

How can I prove the “Six Degrees of Separation” concept programmatically?

风格不统一 提交于 2019-12-04 07:51:45
问题 I have a database of 20 million users and connections between those people. How can I prove the concept of "Six degrees of separation" concept in the most efficient way in programming? link to the article about Six degrees of separation 回答1: You just want to measure the diameter of the graph. This is exactly the metric to find out the seperation between the most-distantly-connected nodes in a graph. Lots of algorithms on Google, Boost graph too. 回答2: You can probably fit the graph in memory

Computing the nth 6 character permutation of an alphabet

流过昼夜 提交于 2019-12-04 07:48:41
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 grab a slice of the resulting permutations via the following command (this example grabs the first 10

Good simple algorithm for generating necklaces in Scheme?

强颜欢笑 提交于 2019-12-04 05:20:14
A k-ary necklace of length n is an ordered list of length n whose items are drawn from an alphabet of length k, which is the lexicographically first list in a sort of all lists sharing an ordering under rotation. Example: (1 2 3) and (1 3 2) are the necklaces of length 3 from the alphabet {1 2 3}. More info: http://en.wikipedia.org/wiki/Necklace_(combinatorics) I'd like to generate these in Scheme (or a Lisp of your choice). I've found some papers... Savage - A New Algorithm for Generating Necklaces Sawada - Generating Bracelets in Constant Amortized Time Sawada - Generating Necklaces with

Determine whether a symbol is part of the ith combination nCr

怎甘沉沦 提交于 2019-12-04 04:46:46
问题 UPDATE: Combinatorics and unranking was eventually what I needed. The links below helped alot: http://msdn.microsoft.com/en-us/library/aa289166(v=vs.71).aspx http://www.codeproject.com/Articles/21335/Combinations-in-C-Part-2 The Problem Given a list of N symbols say {0,1,2,3,4...} And NCr combinations of these eg. NC3 will generate: 0 1 2 0 1 3 0 1 4 ... ... 1 2 3 1 2 4 etc... For the ith combination (i = [1 .. NCr]) I want to determine Whether a symbol (s) is part of it. Func(N, r, i, s) =

MATLAB: All possible combinations of binary matrices

假如想象 提交于 2019-12-04 03:52:37
问题 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

All ways of dividing an array (element combinations) into a custom partition

笑着哭i 提交于 2019-12-04 03:43:53
问题 I want to divide array of n elements to given size subarrays with all possible combinations of elements. For instance: Array: {1,2,3,4} - can be n elements, 1 < n < 100. It can have duplicates. Given size pattern (just example, could be different): [2 -subarrays, 2-elements] Expected result: {1,2},{3,4} {1,3},{2,4} {1,4},{2,3} or {2,1},{3,4} {1,3},{4,2} {3,2},{1,4} etc.. As You can see, the order of elements in subarrays, or the order of subarrays in sets of subarrays does not matter. It has

Algorithm - generating all combinations from items that must be chosen in sequence

廉价感情. 提交于 2019-12-04 03:41:05
问题 I'm looking to find out if a particular algorithm already exists. I want to use it in an application, but I've also seen this come up in several Project Euler problems too. I'm looking to calculate a specific type of permutation/output set, where the next item chosen must be one from a finite set of options in only the following set. For example, say I've got 3 arrays $a1 = array("a", "b", "c"); $a2 = array("d", "e", "f"); $a3 = array("g", "h", "i"); I'm looking to generate all the

Listing combinations WITH repetitions in Scala

自古美人都是妖i 提交于 2019-12-04 01:04:01
问题 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