如何计算Big O表示法中递归算法的复杂度?

时间:2015-10-17 08:46:12

标签: big-o time-complexity

递归算法具有复杂性: W(N)= 2W(N / 2)+Θ(n)的

我的解决方案或猜测是O(n)。

如何解决这种复杂问题?

1 个答案:

答案 0 :(得分:1)

这样的情况由Master Theorem涵盖。它也很容易直接看到:

#config/routes.rb
resources :zombies #-> provides routes to interact with Zombie objects

#app/controller/zombies_controller.rb
class ZombiesController < ApplicationController
   def index
      @zombies = Zombie.all #-> show ALL zombie objects
   end

   def show
      @zombie = Zombie.find params[:id] #-> find a single zombie object
   end

   def new
      @zombie = Zombie.new #-> new zombie object
   end

   def create
      @zombie = Zombie.new zombie_params
      @zombie.save #-> save the new Zombie object
   end

   private

   def zombie_params
      params.require(:zombie).permit(:zombie, :params)
   end
end

#app/models/zombie.rb
class Zombie < ActiveRecord::Base
   has_many :enemies #-> each zombie object has many enemies
end

因此,对于每个递归步骤,您将获得另一个Theta(n),并且递归的深度为log n。因此总的努力是O(n log n)。