FAQ Database Discussion Community

Algorithm to get all the combinations of size n from an array (Java)? [closed]

java,combinations,combinatorics
Right now I'm trying to write a function that takes an array and an integer n, and gives a list of each size n combination (so a list of int arrays). I am able to write it using n nested loops, but this only works for a specific size of...

Matlab - generating combinations of input and their sum

matlab,matrix,combinations,combinatorics,cartesian-product
I have to make a function that takes 3 inputs like e0, e1 and e2. The function will have 2 outputs x and y. x will be the combinations of e0, e1 and e2. y will be a column vector containing sums of columns of x. The following conditions must...

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

algorithm,sorting,combinations,combinatorics,gray-code
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...

Find two disjoint susequences with minimum difference( > n)

algorithm,dynamic-programming,combinatorics,subsequence
You are given an array of positive numbers and you need to find two disjoint sub sequences with minimum difference less than or equal to n. These sub sequences may or may not be contiguous For Example if array = [10,12,101,105,1000] and n = 10 Ans = [10,105] & [12,101]...

python: find all Latin Squares of a set (or partial square with fewer columns)

python,algorithm,permutation,combinatorics
EDIT: Thanks to commenter Douglas Zare, I have renamed the title of this post with more appropriate terminology for anybody else who may be looking for something similar. The code from David Eisenstat below was very helpful. ORIGINAL POST: I apologize for the lack of appropriate set theory terminology... but...

Most suitable combinatorics algorithm

java,algorithm,combinations,combinatorics
There are 6 seats and 4 persons, for which seats must be allocated according to some optimality criterion. For example: Allocation 1: _ _ _ _ _ _ 1 2 3 4 Allocation 2: _ _ _ _ _ _ 1 3 2 4 ... Question 1: Which combinatorics problem...

How to generate all partitions of a set

algorithm,set,combinatorics,backtracking
For a set of the form A = {1, 2, 3, ..., n}. It is called partition of the set A, a set of k<=n elements which respect the following theorems: a) the union of all the partitions of A is A b) the intersection of 2 partitions of A...

C# LINQ combinatorics: All Combinations of a Set without the Empty Set

c#,linq,combinatorics
I have a set of strings, and I want to find all possible combinations of the strings and add them to a list. I want to end up with a list of a list of each combination of the strings, minus the empty set. I have created a solution that...

All N Combinations of All Subsets

r,combinatorics
Given a vector of elements, I would like to obtain a list of all possible n-length combinations of subsets of elements. For example, given the (simplest) sequence 1:2, I would like to obtain a list object of the form { {{1},{1}}, {{1},{2}}, {{2},{2}}, {{1},{1,2}}, {{2},{1,2}}, {{1,2},{1,2}} } when n=2. I...

Finding all combinations of multiple variables summing to 1

matlab,matrix,combinatorics,diophantine
I am trying to solve the equation x1 + x2 + x3 + .... + xn = 1 where the values of all xi are restricted to [0, 0.1, 0.2, ..., 0.9, 1]. Currently, I am solving the problem by first generating an n-dimensional array mat, where at each element...

algorithm,combinatorics,probability-theory
I have a collection of arrays that include numbers from 1 to 10. The size of each array is 5. For example [1,2,3,4,5] [3,4,9,8,2] [1,5,7,9,2] [1,2,5,9,7] ........... [3,8,1,6,9] What algorithm should I use to find repeated triads in these arrays? For example one of the results should be 1,2,5 since...

Manual generation of r-combinations in Python

python,combinatorics
I am attempting to manually generate r-combinations (it is absolutely essential to me that I manually generate these and not use itertools). This is my current block of code: def rcombs(items, r): n = len(items) s = [] permuted_items = [] for i in xrange(0, r): s.append(i) for i in...

Permutation At A Railway Track(Stack Implementation)

algorithm,stack,combinatorics
Engines numbered 1, 2, ..., n are on the line at the left, and it is desired to rearrange(permute) the engines as they leave on the right-hand track. An engine that is on the spur track can be left there or sent on its way down the right track,...

String Combinations With Character Replacement

c#,algorithm,combinatorics
I am trying to work through a scenario I haven't seen before and am struggling to come up with an algorithm to implement this properly. Part of my problem is a hazy recollection of the proper terminology. I believe what I am needing is a variation of the standard "combination"...

How to match a set against a set of sets, completely

mapping,combinatorics,computation-theory,np-complete,set-theory
This problem is similar to the "Exact Hitting Set" problem (http://en.wikipedia.org/wiki/Exact_cover#Exact_hitting_set) but with slightly different constraints. I am looking for libraries, implementations, or papers that solve the following. Say I have a set of sets S, and is initialized as follows: S = {N, O, P, E}; N = {1,...

Generating all subpacks of size N from a template pack

NSubsets<N, Pack<Types...>>::type is to be the pack of packs consisting of all subsets of Types... of size N. For example, NSubsets<2, Pack<int, char, double>>::type is to be Pack<Pack<int, char>, Pack<int, double>, Pack<char, double>> One method is to simply take the output of the PowerSet solution from Obtaining all subpacks from...

How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n

c#,.net,linq,recursion,combinatorics
Anyone can elaborate some details on this code or even give a non-Linq version of this algorithm: public static IEnumerable<IEnumerable<T>> Combinations<T> (this IEnumerable<T> elements, int k) { return k == 0 ? new[] { new T[0] } : elements.SelectMany( (e, i) => elements .Skip(i + 1) .Combinations(k - 1) .Select(c...

Is finding a subset with exact cut with other given subsets NP-hard?

algorithm,complexity-theory,combinatorics,computation-theory
I am trying to figure out whether the following problem is NP-hard: Given G_1,..,G_n subsets of {1..m} c_1,..,c_n non-negative integers in {0..m} Find T subset of {1..m} S.T. for all i=1..n, T intersects G_i in exactly c_i elements I tried to find reductions to NP problems such as coloring, or...

Combinations of variable length with fixed set

I have a set of possible colors defined like this: data Peg = Red | Green | Blue | Yellow | Orange | Purple deriving (Show, Eq, Ord) colors = [Red, Green, Blue, Yellow, Orange, Purple] And code which is a combination of colors defined like this: type Code =...

combinatorially check relationship for all entities in a sentence

java,combinatorics
I have a sentence of the following form: <sentence> 0 A sports article in some copies on Sunday about <LOCATION>Boston</LOCATION> 's 1-0 victory against <LOCATION>San Francisco</LOCATION> referred incorrectly to the history of the interleague series between the <ORGANIZATION>Red Sox</ORGANIZATION> and the <ORGANIZATION>Giants</ORGANIZATION> . </sentence> Within the sentence are references to...

Dynamic T-SQL approach for combinatorics/knapsack

sql-server,algorithm,tsql,combinatorics,knapsack-problem
I guess my question has to do with a variant of the knapsack problem, but I can't really come up with a solution for this: Let's say you are in a hardware store and need to buy 21 screws. They only offer them in bags: Bag X - 16 Screws...

Creating lists from data file

python,list,combinatorics
I have a pre-defined list that gives data in the form of (min, max, increment). for example: [[0.0 1.0 0.1 #mass 1.0 5.0 1.0 #velocity 45.0 47.0 1.0 #angle in degrees 0.05 0.07 0.1 #drag coeff. 0.0 0.0 0.0 #x-position 0.0 0.0 0.0]] #y-postion and this goes on a for...

generate and output all possible combinations, from three List only once per unique item

java,string,arraylist,combinatorics
I'm trying to generate all possible combinations of sentences. As variables I have two Strings, one String will be the subject, e.g. health and one will be an object, such as fruit, but then I will have a List<String> of values associated with one "head" word, so in keeping with...

Java: Looking for an algorithm for permutation

java,algorithm,combinatorics
Suppose I have an array. String[] arr = {"a", "b", "c"}; I need to get all possible combinations, like this: a a b a c a b c a c b b b a b c b a c b c a c c a c b c a b...

Filter cartesian products (cartesian powers) in python based on majority constraint

python,combinatorics,cartesian-product
I would like to know the number of elements in the cardinal power X^n (cardinal product of one set X by itself, n times) with a majority constraint: how many elements in X^n have a (relative) majority of one element of the set (say x)? On top of that, I'd...

Summation Combination code

python,combinatorics
Hi I am new to Python and am trying to solve a coding challenge where the objective is to create a function which takes an array of integers (both negative and positive) and a target value (also an integer) and then outputs true if any combinations of the numbers sum...

Pairwise distances of a list of numbers in Haskell

If X is a set of numbers, then ΔX is a multiset of numbers, representing pairwise subtractions between each 2 numbers. For example, if X is a set of points on a line in increasing order, then ΔX is a multiset of pairwise distances between these points. How to write...

Non-boolean “truth table” creation

c#,logic,combinatorics,multiset
I have the following problem: I need to create a table, which is combination of values coming from sets. The cardinality of the elements in the set is unknown, and may vary from set to set, the domain of the values is unknown, and may as well vary from set...

Can someone explain me why the following algorithm for LIS is not O(n)?

algorithm,erlang,dynamic-programming,combinatorics,lis
The following code traverses the list once and finds the LIS. I don't see why the DP algorithm should take O(n2). //C int lis(int *a, int l){ if(l == 0) return 1; else if(a[l] > a[l - 1]) return 1 + lis(a, l - 1); else return lis(a, l -...

Determine all consecutive subsets of the set {1,2,3,…,n}. The subsets should have at least 2 elements

algorithm,computer-science,combinatorics,set-theory
I need to partition a set S={1, 2, 3, … , n} consisting of consecutive numbers such that each subset has has at least 2 elements (rule 1) and it consists of consecutive numbers (rule 2). The rules are: Each subset has at least two elements. All elements of all...

Optimal way to find number of operation required to convert all K numbers to lie in the range [L,R] (i.e. L≤x≤R)

arrays,algorithm,combinatorics
I am solving this question which requires some optimized techniques to solve it. I can think of the brute force method only which requires combinatorics. Given an array A consisting of n integers. We call an integer "good" if it lies in the range [L,R] (i.e. L≤x≤R). We need...

Finding subsets of items with non-conflicting categories

algorithm,set,combinatorics
I have a list of objects with each Item having a cost and a set of resources associated with it (see below). I'm looking for a way to select a subset from this list based on the combined cost and each resource must be contained at most once (not every...

How to find all strings that do not contain substring palindromes

string,algorithm,probability,combinatorics,discrete-mathematics
Disclaimer: This is a problem lifted from HackerRank, but their editorial answer wasn't sufficient so I hoped to get better answers. If it's against any policy, please let me know and I'll take this down. Problem: You are given two integers, N and M. Count the number of strings of...

Fastest solution to list all pairs of n integers?

performance,matlab,matrix,combinations,combinatorics
I want to list all possible pairs of the integers [1, n] with a large n. I find myself looking for the fastest option. This is what I've come up with so far. Matlab's nchoosek and combnk methods recommend n<15 for listing all possible combinations because of the explosive number...

Normalize strings that represent (combinatorical) necklaces [closed]

python,combinatorics,string-comparison,string-matching
I'm trying to match "necklaces" of symbols in Python by looking up their linear representations, for which I use normal strings. For example, the strings "AABC", "ABCA", "BCAA", "CAAB" all represent the same necklace (pictured). In order to get an overview, I store only one of the equivalent strings of...

efficiently generating all integers within a binary mask

combinatorics
Suppose I have some binary mask mask. (e.g. 0b101011011101) Is there an efficient method of computing all integers k such that k & mask == k? (where & is the bitwise AND operator) (alternatively, k & ~mask == 0) If mask has m ones, then there are exactly 2m numbers...

Random sample of character vector, without elements prefixing one another

r,performance,combinatorics
Consider a character vector, pool, whose elements are (zero-padded) binary numbers with up to max_len digits. max_len <- 4 pool <- unlist(lapply(seq_len(max_len), function(x) do.call(paste0, expand.grid(rep(list(c('0', '1')), x))))) pool ## [1] "0" "1" "00" "10" "01" "11" "000" "100" "010" "110" ## [11] "001" "101" "011" "111" "0000" "1000" "0100" "1100"...

matlab algorithm for specific configurations [duplicate]

matlab,combinatorics
This question already has an answer here: Generate all possible combinations of the elements of some vectors (Cartesian product) 3 answers How to effectively implement in matlab this specific combinatorics problem? I have a list of possible values {{v1_1,...v1_n1},{v2_1, ..., v2_n2}, ... {vm_1, ... , vm_nm}} . I need...