Induction and Recursion and Reduction

Where is the reduction? important as in dynamic programming, what is the dynamic function?

There are two major variations of reductions: reducing to a different problem or reducing to a shrunken version of the same.

Puzzler solution 

def cover(board,lab=1,top=0,left=0,size=None):
    if size is None: size = len(board)
    #size of subboard
    s = size//2
    #offsets for outer/inner squares of subboards
    offsets = (0,-1),(size-1,0)
    for dy_outer,dy_inner in offsets:
        for dx_outer,dx_inner in offsets:
            #if the outer corner is not set
            #print ((dy_outer,dx_outer),(top+s+dy_inner,left+s+dx_inner))
            if not board[top+dy_outer][left+dx_outer]:
                #label the inner corner
                #print {(top+s+dy_inner,left+s+dx_inner):lab}
                board[top+s+dy_inner][left+s+dx_inner]=lab

    #next label
    lab +=1
    if s>1:
        for dy in [0,s]:
            for dx in [0,s]:
                #recursive calls, if s is at least 2:
                #print(lab,top+dy,left+dx,s)
                lab = cover(board,lab,top+dy,left+dx,s)

    #return the next available label:
    return lab


board = [[0]*8 for i in range(8)]
board[7][7] = -1
#print(1,0,0,4)
cover(board)
for row in board: print((" %2i"*8) % tuple(row))

                

induction,recursion, and reduction.

Focusing on taking a single step toward a solution, the rest follows automatically.

Reduction means transforming one problem to another. We normally reduce an unknown problem to one we know how to solve.

Induction is used to show that a statement is true for a large class of objects. We do this by first showing it to be true for a base case(such as the number 1, the empty tree...) and then showing that it "carries over" from one object to the next( if it is true for n-1, then it's true for n).

Recursion is what happens when a function calls itself. Here we need to make sure the fuction works correctly for a base case and that it combines results from the recursive calls into a valid solution.

Both induction and recursion involve reducing a problem to a smaller subproblems and then taking one step beyond these, solving the full problem.

To use induction(or recursion), the reduction must be between instances of the same problem of different sizes.

The question is how we can carve up the board into smaller ones of the same shape. It's quadratic, so a natural staring point might be to split it into four smaller squares. The only thing standing between us and a complete solution at that point is that only one of the four board parts has the same shape as the original, with the missing corner. The other three are complete checkerboards. That's easily remedied, however. Just place a single tile so that it covers one corner from each of these three subboards, and, as if by magic, we now have four subproblems, each equivalent to the full problem!

posted on 2012-08-17 13:49  grep  阅读(287)  评论(0编辑  收藏  举报