Questions tagged [combinatorics]

Deals with combinations of entities belonging to a finite set in accordance with certain constraints.

2309 questions
835
votes
40 answers

How do I generate all permutations of a list?

How do I generate all the permutations of a list? For example: permutations([]) [] permutations([1]) [1] permutations([1, 2]) [1, 2] [2, 1] permutations([1, 2, 3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1]
Ricardo Reyes
  • 13,256
  • 4
  • 27
  • 19
277
votes
11 answers

How can I match up permutations of a long list with a shorter list (according to the length of the shorter list)?

I’m having trouble wrapping my head around a algorithm I’m try to implement. I have two lists and want to take particular combinations from the two lists. Here’s an example. names = ['a', 'b'] numbers = [1, 2] the output in this case would…
user1735075
  • 3,221
  • 4
  • 16
  • 16
164
votes
36 answers

Generate list of all possible permutations of a string

How would I go about generating a list of all possible permutations of a string between x and y characters in length, containing a variable list of characters. Any language would work, but it should be portable.
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
133
votes
6 answers

How can I get "permutations with repetitions/replacement" from a list (Cartesian product of a list with itself)?

Suppose I have a list die_faces = [1, 2, 3, 4, 5, 6]. I want to generate all 36 possible results for rolling two dice: (1, 1), (1, 2), (2, 1) etc. If I try using permutations from the itertools standard library: >>> import itertools >>> die_faces =…
Bwmat
  • 4,314
  • 3
  • 27
  • 42
127
votes
13 answers

Fast permutation -> number -> permutation mapping algorithms

I have n elements. For the sake of an example, let's say, 7 elements, 1234567. I know there are 7! = 5040 permutations possible of these 7 elements. I want a fast algorithm comprising two functions: f(number) maps a number between 0 and 5039 to a…
ijw
  • 4,376
  • 3
  • 25
  • 29
89
votes
4 answers

Cartesian product of a dictionary of lists

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…
Seth Johnson
  • 14,762
  • 6
  • 59
  • 85
88
votes
6 answers

Generating permutations lazily

I'm looking for an algorithm to generate permutations of a set in such a way that I could make a lazy list of them in Clojure. i.e. I'd like to iterate over a list of permutations where each permutation is not calculated until I request it, and all…
Brian Carper
  • 71,150
  • 28
  • 166
  • 168
88
votes
12 answers

Generate all permutations of a list without adjacent equal elements

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…
georg
  • 211,518
  • 52
  • 313
  • 390
83
votes
15 answers

Cartesian product of 2 lists in Haskell

I wish to produce the Cartesian product of 2 lists in Haskell, but I cannot work out how to do it. The cartesian product gives all combinations of the list elements: xs = [1,2,3] ys = [4,5,6] cartProd :: [a] -> [b] -> [(a,b)] cartProd xs ys ==>…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
71
votes
12 answers

Generating all Possible Combinations

Given 2 arrays Array1 = {a,b,c...n} and Array2 = {10,20,15....x} how can I generate all possible combination as Strings a(i) b(j) c(k) n(p) where 1 <= i <= 10, 1 <= j <= 20 , 1 <= k <= 15, .... 1 <= p <= x Such as: a1 b1 c1 .... n1 a1 b1…
Amitd
  • 4,769
  • 8
  • 56
  • 82
68
votes
14 answers

Creating all possible k combinations of n items in C++

There are n people numbered from 1 to n. I have to write a code which produces and print all different combinations of k people from these n. Please explain the algorithm used for that.
Prannoy Mittal
  • 1,525
  • 5
  • 21
  • 32
65
votes
17 answers

Combinatoric 'N choose R' in java math?

Is there a built in method in a java library that can compute 'N choose R' for any N, R?
Aly
  • 15,865
  • 47
  • 119
  • 191
63
votes
34 answers

How can I print out all possible letter combinations a given phone number can represent?

I just tried for my first programming interview and one of the questions was to write a program that given a 7 digit telephone number, could print all possible combinations of letters that each number could represent. A second part of the question…
Salaban
  • 683
  • 1
  • 6
  • 4
52
votes
5 answers

Set partitions in Python

I have an array of [1,2,3] I want to make all the possible combinations using all elements of the array: Result: [[1], [2], [3]] [[1,2], [3]] [[1], [2,3]] [[1,3], [2]] [[1,2,3]]
user2880257
  • 537
  • 1
  • 4
  • 3
52
votes
10 answers

Non-redundant version of expand.grid

The R function expand.grid returns all possible combination between the elements of supplied parameters. e.g. > expand.grid(c("aa", "ab", "cc"), c("aa", "ab", "cc")) Var1 Var2 1 aa aa 2 ab aa 3 cc aa 4 aa ab 5 ab ab 6 cc …
Michele
  • 8,563
  • 6
  • 45
  • 72
1
2 3
99 100