DOCX

advertisement
Practical Session 2 – “Constants Don’t Matter!!!”
Algorithm Analysis
There exist c > 0 and n0 > 0 such that:
f(n) = O(g(n))
f(n) = Ω(g(n))
f(n) = Θ(g(n))
0 ≤ f(n) ≤ cg(n)
for each n ≥ n0
There exist c > 0 and n0 > 0 such that:
0 ≤ cg(n) ≤ f(n)
𝑓(𝑛)
|
|<∞
lim 𝑔(𝑛)
𝑛→∞
for each n ≥ n0
There exist c1, c2 > 0 and n0 > 0 such that:
0 ≤ c1g(n) ≤ f(n) ≤ c2g(n)
for each n ≥ n0
𝑓(𝑛)
lim |
|>0
𝑛→∞ 𝑔(𝑛)
𝑓(𝑛)
|
lim 𝑔(𝑛)| = 𝑐
𝑛→∞
0<𝑐<∞
:â€ŦŨĐŨ™ŨžŨ• ŨœŨ‘‎
.â€ŦŨĐŨœŨ™ŨœŨ™Ũ•ŨŠ ŨŨĄŨ™ŨžŨĪŨ˜Ũ•Ũ˜Ũ™ŨŠâ€Ž-â€ŦŨĪŨ•Ũ Ũ§ŨĶŨ™Ũ•ŨŠ Ũ”ŨžŨŠŨŨĻŨŠ Ũ–ŨžŨŸ ŨĻŨ™ŨĶŨ” Ũ”Ũ™Ũ ŨŸ ŨŨ™â€Ž
.â€ŦŨ‘ŨžŨ”ŨœŨš Ũ”Ũ§Ũ•ŨĻŨĄ Ũ‘Ũ“"Ũ› Ũ ŨŠŨ™Ũ™Ũ—ŨĄ ŨœŨĪŨ•Ũ Ũ§ŨĶŨ™Ũ•ŨŠ ŨžŨĄŨ•Ũ’ Ũ–Ũ”‎
Some Basics
Prove: 100 * n + 12 = Θ(n).
Solution:
𝑐1 𝑛 ≤ 100𝑛 + 12 → 𝒄𝟏 = 𝟏, ð‘Ąð‘Ÿð‘Ē𝑒 𝑓𝑜𝑟 𝒆𝒗𝒆𝒓𝒚 𝒏
100𝑛 + 12 ≤ 𝑐2 𝑛 →
𝑐2 ≥ 100 +
12
→
𝑛
𝒄𝟐 = 𝟏𝟎𝟒, ð‘Ąð‘Ÿð‘Ē𝑒 𝑓𝑜𝑟 𝑒ð‘Ģ𝑒𝑟ð‘Ķ 𝒏 ≥ 𝟑
Prove: 𝑛3 + 𝑛2 + 𝑛 = ð‘ķ(𝑛3 ).
Solution:
𝑛3 + 𝑛2 + 𝑛 ≤ 𝑐𝑛3 → 𝒄 = 𝟐, ð‘Ąð‘Ÿð‘Ē𝑒 𝑓𝑜𝑟 𝒆𝒗𝒆𝒓𝒚 𝒏 ≥ 𝟐
Question 1
Prove :
(log n)log n = Ω(n3/2).
Solution:
cn3/2 = c(2log n)3/2 = c(23/2)log n ≤ (log n)log n
cn3/2 ≤ (log n)log n
c=1
n0 = 8
Question 2
Prove that log(𝑛!) = ð›Đ(𝑛 log 𝑛).
Solution:
log(𝑛!) = log(1 ∗ 2 ∗ 3 ∗ … ∗ 𝑛) = log 1 + log 2 + log 3 + … + log 𝑛.
𝑛
∑ log 𝑖 ≤ 𝑛 log 𝑛 ⇒ log(𝑛!) = 𝑂(𝑛 𝑙𝑜𝑔𝑛)
𝑖=1
𝑛
𝑛
∑ log 𝑖 ≥ ∑ log 𝑖 ≥
𝑖=1
𝑖=
𝑛
2
𝑛
𝑛
𝑛
𝑛
𝑛
log ( ) ≥ (log 𝑛 − log 2) = log 𝑛 − ≥∗ 𝑐𝑛 𝑙𝑜𝑔𝑛 ⇒
2
2
2
2
2
⇒ log(n!) = Ω(𝑛 log 𝑛)
Explanation of the inequality:
n
n
1
1
ï‚ģ * : log n  ï‚ģ cn log n => log n  ï‚ģ c log n
2
2
2
2
1
1
log n  ï‚ģ c log n
2
2
c = 0.25
n0 = 8
log(n!) = Θ(n log n)
Question 3
Prove :
(log n)! = Ω (log(n!)).
Solution:
By the previous question, it is sufficient to prove that (log n)! = Ω(n logn).
Observe that for an integer k ≥ 4, we have k! > 2k.
Hence, (logn)! = (logn)(logn -1)! > (logn) ∙2logn -1
= (logn) (n/2) ≥ c∙n logn.
(logn)! = Ω(n logn).
Question 4
Give an example for two functions g(n) and f(n) such that neither g(n) = O(f(n)) nor
g(n) = â„Ķ(f(n)).
Solution:
f(n) = n, g(n) = n1-cos n.
We will prove that
𝑔(𝑛) ≠ ð‘ķ(𝑓(𝑛)):
Suppose there exists a constant c
such that: 𝑛1−cos 𝑛 ≤ 𝑐 ⋅ 𝑛
for each 𝑛 ≥ 𝑛0 .
Then 𝑛− cos 𝑛 ≤ 𝑐 for each 𝑛 ≥ 𝑛0 .
There exists an infinite series of
numbers nk for which
𝑐𝑜𝑠(𝑛𝑘 ) < −0.1.
So there exist infinitely many
numbers n for which
𝑛− cos 𝑛 ≥ 𝑛0.1, while c is a constant.
So there do not exist 𝑐 and 𝑛0 such
that for each 𝑛 ≥ 𝑛0 𝑛− cos 𝑛 ≤ 𝑐.
Contradiction!
We will prove that 𝑔(𝑛) ≠ â„Ķ(𝑓(𝑛)):
Suppose there exists a constant c such that: 𝑛1−cos 𝑛 ≥ 𝑐 ⋅ 𝑛 for each 𝑛 ≥ 𝑛0 .
Then 𝑛− cos 𝑛 ≥ 𝑐 for each 𝑛 ≥ 𝑛0 .
There exists an infinite series of numbers 𝑛𝑘 for which 𝑐𝑜𝑠(𝑛𝑘 ) > 0.1. So there exist
infinitely many numbers 𝑛 for which 𝑛− cos 𝑛 ≤ 𝑛−0.1, while c is a constant. So there
do not exist 𝑐 and 𝑛0 such that for each 𝑛 ≥ 𝑛0 𝑛− cos 𝑛 ≥ 𝑐.
Contradiction!
Question 5
Given the monotone inceasing functions 𝑓(𝑛): ℕ → ℕ , 𝑔(𝑛): ℕ → ℕ such that
𝑓(𝑛) = ð›Đ(𝑔(𝑛)). Prove log(𝑓(𝑛)) = ð›Đ (log(𝑔(𝑛)))
:â€ŦŨĪŨŠŨĻŨ•ŨŸâ€Ž
:â€Ŧ𝑛 ≥ 𝑛 ŨžŨŠŨ§Ũ™Ũ™Ũ Ũ›Ũ™â€Ž0 â€Ŧ𝑐 Ũ›Ũš ŨĐŨœŨ›Ũœâ€Ž1 , 𝑐2 , 𝑛0 > 0 â€Ŧ Ũ§Ũ™Ũ™ŨžŨ™Ũâ€Ž,â€ŦŨžŨ”Ũ ŨŠŨ•ŨŸâ€Ž
0 ≤ 𝑐1 𝑔(𝑛) ≤ 𝑓(𝑛) ≤ 𝑐2 𝑔(𝑛)
.𝑛 ≥ 𝑛′ â€Ŧ)𝑛(𝑓 ŨĒŨ‘Ũ•ŨĻ Ũ›Ũœâ€Ž, 𝑔(𝑛) > 0 â€Ŧ𝑛 Ũ›Ũš ŨĐ‮′ ∊ ℕ â€Ŧ Ũ•ŨœŨ›ŨŸ Ũ§Ũ™Ũ™Ũâ€Ž,â€ŦŨ ŨŠŨ•ŨŸ Ũ›Ũ™ Ũ”ŨĪŨ•Ũ Ũ§ŨĶŨ™Ũ•ŨŠ ŨžŨ•Ũ Ũ•Ũ˜Ũ•Ũ Ũ™Ũ•ŨŠ ŨĒŨ•ŨœŨ•ŨŠâ€Ž
:â€Ŧ𝑛 ≥ 𝑛 ŨžŨŠŨ§Ũ™Ũ™Ũ Ũ›Ũ™â€Ž′ â€Ŧ ŨœŨ›Ũœâ€Ž,â€ŦŨŨ–Ũ™â€Ž
log(𝑐1 𝑔(𝑛)) ≤ log(𝑓(𝑛)) ≤ log(𝑐2 𝑔(𝑛)) = log(𝑐2 ) + log(𝑔(𝑛)) ≤ 2 ∗ log(𝑔(𝑛))
0.5 ∗ 𝑙𝑜𝑔(𝑔(𝑛)) ≤ log(c1 ) + log(g(n)) ≤ log(𝑓(𝑛)) â€Ŧ ŨžŨĄŨĪŨ™Ũ§ Ũ’Ũ“Ũ•Ũœâ€Žn â€ŦŨĒŨ‘Ũ•ŨĻ‮
log(𝑐2 ) ≤ log(𝑔(𝑛)) :â€Ŧ ŨžŨĄŨĪŨ™Ũ§ Ũ’Ũ“Ũ•Ũœ ŨžŨŠŨ§Ũ™Ũ™Ũ Ũ›Ũ™â€Žn â€ŦŨĒŨ‘Ũ•ŨĻ‮
Question 6
Prove or disprove:
1. For any functions 𝑓(𝑛) and 𝑔(𝑛), 𝑓(𝑛) = 𝑂(𝑔(𝑛)) ⇒ 𝑔(𝑛) = â„Ķ(𝑓(𝑛)).
𝑛
2. For any function 𝑓(𝑛), 𝑓(𝑛) =  (𝑓 ( 2)).
Solution:
1
1. 𝑓(𝑛) = 𝑂(𝑔(𝑛)) ⇒ 𝑓(𝑛) ≤ 𝑐𝑔(𝑛) ⇒ 𝑔(𝑛) ≥ ∙ 𝑓(𝑛) ⇒ 𝑔(𝑛) = â„Ķ(𝑓(𝑛)).
𝑐
𝑛
𝑛
2. False, e.g., 𝑓(𝑛) = 2𝑛 . Then 𝑓(𝑛) = 2𝑛 ≠  (22 ) = 𝑓 (2).
𝑛
We will prove that 2𝑛 ≠ 𝑂 (22 ):
𝑛
Suppose there exists a constant c such that: 2𝑛 ≤ 𝑐 ∙ 22 for each 𝑛 ≥ 𝑛0 . Then
𝑛
22 ≤ 𝑐 for each 𝑛 ≥ 𝑛0 . So 𝑐 is not a constant, because it is bigger than a
diverging expression. Contradiction!
Question 7
Given a sorted array A of n distinct integers.
a) Describe an 𝑂(1) time algorithm that determines whether or not there exists an
integer element ð‘Ĩ, such that ðī[1] < ð‘Ĩ < ðī[𝑛] and ð‘Ĩ is not in ðī.
b) Describe an 𝑂(log 𝑛) time algorithm that finds such an ð‘Ĩ.
c) Describe an efficient algorithm that determines whether or not there exists an
element i in A, such that ðī[𝑖] = 𝑖 (1 ≤ 𝑖 ≤ 𝑛). Analyze the running time of the
algorithm.
Solution:
a) In answering this question we assume that A's indexes run from 1 to n
We describe the algorithm SimpleCheck(A,r,q) where r<q are natural numbers.
SimpleCheck(A,r,q)
return (A[q] – A[r] < q-r)
To solve a we run SimpleCheck(A,1,n).
b)
We describe the algorithm SolveB(A,r,q) where r<q are natural numbers.
SolveB(A,r,q)
if (𝑟 + 1 = 𝑞) return ðī[𝑟] + 1
𝑚 ← ⌊(𝑟 + 𝑞)/2⌋
if (SimpleCheck(A,r,m) = true)
return SolveB(A,r,m)
else
return SolveB(A,m,q)
To solve b we use the following algorithm:
if (SimpleCheck(A,1,n) = false)
Print "There is no such element in A"
else
return SolveB(A,1,n)
c)
We describe a recursive algorithm SolveC(A,r,q) where r<q are natural numbers.
SolveC(A,r,q)
if (q < r)
return false
𝑚 ← ⌊(𝑟 + 𝑞)/2⌋
if (A[m] = m)
return true
if (A[m] > m)
return SolveC(A,r,m-1)
else
return SolveC(A,m+1,q)
To solve C we use the call SolveC(A,1,n).
Question 8
You have two jars made from a special secret material. You are in a tall building with
N floors, and you must determine what is the top floor M from which you can drop a
jar to the ground so that it doesn't break on impact. You can throw a jar out a window
as many times as you want as long as it doesn't break. Describe a strategy for finding
the highest safe rung that requires you to drop a jar at most f(n) times, for some function
f(n) that grows slower than linearly. (In other words, it should be the case that
𝑓(𝑛)
lim 𝑛 = 0 ).
𝑛→∞
Solution:
One is tempted to do a binary search with the first jar, but this is not that efficient, since
if you drop your first sphere from the N/2'th floor and it breaks, you need to do a linear
search using the second jar from the 1st floor up - which could mean N/2 -1 total throws
in the worst case.
The solution is to throw the first jar from floors i*sqrt(N) for increasing values of i.
Once it breaks you have a possible range of sqrt(N) values of M, so in total you never
do more than 𝑓(𝑛) = 2√𝑛 − 1 throws.
Improvement:
In the previous solution, if for example the building was 100 stories tall, we would need
19 throws in the worst case. The solution below allows us to do it in 14 throws:
 Throw from floor #14
 If it breaks, we have 13 throws (floor #1 – floor #13). Including the first throw
from floor #14 we used 14 throws.
 If it does not break throw from floor #27 (14+13).
 If it breaks, we have 12 throws (floor #15 – floor #26). Including the first throw
from floor #14 and the second throw from floor #27 we used 14 throw.
 If it does not break throw from floor #39 (14+13+12).
 If it breaks, we have 11 throws (floor #28 – floor #38). Including the first throw
from floor #14, the second throw from floor #27 and the third throw from #39
we used 14 throw.
 Continue in the same manner until we reach #100.
In summary, we start from floor #14 and each throw reduce 1 from the distance to
the next jump until it breaks, then throw all the option in the remaining range.
Where did the 14 come from?
14 is the smallest number that gives: 14+13+12+11+… ≥ 100
If the number of floors is n, we need 1+2+3+….+x ≥ n → x(x+1)/2 ≥ n
x= ïƒĐ√2𝑛 − 1 ïƒđ .
Download