Python implements a full permutation of the base's deep-first search (DFS, depth) solution number

Source: Internet
Author: User

Number of the whole arrangement, is a very simple problem, usually we use pen paper to list the answer, but the number of times, the arrangement of the result is very much, for example, there are 1,2,3,4,5,6,7,8,9 this series, there are 9 numbers, then there are 9! (9 factorial) so many results. That's very big. Today I'm going to introduce a depth-first search to solve the problem of full permutation of this number.

Depth-First Search

First, a brief introduction to depth-first search, the key to depth-first search is what to do now, and what to do next, as it is now. The basic model for depth-first search is:

Dfs (STEP):    judge Boundary: Perform related actions, return    for (i = 1; I <=n; i++):        continue to next Dfs (step+1 )    return

The problem of the full permutation of numbers

Back to the question of the full permutation of the numbers. Note: The following code is used to introduce the core idea of the program, and can not be run directly, only the complete code can be executed, step starting from 1 count.

We use an array of book=[] to record that our number has been selected. Use result to record our results.

First, let's solve the simplest problem: Choose a number that doesn't have a selection and put it on the corresponding digits. So the following code is translated, that is, the step bit is set to i+1 (i is starting from 0, so add one, assuming that our sequence does not have 0, each number does not repeat), and Mark I as checked (book[i] = 1)

 for inch range (N):     if book[i] = = 0        :=        i+1 = 1

Then, proceed to the next step:

def Dfs (STEP):      for inch range (N):         if book[i] = = 0            := I            = 1            dfs (step+1)            = 0

Finally, the boundary condition is processed, and when the steps step is equal to the number of digits +1, the output is:

if step = = n+1:    printf    resultreturn

Keep repeating the above steps to list all the results

The following is the complete code:

#-*-Coding:utf8-*-" "Introduction: This program is mainly a simple implementation of depth-first search algorithm (depth) and solve the problem of the full array of numbers. Implementation method: Recursive Tony Chen right 2016/9/24 first release" "classDFS (object):" "Numlen: For several numbers, such as 3, it is the result of the three-to-one: the results of saving a sequence book: to judge that the number is already lined up" "    def __init__(self, N): Self.numlen=N Self.result= [0 forIinchrange (n)] Self.book= [0 forIinchrange (n)]defDfs (self, s): Step= S-1ifStep = =Self.numlen:r="'             forIinchRange (Self.numlen): R+=str (self.result[i])PrintRreturn        #used to try each possible, that is, the number of step is I         forIinchRange (Self.numlen):ifSelf.book[i] = =0:self.result[step]= I+1Self.book[i]= 1Self.dfs (S+1) Self.book[i]=0return
Full Code

Python implements a full permutation of the base's deep-first search (DFS, depth) solution number

Related Article

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.