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 ๏น .