subset tree

I've been dizzy at work recently, and suddenly thought of reviewing it. I did the code for a subset tree. .

 

In fact, the subset tree is a bit like brute force cracking, the famous knapsack problem in 0-1 planning. Suppose there are N knapsacks, corresponding to N elements.

Then there are:

         Backpack                   1                   2                   3                   ...                   n         
        Do you want       Do you want       Do you want       Do you want       Do you want       Do you want

Then you should or should not deal with the two cases separately, such as the left subtree and the right subtree of a binary tree.

 

The subset tree writing method also refers to the knapsack problem, traverses N knapsacks, decides whether or not the knapsack is needed, and then traverses the entire tree.

  When it is found that the number of traversed layers is full, just print the path~

if num == len(arr):
    outputList.append([arr[index] for index in range(0, num) if cur_arr[index] ])
    return

     When the number of layers is not full, then continue to try

    1. Want the backpack

    2. Don't use the backpack

   Corresponding code:

    # select the numth element
    cur_arr[num] = 1
    subSetTree(arr, cur_arr, num + 1, outputList)
    # unselect the numth element
    cur_arr[num] = 0
    subSetTree(arr, cur_arr, num + 1, outputList)

 So the question is, if you append the element into an array, and then pop it out after the execution is complete?

  [No] The principle is very simple. This subset tree must traverse all the paths of the tree. If you append elements to it, it will inevitably involve adding elements repeatedly. . . must involve dealing with subsequent situations

    1. When a certain path comes to an end, it may take a few steps back, and then continue to go down. . . At this point, the issues involved are extremely complex. . .

    2. Since the size of the elements in the array has changed after recursion, if you use

                    cur_arr[num] = cur_arr[:-1] 
      will cause the wrong data to be modified. . . .
3. This kind of treatment is disgusting anyway. . .

 

def subSetTree(arr, cur_arr, num, outputList):
    if num == len(arr):
        outputList.append([arr[index] for index in range(0, num) if cur_arr[index] ])
        return

    # select the numth element
    cur_arr[num] = 1
    subSetTree(arr, cur_arr, num + 1, outputList)
    # unselect the numth element
    cur_arr[num] = 0
    subSetTree(arr, cur_arr, num + 1, outputList)

def subSetTree(arr, cur_arr, num, outputList):
    if len(arr) == num :
        outputList.append([val for val in cur_arr if val != None])
        return

    # select the numth element
    cur_arr[num] = arr[num]
    subSetTree(arr, cur_arr, num + 1, outputList)

    # Do not select the num-th element, delete the num-th element
    cur_arr[num] = None
    subSetTree(arr, cur_arr, num + 1, outputList)

arr = [1, 2, 3]
cur_arr = range(0,3)
outputList = []
num = 0
subSetTree(arr, cur_arr, num, outputList)
for arr in outputList:
    print arr

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297246&siteId=291194637