Technische Universit¨ Universitat u ¨at Munchen ¨ nchen Fakult at u ¨at f ur ¨ r Informatik Lehrstuhl f ur u Effiziente Algorithmen Algorithmen ¨ r Effiziente Prof. Dr. Harald Racke ¨acke Chintan Chintan Shah
Wintersemester 2013 Solutions for Assignment 1 21 October October 2013 2013
Efficient Algorithms and Datastructures I Question 1 (10 Points) Prove the following statements: 1. f (n) + g (n) Ω( f (n))
∈
2. f (n) O (g (n))
∈ ⇒ f (n) + g (n) ∈ O (g(n)) 3. f (n) ∈ o (g (n)) and g (n) ∈ O (h(n)) ⇒ h (n) ∈ ω (f (n)) 4. f (n) ∈ O (g (n)) and g (n) ∈ O (f (n)) ⇔ f (n) ∈ Θ( g(n)) State whether the following statement is true: 1.
1 Ω(n)
⊆ O (
1 n
)
Solution 1. f (n) + g (n) Ω( f (n)) Taking c = 1 and n0 = 1, we get
∈
∀n ≥ n , c · f (n) = f (n) ≤ f (n) + g (n) 2. f (n) ∈ O (g (n)) ⇒ f (n) + g (n) ∈ O (g (n)) f (n) ∈ O (g(n)) ⇒ ∃c > 0 and ∃n > 0 such that ∀n ≥ n , f (n) ≤ c · g (n) ⇒ ∀n ≥ n , f (n) + g (n) ≤ (c + 1) · g(n) 0
0
0
0
Taking c = c + 1 > 0 and n0 = n 0 , we get n
∀ ≥ n , f (n) + g (n) ≤ c · g(n). 3. f (n) ∈ o (g (n)) and g (n) ∈ O (h(n)) ⇒ h (n) ∈ ω (f (n)) f (n) ∈ o (g (n)) ⇒ ∀c > 0 , ∃n > 0 such that f (n) < c · g(n)∀n ≥ n g (n) ∈ O (h(n)) ⇒ ∃c > 0 , ∃n > 0 such that g (n) ≤ c · h(n)∀n ≥ n
0
1
1
1
1
2
2
2
2
We wish to prove that c0 > 0 , n0 > 0, such that c0 f (n) < h(n). Given c0 , choose c1 = c 1c . Then, for n n 0 = max n1 , n2 ,
∀
∃
≥
0· 2
c0 f (n) =
·
f (n) c1 g(n) g (n) = < c1 c2 c1 c2 c2
·
·
·
{
·
}
≤ c ·ch(n) = h(n) 2
2
4. f (n) O (g (n)) and g (n) O (f (n))
⇔ f (n) ∈ Θ(g(n)) (a) f (n) ∈ O (g(n)) and g (n) ∈ O (f (n)) ⇒ f (n) ∈ Θ( g(n)) f (n) ∈ O (g (n)) ⇒ ∃c > 0 and ∃n > 0 such that ∀n ≥ n , f (n) ≤ c · g(n) g(n) ∈ O (f (n)) ⇒ ∃c > 0 and ∃n > 0 such that ∀n ≥ n , g (n) ≤ c · f (n) ∈
∈
Then, taking c3 =
1 c2
1
1
1
1
2
2
2
2
, c4 = c 1 and n0 = max n1 , n2 , we get n
{
c3 g (n)
∀ ≥ n ,
}
0
≤ f (n) ≤ c · g(n). (b) f (n) ∈ Θ( g (n)) ⇒ f (n) ∈ O (g (n)) and g (n) ∈ O (f (n)) f (n) ∈ Θ(g (n)) ⇒ ∃n , c , c , such that ∀n ≥ n , c · g (n) ≤ f (n) ≤ c · g (n). ·
Taking c2 =
1
c3
0
3
4
4
0
1 Ω(n)
⊆ O (
1 n
4
and c1 = c 4 , we get
∀n ≥ n , f (n) ≤ c · g(n) ⇒ ∀n ≥ n , g(n) ≤ c · f (n) ⇒ 5.
3
0
1
0
2
f (n)
∈ O (g(n)) g (n) ∈ O (f (n))
)
True.
Let f (n) Ω( n). We will show that
∈
f (n)
1 f (n)
∈ O
1
n
.
∈ Ω(n) ⇒ ∃c > 0 and ∃n > 0 such that ∀n ≥ n , f (n) ≥ c · n ⇒ ∀n ≥ n , f (1n) ≤ cn1 0
0
0
⇒
1 f (n)
∈ O
1
n
Question 2 (10 Points) For constants c, > 0 and k > 1, arrange the following functions of n in non-decreasing asymptotic order so that f i (n) = O(f i+1(n)) for two consecutive functions in your sequence. Also indicate whether f i (n) = Θ(f i+1 (n)) holds or not. nk ,
√ n, 2 , n n
1+sin(n)
, log(n!), nk+ , nn , n , nk (log n)c , n!, n log n, 3n , n log log n, n log(n2 )
Solution n o (n) o (n log log n) o (n log n) Θ( n log(n2 )) Θ(log(n!)) o (nk ) o (nk (log n)c ) o (nk+ ) o (2n ) o (3n ) o (n!) o (nn )
√ ∈ ∈
∈
∈
∈ ∈
∈
∈
∈ ∈ n ∈ O (n 0
1+sin(n)
∈ ) ∈ O (n )
∈
2
Question 3 (5 Points) Let f (n) and g (n) be asymptotically non-negative functions. Using the basic definition of Θ-notation, prove that max f (n), g (n) = Θ(f (n) + g (n)).
{
}
2
Solution Let h(n) = max f (n), g (n) . Then,
{
}
h(n) =
f (n),
if f (n) g (n), if f (n) < g (n).
≥
g (n),
We show that c1, c2 , n0 > 0 such that
∃
c1 (f (n) + g (n))
(1) } ≤ c (f (n) + g (n)), ∀n ≥ n . Since f (n) and g (n) are asymptotically non-negative, ∃n > 0 such that f (n) ≥ 0 and g (n) ≥ 0 ∀n ≥ n . Thus, for n ≥ n , f (n) + g (n) ≥ f (n) ≥ 0 and f (n) + g (n) ≥ g (n) ≥ 0. Since for any particular n, h(n) is either f (n) or g (n), we have f (n) + g (n) ≥ h (n) ≥ 0, which shows that h(n) = max {f (n), g (n)} ≤ c (f (n) + g (n)) ∀n ≥ n for c = 1. Similarly, since for any particular n, h(n) is the larger of f (n) and g (n), we have ∀n ≥ n , 0 ≤ f (n) ≤ h(n) and 0 ≤ g(n) ≤ h(n). Adding these two inequalities ≤ h(n), which shows that gives us 0 ≤ f (n) + g (n) ≤ 2h(n), or 0 ≤ h(n) = max {f (n), g (n)} ≥ c (f (n) + g (n)) ∀n ≥ n for c = .
≤
max f (n), g (n)
{
2
0
0
0
0
2
0
0
(f (n)+g(n)) 2
1
0
1
2
1 2
Question 4 (5 Points) Show that for any real constants a and b, where b > 0, (n + a)b = Θ(nb )
Solution Observe that n + a n + a 2 n when a n . 1 Also, n + a n a n when a 2 So, when 2 a n , we have
≤
| |≤ | | ≤ ≥ −| | ≥ | | ≤ | |≤
0
n
≤ 2 ≤ n + a ≤ 2n ⇒ ⇒
So taking c1 =
1 b , 2
n
2
. n
b
≤ ≤ ≤ ≤
0
2 1 2
0
( n + a)b
b
≤ (2n) , since b > 0
b
nb
( n + a)b
b
c2 = 2b and n0 = 2 a satisfies the definition.
||
3
b
≤ 2 n