By Prof. Ruiz
Instructions
Solution:
In order to show that f(n)+g(n) = O(h(n)), we need to find constants k and n0 such that for all n ≥ n0, f(n)+g(n) ≤ kh(n). Since f(n) = O(h(n)) and g(n) = O(h(n)) then by definition:Take k = k1 + k2, and n0= max(n1,n2). Then
- ∃ k1 ∃ n1 ∀ n ≥ n1, f(n) ≤ k1h(n).
- ∃ k2 ∃ n2 ∀ n ≥ n2, g(n) ≤ k2h(n).
f(n)+g(n) ≤ k1h(n) + k2h(n) = (k1 + k2)h(n) = kh(n), for all n ≥ n0.
Hence f(n)+g(n) = O(h(n)).
Solution:
Remember that 22n = 2n+n = 2n*2n. Hence:
limn→inf(f(n)/g(n)) = limn→inf(22n/2n) = limn→inf(2n*2n/2n) = limn→inf(2n) = infinity.
Therefore, by the theorem stated in class, f(n) = Ω(g(n)).
Input:
Output:
Algorithm: (See next page)
Instruction
Time taken per instruction
Number of iterations
{
stable := true . . . . . . . . . . . . . . . . . .
m := 1 . . . . . . . . . . . . . . . . . .
while stable AND m ≤ n do { . . . . . . . . . . . . .
w := 1 . . . . . . . . . . . . . . . . . . .
while stable AND w ≤ n do { . . . . . . . . . . . . . .
(* we'll check below if (m,w) is an unstable pair *)
if (InverseMenPrefer[m,w] < InverseMenPrefer[m,wife[m]])
AND
(InverseWomenPrefer[w,m] < InverseWomenPrefer[w,husband[w]]) . .
then {
stable := false . . . . . . . . . . . . .
}
w := w + 1 . . . . . . . . . . . . . . . . . . .
}
m := m + 1 . . . . . . . . . . . . . . . . . . .
}
return(stable) . . . . . . . . . . . . . . . . . . .
}
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
1
1
---------
|
|
|
-- |
| |
| |
| |
| n | n
|times |times
| |
| |
| |
| |
| |
-- |
|
---------
1
Solution:
Let's start by noting that the "if condition"(InverseMenPrefer[m,w] < InverseMenPrefer[m,wife[m]]) AND (InverseWomenPrefer[w,m] < InverseWomenPrefer[w,husband[w]])is true
if and only if man m prefers woman w over his wife and w prefers m over her husband
if and only if the pair (m,w) is unstable
if and only if the input perfect matching is unstable.Let's show now that the algorithm always outputs the correct answer for the given input perfect matching:
- If the input perfect matching is stable, then no unstable pair exists and hence the if condition is never true. Then, the variable "stable", which has been set to true at the beginning of the program, is never set to false, and the program correctly outputs "true".
- If the input perfect matching is unstable, then there is at least one unstable pair (m,w) that makes the if condition true. Then, the variable "stable" is set to false, stopping the iterations of both while loops, and making the program correctly output "false".
Solution:
T(n) = C1 + C2 + [C3 + C4 + [C5 + C6 + C7 + C8]*n + C9]*n + C10 = C' + [C'' + C'''*n]*n = C' + C''n + C'''n2 where: C' = C1 + C2 + C10 C'' = C3 + C4 + C9 C''' = C5 + C6 + C7 + C8
Solution:
T(n) = C' + C''n + C'''n2. Let g(n) = n2.
In order to show that T(n) = Θ(g(n)) we need to find constants k1, k2, and n0 such that for all n ≥ n0, k1n2 ≤ T(n) ≤ k2n2.
Note that:Hence ∃ k1=C''' ∃ k2=(C'+ C''+ C''') ∃ n0=max(0,1) ∀ n ≥ n0, k1g(n) ≤ T(n) ≤ k2g(n). Therefore T(n) = Θ(g(n)).
- T(n) = C'+ C''n + C'''n2 ≤ C'n2 + C''n2 + C'''n2 = (C'+ C''+ C''') n2 = (C'+ C''+ C''') g(n), for all n ≥ 1, and
- T(n) = C'+ C''n + C'''n2 ≥ C'''n2 = C'''g(n), for all n ≥ 0.
Solution:
The perfect matching should be such that m1 and w1 (the 1st man and the 1st woman) form an unstable pair. For this particular case, the runtime of the algorithm would be:
T(n) = C1 + C2 + C3 + C4 + C5 + C6 + C7 + C8 + C9 + C10, that constant.
Solution:
T(n) = C' + C''n + C'''n2. Let h(n) = 1.
In order to show that T(n) = Ω(h(n)) we need to find constants k and n0 such that for all n ≥ n0, T(n) ≥ k*1.
Note that: T(n) = C'+ C''n + C'''n2 ≥ 1 for all n ≥ 1. Hence ∃ k=1 ∃ n0=1 ∀ n ≥ n0, T(n) ≤ k*1. Therefore T(n) = Ω(h(n)).
Solution:
![]()
No. Let TA and TB be the runtime functions of algorithms A and B, respectively. By definition, of Θ, TA(n) has the same growth rate as n2 for large enough n's (that is, for n ≥ nA for some constant nA ≥0), and TB(n) has the same growth rate as n3 for large enough n's (that is, for n ≥ nB for some constant nB ≥0). But for some n0 < max(nA,nB), it might happen that TA(n0) > TB(n0), and then B would be preferable to A on inputs of size n0.
Solution:
Yes. Note that O(.) does not provide tight bounds. Take for example f(n)=n2+n and g(n)=n. It is true for these functions that f(n)=O(n2) and g(n)=O(n3). Nevertheless, g(n) ≤ f(n) for all n, and hence an implementation of algorithm D can be more efficient than an implementation of algorithm C.