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

In summary, Permutations can be calculated by using the algorithm permute(n,A) which permutes n objects in the list A. There are n! possible permutations for an array of length n. A function f(x, y) can be used to describe the value of a specific permutation in the xth position. This can be done using the parametrization of permutations known as Young Tableaux.
  • #1
yetar
54
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
  • #2
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
 
  • #3
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?
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 

1. What is a permutation?

A permutation is an arrangement of objects or values in a specific order. It is a way to rearrange a set of items and is often used in mathematics and statistics.

2. How do you calculate permutations?

To calculate permutations, you can use the formula nPr = n! / (n-r)!, where n is the total number of items and r is the number of items being selected. For example, if you have 5 items and want to select 3, the calculation would be 5P3 = 5! / (5-3)! = 5! / 2! = (5x4x3x2x1) / (2x1) = 60.

3. What is the difference between a permutation and a combination?

A permutation is an arrangement of objects in a specific order, while a combination is a selection of objects without regard to order. For example, the permutations of ABC would be ABC, ACB, BAC, BCA, CAB, CBA, while the combinations would be ABC, AC, BC, AB, A, B, C.

4. How do permutations relate to probability?

Permutations are often used in probability to calculate the number of possible outcomes when a specific number of items are selected. For example, if you have 10 marbles and want to select 3, the probability of selecting a specific set of 3 marbles would be 1/10P3 = 1/120.

5. In what real-life situations are permutations used?

Permutations are used in a variety of real-life situations, including lottery number selection, combination locks, seating arrangements, and computer programming. They can also be used in analyzing and predicting outcomes in games of chance and in data analysis.

Similar threads

Replies
3
Views
809
Replies
2
Views
2K
  • General Math
Replies
1
Views
718
  • General Math
Replies
2
Views
1K
  • General Math
Replies
2
Views
2K
  • General Math
Replies
1
Views
1K
  • General Math
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • General Math
Replies
0
Views
814
Replies
2
Views
2K
Back
Top