Recursive tree: how to use a tree to solve the time complexity of recursive algorithms?

Recursive tree: how to use a tree to solve the time complexity of recursive algorithms?

Recursive tree and time complexity analysis

Recursion is a big problem into small problems to solve, the decomposition process of painting layer by layer mapping, in fact, a tree is a recursive tree

Real one: the analysis of quick discharge time complexity

Recursive tree analysis of the average case time complexity of the fast row

After each partition, partition size ratio of the two is 1: K, taking k = 9, that is, each partition is very uneven, a partition is another partition nine times faster during extraction, each partition must All data to be traversed partition section, so that each layer is traversed by partitioning data is the sum of the number n, H is the height of the tree recursively, traversing process data fast row number is h * n, the time complexity is O (h * n)

Fast discharge condition is that between the end of the cell to be sorted is one size, i.e. the size of the data in the leaf node is 1, n 1 from the root node to the leaf node, recursively a shortest path tree are multiplied by each 1 / 10 1/10 , a longest path are multiplied by each 9 / 10 9/10 , so the shortest path from the root node to the leaf node l o g   2   n log~2~n , longest path l o g   10 / 9   n log~10/9~n , when the partition size ratio is 1: 9, the time complexity is quick drain O ( n l o g n ) O (nlogn)

If K = 99, partition and not the average, two partition size is 1:99, the time complexity is how much?

K = 99, the shortest path tree is log100n, time complexity is still O ( n l o g n ) O (nlogn) , no matter how many base number, the fast discharge time complexity is O ( n l o g n ) O (nlogn)

Combat II: Analysis Fibonacci number of time complexity

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

Recursive tree tree height is how much? f (n) is decomposed into f (n-1) & f (n-2), every time the data size is -1-2, leaf size is 1 or 2, -1 if every time, the longest path is n, if every time -2, the shortest path is n / 2

The combined operation of each of the decomposition requires only addition operations sequentially, from top to bottom, a first layer 1 is the total time consumed, total time consumption is the second layer 2, the third layer is the total time consumed 2 2 2^2 , ...... k-th layer is time consuming 2 ( k 1 ) 2^(k-1) , the total time consumption is the sum of time spent in each layer of the algorithm, the time complexity of the algorithm is between O ( 2 n ) O (2 ^ n) , and O ( 2 ( n / 2 ) ) O (2 ^ (n / 2)) between the time complexity is exponential and very high

Actual III: Analysis of the time complexity of the whole arrangement

How to put all permutations of n data are to find out?

For example, 1,2,3 three data, there are six different arrangements, then how to print a set of data through a programming all permutations of it? Can be implemented using recursive

We identified the last bit, the rest of the arrangement becomes n-1 data to solve, and the last bit of data can be any one of n data, so its value will have n kinds of situations, so "n data arrangement" problem decomposed into n "subproblems n-1 pieces of data arranged in"

假设数组中存储的是1,2,3……n
f(1,2……n) = {最后一位是1,f(n-1)}  +  {最后一位是2,f(n-1)}   +  {最后一位是n,f(n-1)}
//调用方式:
//int[] a = {1,2,3,4} ;      printPermutations(a ,4,4);
//k表示要处理的子数组的数据个数

public void printPermutations(int[] data , int n , int k){
  if (k == 1){
    for (int i = 0 ;i < n ; ++ i){
      System.out.print(data[i] + " ");
    }
    System.out.println();
  }
  for(int i = 0;i < k ;++i){
    int tmp = data[i] ; 
    data[i] = data[k-1];
    data[k-1] = tmp;
    
    printPermutaations(data , n,k-1);
    
    tmp = data[i];
    data[i] = data[k-1];
    data[k-1] = tmp;
  }
}

Each layer has a decomposition exchange operations n times, the second layer there are n nodes, each node requires decomposition times n-1 exchange, the second exchange layer is a total number of n * (n-1), the third layer exchange number is n * (n-1) (n-2), and so on

The life cycle of a cell is three hours, one hour split once, after seeking n hours, the number of cells in the container?

That recurrence formula is f (n) = f (n-1) * 2 - f (n-3)

But it's wrong

Because the first hour of a two cells have died in the third hour, fourth hour so only one dead cells, n = 4 when it is 13

1,2,4,7,13

The number of dead cells are not three hours before the new cells and old cells, because the old cells in n time has died, then the number of dead cells should be the n-3 cell count time freshmen, so the correct recurrence formula is f (n) = 2f (n-1) - f (n-4)

if(n<0) return 0;
if(n == 0) return 1;
if(n ==1) return 2;
if(n ==2) return 4;
if(n == 3) return 7;

xiaozhuanlan.coom / topic / 6091358742

Published 76 original articles · won praise 9 · views 9193

Guess you like

Origin blog.csdn.net/ywangjiyl/article/details/104382439