C # permutation and combination classes
//-----------------------------------------------------------------------------
//
Algorithm: Permutation combination class
//
Copyright (C) snowdust
Personal blog Http://blog.csdn.net/snowdust & http://snowdust.cnblogs.com
MSN & Email [email protected]
//
This source code is free for all kinds of software (including commercial software)
Allow further modification and development of this Code
However, you must keep this copyright information intact.
//
The calling method is as follows:
//
1.GetPermutation (t[], StartIndex, EndIndex)
Arranges startindex to Endindex, remaining elements unchanged
//
2.GetPermutation (t[])
Returns the full array of all elements
//
3.GetPermutation (t[], N)
Returns the arrangement of n elements in an array
//
4.GetCombination (t[], N)
Returns a combination of n elements in an array
//
Version history:
V0.1 2010-01-20 Summary: first-time creation
//
//-----------------------------------------------------------------------------
Using System;
Using System.Collections.Generic;
namespace algorithms
{
public class Permutationandcombination<t>
{
<summary>
Swap two variables
</summary>
<param name= "A" > Variable 1</param>
<param name= "B" > variable 2</param>
public static void Swap (Ref t A, ref T B)
{
T temp = A;
A = b;
b = temp;
}
<summary>
Recursive algorithm for array combination (Private member)
</summary>
<param name= "List" > Return model </param>
<param name= "T" > Array </param>
<param name= "n" > Auxiliary variables </param>
<param name= "M" > Auxiliary variables </param>
<param name= "B" > Auxiliary array </param>
<param name= "M" > Auxiliary variable m</param>
private static void Getcombination (ref list<t[]> List, t[] T, int n, int m, int[] b, int m)
{
for (int i = n; i >= m; i--)
{
B[m-1] = i-1;
if (M > 1)
{
Getcombination (ref list, T, I-1, m-1, B, M);
}
Else
{
if (list = = null)
{
List = new list<t[]> ();
}
t[] temp = new T[m];
for (int j = 0; J < B.length; J + +)
{
TEMP[J] = t[b[j]];
}
List. ADD (temp);
}
}
}
<summary>
Recursive algorithm permutation (private member)
</summary>
<param name= "List" > returned lists </param>
<param name= "T" > Array </param>
<param name= "StartIndex" > Start label </param>
<param name= "EndIndex" > End label </param>
private static void Getpermutation (ref list<t[]> List, t[] T, int startIndex, int endIndex)
{
if (StartIndex = = EndIndex)
{
if (list = = null)
{
List = new list<t[]> ();
}
t[] temp = new T[t.length];
T.copyto (temp, 0);
List. ADD (temp);
}
Else
{
for (int i = startIndex; I <= endIndex; i++)
{
Swap (ref T[startindex], ref t[i]);
Getpermutation (ref list, T, StartIndex + 1, endIndex);
Swap (ref T[startindex], ref t[i]);
}
}
}
<summary>
Order from the starting label to the end label, the remaining elements are unchanged
</summary>
<param name= "T" > Array </param>
<param name= "StartIndex" > Start label </param>
<param name= "EndIndex" > End label </param>
<returns> pattern from start label to end label arrangement </returns>
public static list<t[]> getpermutation (t[] T, int startIndex, int endIndex)
{
if (StartIndex < 0 | | endIndex > T.LENGTH-1)
{
return null;
}
list<t[]> list = new list<t[]> ();
Getpermutation (ref list, T, StartIndex, EndIndex);
return list;
}
<summary>
Returns the full array of all elements
</summary>
<param name= "T" > Array </param>
<returns> All-arranged paradigm </returns>
public static list<t[]> getpermutation (t[] T)
{
Return getpermutation (t, 0, t.length-1);
}
<summary>
Find the arrangement of n elements in an array
</summary>
<param name= "T" > Array </param>
<param name= "n" > Number of elements </param>
<returns> arrangement of n elements in an array </returns>
public static list<t[]> getpermutation (t[] T, int n)
{
if (n > T.length)
{
return null;
}
list<t[]> list = new list<t[]> ();
list<t[]> C = getcombination (T, N);
for (int i = 0; i < C.count; i++)
{
list<t[]> L = new list<t[]> ();
Getpermutation (ref L, C[i], 0, n-1);
List. AddRange (l);
}
return list;
}
<summary>
Find the combination of n elements in an array
</summary>
<param name= "T" > Array </param>
<param name= "n" > Number of elements </param>
<returns> pattern of the combination of n elements in an array </returns>
public static list<t[]> getcombination (t[] T, int n)
{
if (T.length < n)
{
return null;
}
int[] temp = new Int[n];
list<t[]> list = new list<t[]> ();
Getcombination (ref list, T, T.length, N, temp, n);
return list;
}
}
}
Call:
int[] arr = new INT[6];
for (int i = 0; i < arr. Length; i++)
{
Arr[i] = i + 1;
}
Arrange
list<int[]> lst_permutation = Algorithms.permutationandcombination<int>. Getpermutation (arr, 3);
Find a combination
list<int[]> lst_combination = Algorithms.permutationandcombination<int>. Getcombination (arr, 3);
C # permutation and combination classes