当前位置:网站首页>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 :
边栏推荐
- Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
- Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel
- BGP ---- border gateway routing protocol ----- basic experiment
- Application of safety monitoring in zhizhilu Denggan reservoir area
- C language structure to realize simple address book
- The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
- Latex arranges single column table pictures in double column format articles
- DML statement of MySQL Foundation
- Hands on deep learning (44) -- seq2seq principle and Implementation
- AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
猜你喜欢
Hands on deep learning (43) -- machine translation and its data construction
Hands on deep learning (44) -- seq2seq principle and Implementation
Hands on deep learning (37) -- cyclic neural network
Introduction to extensible system architecture
Basic data types of MySQL
【Day1】 deep-learning-basics
Custom type: structure, enumeration, union
Reasons and solutions for the 8-hour difference in mongodb data date display
Latex learning insertion number - list of filled dots, bars, numbers
Today's sleep quality record 78 points
随机推荐
The future education examination system cannot answer questions, and there is no response after clicking on the options, and the answers will not be recorded
Es advanced series - 1 JVM memory allocation
A little feeling
Latex arranges single column table pictures in double column format articles
Differences among opencv versions
Idea SSH channel configuration
For programmers, if it hurts the most...
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
Advanced technology management - how to design and follow up the performance of students at different levels
Histogram equalization
Native div has editing ability
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2
今日睡眠质量记录78分
Hands on deep learning (44) -- seq2seq principle and Implementation
DDL statement of MySQL Foundation
Use the data to tell you where is the most difficult province for the college entrance examination!
leetcode1-3
[200 opencv routines] 218 Multi line italic text watermark