How to resolve the algorithm Permutations/Rank of a permutation step by step in the Julia programming language
How to resolve the algorithm Permutations/Rank of a permutation step by step in the Julia programming language
Table of Contents
Problem Statement
A particular ranking of a permutation associates an integer with a particular ordering of all the permutations of a set of distinct items. For our purposes the ranking will assign integers
0.. ( n ! − 1 )
{\displaystyle 0..(n!-1)}
to an ordering of all the permutations of the integers
0.. ( n − 1 )
{\displaystyle 0..(n-1)}
. For example, the permutations of the digits zero to 3 arranged lexicographically have the following rank: Algorithms exist that can generate a rank from a permutation for some particular ordering of permutations, and that can generate the same rank from the given individual permutation (i.e. given a rank of 17 produce (2, 3, 1, 0) in the example above). One use of such algorithms could be in generating a small, random, sample of permutations of
n
{\displaystyle n}
items without duplicates when the total number of permutations is large. Remember that the total number of permutations of
n
{\displaystyle n}
items is given by
n !
{\displaystyle n!}
which grows large very quickly: A 32 bit integer can only hold
12 !
{\displaystyle 12!}
, a 64 bit integer only
20 !
{\displaystyle 20!}
. It becomes difficult to take the straight-forward approach of generating all permutations then taking a random sample of them. A question on the Stack Overflow site asked how to generate one million random and indivudual permutations of 144 items.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Permutations/Rank of a permutation step by step in the Julia programming language
The provided Julia code demonstrates the generation and display of permutations and ranks of objects using built-in Julia functions. Here's a detailed explanation:
-
Generating Permutations:
nobjs
is the number of objects we want to permute.a
is a vector containing integers from 1 tonobjs
, representing the objects.- The
nthperm
function is used to generate thei
-th permutation of the objects ina
. - A loop iterates through all possible permutations (from 1 to
factorial(nobjs)
) and prints the permutation and its rank.
-
Calculating Rank:
- The
nthperm
function is also used to find the rank of a given permutation. prank
represents the rank of the current permutation.
- The
-
Generating Random Permutations:
- This part of the code generates
nsamp
random permutations ofnobjs
objects without repeating any ranks. - The
randperm
function creates a random permutation of the objects. - The
nthperm
function calculates the rank of the random permutation. - The loop ensures that no rank is repeated by checking if
prank
is already inptaken
, an array that stores the ranks of previously generated permutations. - If
prank
is already inptaken
, the code generates a new random permutation until a unique rank is found.
- This part of the code generates
-
Printing Output:
- The code prints the generated permutations and their ranks.
Overall, this code provides a clear demonstration of how to use Julia's built-in functions to manipulate permutations and ranks, a common operation in combinatorial problems.
Source code in the julia programming language
using Printf
nobjs = 4
a = collect(1:nobjs)
println("All permutations of ", nobjs, " objects:")
for i in 1:factorial(nobjs)
p = nthperm(a, i)
prank = nthperm(p)
print(@sprintf("%5d => ", i))
println(p, " (", prank, ")")
end
nobjs = 12
nsamp = 4
ptaken = Int[]
println()
println(nsamp, " random permutations of ", nobjs, " objects:")
for i in 1:nsamp
p = randperm(nobjs)
prank = nthperm(p)
while prank in ptaken
p = randperm(nobjs)
prank = nthperm(p)
end
push!(ptaken, prank)
println(" ", p, " (", prank, ")")
end
You may also check:How to resolve the algorithm Roots of unity step by step in the AWK programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Documentation step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Koch curve step by step in the Racket programming language