DOCX

advertisement
Practical Session 2 – “Constants Don’t Matter!!!”
Algorithm Analysis
Notation
Limit
(if exists)
Definition
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:
๐‘“(๐‘›) = ๐‘›
1
๐‘”(๐‘›) = {๐‘”(๐‘› − 1)
๐‘›2
๐‘–๐‘“ ๐‘› = 1
๐‘–๐‘“ ๐‘”(๐‘› − 1) ≥ √๐‘›
๐‘œ๐‘กโ„Ž๐‘’๐‘Ÿ๐‘ค๐‘–๐‘ ๐‘’
or
๐‘“(๐‘›) = ๐‘›2
1
๐‘”(๐‘›) = {๐‘”(๐‘› − 1)
๐‘›2.1
๐‘–๐‘“ ๐‘› = 1
๐‘–๐‘“ ๐‘”(๐‘› − 1) ≥ ๐‘›1.9
๐‘œ๐‘กโ„Ž๐‘’๐‘Ÿ๐‘ค๐‘–๐‘ ๐‘’
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 ) = ๐‘“ ( ).
๐‘›
We will prove that 2
2
๐‘›
2
≠ ๐‘‚ (2 ):
๐‘›
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 // if (A[m] > m)
return SolveC(A,m+1,q)
To solve C we use the call SolveC(A,1,n).
Question 8
You have two smartphone devices 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 device to the ground so that it doesn't break on impact. You can throw
a device 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 device 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 device, 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 device from the 1st floor up - which could mean N/2 -1
total throws in the worst case.
The solution is to throw the first device 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