Click or drag to resize

Permutation Class

Represents a specific arrangement of distinct picks from a supplied range.
Inheritance Hierarchy
SystemObject
  Kaos.CombinatoricsPermutation

Namespace:  Kaos.Combinatorics
Assembly:  KaosCombinatorics (in KaosCombinatorics.dll) Version: 5.0.0.6
Syntax
C#
public class Permutation : IComparable, 
	IEnumerable, IComparable<Permutation>, IEquatable<Permutation>, 
	IEnumerable<int>

The Permutation type exposes the following members.

Constructors
  NameDescription
Public methodPermutation
Initializes an empty permutation instance.
Public methodCode examplePermutation(Int32)
Initializes a new permutation of Rank 0 with the supplied number of elements.
Public methodCode examplePermutation(Int32)
Initializes a new permutation from the elements supplied in source.
Public methodPermutation(Permutation)
Initializes a new instance that is copied from the supplied permutation.
Public methodCode examplePermutation(Int32, Int32)
Initializes a new permutation of Rank 0 with the supplied number of picks from the supplied number of choices.
Public methodPermutation(Int32, Int32)
Initializes a new permutation from the elements supplied in source picked from the supplied number of choices.
Public methodCode examplePermutation(Int32, Int32, Int64)
Initializes a new permutation of the supplied rank with the supplied number of picks from the supplied number of choices.
Top
Properties
  NameDescription
Public propertyChoices
Number of available choices for the elements of the Permutation.
Public propertyCode exampleItem
Get an element of the Permutation at the supplied column.
Public propertyStatic memberMaxChoices
Returns the maximum number of elements that may be in a Permutation.
Public propertyPicks
Number of elements in the Permutation.
Public propertyCode examplePlainRank
Row index of the sequence in the plain ordered Permutation table.
Public propertyCode exampleRank
Row index of the sequence in the lexicographically ordered Permutation table.
Public propertyRowCount
Returns number of distinct possible arrangements of this Permutation.
Public propertyCode exampleSwaps
Returns number of element swaps needed to transform this Permutation into Rank 0.
Top
Methods
  NameDescription
Public methodCode exampleBacktrack
Advance Rank a minimum while changing element at nodeIndex.
Public methodCompareTo(Object)
Compare 2 Permutations.
Public methodCompareTo(Permutation)
Compare 2 Permutations.
Public methodCopyTo
Copy the entire sequence to the supplied destination.
Public methodEquals(Object)
Indicate whether 2 Permutations have the same value.
(Overrides ObjectEquals(Object).)
Public methodEquals(Permutation)
Indicate whether 2 Permutations have the same value.
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodCode exampleGetEnumerator
Enumerate all elements of a Permutation.
Public methodGetHashCode
Get the hash oode of the Permutation.
(Overrides ObjectGetHashCode.)
Public methodCode exampleGetRows
Iterate thru all rows of the Permutation table for every value of Rank ascending.
Public methodCode exampleGetRowsForAllChoices
Iterate thru all rows of all Permutation tables for every Choices value in the range (1..Choices).
Public methodCode exampleGetRowsForAllPicks
Iterate thru all rows of all Permutation tables for every Picks value in the range (1..Picks).
Public methodCode exampleGetRowsOfPlainChanges
Iterate thru all rows of the Permutation table while swapping only 2 values in each result.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodStatic memberCode examplePermuteT
Apply a Permutation sequence to rearrange the supplied list or array.
Public methodCode exampleToString
Provide a readable form of the Permutation sequence.
(Overrides ObjectToString.)
Top
Operators
  NameDescription
Public operatorStatic memberEquality
Indicate whether 2 Permutations are equal.
Public operatorStatic memberGreaterThan
Indicate whether the left Permutation is greater than the right Permutation.
Public operatorStatic memberGreaterThanOrEqual
Indicate whether the left Permutation is greater than or equal to the right Permutation.
Public operatorStatic memberInequality
Indicate whether 2 Permutations are not equal.
Public operatorStatic memberLessThan
Indicate whether the left Permutation is less than the right Permutation.
Public operatorStatic memberLessThanOrEqual
Indicate whether the left permutation is less than or equal to the right permutation.
Top
Remarks

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

Examples

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,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