Question 1: Given the Selection Sort and Binary search algorithms

advertisement
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
Download