Cho biết độ phức tạp của đoạn mã giả sau dùng ký hiệu big-O.int F(int n)if (n <= 2)return 1elsereturn F(n − 2) * F(n − 2)a.O(2^(n/2))b.O(2^n)c.O(n^2)d.O( n )
Question
Cho biết độ phức tạp của đoạn mã giả sau dùng ký hiệu big-O.
int F(int n)
if (n <= 2)
return 1
else
return F(n − 2) * F(n − 2)
a. O(2^(n/2))
b. O(2^n)
c. O(n^2)
d. O(n)
Solution
Breakdown the Problem
- We need to analyze the given pseudocode for the function
F(n)
. - Determine the time complexity using big-O notation.
Relevant Concepts
-
The function
F(n)
is defined as:- If , it returns .
- Otherwise, it returns .
-
This recursive function is essentially splitting into two calls of
F(n-2)
.
Analysis and Detail
-
The recursive case can be rewritten as .
-
Each recursive call reduces by 2, leading to this recurrence relation: and for .
-
To solve this recurrence, we can expand it: Continuing this until we reach the base case, we have such that . Thus, .
-
Replacing (the base case) into the equation, we find:
Verify and Summarize
- The time complexity is , which can also be expressed as .
Final Answer
The time complexity of the given pseudocode is a. O(2^(n/2)).
Similar Questions
Cho biết độ phức tạp của đoạn mã giả sau dùng ký hiệu big-O.s = 0for (i = 1; i <= n; i *= 3)s = s + a[i]a.O( n )b.O(n^2)c.O(logn)d.O(nlogn)Clear my choice
A growth function that is O(n) is ____________________ A. constant B. logarithmic C. linear D. quadratic E. exponential
Prove or Disprove (4 Marks)a. ¼ n 2 - 2n + ½ Ɛ O(n 2 )b. 4n 2 + 25 n – 2010 = (n2)c. 8n 2 + 2n - 3 O(n 2 )d. 2n 2 = 4n + O(22n)
Which of the following time complexities indicates the slowest growing function?O(n!)O(2^n)O(n^3)O(n log n)
f(n) = 2^(2n)Which of the following correctly represents the above function?Select one:O(2^n)Ω(2^n)Θ(2^n)None of these
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.