for i <- 1 to n
for j <- i to n
for k <- i to j
What is the mathematical expression for the run time of the second loop?
Is it the Sum of j = 1 to n of n = n^2?
How do you derive it since its dependent on both i and n?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s approach the problem in two ways:
1. solution
If you need to compute the second loop run time function, you will have to express it as a function of both
nandi. So, you will fixi, because it enters the second loop as a constant, and you don’t change its value anywhere in the 2nd or 3rd loop. You can write your algorithm in an equivalent form, if you want to see why the second loop running time depends on bothiandn:When
j=i,doStuff()is executed only once, becausej=iandk=j=i, so the loop stops after just 1 iteration.When
j=i+1,doStuff()is executed 2 times, etc.You can derive a rule, that for any value of
j,doStuff()is executed(j-i+1)times, i.e. 1 time whenj=i, 2 times whenj=i+1, 3 times whenj=i+3,…, n-i+1 times when j=i. That means that the running time function is:Later, when you try to derive the complexity function of the entire algorithm, you will see that for each i from 1 to n, you have f(n,i)
doStuff(), so the whole algorithm will have the running time:You will have to do some maths here and use formulas for progression sums. However, there is another approach, far more elegant, but maybe tricky to understand.
2. solution
It is evident that for every j and k in your algorithm:
i <= j,k <= nk >= jSo, what your code does is actually choosing two positive integers j and k from [i,n] where j<=k. Set containing those pairs j,k is actually a combination with repetition where we choose two elements from n-i+1 elements with repetition. Don’t get confused because in some choices the first number won’t be less than the second one. You can always switch them, i.e. rewrite them in the non descending order, and then consider the first to be your j, and the second one to be your k. Those two numbers can be selected in (n-i+1)(n-i+2)/2 ways, and that is the running time function for your algorithm. With more thinking, and less error prone math calculations you can get your solution in a far more elegant way.
The same logic applies if you try to determine the running time function of the entire algorithm.
Useful links with math formulas
http://en.wikipedia.org/wiki/Combinations