-1

I'm revising for my finals and from looking at previous exam papers I have a found a popular question that has came up almost every year, worth a lot of marks. Each year the question is written very similar with minor changes. I've not really been good at analysing algorithms this semester, so I was hoping based on the questions you guys could provide some resources links to specific things that'll help me learn to understand these type of questions. It'd help me a ton come the exam.Thanks.

I'M NOT EXPECTING ANY OF YOU GUYS TO WORK THESE OUT, JUST POINT ME IN A DIRECTION OF RESOURCES THAT'LL HELP ME OUT. As I'm really struggling with understanding, i have been through my notes provided by college but it hasn't really helped.

enter image description here


enter image description here

user26090
  • 27
  • 3
  • 2
    We expect you to put a bit more work in to your question rather than just dumping the problem on here. What approaches have you tried, where did you get stuck? You request resources "to help you out". Why is the course material not sufficient? What parts doesn't it cover, what do you need extra information on? – Tom van der Zanden Dec 22 '14 at 20:33

2 Answers2

0

I'm not sure I can provide any links, but here is the gist of it. The number of iterations in the inner loop in the first version is $$ \sum_{i=1}^Z \sum_{j=n^2-X_i}^{n^2+Y_i^2} 1 = \sum_{i=1}^Z (Y_i^2 + X_i + 1). $$ This is going to be smallest when $Z,X_i,Y_i^2$ are smallest, and largest when they are largest. To see just how small or how large, you need to perform some calculation. The smallest sum, for example, is attained for $Z = 1$, $X(1) = 0$ and $Y(1) = 1$, in which case it is $2$. The question actually asks you to count basic operations, and that depends on your set of basic operations; but it can only affect the result up to a multiplicative constant.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
  • Thanks for your reply. I can follow your first two summations ( Sorry I'm not very Mathematical ) for the third one after the 1 = .. what's the reason for the "1 =" and i understand (yi^2..though from here i don't see why you have xi + 1 since in the forloop it's n*n. Sorry if what I'm saying is idiotic :L – user26090 Dec 22 '14 at 21:16
  • The $1$ just signifies that each iteration of the inner loops is one iteration of the inner loop. If $j$ goes from $A$ to $B$ that it goes through $B-A+1$ values, which is how I get to the second sum. – Yuval Filmus Dec 22 '14 at 21:33
-1

First of all think about the operations perform in each loop. For example, let's look at the first problem this way. Assume Array.length is 5, so $1\le i,z\le 5$ (taking the array to start at $1$).

X(i) =1^2, 2^2, ....5^2 ={1,4,9,16,25}, 
 Y(i) = 1^4, 2^4,....5^4 ={1,16,81,256,625}

for int i = 1 to Array.length
   for int j = n^2-X(i) to Y(i)^2 + n^2
            k = 0;

when z = 5, X(5) = 25, Y(5)= 625, so the inner loop looks like this

        for int j = 0 to 625^2 + 25; // 25-25 to 625*625 + 25
                  k = 0;

you can see that the inner loop dominates, n^4 + n^2, so the two loops combined look like n(2(n^4 + n^2)) or 2(n^5 + n^3). Notice that K = 0 is evaluated n^4+n^2 times leading to the 2(n^4+n^2). Therefore the worse case time complexity is 2(n^5 + n^3) or just O(n^5), always consider the dominant term in the polynomial. I recommend you always make up some values, plug them into the loops variables and see what shows up. I hope this helps.

Here is link to some examples you can also look at. http://www.cs.sfu.ca/CourseCentral/225/johnwill/cmpt225_4onotation.pdf. Good luck with your finals.

David Richerby
  • 81,689
  • 26
  • 141
  • 235
  • I find it very hard to tell what you're doing, here. I started editing to improve the writing but I found I couldn't understand it well enough to edit. What do you mean by "X(i) =1^2, 2^2, ....5^2 ={1,4,9,16,25}"? In what sense is that variable equal to that vector equal to that set? Equals means "is exactly the same as"; using it as something to write between thoughts, almost like a comma, results in things that are impossible to understand. – David Richerby Dec 22 '14 at 22:28
  • Hi David, sorry about my mistake. After looking at the array bounds, X(i) assumes values in 1,2,3,....25, in my example, not 1^2,2^2...5^2. So Y(i) would also assume values in 1,2,3,....5^4, again assuming the array contains 5 elements. Then the worse case is when i=z=5 and X(i) =25, the largest value in the range of 1...25. Then the inner loop is guaranteed to start from 0 to Y(i)^2 + n^2. If Y(i) happens to be 625, the largest value in 1...625, then the inner loop still remains the same as above and the time complexity is also the same. I hope this makes more sense. – user3422517 Dec 23 '14 at 03:46