Chapter 14, Data Structure and Algorithm Application
1. Divide and conquer
For a problem of size n, if the problem can be easily solved (for example, the size n is small), it can be solved directly; otherwise, itsDecompose into k smaller sub-problems, these sub-problems are independent of each other and have the same form as the original problem, recursively solve these sub-problems, and then combine the solutions of each sub-problem to obtain the solution of the original problem
- The size of the problem can be easily solved by reducing the size of the
- This problem can be broken down into severalSame problem on a smaller scale
- The solution to the subproblems decomposed by this problem can bemerged into the solution of the problem
- The sub-problems decomposed into this problem aremutually independant
1.1. Recursive technology
Recursion is to call yourself during the running process
Fibonacci sequence
int F(int n)
{
if(n=0) return 1;
if(n=1) return 1;
if(n>1) return F(n-1)+F(n-2);
}
1.2, binary search
Binary search is also called binary search (Binary Search), which is a more efficient search method. However, the binary search requires that the linear table must adopt a sequential storage structure, and the elements in the table are arranged in order according to the key
function Binary_Search(L,a,b,x){
if(a>b) return -1;
else{
m=(a+b)/2;
if(x==L[m])return m;
else if(x>L[m])
return(Binary_Search(L,m+1,b,x));
else
return(Binary_Search(L,a,m-1,x));
}
}
2. Backtracking method
The backtracking method is a kind of optimal search method, which searches forward according to the optimal conditions to achieve the goal. But when the search reaches a certain step, it is found thatThe original choice is not good or can not achieve the goal,At onceTake a step back and choose again. This technique of going back and going if it doesn't work out is the backtracking method.
3. Greedy method
always make the currentthe best choice, without taking it into consideration as a whole, the choice it makes at each step is only the current steplocal optimum, but it is not necessarily the best choice overall. Since it does not have to exhaust all possible solutions in order to find the optimal solution, it consumes less time,Generally, a satisfactory solution can be obtained quickly, but the optimal solution cannot be obtained。
4. Dynamic programming method
In solving the problem, for each decision step,List all possible local solutions, and then according to a certain judgment condition, those local solutions that are definitely not able to obtain the optimal solution are discarded, and they are screened at each step to ensure that the global solution is the optimal solution at each step.Optimal solution。