0

So I have an algorithm that goes like this:

for i=0:360

 C...

 for j=0:a_j[i]

   C...

   for t=0:a_t[i][j]

     C...
   end
 end
end

So I have three loops but both inner loops depend on the value of the outer loops. How can I measure its Big O notation complexity?

Also what if I have memory asignments between these loops? Are they counted as Cs?

8
  • 4
    It's impossible to say, because we don't know what a_j and a_t are. Commented Jun 6, 2013 at 17:13
  • If this is basically for i = 0 to 360; for j = 0 to i; for t = 0 to j;, then complexity is O(n ^ 3). Commented Jun 6, 2013 at 17:15
  • @OliCharlesworth Well... I guess they're arrays. Commented Jun 6, 2013 at 17:16
  • @H2CO3 Where did you find the n? Commented Jun 6, 2013 at 17:16
  • 1
    @H2CO3: Indeed. But what's inside? Commented Jun 6, 2013 at 17:17

2 Answers 2

3

You are not saying anything about the rest of the code, so assuming the rest is primitive operations of constant complexity.

If the entries of the two arrays are less than a constant value, then the complexity is O(1)-- the processing does not depend on any value input from outside your code. If they are functions of an input variable, then the complexity depends on the functions they are processed into those arrays and in this case you have to be more specific.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I get it. The arrays do depend on an input variable. There are many operations done inside the innermost array. For example a sum of all the values of t into a result[i][j] and several others like that.
You have to be specific what those operns are. Depends still.
1

I'm not sure what a_j and a_t are exactly. If a_j and a_t are constant, then the complexity are O(1). If a_j and a_t are the n, which may means input variables, then we have to compute the complexity.

In a word, the complexity of this program is decided by how you define a_j and a_t.

Anyway, I can compute how many loops that your program executes.

Here is the code written in python:

ret = 0
for i, v in a_j:
    ret += v * sum(a_t[i])
if not ret: ret = 1
ret *= 360

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.