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 ïđ .