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
21
votes
3 answers

The amortized complexity of std::next_permutation?

I just read this other question about the complexity of next_permutation and while I'm satisfied with the response (O(n)), it seems like the algorithm might have a nice amortized analysis that shows a lower complexity. Does anyone know of such an…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
21
votes
2 answers

How to permutate tranposition in tensorflow?

From the docs: Transposes a. Permutes the dimensions according to perm. The returned tensor's dimension i will correspond to the input dimension perm[i]. If perm is not given, it is set to (n-1...0), where n is the rank of the input tensor.…
alvas
  • 115,346
  • 109
  • 446
  • 738
21
votes
11 answers

The most elegant way to generate permutations in SQL server

Given a the following table: Index | Element --------------- 1 | A 2 | B 3 | C 4 | D We want to generate all the possible permutations (without repetitions) using the elements. the final result (skipping some rows) will…
SDReyes
  • 9,798
  • 16
  • 53
  • 92
21
votes
4 answers

Given n and k, return the kth permutation sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3 ) : "123" "132" "213" "231" "312" "321" Given n and k, return the kth permutation…
explorer
  • 737
  • 1
  • 8
  • 23
21
votes
9 answers

Given a BST and its root, print all sequences of nodes which give rise to the same bst

Given a BST, find all sequences of nodes starting from root that will essentially give the same binary search tree. Given a bst, say 3 / \ 1 5 the answer should be 3,1,5 and 3,5,1. another example 5 / \ 4 7 / /…
user2892884
  • 329
  • 1
  • 2
  • 5
21
votes
5 answers

Number of n-element permutations with exactly k inversions

I am trying to efficiently solve SPOJ Problem 64: Permutations. Let A = [a1,a2,...,an] be a permutation of integers 1,2,...,n. A pair of indices (i,j), 1<=i<=j<=n, is an inversion of the permutation A if ai>aj. We are given integers n>0 and…
20
votes
12 answers

Code to enumerate permutations in Scala

I coded a function to enumerate all permutations of a given list. What do you think of the code below? def interleave(x:Int, l:List[Int]):List[List[Int]] = { l match { case Nil => List(List(x)) case (head::tail) => (x :: head ::…
Michael
  • 10,185
  • 12
  • 59
  • 110
20
votes
3 answers

Shuffle list, ensuring that no item remains in same position

I want to shuffle a list of unique items, but not do an entirely random shuffle. I need to be sure that no element in the shuffled list is at the same position as in the original list. Thus, if the original list is (A, B, C, D, E), this result would…
jdeisenberg
  • 201
  • 2
  • 4
20
votes
7 answers

Permute all unique enumerations of a vector in R

I'm trying to find a function that will permute all the unique permutations of a vector, while not counting juxtapositions within subsets of the same element type. For example: dat <- c(1,0,3,4,1,0,0,3,0,4) has factorial(10) > 3628800 possible…
Steve
  • 5,727
  • 10
  • 32
  • 30
20
votes
6 answers

Filter a Set for Matching String Permutations

I am trying to use itertools.permutations() to return all the permutations of the string and return only the ones which are members of a set of words. import itertools def permutations_in_dict(string, words): ''' Parameters ---------- …
Meruemu
  • 611
  • 1
  • 8
  • 28
20
votes
9 answers

Algorithm to apply permutation in constant memory space

I saw this question is a programming interview book, here I'm simplifying the question. Assume you have an array A of length n, and you have a permutation array P of length n as well. Your method will return an array where elements of A will appear…
ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
19
votes
5 answers

How to create a permutation in c++ using STL for number of places lower than the total length

I have a c++ vector with std::pair objects. I am trying to generate permutations of the objects of the vector using std::next_permutation(). However, I want the permutations to be of a given size, you know, similar to…
kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39
19
votes
6 answers

Find all list permutations of splitting a string in Python

I have a string of letters that I'd like to split into all possible combinations (the order of letters must remain fixed), so that: s = 'monkey' becomes: combinations = [['m', 'onkey'], ['mo', 'nkey'], ['m', 'o', 'nkey'] ... etc] Any ideas?
cyrus
  • 1,338
  • 3
  • 17
  • 26
19
votes
4 answers

Torch: How to shuffle a tensor by its rows?

I am currently working in torch to implement a random shuffle (on the rows, the first dimension in this case) on some input data. I am new to torch, so I have some troubles figuring out how permutation works.. The following is supposed to shuffle…
DaveTheAl
  • 1,995
  • 4
  • 35
  • 65
19
votes
11 answers

Randomly permute N first elements of a singly linked list

I have to permute N first elements of a singly linked list of length n, randomly. Each element is defined as: typedef struct E_s { struct E_s *next; }E_t; I have a root element and I can traverse the whole linked list of size n. What is the most…
psihodelia
  • 29,566
  • 35
  • 108
  • 157