Question 1: Given the Selection Sort and Binary search algorithms you have already seen in introduction to CS: public static void selectionSort(int[] arr){ // Assumes a!=null, a is sorted int i = 0; public static int binarySearch(int[] arr, int key){ while (i<arr. length-1){ int ans = -1; // default (not found) value int minInd = minIndex(arr,i) ; boolean found = false; swap(arr,i,minInd) ; int low = 0; i=i+1; int high = arr. length-1; } while (low <= high & !found){ } // selectionSort int middle = (low+high)/2; if(arr[middle] == key){ // Assumes arr!=null ans = middle; public static int minIndex(int[] arr, int from) { found = true; int ans = from; } for(int i =from+1; i<arr. length; i=i+1) else if (key < arr[middle]) if (arr[ans] > arr[i]) high = middle-1; ans = i; else low = middle+1; return ans; } } // minIndex return ans; } // binarySearch //swap element arr[i] with arr[j] public static void swap(int[] arr, int i, int j){ int tmp = arr[i] ; arr[i] = arr[j] ; arr[j] = tmp; } // swap Analyze the run-time of each of the functions above (denote arr.length =n): 1. Directly (without bounds). minIndex : n-from+2 swap : 3 selectionSort: ๐2 2 + 11 ๐ 2 +2 binarySearch: 5 + 2 log 2 ๐ 2. Using asymptotic notations (prove your claims). minIndex : O(n) , pick any number bigger than 1 as constant c and index bigger than 2−๐๐๐๐ ๐−1 swap : O(1) pick constant 4 or higher selectionSort: O(n^2), pick constant c bigger than 1/2 and any index bigger than 11 2 121 −4+8๐ 4 − +√ 1−2๐ 5 ๏ท ๏ท ๏ท binarySearch: O(log(n)) , pick any constant c>2 and index bigger than 2๐−2 Assume that getting the length of an array, accessing a location and mathematical operations (including a chain of operations, meaning for example (1+1)/2 ) are atomic actions. Assume worst case scenario, for example in the binary search we never find the value we search for, until the last search. Do not count loop condition checking (for, while ..) as operations. Question 2: Look at the following function: public static int sortAndBinarySearch(int[] a, int key){ int[] b = selectionSort(a); int found = binarySearch(b,key); return found; } // binarySearch Categorize the following functions into 3 groups by their relationship with sortAndBinarySearch (๐กโ๐ ๐๐๐๐ข๐๐ ๐ค๐ ๐ค๐๐๐ก ๐๐๐ Ω, ๐, Θ ) SortAndBinarySearch: O(n^2) 1. ๐ : in Ω 2. ๐3 + 2๐2 − 100001 : in O 3. log 2 (๐) : in Ω 2 4. log 2 (3๐ + 9) : in Θ ๐4 −2 5. ๐2 +300 : in Θ 6. (๐ − 1)! : in O 7. ๐๐ : in ๐ For example the running time of mergeSort is ๐๐๐๐๐ and if we want to compare it to the functions ๐๐๐๐๐ , ๐ and ๐2 then ๐ ∈ ๐(๐๐๐๐๐), ๐2 ∈ Ω(๐๐๐๐๐) and ๐๐๐๐๐ ∈ Θ(๐๐๐๐๐) ๏ท ๏ท Assume worst case scenario, for example in the binary search we never find the value we search for, until the last search. Do not count return statements or condition checking (for, while ..) as operations. Question 3: Prove / disprove: 2 1. There exist f such that ๐(๐) = ๏(log ๐) and (๐(๐)) = ๐(๐(๐)). False. 2 Let's falsely assume that there exist ๐0 , ๐ ≥ 0 such that ∀๐ > ๐0, (๐(๐)) ≤ ๐ ⋅ ๐(๐). Then ∀๐ > ๐0, ๐(๐) ≤ ๐, in contradiction to the fact that ๐(๐) = Ω(log ๐). 2. If f and g are monotonically increasing functions, such that ๐(๐(๐)) = ๐(๐) and ๐(๐) = ๏(๐), then ๐(๐) = ๐(๐). True. ๐(๐(๐)) = ๐(๐), so there exist constants ๐1 , ๐1 > 0 such that ∀๐ ≥ ๐1, ๐(๐(๐)) ≤ ๐1 ๐. ๐(๐) = ๏(๐), so there exist constants ๐2 , ๐2 > 0 such that ∀๐ ≥ ๐2 , ๐(๐) ≥ ๐2 ๐ Case 1: if ๐(๐) is bounded by ๐2 than it is trivial, since ๐(๐) = ๐(1). Case 2: there exist a constant ๐3 such that ∀๐ ≥ ๐3 , ๐(๐) ≥ ๐2 (since g is monotonically increasing). Let ๐0 = max{๐1 , ๐3 }, then ∀๐ ≥ ๐0, ๐ ๐2 ๐(๐) ≤ ๐(๐(๐)) ≤ ๐1 ๐ => ๐(๐) ≤ ๐1 ⋅ ๐, thus ๐(๐) = ๐(๐). 2 3. max(๐(๐), ๐(๐)) = Θ(๐(๐) + ๐(๐)) True. We need to show ๐1 (๐(๐) + ๐(๐)) ≤ max(๐(๐), ๐(๐)) ≤ ๐2 (๐(๐) + ๐(๐)). Selectin ๐2 = 1 shows the second inequality, since the maximum must be smaller than the sum. 1 Selectin ๐1 = 2 shows the first inequality, since the maximum is always greater than the average. Question 4: Prove or give a counter example for the following asymptotic notation properties (in all sections you may assume that ๐(๐) ≥ 1 and ๐(๐) ≥ 1 for all n): 2 a. ๐(๐) = ๐ ((๐(๐)) ) 2 We need to show ๐(๐) ≤ ๐ ⋅ (๐(๐)) . Divide both sides by ๐(๐) (it is ok since ๐(๐) ≥ 1 for all n). We get 1 ≤ ๐ ⋅ ๐(๐) 2 Choose ๐ = 1, ๐0 = 1. Then ๐(๐) ≤ ๐ ⋅ (๐(๐)) ∀ ๐ ≥ ๐0 . b. If ๐(๐) = ๐(๐) then 2 ๐(๐) = ๐(2๐ ) The claim is false. Counter example: Let ๐(๐) = 2๐, so the condition is met: ๐(๐) = ๐(๐). However, in order to prove that 2 ๐(๐) = ๐(2๐ ) we need to show that for some constant c and for all ๐ ≥ ๐0 : 22๐ ≤ ๐ ⋅ 2๐ . By dividing by 2๐ , we get: 2๐ ≤ ๐, which cannot be true for all ๐ ≥ ๐0 . Question 5: Prove the following claims: a. (๐ + 1)5 = ๐(๐5 ) We need to show that (๐ + 1)5 ≤ ๐ ⋅ ๐5 ๐๐๐ ๐ ≥ ๐0 . 1 5 divide both sides by ๐5 ⇒ (1 + ๐) ≤ ๐. Choose ๐0 = 1, ๐ = 32. b. 3๐ ⋅ log ๐ + 2๐ = ๐บ(๐ ⋅ log ๐) We need to show that ๐ ⋅ ๐ ⋅ log ๐ ≤ 3๐ ⋅ log ๐ + 2๐ ๐๐๐ ๐ ≥ ๐0 . 2 divide both sides by ๐ ⋅ log ๐ ⇒ ๐ ≤ 3 + log ๐. Choose ๐0 = 2, ๐ = 3. c. ๐ ⋅ (2๐ + 3๐+1 )2 = ๐(๐ ⋅ 9๐ ) ๐ ⋅ (2๐ + 3๐+1 )2 = ๐ ⋅ ((2๐ )2 + 2 ⋅ 2๐ ⋅ 3๐+1 + (3๐+1 )2 ) = ๐ ⋅ (22๐ + 2 ⋅ 2๐ ⋅ 3 ⋅ 3๐ + 32๐+2 ) = ๐ ⋅ (4๐ + 6 ⋅ 6๐ + 9 ⋅ 9๐ ) = ๐(๐ ⋅ 9๐ ) d. log 3 2๐ = ๐(๐) log 3 2๐ ≤ log 2 2๐ = ๐ ⋅ log 2 2 = ๐ ≤ ๐ ⋅ ๐. This holds for c=1 and any n. Question 6: Solve the following recurrence. Assume that ๐(1) = ๐(2) = ๏(1). ๐ 2 a. ๐(๐) = 2๐ ( ) + ๐3 Use the Master theorem ๐log๐ ๐ = ๐log2 2 = ๐ ๐(๐) = ๐3 = Ω(๐1+๐ ), ๐๐๐ก๐ ๐โ๐๐๐ ๐ ๐ = 1 ๐ ๐ 3 2 ๐ ⋅ ๐ ( ) = 2 ⋅ ( ) < ๐ ⋅ ๐3 ⇒ ๐3 < ๐ ⋅ ๐3 ๐ 2 8 2 True for every ≤ ๐ < 1 8 Third case of the Master theorem: ๐(๐) = Θ(๐3 ) ๐ b. ๐(๐) = 2๐ (4 ) + 1 Iteration methos: ๐ ๐ ๐ ๐(๐) = 1 + 2๐ (4 ) = 1 + 2 (1 + 2๐ (16)) = 1 + 2 (1 + 2 (1 + 2 (64))) = 1 + 2 + 22 + โฏ + 2log4 ๐ = 1 ⋅ 2log4 ๐ −1 2−1 = 2log2 √๐ − 1 = √๐ − 1 = Θ(√๐) 1 Master theorem: Since 1 is ๐(๐log4 2 ) then ๐ = Θ(๐log4 2 ) = Θ (๐2 ) c. ๐(๐) = ๐(๐ − 1) + ๐ Use Iteration method ๐(๐) = ๐(๐ − 1) + ๐ = = [๐(๐ − 2) + (๐ − 1)] + ๐ = ๐(๐ − 2) + ๐ + (๐ − 1) = = [๐(๐ − 3) + (๐ − 2)] + ๐ + (๐ − 1) = ๐(๐ − 3) + ๐ + (๐ − 1) + (๐ − 2) = … = ๐(๐ − ๐) + ๐ + (๐ − 1) + (๐ − 2) + โฏ + (๐ − ๐ + 1) After ๐ = ๐ − 1 iterations we have reached T(1). ๐ ⋅ (๐ + 1) ๐(๐) = ๐(1) + 2 + โฏ + ๐ = Θ(1) + − 1 = Θ(๐2 ) 2 1 d. ๐(๐) = ๐ (๐2 ) + 1 Use iteration method: 1 ๐(๐) = ๐ (๐2 ) + 1 = 1 1 1 1 = [๐ (๐4 ) + 1] + 1 = ๐ (๐2 ) + 2 = [๐ (๐8 ) + 1] + 2 = ๐ (๐8 ) + 3 … 1 1 = [๐ (๐2๐ ) + 1] + ๐ − 1 = ๐ (๐2๐ ) + ๐ After *๐ = log log ๐ iterations we have reached T(1). 1 ๐(๐) = ๐ (๐2log log ๐ ) + log log ๐ = ๐(2) + log log ๐ = Θ(1) + log log ๐ = ๐(log log ๐) ๐ ๐ * log log ๐ = ๐ ⇔ 2log log ๐ = 2๐ ⇔ log ๐ = 2๐ ⇔ 2log ๐ = 22 ⇔ ๐ = 22 ⇔ 1 ๐ ๐2๐ = 2√๐ = 2 e. ๐(๐) = 2๐(๐ − 1) + ๐(1) Use the iteration method: ๐(๐) = 2๐(๐ − 1) + ๐(1) = = 2[2๐(๐ − 2) + ๐(1)] + ๐(1) = 4๐(๐ − 2) + 2 ⋅ ๐(1) + ๐(1) = = 2[4๐(๐ − 3) + ๐(1)] + 2 ⋅ ๐(1) + ๐(1) = 8๐(๐ − 3) + 4 ⋅ ๐(1) + 2 ⋅ ๐(1) + ๐(1) = … = 2๐ ๐(๐ − ๐) + ๐(1) ⋅ [1 + 2 + 4 + โฏ + 2๐−1 ] After ๐ = ๐ − 1 iterations we have reached T(1). ๐(๐) = 2๐−1 ⋅ ๐(1) + ๐(1) ⋅ (2๐−1 − 1) = Θ(2๐ ) Question 7: ๐ Given ๐(๐) = 16๐ (4 ) + ๐(๐), find a function ๐(๐) (if exists) such that the solution of T(n) is equal to the written in the following sections. Prove your answer for each section. Following the Master theorem, ๐log๐ ๐ = ๐2 . for ๐(๐) = Θ(1) the solution for ๐(๐) will be Θ(๐2 ), so a solution for ๐(๐) will be at least Θ(๐2 ) for all ๐(๐) = Ω(1). 1) ๏(๐ log ๐) No solution. (Note that the fact that there's no solution doesn't derive from the fact that we don't get a solution from the master theorem – we know that there are cases for which the master theorem doesn't hold – but because for ๐(๐) of the lowest possible order, the solution is Θ(๐2 ). 2) ๏(๐2 ) 3 ๐2 ๐log๐ ๐ = ๐log4 16 = ๐2 First case of the master theorem: ๐(๐) = ๐(๐log๐ ๐−๐ ) = ๐(๐2−๐ ). 3 ๐(๐) = ๐2 3) ๏(๐2 ⋅ log ๐) ๐2 Second case of the master theorem: ๐(๐) = ๐(๐log๐ ๐ ) = ๐(๐2 ). ๐(๐) = ๐2 4) ๏(๐4 ) ๐4 Third case of the master theorem: ๐(๐) = ๐(๐log๐ ๐+๐ ) = ๐(๐2+๐ ). ๐ ๐ ๐ 4 ๐4 1 ๐ ⋅ ๐ ( ) = 16 ⋅ ๐ ( ) = 16 ⋅ ( ) = 16 ⋅ 2 ≤ ๐ ⋅ ๐4 ⇒ ๐โ๐๐๐ ๐ ๐ ๐ . ๐ก ≤๐<1 ๐ 4 4 16 16 ๐(๐) = ๐4