算法导论第四章学习笔记

本文介绍了三种常用的递归式分析方法:代换法、递归树法和主定理法。通过这些方法可以有效地推导出算法的时间复杂度。文章详细解释了每种方法的应用步骤,并提供了具体的例子。

递归式

Substitution method

-----a damn clever method to prove the complexity of algorithms.

 

The substitution method for solving recurrences entails two steps:

1>     Guess the form of the solution.

2>     Use mathematical induction to find the constants and show that the solution works.

The substitution method can be used to establish either upper or lower bounds on a recurrence. As an example, let us determine an upper bound on the recurrence

Example: T(n) = 4T(n/2) + n

[Assume that T(1) = Θ(1).]

Guess O(n3) . (Prove O and Ω separately.)

Assume that T(k) ck3 for k < n .

Prove T(n) cn3 by induction.

We must also handle the initial conditions, that is, ground the induction with base cases.

Base: T(n) = Θ(1) for all n < n0, where n0 is a suitable constant.

For 1 n < n0, we have “Θ(1)” cn3, if pick c big enough.

 

Typically, it’s too easy to get an bound but not tight bound, just as what has been shown above, The tight bound is T(n) = O(n2).So the substitution is often used to justify rather than finding the complexity of algorithms.

Recursion-tree method

------an excellent method to guess the complexity of algorithms.

 

A recursion tree models the costs (time) of a recursive execution of an algorithm.

The recursion tree method is good for generating guesses for the substitution method.

The recursion-tree method can be unreliable, just like any method that uses ellipses (…).

The recursion-tree method promotes intuition, however.

Ø  In a recursion tree, each node represents the cost of a single subproblem somewhere in the set of recursive function invocations. We sum the costs within each level of the tree to obtain a set of per-level costs, and then we sum all the per-level costs to determine the total cost of all levels of the recursion. Recursion trees are particularly useful when the recurrence describes the running time of a divide-and-conquer algorithm.

Example: T (n) = 3T (n/4) + Θ(n2).

-            The cn2 term at the root represents the cost at the top level of recursion, and the three subtrees of the root represent the costs incurred by the subproblems of size n/4.

-            Since the root's contribution to the total cost is cn2, the root contributes a constant fraction of the total cost. In other words, the total cost of the tree is dominated by the cost of the root.

The master method

----- an great method to find the complexity of algorithms.

Three common cases

Compare f (n) with nlogba:

1.        f (n) = O(nlogba ε) for some constant ε > 0.

          f (n) grows polynomially slower than nlogba (by an nε factor).

Solution: T(n) = Θ(nlogba) .

2.        f (n) = Θ(nlogba lgkn) for some constant k 0.

          f (n) and nlogba grow at similar rates.

Solution: T(n) = Θ(nlogba lgk+1n) .

3.        f (n) = Ω(nlogba + ε) for some constant ε > 0.

          f (n) grows polynomially faster than nlogba (by an nε factor), and f (n) satisfies the regularity condition that a f (n/b) c f (n) for some constant c < 1.

Solution: T(n) = Θ( f (n)) .

Idea of master theorem

Why Case1 ?

Why Case2 ?

Why Case3 ?

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值