•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Computer Science and Software Design section within the Software Development category of DaniWeb, a massive community of 427,196 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,159 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Computer Science and Software Design advertiser: Programming Forums
Views: 64435 | Replies: 56
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
hi, im trying to do the dominant behaviour, which will compute the total run time, but im not sure if im on the right track. heres what i have:
Function mystery(n)
r = 0
for i = 1 to n -1 do
for j = i + 1 to n do
for k = 1 to j do
r = r + 1
return r
END
first thought
the function sets the value of r to 1 (one operation). then it tests the second loop to loop 1 to n- 1 times...(it runs the body of the for loop exactly n - 1 times. the body performs two other for loops; ' j, which is i + 1 to n ' and ' k which runs 1 to j times.'
other thought
the outer most loop executes n-1 times (worst case?). the next innermost one on the order exectures i + n-1 times (loops based on first for loop?).
the addition is based on two for loops....
any suggestions, id appreciate it...
Function mystery(n)
r = 0
for i = 1 to n -1 do
for j = i + 1 to n do
for k = 1 to j do
r = r + 1
return r
END
first thought
the function sets the value of r to 1 (one operation). then it tests the second loop to loop 1 to n- 1 times...(it runs the body of the for loop exactly n - 1 times. the body performs two other for loops; ' j, which is i + 1 to n ' and ' k which runs 1 to j times.'
other thought
the outer most loop executes n-1 times (worst case?). the next innermost one on the order exectures i + n-1 times (loops based on first for loop?).
the addition is based on two for loops....
any suggestions, id appreciate it...
Last edited by CaribGirlC : Oct 15th, 2007 at 3:08 pm.
•
•
Join Date: Oct 2007
Location: Cambridge, MA
Posts: 269
Reputation:
Rep Power: 2
Solved Threads: 22
For any function?
Here's a tip: For any function f, f(n) = O(f(n)). It's true! He he he.
Now, given T(1) = 1 and T(n) = 2T(n/4) + 1, you can find the order of growth using the Master theorem.
One way, though, to think about the above problem intuitively, is to ask what the statement "T(n) = 2T(n/4)+1" really means. Another way of writing it is to say, "T(4n) = 2T(n) + 1". That is, every time you multiply n by four, the value of T(n) multiplies by 2. (For large values of n, adding 1 has a negligible effect.)
What function has that property, where multiplying the input by four results in the output being multiplied by 2? We know the function is sublinear, because if it were linear, we would find that T(4n) is approximately 4T(n), for large n. But it's only 2T(n)
We know it's bigger than logarithmic, because we know that for logarithmic functions, multiplying a constant by the input results in a mere constant being added to the output.
So, what function has the property that T(4n) is 2T(n)? Make some guesses. And this will be the function that conveniently represents your order of growth.
Here's a tip: For any function f, f(n) = O(f(n)). It's true! He he he.
Now, given T(1) = 1 and T(n) = 2T(n/4) + 1, you can find the order of growth using the Master theorem.
One way, though, to think about the above problem intuitively, is to ask what the statement "T(n) = 2T(n/4)+1" really means. Another way of writing it is to say, "T(4n) = 2T(n) + 1". That is, every time you multiply n by four, the value of T(n) multiplies by 2. (For large values of n, adding 1 has a negligible effect.)
What function has that property, where multiplying the input by four results in the output being multiplied by 2? We know the function is sublinear, because if it were linear, we would find that T(4n) is approximately 4T(n), for large n. But it's only 2T(n)
We know it's bigger than logarithmic, because we know that for logarithmic functions, multiplying a constant by the input results in a mere constant being added to the output.
So, what function has the property that T(4n) is 2T(n)? Make some guesses. And this will be the function that conveniently represents your order of growth.
Last edited by sarehu : Mar 11th, 2008 at 12:11 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Computer Science and Software Design Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
ajax asp blog browsing business software computer dell design developer development erp systems firefox india intel internet it linux media microsoft mmorpg msdn networking news office online open open-source operating programming project management publishing rss science security software software selection source sql sun super system technology evaluation tips vista warez web wiki windows xp
- calculate time complexity of the algorithm (Computer Science and Software Design)
- Help On Time Complexity Algorithm (Computer Science and Software Design)
Other Threads in the Computer Science and Software Design Forum
- Previous Thread: MATLAB gaussain elimination
- Next Thread: Computer Based Leaning


Linear Mode