How can I efficiently calculate all possible permutations of an array?

Click For Summary

Discussion Overview

The discussion centers around calculating all possible permutations of an array, exploring algorithms for generating these permutations efficiently. Participants delve into both the theoretical aspects of permutations and practical algorithmic implementations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant asks for an efficient algorithm to calculate all permutations of an array of length n.
  • Another participant mentions that the number of permutations is n!, explaining the factorial concept as a product of decreasing choices for each slot in the array.
  • A clarification is made that the initial inquiry was about generating the permutations themselves, not just counting them.
  • A proposed recursive algorithm is shared, detailing how to generate permutations by fixing one element and recursively permuting the remaining elements.
  • One participant notes the impracticality of writing down all permutations for larger n, specifically mentioning that for n=6, there are 720 permutations.
  • A later post introduces a more complex question about finding a function that can determine the value of a specific permutation at a given position, suggesting a desire for a mathematical representation of permutations.
  • Another participant asserts that there is a unique function for this purpose, distinguishing between a function and an algorithm, and references Young Tableaux as a related concept.

Areas of Agreement / Disagreement

Participants express differing views on the feasibility of generating and writing down all permutations, with some acknowledging the exponential growth of permutations as n increases. The discussion about finding a function to describe permutations also indicates a lack of consensus on the approach to take.

Contextual Notes

The discussion includes assumptions about the definitions of permutations and factorials, and the complexity of generating permutations is highlighted without resolving the mathematical intricacies involved.

Who May Find This Useful

This discussion may be useful for individuals interested in combinatorial algorithms, recursive programming techniques, and mathematical representations of permutations.

yetar
Messages
53
Reaction score
0
How do I calculate all possible permutations of an array of length n?
If I draw on a paper, I can do myself permutations of 3 or 4 length arrays.
However, I want an algorithm to calculate all possible permutation. And calculate it as fast as possible.

Do you know how to do it?

I would appreciate any help.
Thanks.
 
Mathematics news on Phys.org
n! that is n factorial

there are n choices for the 1st slot in the array, n-1 for the second, n-2 for the third, etc. multiply them together
 
kesh said:
n! that is n factorial

there are n choices for the 1st slot in the array, n-1 for the second, n-2 for the third, etc. multiply them together
I didnt mean to count how many permutations there are.
What I ment is, how to "write down" those permutation.
Lets say you have some array with n different values.
How do you write down all n! possible permutations of these values?
 
Here is a "recursion" algorithm, permute(n,A), which permutes n objects in the list A(say, abcde...)

If n is 1, write it, go to a new line and stop
else
for i= 1 to n, write ai, permute(n-1,A-ai).

For example, if n= 4, A= {a,b,c,d}
we would have:
a followed by all permutations of {b,c,d}
b followed by all permutations of {a,c,d}
c followed by all permutations of {a,b,d}
d followed by all permutations of {a,b,c}

Of course, "all permutations of {b,c,d} would be
b followed by cd and dc
c followed by bd and db
d followed by bc and cb

So this algorithm would give

abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba

Giving all 4!= 24 permutations.

5 would follow the same pattern except that there would be 5!= 120 of them! Must simpler to do it on a computer.
 
Since there are n! permutations, and n!=720 when n=6 it should be readily apparent the the desire to write them all down is one that cannot be satisfied.
 
Ok, let's make it more difficult.
Is it possible to find a function f(x, y) that will describe n! permutations of a n sized array of natural numbers?
You have created the permutations recursivly, but I wish for a number of permutation y and a position in the permutation x, to know what is the value of that specific permutation in the xth position.
The permutations may be arranged in any order, but I find it hard to even find such a function for n=3.
 
There is exactly one function that does what you want. You're confusing 'function' with 'algorithm to evaluate the function at a given input'.

Permutations are parametrized by Young Tableaux. Google for them.
 

Similar threads

Replies
17
Views
9K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K