Recursive tree - Analysis of time complexity of recursive algorithm

Recursive tree

 

Recursive idea is that the big problem into small problems to solve, and then break down the problem into small little problem.

Such decomposition layer by layer, until the problem of the size of the data is broken down small enough not continue until the recursive decomposition.

If we put this layer by layer decomposition drawn map, it is actually a tree. We give tree erected a name, called recursive tree.

 

Time - recursive code complexity analysis a lot of trouble, you can use recursive formula to solve the time complexity of the algorithm, such as solving merge sort, quick sort of time complexity

But some cases, such as the average time complexity of quick discharge of analysis, with the recurrence formula, then, involves very complex mathematical derivation.

At this point we will be able to analyze the time complexity of recursive algorithm by means of a recursive tree

 

How to solve the time complexity of recursive tree

 

Recursive tree merge sort time complexity analysis

 

Each will merge sort data into two, because every time decomposition are divided into two, so the price is very low, we recorded as consumption on the time constant 1.

Merge algorithm merge operation is time-consuming, that is, the two sub-arrays into large arrays.

The sum of the time consumed by each layer merge operation is the same, with the size of the data you want to sort of related. Each layer of the merge operation consumes time denoted by n. 

Then only needs to know the tree height h, of each layer multiplied by n time consuming height h, can get the overall time complexity of O (n * h).

Can be seen from the principle of merge sort, merge sort recursion tree is a full binary tree, and full binary tree height is approximately log n.

Therefore, recursive merge sort time complexity is O (nlogn).

 

Of course, in addition to the time complexity of recursive tree merge sort Recursive analysis may also be analyzed using the recursion formulas

Specific methods merge sort in my analysis there: https://blog.csdn.net/qq_42006733/article/details/104415767

 

Recursive tree analysis of the time complexity of quicksort

 

About recursive formula to analyze the time complexity of recursive algorithm to quickly sort relative to the complex for it.

Quick sort in the best case, the partition point selection and reasonable, each partition can be divided into two times.

This time with the recurrence equation T (n) = 2T (n / 2) + n, can easily derive the time complexity is O (nlogn).

 

However, each partition may not be so lucky, just two.

Assuming that the average case, after each partition, partition size ratio of two 1: k.

When k = 9, if the method of recursion formulas time complexity to solve it, recursion formula can be written as T (n) = T (n / 10) + T (9 * n / 10) + n.

Time complexity is analyzed using the recursion formulas are also possible, but very complicated derivation.

In this case the use of a simple recursive tree on the lot.

 

Still assuming that k = 9 when the rapid sorting process, all data must traverse each partition to be partitioned section.

With the merge sort, every layer traversed by partitioning data is the sum of the number n.

As long as we find recursive tree of height h, the number of data traversing this fast discharge process is h * n, that is, the time complexity is O (h * n).

But merge sort in each partition is divided into two, so merge sort recursion tree is a full binary tree. Height of log n

The quick sort each partition is not evenly into two, so recursive tree is not full binary tree. It is not the height of log n.

Merge sort, starting with the proviso that the combined data has been divided into the individual, and the end condition is that the quick sort cells to be sorted between a size of 1.

Then that leaf nodes in the data scale is 1. The root is n.

Then the recursive tree in a shortest path each time multiplied by 1/10, one of the longest path every time multiplied by 9/10.

The sum of the data on the number of traversing between nlog10 n and nlog910 n.

Depending on the complexity of the big O notation, base number of the number of complexity no matter how much we unified written log n 

When the partition size ratio is 1: 9, the time complexity is still quick sort O (nlogn).

When k = 99, that is to say each on average not more partitions, or a similar method, find the shortest path tree is log100 n, the longest path is the log 99/100 n

Just change the base number, but also the time complexity is still O (nlogn).

Regardless of the value of k is the number, or even 999, as long as the value of k with n not change, is a constant determined in advance, that fast discharge time complexity is O (nlogn).

So the average time is fast row complexity O (nlogn).

 

 

Recursive tree analysis of the complexity of the Fibonacci number time

The Fibonacci recursion is the classic columns, F. (. 1) =. 1, F. (2) =. 1,  F. (N-) = F. (N--. 1) + F. (N-- 2) ( n-  ≥. 3, n-  ∈ N *)

With code indicates that

int f(int n) {
  if (n == 1) return 1;
  if (n == 2) return 2;
  return f(n-1) + f(n-2);
}

 

In the same manner, f (n) is decomposed into f (n-1) and f (n-2), the size of each data is -1 or -2, the data size of the leaf node is 1 or 2.

Went to the root leaf nodes, each path is of varying lengths.

If every time -1, that is about the n-longest path; -2 if every time, that is the shortest path about n / 2. 

 

After the merger the number of columns decomposition Fibonacci just one addition, if the time consumed by the addition operation denoted by 1.

From the top down, the total time consumption of the first layer is 1, the total time consumption is the second layer 2, the total time of the third layer 22 is consumed.

And so on, time is consumed by the k-th layer 2 ^ k-1

If the path lengths are n, then this is the sum of 2 ^ n-1.

If the path length is 2n, the total consumption of time that is 2 ^ (n / 2) -1 of the algorithm.

Therefore, the time complexity of the algorithm is the Fibonacci on columns between O (2 ^ n) and O (2 ^ (n / 2)).

Although the results obtained in this way is just a range, but has been clear that the time complexity of the algorithm is exponential, it is very high.

Published 113 original articles · won praise 25 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_42006733/article/details/104659018