Please support our Computer Science advertiser: Programming Forums
![]() |
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Hi all!
I'm having some trouble computing the time complexity of the following code:
(A and B are doubles greater than 1)
He's what I got to so far:
function "do()" calls itself recursively until:
^k}\leq1\\n\cdot\left(\frac{A}{A+B}\right)^k\leq1\\k\leq\log_{\frac{A}{A+B}}{\frac{1}{n}})
So far the time complexity is
(since each recursive call the do() function calls the print() function who's complexity is
). But then the function starts calling itself from the second recursive call. I'm not sure how to continue from here.
Please help!
I'm having some trouble computing the time complexity of the following code:
(A and B are doubles greater than 1)
c Syntax (Toggle Plain Text)
void do(int n) { if (n<=1) return; print(n); do(n*A/(A+B)); do(n*B/(A+B)); } void print(int n) { int i; for (i=0;i<ni++) printf("-"); }
He's what I got to so far:
function "do()" calls itself recursively until:
So far the time complexity is
Please help!
•
•
Join Date: Aug 2007
Location: Adelaide, South Australia
Posts: 451
Reputation:
Rep Power: 3
Solved Threads: 57
The print function is O(n), so you were right the first time. Although n changes each call to print, it is still O(n) since each time print is called it takes roughly n steps to compute.
I'm not completely sure, but I think the second recursive call will also be O( n.log(1/n) ), since it basically does the same thing as the first recursive call. (That's assuming your logic was correct - I haven't checked it). So your overall algorithm is:
O( n.log(1/n) + n.log(1/n) )
= O( 2n.log(1/n) )
This is equivalent to O( n.log(1/n) ) since the constant '2' has little bearing over the time complexity as n -> infinity.
I hope that this helps, let me know if you need more explanation
I'm not completely sure, but I think the second recursive call will also be O( n.log(1/n) ), since it basically does the same thing as the first recursive call. (That's assuming your logic was correct - I haven't checked it). So your overall algorithm is:
O( n.log(1/n) + n.log(1/n) )
= O( 2n.log(1/n) )
This is equivalent to O( n.log(1/n) ) since the constant '2' has little bearing over the time complexity as n -> infinity.
I hope that this helps, let me know if you need more explanation
The answer is 42.
•
•
Join Date: Oct 2007
Location: Cambridge, MA
Posts: 269
Reputation:
Rep Power: 2
Solved Threads: 22
A/(A+B) is less than 1, so O(log base A/(A+B) of (1/n)) is not equivalent to O(log (1/n)). Rather, it's equivalent to O(- log (1/n)), since log(A/(A+B)) < 0. Then, since log (1/n) = - log (n), we have O(- log (1/n)) = O(log n). Then the whole thing would be O(n log n).
The reasoning here was kind of vague and lucky.
Let's define T to be the number of steps of our function. Then
T(n) = n + T(n*(A/(A+B))) + T(n*(B/(A+B))
Let u=A/(A+B), v=(B/(A+B)). Then
T(n) = n + T(un) + T(vn).
You could then use the Akra-Bazzi method to show it's O(n log n), noting that u+v=1. http://en.wikipedia.org/wiki/Akra-Bazzi_method
The reasoning here was kind of vague and lucky.
Let's define T to be the number of steps of our function. Then
T(n) = n + T(n*(A/(A+B))) + T(n*(B/(A+B))
Let u=A/(A+B), v=(B/(A+B)). Then
T(n) = n + T(un) + T(vn).
You could then use the Akra-Bazzi method to show it's O(n log n), noting that u+v=1. http://en.wikipedia.org/wiki/Akra-Bazzi_method
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
A/(A+B) is less than 1, so O(log base A/(A+B) of (1/n)) is not equivalent to O(log (1/n)). Rather, it's equivalent to O(- log (1/n)), since log(A/(A+B)) < 0. Then, since log (1/n) = - log (n), we have O(- log (1/n)) = O(log n). Then the whole thing would be O(n log n).
The reasoning here was kind of vague and lucky.
Let's define T to be the number of steps of our function. Then
T(n) = n + T(n*(A/(A+B))) + T(n*(B/(A+B))
Let u=A/(A+B), v=(B/(A+B)). Then
T(n) = n + T(un) + T(vn).
You could then use the Akra-Bazzi method to show it's O(n log n), noting that u+v=1. http://en.wikipedia.org/wiki/Akra-Bazzi_method
Thanks a lot for the help!
That clarified things out for me.
![]() |
Similar Threads
Other Threads in the Computer Science Forum
- Multiprocessor Architecture and Algorithms (Computer Science)
- Variable scope problem (C++)
- Question: Linear Time Sorting Problem (Computer Science)
- Pipelining (Assembly)
Other Threads in the Computer Science Forum
- Previous Thread: HELP with MIPS
- Next Thread: software design
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode