In: Computer Science
Calculate the Big-O time complexity. Show work
1. n^2 + 3n + 2
2. (n^2 + n)(n ^2 + π/2 )
3. 1 + 2 + 3 + · · · + n − 1 + n
Calculate the Big-O time complexity of the following:
• The upper bound notation for the function F(n) is provided by the Big-OH (O) notation. For the all suitable large value of n, the function F(n) = O(g(n)) if we can prove following statement.
• O(g) is the set of function from non-negative integers to positive real numbers such that for some real constant C >0 and some non-integer constant number n0:
F(n) ≤ C * g(n) + n0 for all n ≥ n0
(1) n2 + 3n + 2
Time complexity: O(n2)
Description:
F(n) ≤ C * g(n) + n0 for all n ≥ n0
⇒ n2 + 3n + 2 ≤ n2 + 3n + n ;n ≥ 2
⇒ n2 + 3n + 2 ≤ n2 + 3n + n ;n ≥ 2
⇒ n2 + 3n + 2 ≤ n2 + 4n ;n ≥ 2
⇒ n2 + 3n + 2 ≤ n2 + n2 ;n2 ≥ 4n
⇒ n2 + 3n + 2 ≤ 2n2 ;n2 ≥ 4n
So, g(n) = n2 and C = 2
F(n) = O(g(n))
F(n) = O(n2)
(2) (n2 + n)(n2 + π/2)
Time complexity: O(n4)
Description:
(n2 + n)(n2 + π/2) = n4 + n3 + (π/2)n2 + (π/2)n
F(n) ≤ C * g(n) + n0 for all n ≥ n0
⇒ n4 + n3 + (π/2)n2 + (π/2)n ≤ n4 + n3 + (π/2)n2 + n2 ; n2 ≥ (π/2)n
⇒ n4 + n3 + (π/2)n2 + (π/2)n ≤ n4 + n3 + (π)n2 ; n2 ≥ (π/2)n
⇒ n4 + n3 + (π/2)n2 + (π/2)n ≤ n4 + n3 + n3 ; n3 ≥ (π)n2
⇒ n4 + n3 + (π/2)n2 + (π/2)n ≤ n4 + 2n3 ; n3 ≥ (π)n2
⇒ n4 + n3 + (π/2)n2 + (π/2)n ≤ n4 + n4 ; n4 ≥ 2n3
⇒ n4 + n3 + (π/2)n2 + (π/2)n ≤ 2n4 ; n4 ≥ 2n3
So, g(n) = n4 and C = 2
F(n) = O(g(n))
F(n) = O(n4)
(3) 1 + 2 + 3 + · · · + n − 1 + n
Time complexity: O(n2)
Description:
1 + 2 + 3 + · · · + n − 1 + n = n(n + 1)/2 = n2/2 + n/2
F(n) ≤ C * g(n) + n0 for all n ≥ n0
⇒ n2/2 + n/2 ≤ n2/2 + n2 ; n2 ≥ n/2
⇒ n2/2 + n/2 ≤ (3/2)n2 ; n2 ≥ n/2
So, g(n) = n2 and C = 3/2
F(n) = O(g(n))
F(n) = O(n2)