簡體   English   中英

求解遞歸T(floor [n / 2])+ T(ceil [n / 2])+ n-1

[英]solving the recurrence T(floor[n/2]) + T(ceil[n/2]) + n - 1

我的復發如下:

T(n) = c for n = 1.
T(n) = T(floor[n/2]) + T(ceil[n/2]) + n - 1 for n > 1.

在我看來,這似乎是歸並排序,所以我猜想遞歸的解決方案是Θ(nlogn)。 根據主方法,我有:

a) Θ(1) for n = 1 (constant time).
b) If we drop the floor and ceil we have: (step1)
   T(N) = 2T(N/2) + n - 1 => a = 2, b = 2.
   logb(a) (base b) = lg(2) = 1 so n^lg(2) = n^1 = n

仔細研究一下,我們知道我們擁有主方法的情況2:

 if f(n) = Θ(log(b)a) our solution to the recurrence is T(n) = Θ(log(b)a log(2)n)

解的確是T(n)=Θ(nlogn),但我們的常數因子為1。我的第一個問題是:在步驟1中,我們放棄了天花板和地板。 這個對嗎 ? 第二個問題是我如何擺脫常數因子1? 我會掉嗎? 或者我應該將其命名為d並證明n-1的確是n(如果是,我如何證明它?)。 最后,用替代方法證明它更好嗎?

編輯:如果我們使用替換方法,我們得到:

  We guess that the solution is O(n). We need to show that T(n) <= cn.
  Substitutting in the recurrence we obtein 
  T(n) <= c(floor[n/2]) + c(ceil[n/2]) + n/2 - 1 = cn + n/2 - 1 

那么這不是合並排序嗎? 我想念什么?

很久以前,但是這里

步驟1,我們放下了天花板和地板。 這個對嗎 ?

我寧願說

T(floor(n/2)) + T(floor[n/2)) <= T(floor(n/2)) + T(ceil[n/2)) 
T(floor(n/2)) + T(ceil[n/2)) <= T(ceil(n/2)) + T(ceil[n/2)) 

如果它們不相等,它們相差1(您可以忽略任何常數)

第二個問題是我如何擺脫常數因子1?

你無視它。 其背后的原因是:即使常數為10 ^ 100,即使n變大,常數也會很小。 在現實生活中,您不能真正忽略真正的大常數,但這就是現實生活和理論的不同之處。 在任何情況下,1的差異最小。

最后最好用替代法證明

您可以證明自己的喜好,有些只是簡單一些。 越簡單通常越好,但是其他的“更好”毫無意義。 所以我的回答是不。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM