سوال ۶۷ کنکور ارشد علوم کامپیوتر ۱۳۹۹
اهمیت
۰/۵
مرتبه زمانی الگوریتم f2 کدام است؟
f1(n)
{
for(x = ۰ , i = ۰ ; i < n ; i++)
x++
return(x)
}
f2(n)
{
if(n == ۰) return(۰)
else return f2(n/۲) + f1(n) + f2(n/۲)
}
پاسخ صحیح: گزینهٔ C —
پاسخ تشریحی
Step 1 — Cost of f1(n)
The loop for(x=0, i=0; i<n; i++) x++ executes the body exactly times, so
Step 2 — Set up the recurrence for f2(n)
For , f2 makes two recursive calls on inputs of size , plus one call to f1(n) (which costs ) and for the additions:
Step 3 — Apply the Master Theorem
Here , , and . The watershed function is
Since , we are in Case 2 of the Master Theorem, which adds a logarithmic factor:
This is exactly the merge-sort style recurrence: levels of recursion, each doing total work.
Hence the time complexity of f2 is — option 3.
Exam trick: The recurrence is the canonical merge-sort recurrence; recognize it on sight as without re-deriving the Master Theorem.
#master-theorem#recurrence#divide-and-conquer#time-complexity#loop-analysis#asymptotic-analysis