代码之家  ›  专栏  ›  技术社区  ›  letsCode

理解背包问题O(2^n),其中T(n)

  •  0
  • letsCode  · 技术社区  · 6 年前

    问题:背包的最佳解是O(2^n),那么一个配分函数(下面)在n上的速度会有多快?

    这就像说O(n+2^n)一样简单吗? 或者这是错的,它是O(2^n),因为我们现在知道T(n)是什么。

    配分函数:

    bool Partition(List S){  
         create two lists //O(n) + O(n) 
         M = (Sum of all elements in S)/2; //O(n) 
         K = (Sum of all elements in S)/2; //O(n)
         if(Knapsack(Profits, Weights, M, K) == true) // O(2^n) 
              return true; 
         else 
         return false; 
    } 
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   AbsoluteSpace    6 年前

    时间复杂性只是 O(2^n) 因为 O(n + 2^n) O(2^n) .