-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 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
  • 280,205
  • 27
  • 317
  • 514
-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
  • 82,470
  • 26
  • 145
  • 239