Non-recursive full-array python implementation

Source: Internet
Author: User

pythonalgorithm

Full Array (permutation)
排列(英语:Permutation)是将相异物件或符号根据确定的顺序重排。每个顺序都称作一个排列。例如,从一到六的数字有720种排列,对应于由这些数字组成的所有不重复亦不阙漏的序列,例如4, 5, 6, 1, 2, 3 与1, 3, 5, 2, 4, 6。【From Wikipedia】从n个相异元素中取出 k个元素,k个元素的排列数量为:

\[{p_{k}^{n}={\frac {n!} {(N-K)!}}} \]

其中P意为Permutation(排列),!表示阶乘运算。全排列而取k为n,则结果为n!。
Full permutation generation algorithm
    1. Dictionary Ordering method

      The dictionary order is to arrange the elements in the Order of the dictionary (A-Z, 1-9). The order of the dictionaries as the basis for comparison, you can compare the size of two strings. For example, "1" < "five" < "+" < "153" is the result of comparing each digit bit by one. For a string "123456789", you can know that the smallest string is "123456789" and the largest string "987654321". In this way, the string is generated by the dictionary sequence method to generate the full array, which is to generate "123456789", "123456798"->......-> "987654312", "987654321" and so on. The dictionary ordering method requires that this is the same prefix as the next with the longest possible, that is, the change is limited to the shortest possible suffix.

    2. The algorithm of Neighbor transposition is first proposed by Johnson-trotter, and it is an algorithm that can generate the whole permutation quickly. Its next full permutation is always the last full permutation of a pair of adjacent two bits obtained. If an arrangement of n-1 elements is known, n is inserted into different positions of the arrangement, and n elements are arranged. In this way, an arrangement of any n elements can be produced. There is a drawback to this approach: to produce an arrangement of n elements, we must know and store the permutations of all n-1 elements before producing all the N-order permutations.
    3. Increment carry method This algorithm is based on the sequential increment of the number of the system [3]. Incrementing a number means that the number of digits is incremented as the number of digits increases. In general, the rightmost binary is 2, the second right of the binary is 3, and so on. The n-bit increment rounding consists of a total of n! numbers, so it can be combined with a full-array generation algorithm.
    4. Descending Rounding Method

This method is similar to the principle of increment carry system, and the difference is that it is defined as "descending carry system number" which decreases with the increment of the number of digits. This type of binary is generally the leftmost system is 2, the second left of the binary is 3. The rest of the principle is basically the same as the increment rounding method.
---
# # #Python实现

# # # #字典序法
###### Non-recursive algorithm

设P是集合{1,2,……n-1,n}的一个全排列:P=P1P2……Pj-1PjPj+1……Pn(1≤P1,P2,……,Pn≤n-1)1.从排列的右端开始,找出第一个比右边数字小的数字的序号j,即j=max{i|Pi&lt;Pi+1,i>j}在Pj的右边的数字中,找出所有比Pj大的数字中最小的数字Pk,即k=min{i|Pi>Pj,i>j}2.交换Pi,Pk

3. Pj+1pj+2 the descending portion of the right end. PN reversal, because J right end of the number is descending, so only need its left and right exchange, until the middle, so you can get a new arrangement P ' =p1p2 ... PJ-1PKPN ... Pj+2pj+1
###### Code

#!/usr/bin/env Python2#-*-Coding:utf-8-*-"""@author: Gsharp"""defSwap (N,A,B): N[a],n[b]=N[b],n[a]return NonedefReverse (N,begin):if Len(n)>Begin:i=Begin J= Len(n)-1         whileI<J:swap (N,I,J) I+= 1J-= 1    returnNdefFindmin (n,i): J= Len(n)-1K=I+ 1     whileJ>IifN[J]>N[i] andN[J]<N[k]: K=J J-= 1    returnKdefPermut (n): Count= 0J= Len(n)-1      ifJ< 1:returnNElse:PrintN Count+= 1         whileJ>= 1: I=J- 1            ifN[i]<n [j]: K=Findmin (n,i) Swap (n,i,k) Reverse (n,j) J= Len(n)- 1Count+= 1                PrintNElse: j-= 1    PrintCountn=[1,2,3,4,5,6]permut (N)

Attention:

    1. This can only be ordered for lists with comparable values, for example, ' ~ ', '! ', ' @ ', ' # ' cannot be sorted directly.
    2. The initial sequence must be the smallest sequence, otherwise the entire arrangement cannot be listed. You can use quick sort to sort after as input.

Non-recursive full-array python implementation

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.