Author | Message |
---|---|
G190852562
Posts: 162
|
Posted 00:01 Dec 07, 2014 |
1. What’s the running time of T(n) = 4 * T(n/2) + n * log n (The correct answer should be O(n^2) , sorry about previous mistake ) O(n * log n) BinarySearch(A, p, r, k) if p > r return -1 else q = (p+r) / 2 if k == A[q] return q else if k < A[q] return BinarySearch(A, p, q-1, k) else return BinarySearch(A, q+1, r, k)
What is the recurrence relation for this code? T(n) = 2 * T(n/2) + n Hint: Even if you got the recurrence relation in the previous question wrong, you should be able to figure out the answer to this question from first principles. T(n) = O(n * log n) Yes. It would be a straightforward change to the inversion-counting code. Last edited by G190852562 at
00:08 Dec 07, 2014.
|
yxu22
Posts: 25
|
Posted 10:07 Dec 07, 2014 |
1. d is not 1. d is some number greater than 1 and less than 2. If f(n) is the rest part after 4 * T(n/2), which is f(n) = n * log n in this question, then d is the largest power of n in f(n)
3. The answer is the last one. a is the number of sub problem b is how you would divide the main problem ( In binary search, we would divide the whole input into half.) , (You may have the same understanding in different words description) d is the running time we need to solve the rest part of code except the recursion.
4. Yes
|
G190852562
Posts: 162
|
Posted 14:04 Dec 07, 2014 |
1. Why is 1 the largest power of d? Would it be incorrect to say that it's 1.99? Also, Is the last term always f(n)? Say instead of n log n it is n^2 (we would then have an obvious d; d = 2). 3. The number of sub-problems is 1 because we're always working with the same array, right? |
vsluong4
Posts: 87
|
Posted 23:00 Dec 07, 2014 |
3. The number of subproblems is 1 because each time the function is called, it only calls itself again 1 other time.
Compare this to merge sort
|
G190852562
Posts: 162
|
Posted 23:12 Dec 07, 2014 |
Understood. Thank you. Do you know how to answer my 3rd question? For f(n) = n * log n, the log n part isn't taken into consideration when finding d; why is that? |
vsluong4
Posts: 87
|
Posted 23:25 Dec 07, 2014 |
We did cover logarithmic factors in the f(n) term in class. Last edited by vsluong4 at
23:32 Dec 07, 2014.
|
G190852562
Posts: 162
|
Posted 23:41 Dec 07, 2014 |
I tried reading this yesterday but it was quite difficult to understand. This is what tripped me: Since f( n ) = n * log n = Ω( n ) and the regularity condition holds, namely, a * f(n/b) = ¾ * n * log n = c * f(n) for c = ¾, we can apply case 3, and find T(n) = Θ(n * log n) First it's from obtaining Ω( n ) from n * log n. |
vsluong4
Posts: 87
|
Posted 23:51 Dec 07, 2014 |
You're wasting your efforts, it won't be on the final. Well, that's probably not a good thing to say since it's always good to learn. |