Recursive example + Master expression

Recursive example + master expression

What is recursion: A method of solving a problem by repeatedly decomposing it into subproblems of the same kind.

                  可以通过调用自身来进行递归。

Example : Use the recursive method to find the maximum value in the array
Idea : Divide the array into two parts, find the maximum value in the two parts respectively, and finally compare the two obtained maximum values ​​to obtain the final maximum value.
Implementation code : (the recursive process is below)

public int FindMaxNumber(int[] arr,int start,int end)
{
    
    
    if (start == end)
    {
    
    
        return arr[start];
    }
    int mid = start + ((end - start) >> 1);//防止因为end+start太大超出int范围,对比int mid = (start + end) / 2;
    //int mid = (start + end) / 2;
    int maxLeft = FindMaxNumber(arr, start, mid);//找到左侧最大值
    int maxRight = FindMaxNumber(arr, mid+1,end);//找到右侧最大值
    int max = Math.Max(maxLeft, maxRight);//比较左边的最大值和右边最大值哪个更大
    return max;
}

Recursive process :
The example array arr[3,2,5,6,7,4]
corresponds to the subscript value [0,1,2,3,4,5]
The recursive process is as shown in the figure below:
insert image description here
Note:
1. If you want to experience recursion process, you can set the length of the array to be smaller, and then perform Debug on the compilation platform to experience the recursive process. I think this will help you understand the recursive process better.
2. In the code, when calculating mid, use
int mid = start + ((end - start) >> 1);
instead of using (start+end)/2 to calculate mid, this is because if start and end are very large. Although start is within the range of int type and end is also within the range of int type, start+end may exceed the range of int type.

Master expression

What is a Master Expression: Estimate the time complexity of a series of special recursive behaviors.
Expression content:

              T(N)=a*T(N/b)+O(N^d);

a: The number of sub-problem calls
N/b: The size of the sub-problems is N/b
O(N^d): The time complexity of the remaining process except the sub-problem calls

If:
insert image description here
So what is the special recursive behavior?
Answer: Satisfying the subproblem is a recursion of equal size

Take the above code as an example

public int FindMaxNumber(int[] arr,int start,int end)
{
    
    
    if (start == end)
    {
    
    
        return arr[start];
    }
    int mid = start + ((end - start) >> 1);//防止因为end+start太大超出int范围,对比int mid = (start + end) / 2;
    //int mid = (start + end) / 2;
    int maxLeft = FindMaxNumber(arr, start, mid);//找到左侧最大值
    int maxRight = FindMaxNumber(arr, mid+1,end);//找到右侧最大值
    int max = Math.Max(maxLeft, maxRight);//比较左边的最大值和右边最大值哪个更大
    return max;
}

In this code, the subproblems are

int maxLeft = FindMaxNumber(arr, start, mid);//找到左侧最大值
int maxRight = FindMaxNumber(arr, mid+1,end);//找到右侧最大值

The size of the subproblem is N/2, so it meets the conditions of the master expression, and the master expression can be used to calculate its time complexity.

Where a=2, because the sub-problem is called twice
b=2, because the sub-problem scale is N/2
d=0, because the time complexity of the remaining process is O(1);

So calculate
log2(2)=1>d

The time complexity is O(N^log2(2))=O(N)

If you have any questions, please leave a message ヾ( ̄▽ ̄) Bye Bye

Guess you like

Origin blog.csdn.net/weixin_51565051/article/details/129766471