Tracing to the bottom-loop, iteration, divide and conquer, backtracking

In the computer world, it is infinitely derived from the most basic for loop, while loop, if...else, no matter how complicated the logic is, most of them can be summarized into the above three types. Of course, unless the original logic is non-repetitive, unconditional branch.

1. The cycle (repetition)
keeps repeating, beginning and ending

Loop realization

private loop(){ for(start; end; loop termination){ expression1; expression2; expression3; } } def loop(): for start in end/loop_termination: expression1; expression2; expression3; Second, recursive characteristics: self-similarity, Beginning and ending












Realization: Go back and come back, enough to stop

When did you think of recursion?

The sub-question does the same thing as the original question

Recursive implementation:

private void recursion(int level,int param1,int param2...):{ // Terminato (recursion terminato) if(level> MAX_LEVEL){ # process_rsult return } // Process logic in current level) process (level, data1, data2…) // Enter the next level (dill down) recursion(level: level + 1, newParam): // If you need to restore the state of this level } def recursion(level, param1, param2…): # Terminato (recursion terminato) if level> MAX_LEVEL: # process_rsult return # Process logic in current level (process logic in current level) process(level, data1, data2…) # Enter the next level (dill down) self.recursion(level + 1, param1, param2…): # If you need to restore the state of this layer 2. Divide and conquer






















Definition: Divide and conquer, group officials into one

When did you think of divide and conquer?

When complex problems can be broken down into simple sub-problems

Divide and conquer realization:

private static int divide_conquer(Problem, Param1, Param2…) { // Termination condition if (problem == NULL) { int res = process_last_result(); return res; } // Split subproblems subProblems = split_problem(problem)






res0 = divide_conquer(subProblems[0])
res1 = divide_conquer(subProblems[1])

// Combine the results of sub-problems
result = process_result(res0, res1);

return result;
}
def divide_conquer(Problem, Param1, Param2…):
# Termination condition
if problem is None:
return
# Split subproblems
subproblems = split_problem(problem, data)
subresult1 = self.divide_conquer(subproblems[0], p1,… )
subresult2 = self.divide_conquer(subproblems[1], p1, …)
subresult3 = self.divide_conquer(subproblems[2], p1, …)

# Combining sub-problem results
result = process_result(subresult1, subresult2, subresult3, …)
three : Backtracking
Adopt the "trial and error" thinking, try to solve the problem "step by step". In a step-by-step process. According to the results of the upper layer, try this layer to solve the problem optimally, and if this layer is not optimal than the upper layer, go back.

Fourth, DP (Dynamic programming) dynamic programming / dynamic recursion
definition

In both contexts it refers to simplifying a complicated problem by breaking it down into simpler sub-problems in a recursive manner. While some decision problems cannot be taken apart this way, decisions that span several points in time do often break apart recursively.

In both cases, it refers to recursively decomposing a complex problem into simpler sub-problems to simplify it. Although some decision-making problems cannot be decomposed in this way, decisions that span multiple points in time are usually decomposed recursively.

Simplifying a complicated problem by breaking it down into simpler sub problem(in a recursibe manner)

Decompose a complex problem into simpler sub-problems and simplify it (in a recursive way)

Bottom up

Key points of dynamic planning:

Optimal substructure
storage intermediate state
recursive formula (state transition equation, DP equation)
eg

One-dimensional

Fib:
opt[i] = opt[n - 1] + opt[n - 2]

Two-dimensional

opt[i][j] = opt[i + 1][j] + opt[i][j + 1]
Take the Fibonacci sequence as an example:

F(0) = 0, F(1) = 1

F(N) = F(N-1) + F(N-2) (N >= 2)
recursion (silly recursion):

If F(4) is calculated; need to be calculated

lin1 F(4) = f(3)、f(2),
lin2 F(3):f(2)、f(1), F(2) = f(1) + f(0)
DP:

i(0) = 0, i(1) = 1
[0, 1, 1, 2, 3, 5]
Summary
Dynamic programming, recursion, divide and conquer, no essential difference

Commonality: Duplicate sub-problems

Opposite sex: optimal sub-structure, sub-optimal in midway elimination

More articles, contact bloggers, technical exchanges, business cooperation

Scan code or search: Product Coder
Insert picture description here

Guess you like

Origin blog.csdn.net/wzp7081/article/details/108763417