当前位置:网站首页>Recursion and divide and conquer strategy
Recursion and divide and conquer strategy
2022-07-04 10:25:00 【sqjddb】
* Recursion and divide and conquer strategy
>> The basic idea of recursion and divide and conquer
Sometimes it is difficult to solve a big problem directly , The idea of divide and conquer is : Divide the big problem into smaller ones , In order to break each one , Divide and rule . Apply divide and conquer means repeatedly , Make the sub problem smaller , The solution of the subproblem is usually the same as that of the original problem , Naturally leads to recursive processes . Divide and conquer and recursion are twin brothers , Many efficient algorithms have been produced .
>> The advantages and disadvantages of recursion

>> Conditions for the application of divide and conquer
The problems that can be solved by divide and conquer generally have the following characteristics :
• The problem can be easily solved by reducing its scale to a certain extent
• The problem can be divided into several smaller problems of the same size
• The solution of the subproblem can be combined into the solution of the problem
• The subproblems are independent of each other , I.e. Zi Wen
There are no common sub problems between the questions
>> The idea of balanced subproblem

>> Recursive classic program


①Ackerman function
Ackerman function A(n,m) There are two independent integer variables m>=0,n>=0, Its definition is as follows
A(1,0)=2;
A(0,m)=1 m>=0
A(n,0)=n+2 n>=2
A(n,m)=A(A(n-1,m),m-1) n,m>=1
This is a double recursive function
A(n,m) Each independent variable of defines a univariate function . The third form of recursion defines the function “ Add 2”.
m=1 when , because A(1,1)=A(A(0,1),0)=A(1,0)=2
as well as A(n,1)=A(A(n-1),1),0)=A(n-1,1)+2 (n>1), therefore A(n,1)=2n (n>=1), namely A(n,1) Defined the function “ ride 2”
When m=2 when , because A(1,2)=A(A(0,2),1)=A(1,1)=2
A(n,2)=A(A(n-1,2),1)=2A(n-1,2), therefore A(n,2)=2^n
And so on 
among 2 The number of layers is n.
A(n,4) The growth rate of has become unimaginably fast , It is impossible to write a general term formula to express this function
② The total permutation problem
Design a recursive algorithm to generate n Elements {r1,r2,…,rn} The whole arrangement
R={1,2,3,4}
Output :1234 1243 1324 …… 4312 4132 4123
The procedure is as follows :
Template<class type>
void Perm(Type list[ ], int k, int m)
{
if (k==m)
{
for (int i=0;i<=m;i++)
cout<<list[i];
cout<<endl;
}
else
for(i=k;i<=m;i++)
{
Swap(list[k],list[i]);
Perm(list,k+1,m);
Swap(list[k],list[i]);
}
}
Core statement :
Swap(list[k],list[i]);
Perm(list,k+1,m);
Swap(list[k],list[i]);

The process is tedious , Thinking about the meaning of recursion is of great help in clarifying ideas
③ Integer partition problem
Integer partition problem : Put a positive integer n Expressed as the sum of several positive integers
n=n1+n2+…+nk, n1≥n2≥…≥nk≥1, k≥1
Find out how many ways to divide :
The procedure is as follows :
int a(int n, int m)
{
if((n<1) or (m<1))
return 0;
if((n==1) or (m==1))
return 1;
if(n<m)
return q(n,n);
if(n==m)
return q(n,m-1)+1;
return q(n,m-1)+q(n-m,m);
}
④ The hanotta problem
Take the example of Hanoi tower to explain the recursive idea 
So the core code has three lines , Explain the following :
// Parameter means : hold A Move to according to the rules on B On ,C For the auxiliary tower
Void Hanoi(int n, int A, int B, int C)
{
if(n>0)
{
Hanoi(n-1,A,C,B); // hold n-1 From the A Move to C,B For the auxiliary tower
Move(n,A,B); // Take the biggest one from A Move to B
Hanoi(n-1, C,B,A); // hold n-1 From the C Move to B,A For the auxiliary tower
}
}
Actually Tower of Hanoi's non recursive program It's also worth learning
>> Deep understanding of divide and conquer
The time complexity of the algorithm :
Want to deeply understand the time complexity of the algorithm , Readers are advised to make clear Algorithm time complexity The calculation method of
Good article recommends
The following examples analyze the time complexity of the algorithm , Understand the above k,m The meaning of
① Two point search



The loop is executed O(logn) Time , The comparison operation in each cycle requires O(1) Time 
Recursive tree drawing 

② Multiplication of large integers

The procedure is as follows :
③Strassen Matrix multiplication
Matrix multiplication :
To reduce time complexity , Made such an attempt :
Strassen To reduce the time complexity

④ Chessboard coverage




The procedure is as follows :
Be careful :tile Global variable
Complexity analysis :
边栏推荐
- Rhcsa12
- DCL statement of MySQL Foundation
- Architecture introduction
- 【Day2】 convolutional-neural-networks
- Kotlin: collection use
- 有老师知道 继承RichSourceFunction自定义读mysql怎么做增量吗?
- Latex learning insertion number - list of filled dots, bars, numbers
- Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
- The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
- Network disk installation
猜你喜欢

Dynamic memory management

Some summaries of the third anniversary of joining Ping An in China

Qtreeview+ custom model implementation example

RHCE - day one

入职中国平安三周年的一些总结

Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2

Two way process republication + routing policy

Devop basic command

Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)

Delayed message center design
随机推荐
Hands on deep learning (43) -- machine translation and its data construction
SQL replying to comments
今日睡眠质量记录78分
Hlk-w801wifi connection
Dynamic memory management
How can Huawei online match improve the success rate of player matching
Exercise 7-8 converting strings to decimal integers (15 points)
Kotlin: collection use
Architecture introduction
OSPF comprehensive experiment
leetcode1-3
Hands on deep learning (45) -- bundle search
PHP code audit 3 - system reload vulnerability
Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
MPLS: multi protocol label switching
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
Advanced technology management - how to design and follow up the performance of students at different levels
Hands on deep learning (46) -- attention mechanism
BGP advanced experiment
What is an excellent architect in my heart?
