Questions tagged [permutation]

A permutation is an arrangement of objects into a particular order.

A permutation is an arrangement of objects into a particular order.

For example, permutations of a set such as {1,2,3} are (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1). The number of permutation of n objects is n! = n*(n-1)*(n-2)*...*3*2*1

References

5231 questions
79
votes
18 answers

Generating permutations of a set (most efficiently)

I would like to generate all permutations of a set (a collection), like so: Collection: 1, 2, 3 Permutations: {1, 2, 3} {1, 3, 2} {2, 1, 3} {2, 3, 1} {3, 1, 2} {3, 2, 1} This…
SimpleVar
  • 14,044
  • 4
  • 38
  • 60
68
votes
4 answers

All possible permutations of a set of lists in Python

In Python I have a list of n lists, each with a variable number of elements. How can I create a single list containing all the possible permutations: For example [ [ a, b, c], [d], [e, f] ] I want [ [a, d, e] , [a, d, f], [b, d, e], [b, d, f],…
Ian Davis
  • 1,209
  • 1
  • 9
  • 8
68
votes
14 answers

Generate all binary strings of length n with k bits set

What's the best algorithm to find all binary strings of length n that contain k bits set? For example, if n=4 and k=3, there are... 0111 1011 1101 1110 I need a good way to generate these given any n and any k so I'd prefer it to be done with…
kevmo314
  • 4,223
  • 4
  • 32
  • 43
62
votes
13 answers

Generating all distinct permutations of a list in R

I'm trying to create a list of permutations of a list, such that, for example, perms(list("a", "b", "c")) returns list(list("a", "b", "c"), list("a", "c", "b"), list("b", "a", "c"), list("b", "c", "a"), list("c", "a", "b"), list("c", "b",…
tresbot
  • 1,570
  • 2
  • 15
  • 19
53
votes
6 answers

Why does Python's itertools.permutations contain duplicates? (When the original list has duplicates)

It is universally agreed that a list of n distinct symbols has n! permutations. However, when the symbols are not distinct, the most common convention, in mathematics and elsewhere, seems to be to count only distinct permutations. Thus the…
ShreevatsaR
  • 38,402
  • 17
  • 102
  • 126
51
votes
2 answers

What does this list permutations implementation in Haskell exactly do?

I am studying the code in the Data.List module and can't exactly wrap my head around this implementation of permutations: permutations :: [a] -> [[a]] permutations xs0 = xs0 : perms xs0 [] where perms [] _ = [] …
tonlika
  • 707
  • 2
  • 7
  • 12
50
votes
8 answers

Finding n-th permutation without computing others

Given an array of N elements representing the permutation atoms, is there an algorithm like that: function getNthPermutation( $atoms, $permutation_index, $size ) where $atoms is the array of elements, $permutation_index is the index of the…
Simone Margaritelli
  • 4,584
  • 10
  • 45
  • 70
49
votes
20 answers

Are there any better methods to do permutation of string?

void permute(string elems, int mid, int end) { static int count; if (mid == end) { cout << ++count << " : " << elems << endl; return ; } else { for (int i = mid; i <= end; i++) { swap(elems, mid, i); …
Jichao
  • 40,341
  • 47
  • 125
  • 198
43
votes
13 answers

Permutation algorithm without recursion? Java

I would like to get all combination of a number without any repetition. Like 0.1.2, 0.2.1, 1.2.0, 1.0.2, 2.0.1, 2.1.0. I tried to find an easy scheme, but couldn't. I drew a graph/tree for it and this screams to use recursion. But I would like to do…
Andreas Hornig
  • 2,439
  • 5
  • 28
  • 36
42
votes
6 answers

How to generate all permutations of a string in PHP?

I need an algorithm that return all possible combination of all characters in one string. I've tried: $langd = strlen($input); for($i = 0;$i < $langd; $i++){ $tempStrang = NULL; $tempStrang .= substr($input, $i, 1); for($j = $i+1, $k=0;…
Johan
  • 18,814
  • 30
  • 70
  • 88
40
votes
12 answers

Permutations - all possible sets of numbers

I have numbers, from 0 to 8. I would like in result, all possible sets of those numbers, each set should use all numbers, each number can occur only once in a set. I would like to see solution made in PHP that could print out result. Or, at least,…
Deele
  • 3,728
  • 2
  • 33
  • 51
40
votes
11 answers

Recursively print all permutations of a string (Javascript)

I've seen versions of this question for other languages, but not for JS. Is it possible to do this recursively in one function? I understand that I need to take the first element in the string, and then append it to each solution to the recursion on…
singmotor
  • 3,930
  • 12
  • 45
  • 79
40
votes
16 answers

How to tell if an array is a permutation in O(n)?

Input: A read-only array of N elements containing integer values from 1 to N (some integer values can appear more than once!). And a memory zone of a fixed size (10, 100, 1000 etc - not depending on N). How to tell in O(n) if the array represents a…
INS
  • 10,594
  • 7
  • 58
  • 89
40
votes
14 answers

counting combinations and permutations efficiently

I have some code to count permutations and combinations, and I'm trying to make it work better for large numbers. I've found a better algorithm for permutations that avoids large intermediate results, but I still think I can do better for…
Christian Oudard
  • 48,140
  • 25
  • 66
  • 69
38
votes
11 answers

Generating all 5 card poker hands

This problem sounds simple at first glance, but turns out to be a lot more complicated than it seems. It's got me stumped for the moment. There are 52c5 = 2,598,960 ways to choose 5 cards from a 52 card deck. However, since suits are interchangeable…
Nick Johnson
  • 100,655
  • 16
  • 128
  • 198