Home Backend Development C#.Net Tutorial What is the time complexity of recursive algorithm

What is the time complexity of recursive algorithm

Oct 24, 2019 am 10:53 AM
time complexity recursion

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.

What is the time complexity of recursive 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:

What is the time complexity of recursive algorithm

You can consider an example before introducing the recursive tree:

T(n) = 2T(n/2) + n2
Copy after login

Iterate 2 times to get:

T(n) = n2 + 2(2T(n/4) + (n/2) 2)
Copy after login

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)
Copy after login

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)
Copy after login

This happens to be a tree structure, from which the recursive tree method can be derived.

What is the time complexity of recursive algorithm

(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
Copy after login

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:

What is the time complexity of recursive algorithm

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

What is the time complexity of recursive algorithm

##that is,

T(n) = O( nlogn) 

Summary, use this method to solve the complexity of the recursive algorithm:


f(n) = af(n/b) + d(n)
Copy after login
1. When d(n) is a constant:

 

What is the time complexity of recursive algorithm

2. When d(n) = cn:

What is the time complexity of recursive algorithm

3. When d(n) is other situations, the recursion tree can be used for analysis .

From the second case, if the divide-and-conquer method is used to improve the original algorithm, the focus is to use new calculation methods to reduce the value of a.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recursive implementation of C++ functions: Is there a limit to recursion depth? Recursive implementation of C++ functions: Is there a limit to recursion depth? Apr 23, 2024 am 09:30 AM

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.

Do C++ lambda expressions support recursion? Do C++ lambda expressions support recursion? Apr 17, 2024 pm 09:06 PM

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.

Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Apr 22, 2024 pm 03:18 PM

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.

Recursive program to find minimum and maximum elements of array in C++ Recursive program to find minimum and maximum elements of array in C++ Aug 31, 2023 pm 07:37 PM

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

Count the number of occurrences of a substring recursively in Java Count the number of occurrences of a substring recursively in Java Sep 17, 2023 pm 07:49 PM

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

How to analyze the time complexity of C++ recursive functions? How to analyze the time complexity of C++ recursive functions? Apr 17, 2024 pm 03:09 PM

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).

Detailed explanation of C++ function recursion: application of recursion in string processing Detailed explanation of C++ function recursion: application of recursion in string processing Apr 30, 2024 am 10:30 AM

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.

C++ Recursion Advanced: Understanding Tail Recursion Optimization and Its Application C++ Recursion Advanced: Understanding Tail Recursion Optimization and Its Application Apr 30, 2024 am 10:45 AM

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.

See all articles