Data Structure Lecture Note (Week 1, Lecture 3)

Recursion

Master Theorem

( b ≥ 1 b\ge 1 b1)

Suppose F ( N ) = a F ( N b ) + N c F(N) = aF(\frac{N}{b}) + N^c F(N)=aF(bN)+Nc, let c 0 = log ⁡ b a c_0 = \log_b a c0=logba, then:

  • $F(N) = \Theta(N^c) $ if c 0 < c c_0 < c c0<c
  • F ( N ) = Θ ( N c log ⁡ N ) F(N) = \Theta(N^c\log N) F(N)=Θ(NclogN) if c 0 = c c_0 = c c0=c
  • F ( N ) = Θ ( N c 0 ) F(N) = \Theta(N^{c_0}) F(N)=Θ(Nc0) if c 0 > c c_0 > c c0>c

Proof:

How to calculate F ( N ) = a F ( N b ) + N c F(N) = aF(\frac{N}{b}) + N^c F(N)=aF(bN)+Nc? We need to expand it into recursion and non-recursion parts

在这里插入图片描述

That is, F ( N ) = N c [ 1 + ( a / b c ) + ( a / b c ) 2 + . . . + ( a / b c ) log ⁡ b N ] F(N) = N^c [1 + (a/b^c) + (a/b^c)^2+...+(a/b^c)^{\log_b N}] F(N)=Nc[1+(a/bc)+(a/bc)2+...+(a/bc)logbN]

If a / b c < 1 a/b^c < 1 a/bc<1, the bracket is a convergent geometric sequence, which would finally converge to some real number

If a / b c = 1 a/b^c = 1 a/bc=1, we simply add log ⁡ b N \log_b N logbN times 1

If a / b c > 1 a/b^c > 1 a/bc>1, the brackets give $ (1-(a/bc){\log_bN + 1}) / (1- a/b^c) = \Theta( ((\frac{a}{b^c}) ^{\log_b N}) ) = \Theta(a^{\log_b N} / N^c) = \Theta ( a^{\log_a N / \log_a b } / N^c ) = \Theta (N^{c_0} / N^c) $ , so overall yields Θ ( N c 0 ) \Theta (N^{c_0}) Θ(Nc0)

This finished the proof for the Master Theorem.


Remark: the reason why it produces at most polynomial complexity is, each time it solves a subproblem with problem size reduced by greater or equal to 1.

Paradigm in Recursion and Pitfalls

Step1: Identify the subproblem

Step2: Solve the origin problem by solving subproblems, then combine the solutions

Or

Step2’: Solve the origin problem by first tackling an easier part, then reduce it to a smaller problem.


Pitfall: when you try to use recursion, make sure that

  • Subproblems CAN be combined to solve the origin problem. For example, if you are asked to carry a big artifact into the museum, and you think it’s too heavy. You should not try to slice it into small pieces and carry multiple times… Because they cannot be glued OR you do want the artifact intact!
  • Subproblems should be ABLE to solved. This means, if the problem have certain constraints, your subproblem must satisfy all the constraints!

Examples

Look for a word in a dictionary.

Hanoi Tower Problem (very notorious problem with sole purpose used in academia to train recursion)

Max sum subarray:

​ Input: Array[1,…,n] containing n intergers

​ Output: Compute a subarray A[ i ∗ i^* i, …, $j^* $] with maximum sum.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值