Algorithm (full permutation algorithm encapsulation)

Source: Internet
Author: User

This algorithm is one of the whole arrangement in the textbook, I only do encapsulation, in this thanks to find algorithms and propagation algorithm of the Daniel.
    <summary>///full permutation algorithm, algorithm principle: Perm (n) =[n]*pern (n-1).    The full arrangement of n equals the number of n is placed in the nth position, and the remaining number of N-1 is all arranged.    One use of this algorithm is to perform determinant expansion and calculation, which is the purpose of this encapsulation algorithm. </summary> public class Permulation {//<summary>///&LT;/SUMMARY&G        T        Private list<list<int>> _permarray {get; set;}        <summary>///to reuse.        </summary> private dictionary<string, list<int>> _norepeatarray {get; set;}        <summary>///array of integers///</summary> private int[] _numbers {get; set;}        <summary>///number of elements///</summary> private int _n {get; set;}        <summary>//whether to go heavy.                </summary> private bool _removedup {get; set;}        <summary>////Full rank count///</summary> public int TotalCount {get; set;} <summary>/Arrange results///</summary> public list<list<int>> Permulationarray {get            {return _permarray; }}///<summary>///For any given array of numbers///</summary>//<param name= "Numb ERs "> Arrays </param>//<param name=" Removedup "> whether to redo </param> public permulation (int[] Numbe            RS, bool Removedup = false) {_norepeatarray = new dictionary<string, list<int>> ();            _permarray = new list<list<int>> ();            TotalCount = 0;            _numbers = Numbers;            _n = Numbers.count ();        _removedup = Removedup; }///<summary>//Natural number 1-n full arrangement///</summary>//<param name= "N" ></param&        Gt            Public permulation (int N) {_permarray = new list<list<int>> ();            TotalCount = 0; _numbers = new Int[n];            for (int i = 1; I <= N; i++) {_numbers[i-1] = i;        } _n = N;        }///<summary>//Exchange location.        </summary>//<param name= "Nums" ></param>//<param name= "I" ></param> <param name= "J" ></param> private void Swap (int[] Nums, int i, int j) {int T            Hetemp = Nums[i-1];            NUMS[I-1] = nums[j-1];        Nums[j-1] = thetemp;            }///<summary>/////</summary> public void docalculation () {        Doarray (1);        }///<summary>//The recursive algorithm is fully arranged.        </summary>//<param name= "Nextindex" ></param> private void Doarray (int nextindex)                {if (Nextindex > _n) {var thenums = new list<int> (); Using the dictionaryThe body's string hash algorithm is weighed.                var theseqstr = "";                    for (int i = 0; i < _n; i++) {//Note that this needs to be segmented to prevent duplication caused by 1 23 and 12 3.                    Theseqstr + = "," + _numbers[i];                Thenums.add (_numbers[i]);                    } if (_removedup) {if (!_norepeatarray.containskey (THESEQSTR))                        {_norepeatarray.add (theseqstr, thenums);                        _permarray.add (thenums);                    totalcount++;                    }} else {_permarray.add (thenums);                totalcount++;                }} else {//is exchanged with all subsequent locations, but note that each time the exchange is completed, it should be restored.                    for (int i = nextindex; I <= _n; i++) {Swap (_numbers, Nextindex, i);                    Doarray (Nextindex + 1); Restoration                   Swap (_numbers, Nextindex, i); }            }        }    }


Note: This algorithm is simply tested and not tested in large quantities.

Algorithm (full permutation algorithm encapsulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.