本博客所有文章分類的總目錄:本博客博文總目錄-實時更新
本博客其他.NET開源項目文章目錄:【目錄】本博客其他.NET開源項目文章目錄
前言
本文今天介紹的.NET開源組件是 KwCombinatorics,它是.NET平台一個高效的生成排列組合序列的開源類庫,它提供了4種生成排列與組合序列的方式。雖然原理和功能都很簡單,但是這個類庫在軟件測試、組合數學以及密碼學等方面都有很大的用處。很早就接觸了這個類庫,以前在一些小程序中也使用過,有時候為了遍歷所有可能的組合,自己去寫循環,生成,的確很繁瑣,有了 KwCombinatorics 之后,都變得簡單寫了,接下來將詳細介紹該類庫的使用。1.Permutation類基本介紹
Permutation類是根據指定的對象列表,選擇一些不同的對象形成一個排列,排列是有順序的,而且每一次不重復選擇同一個對象。其實有了第一篇的基礎,這個類的使用也很簡單,還是先看看Permutation類的初始化,幾個主要的構造函數如下:
1 Permutation()//Make an empty Permutation. 2 Permutation(Int32)//Make a new Permutation of all the supplied number of choices with a Rank of 0. 3 Permutation(Int32[])//Make a new Permutation from the supplied elements. 4 Permutation(Permutation)//Make a copy of a Permutation. 5 Permutation(Int32,Int32)//Make a new Permutation with picks number of elements taken from a possible number of choices of Rank 0. 6 Permutation(Int32[],Int32)//Make a new Permutation from the supplied elements taken from the available number of choices. 7 Permutation(Int32,Int32,Int64)//Make a new Permutation with picks number of elements taken from a possible number of choices of the supplied rank.
參數主要有下面幾個注意點,參數的意義和Combination類似:
choices:可供選擇的數的個數,如果不提供實際的源數據source,默認就是從0開始的整數;
picks:要選擇組合序列的個數
source:可以直接用初始化列表,而不是固定的從0開始
rank:這個屬性是我認為使用這個組件最強大的地方,因為是按照升序生成所有的組合序列, 而rank就是指定你要選擇的在整個組合序列中當前rank位置的組合序列。
下面用幾個例子說明幾個主要方法的使用情況。
2.獲取所有N選K的排列列表
設從{0,1,2}3個元素中,每次取2個的排列,所有的排列情況有哪些呢?直接上代碼,比較容易看得懂:
1 var pn = new Permutation (choices:3,picks:2); 2 3 Console.WriteLine ("n={0} , picks={1}", pn.Choices,pn.Picks); 4 5 foreach (var row in pn.GetRows()) 6 Console.WriteLine ("{0,2}: {1}", row.Rank, row);
運行結果如下:
1 n=3 , picks=2 2 0: { 0, 1 } 3 1: { 0, 2 } 4 2: { 1, 0 } 5 3: { 1, 2 } 6 4: { 2, 0 } 7 5: { 2, 1 }
同理,要獲取當前數據源,所有可能的組合情況呢,Permutation也提供了GetRowsForAllPicks方法,看代碼:
1 var pn = new Permutation (choices:3,picks:2); 2 3 Console.WriteLine ("n={0} , picks={1}", pn.Choices,pn.Picks); 4 5 foreach (var row in pn.GetRowsForAllPicks()) 6 Console.WriteLine ("{0,2}: {1}", row.Rank, row);
結果:
1 n=3 , picks=2 2 0: { 0 } 3 1: { 1 } 4 2: { 2 } 5 0: { 0, 1 } 6 1: { 0, 2 } 7 2: { 1, 0 } 8 3: { 1, 2 } 9 4: { 2, 0 } 10 5: { 2, 1 }
3.任意對象列表的N選K排列
1 var things = new List<object>{"apple","bench","chair"}; 2 3 foreach (var row in new Permutation (things.Count,2).GetRows()) 4 { 5 foreach (var mix in Permutation.Permute (row, things)) 6 Console.Write ("{0} ", mix); 7 Console.WriteLine (); 8 }
結果如下:
1 apple bench 2 apple chair 3 bench apple 4 bench chair 5 chair apple 6 chair bench
當然其他對象也類似,大家可以依次類推。
4.高級—獲取任意Rank位置的組合
這個類也提供了Rank功能,也是需要獲取指定位置的排列,為了和組合的對比,我們采用了幾乎一樣的代碼,但參數不一樣,看代碼和結果就知道了:
1 //初始化一個排列,從4個數中,選擇2個的所有排列中,取位置2的排列(從0開始) 2 var pn = new Permutation (choices:4, picks:2, rank:2); 3 4 Console.WriteLine ("{0} n={1}, k={2}, rank={3}\n", pn, pn.Choices, pn.Picks, pn.Rank); 5 6 //設置Rank為-1,默認取最后一個位置的排列 7 pn.Rank = -1; 8 string text = pn.ToString() + " n=" + pn.Choices + ", k=" + pn.Picks + ", last=" + pn.Rank; 9 Console.WriteLine (text); 10 11 //將當前Rank+1,的排列 12 pn.Rank = pn.Rank + 1; 13 Console.WriteLine ("\n{0} n={1}, k={2}, rank={3}", pn, pn.Choices, pn.Picks, pn.Rank);
結果:
1 { 0, 3 } n=4, k=2, rank=2 2 3 { 3, 2 } n=4, k=2, last=11 4 5 { 0, 1 } n=4, k=2, rank=0
5.資源
資源參考前一篇文章的資源,幾乎一樣:開源.NET排列組合組件KwCombinatorics使用(一)——組合生成類
如果本文資源或者顯示有問題,請參考 本文原文地址:http://www.cnblogs.com/asxinyu/p/4261451.html
