What is the time complexity of recursive algorithm
The time complexity of the recursive algorithm is: [T(n)=o(f(n))], which means that as the problem size n increases, the execution time growth rate of the algorithm and f(n) The growth rate is proportional to the growth rate, which is called the asymptotic time complexity of the algorithm.
Time complexity of recursive algorithm
Time complexity:
Generally, the number of times the basic operations are repeated in the algorithm is a function f(n) of the problem size n, and then analyze the change of f(n) with n and determine the order of magnitude of T(n). Here ‘o’ is used to represent the order of magnitude and gives the time complexity of the algorithm.
T(n)=o(f(n));
It means that as the problem size n increases, the execution time growth rate of the algorithm is proportional to the growth rate of f(n) , which is called the asymptotic time complexity of the algorithm. And we generally discuss worst-case time complexity.
Recommended course: C language tutorial
Space complexity:
The space complexity of the algorithm is not actually occupied space, but to calculate the number of auxiliary space units in the entire algorithm space, which has nothing to do with the scale of the problem. The space complexity S(n) of an algorithm is defined as the order of magnitude of the space consumed by the algorithm.
S(n)=o(f(n))
If the auxiliary space required for algorithm execution is a constant relative to the input data n, it is called the space complexity of the algorithm The auxiliary space is o (1);
The space complexity of the recursive algorithm: the recursion depth n*the auxiliary space required for each recursion. If the auxiliary space required for each recursion is a constant, the recursion space complexity is o (n).
The calculation equation of the time complexity of the recursive algorithm is a recursive equation:
You can consider an example before introducing the recursive tree:
T(n) = 2T(n/2) + n2
Iterate 2 times to get:
T(n) = n2 + 2(2T(n/4) + (n/2) 2)
You can continue to iterate and fully expand it to get:
T(n) = n2 + 2((n/2) 2 + 2((n/22)2 + 2((n/23) 2 + 2((n/24) 2 +…+2((n/2i) 2 + 2T(n/2i + 1)))…))))……(1)
And when n/2i 1 == 1, Iteration ends.
Expand the parentheses of formula (1), we can get:
T(n) = n2 + 2(n/2)2 + 22(n/22) 2 + … + 2i(n/2i)2 + 2i+1T(n/2i+1)
This happens to be a tree structure, from which the recursive tree method can be derived.
(a)(b)(c)(d) in the figure are the 1st, 2nd, 3rd, and nth steps of the recursive tree generation respectively. In each node, the current free item n2 is retained, and the two recursive items T(n/2)
T(n/2) are allocated to its two child nodes respectively, and so on.
The sum of all nodes in the graph is:
[1 + 1/2 + (1/2)2 + (1/2)3 + … + (1/2)i] n2 = 2n2
It can be seen that its time complexity is O(n2)
The rule of the recursive tree can be obtained as :
(1) The nodes of each layer are T(n) = kT(n / m) The value of f(n) in f(n) under the current n/m;
(2) The number of branches of each node is k;
(3) The right side of each layer marks the sum of all nodes in the current layer.
Another example:
T(n) = T(n/3) T(2n/3) n
its recursive tree As shown in the figure below:
It can be seen that the value of each layer is n, and the longest path from the root to the leaf node is:
Because the final recursive The stop is at (2/3)kn == 1. Then
then
T(n) = O( nlogn)
Summary, use this method to solve the complexity of the recursive algorithm:f(n) = af(n/b) + d(n)
The above is the detailed content of What is the time complexity of recursive algorithm. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

We take the integer array Arr[] as input. The goal is to find the largest and smallest elements in an array using a recursive method. Since we are using recursion, we will iterate through the entire array until we reach length = 1 and then return A[0], which forms the base case. Otherwise, the current element is compared to the current minimum or maximum value and its value is updated recursively for subsequent elements. Let’s look at various input and output scenarios for this −Input −Arr={12,67,99,76,32}; Output −Maximum value in the array: 99 Explanation &mi

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive procedure. A recursive function is a function that calls itself within its definition. If str1 is "Iknowthatyouknowthatiknow" and str2 is "know" the number of occurrences is -3. Let us understand through examples. For example, input str1="TPisTPareTPamTP", str2="TP"; output Countofoccurrencesofasubstringrecursi

Time complexity analysis of recursive functions involves: identifying base cases and recursive calls. Calculate the time complexity of the base case and each recursive call. Sum the time complexity of all recursive calls. Consider the relationship between the number of function calls and the size of the problem. For example, the time complexity of the factorial function is O(n) because each recursive call increases the recursion depth by 1, giving a total depth of O(n).

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.
