Permutation Class |
Namespace: Kaos.Combinatorics
public class Permutation : IComparable, IEnumerable, IComparable<Permutation>, IEquatable<Permutation>, IEnumerable<int>
The Permutation type exposes the following members.
Name | Description | |
---|---|---|
Permutation | Initializes an empty permutation instance. | |
Permutation(Int32) | Initializes a new permutation of Rank 0 with the supplied number of elements. | |
Permutation(Int32) | Initializes a new permutation from the elements supplied in source. | |
Permutation(Permutation) | Initializes a new instance that is copied from the supplied permutation. | |
Permutation(Int32, Int32) |
Initializes a new permutation of Rank 0
with the supplied number of picks from the supplied number of choices.
| |
Permutation(Int32, Int32) |
Initializes a new permutation from the elements supplied in source picked
from the supplied number of choices.
| |
Permutation(Int32, Int32, Int64) |
Initializes a new permutation of the supplied rank
with the supplied number of picks from the supplied number of choices.
|
Name | Description | |
---|---|---|
Choices | Number of available choices for the elements of the Permutation. | |
Item | Get an element of the Permutation at the supplied column. | |
MaxChoices |
Returns the maximum number of elements that may be in a Permutation.
| |
Picks | Number of elements in the Permutation. | |
PlainRank | Row index of the sequence in the plain ordered Permutation table. | |
Rank | Row index of the sequence in the lexicographically ordered Permutation table. | |
RowCount | Returns number of distinct possible arrangements of this Permutation. | |
Swaps |
Returns number of element swaps needed to transform this Permutation into Rank 0.
|
Name | Description | |
---|---|---|
Backtrack | Advance Rank a minimum while changing element at nodeIndex. | |
CompareTo(Object) | Compare 2 Permutations. | |
CompareTo(Permutation) | Compare 2 Permutations. | |
CopyTo | Copy the entire sequence to the supplied destination. | |
Equals(Object) | Indicate whether two Permutations have the same value. (Overrides ObjectEquals(Object).) | |
Equals(Permutation) | Indicate whether two Permutations have the same value. | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetEnumerator | Enumerate all elements of a Permutation. | |
GetHashCode | Get the hash oode of the Permutation. (Overrides ObjectGetHashCode.) | |
GetRows | Iterate thru all rows of the Permutation table for every Rank ascending. | |
GetRowsForAllChoices | ||
GetRowsForAllPicks | ||
GetRowsOfPlainChanges |
Iterate thru all rows of the Permutation table
while swapping only two values in each result.
| |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
PermuteT | Apply a Permutation sequence to rearrange the supplied list or array. | |
ToString | Provide a readable form of the Permutation sequence. (Overrides ObjectToString.) |
Name | Description | |
---|---|---|
Equality | Indicate whether 2 Permutations are equal. | |
GreaterThan | Indicate whether the left Permutation is greater than
the right Permutation. | |
GreaterThanOrEqual | Indicate whether the left Permutation is greater than
or equal to the right Permutation. | |
Inequality | Indicate whether 2 Permutations are not equal. | |
LessThan | Indicate whether the left Permutation is less than
the right Permutation. | |
LessThanOrEqual | Indicate whether the left permutation is less than or equal to
the right permutation. |
The defining variables of a permutation are n which is the number of possible choices and k which is the number of distinct picks from those choices. When k is less than n, this is a k-permutation also known as a variation. Permutations are contrasted to combinations where the arrangement is generally not significant (except for ranking).
The Permutation class uses the inherent sequencing of the elements to arrange the rows into an lexicographically ordered table. Support for k-permutations is provided by supplying a picks value that is less than the supplied choices value to the appropriate constructors.
Use the Choices property to get the number of elements to choose from. Use the Picks property to get the number of elements of a Permutation. Use the RowCount property to get the number of distinct possible sequences of a Permutation. Use the indexer to get a specified element of the sequence. Use the default enumerator to iterate thru the elements of a Permutation. Use the Permute method to rearrange a supplied array based on the current sequence.
Use the Rank property to get or set the row index in a lexicographically ordered Permutation table of all possible sequences. Use GetRows to iterate thru all possible sequences of the Permutation ordered by Rank. Use GetRowsForAllChoices to iterate thru every table of all choices in the range (1..Choices). Use GetRowsForAllPicks to iterate thru every table of all picks in the range (1..Picks).
Use the PlainRank property to get or set the row index in a table ordered for plain changes where adjacent rows differ by only a single swap of 2 adjacent elements. Use GetRowsOfPlainChanges to iterate thru all possible sequences of a Permutation ordered by PlainRank.
Use the Swaps property to get the number of element swaps that would transform a row into the sequence of Rank 0. Use the Backtrack method to minimally advance Rank while changing a specified element.
The default appearance of a Permutation row is a list of integers (starting at 0) enclosed in braces. The appearance may be tailored 3 ways:
For more information about permutations and k-permutations, see:
https://en.wikipedia.org/wiki/Permutation
https://en.wikipedia.org/wiki/Eight_queens_puzzle
Iterating thru new Permutation (4).GetRows() produces:
{ 0, 1, 2, 3 }
{ 0, 1, 3, 2 }
{ 0, 2, 1, 3 }
{ 0, 2, 3, 1 }
{ 0, 3, 1, 2 }
{ 0, 3, 2, 1 }
{ 1, 0, 2, 3 }
{ 1, 0, 3, 2 }
{ 1, 2, 0, 3 }
{ 1, 2, 3, 0 }
{ 1, 3, 0, 2 }
{ 1, 3, 2, 0 }
{ 2, 0, 1, 3 }
{ 2, 0, 3, 1 }
{ 2, 1, 0, 3 }
{ 2, 1, 3, 0 }
{ 2, 3, 0, 1 }
{ 2, 3, 1, 0 }
{ 3, 0, 1, 2 }
{ 3, 0, 2, 1 }
{ 3, 1, 0, 2 }
{ 3, 1, 2, 0 }
{ 3, 2, 0, 1 }
{ 3, 2, 1, 0 }
Iterating thru new Permutation (4, 3).GetRows() produces:
{ 0, 1, 2 }
{ 0, 1, 3 }
{ 0, 2, 1 }
{ 0, 2, 3 }
{ 0, 3, 1 }
{ 0, 3, 2 }
{ 1, 0, 2 }
{ 1, 0, 3 }
{ 1, 2, 0 }
{ 1, 2, 3 }
{ 1, 3, 0 }
{ 1, 3, 2 }
{ 2, 0, 1 }
{ 2, 0, 3 }
{ 2, 1, 0 }
{ 2, 1, 3 }
{ 2, 3, 0 }
{ 2, 3, 1 }
{ 3, 0, 1 }
{ 3, 0, 2 }
{ 3, 1, 0 }
{ 3, 1, 2 }
{ 3, 2, 0 }
{ 3, 2, 1 }
Iterating thru new Permutation (4).GetRowsOfPlainChanges() produces:
{ 0, 1, 2, 3 }
{ 0, 1, 3, 2 }
{ 0, 3, 1, 2 }
{ 3, 0, 1, 2 }
{ 3, 0, 2, 1 }
{ 0, 3, 2, 1 }
{ 0, 2, 3, 1 }
{ 0, 2, 1, 3 }
{ 2, 0, 1, 3 }
{ 2, 0, 3, 1 }
{ 2, 3, 0, 1 }
{ 3, 2, 0, 1 }
{ 3, 2, 1, 0 }
{ 2, 3, 1, 0 }
{ 2, 1, 3, 0 }
{ 2, 1, 0, 3 }
{ 1, 2, 0, 3 }
{ 1, 2, 3, 0 }
{ 1, 3, 2, 0 }
{ 3, 1, 2, 0 }
{ 3, 1, 0, 2 }
{ 1, 3, 0, 2 }
{ 1, 0, 3, 2 }
{ 1, 0, 2, 3 }