Try   HackMD
tags: ADA 3.2 Recursion-Tree Method

ADA 3.2: Recursion-Tree Method

Textbook Chapter 4.4 - The recursion-tree method for solving recurrences

Review

Merge Sort

之前ADA 2.4有用過 (Merge Sort)

T(n)={O(1)if n=12T(n/2)+O(n)if n2

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

* T(n): 處理size為N所耗費的時間

Recursion-Tree Method 遞迴樹法

Solving recurrence

  • Substitution method(取代法)
  • Recursion-Tree method(遞迴樹法)
  • Master method(套公式大法/大師法)

Steps to solve

  • Expand a recurrence into a tree (把樹給展開)
  • Sum up the cost of all nodes as a good guess (加總)
  • Verify the guess as in substitution method (驗證)

Advantages

  • Promote intuition
  • Generate good guesses for the subtitution method
    => 比較直覺, 容易猜

Recursion-Tree Example

舉例

T(n)=2T(n/2)+cn
(Merge Sort)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

每層都是cn, 故只要求出樹高就知道總和

2h=n
=>k=log2n

總和則為

cn tree height
=>cnlog2n

=>O(nlogn)

T(n)=T(n/4)+T(n/2)+cn2

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

T(n)=T(n/4)+T(n/2)+cn2

=(T(n4/4)+T(n4/2)+c(n4)2)+(T(n2/4)+T(n2/2)+c(n2)2)+cn2

=...

累加後為

T(n)(1+516+(516)2+(516)3+...)cn2

=(1(516)n)1516cn2

=1611cn2

=O(n2)

當子問題足夠小時(切割次數趨近無限), 可以以O(1)秒解掉

(516)n=0就可以忽略

等比級數相加公式
💡

a1=a,an=arn1,Sn=a(1rn)1r

Proof

S0=a+ar+ar2+ar3+...+arn1...(1)

兩邊同乘

r
rS0=ar+ar2+ar3+...+arn1+arn...(2)

(1)(2)
(1r)S0=aarn

S0=aarn(1r)

=a(1rn)(1r)

簡單證明

0<=r<=1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →