1

I'm learning how to find algorithm complexity and I can't figure out what's the complexity of this algorithm. Could someone explain to me how to get the answer?

void algorithm(int a, int b) {
    while (a >= b) {
        int x = a - b;
        for (int i = 0; i <= x; i++) {
            std::cout << "complexity of this algorithm?";
        }
        a = x;
    }
}

Please any input is welcome. This is what I have so far:

This is what I have so far

2
  • What scenario are you considering in your question? best time? average? worst? Commented Oct 14, 2020 at 19:54
  • best & worst, if there is a best...I,m not sure if the best case would be different from the worst case. Commented Oct 14, 2020 at 19:56

2 Answers 2

2

As a is modified, you should have it in parameters of the sum:

(1) x = a - b  // first iteration
(2) x = a - 2b // second iteration
(3) x = a - 3b
...

if a = k * b, the outer loop iterates k times. Hence, the final complexity is:

(a - b) + (a - 2b) + ... + (a - (k-1) b) = 
(k-1) b + (k-2) b + ... + b = k * (k-1) * b/2

As you have mentioned , time complexity is .

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

1 Comment

@UweJ, you realise that that's effectively the same complexity as given here? only OmG has been a little more precise. (in complexity we often don't care about ceilings/floors, etc, but in this case if a and b grow together then then ceiling remains relevant even in complexity theory).
0

The complexity is (a^2/b)

As I described in the image, you need to sum up all "x", then you will get the complexity.

in summation part for (-b -2b -3b - ... -nb) you can write :
[![enter image description here][1]][1]-b (1+2+...)
so this is -b*(n(n+1)/2)

summation_part_definition_link

enter image description here

So in the end, if "a" and "b" were for the same order then the result is :

O(c) = 0 (c is numeric)

this means complexity is in numeric order. but if "a" was for upper order then the result is :

O((a^2)/b)

4 Comments

@ItsNotMyFault don't you agree with this solution?
I don't understand most of the simplification of your summation and since I don't know the answer I can't really agree with anything. Which Summation rule do you use here ? ( I don't understand your reprensation of the summation) Is the Xi some sort of substitution?
@ItsNotMyFault check this link and my edits again. link
When I simplify the sommation (same sommation you have, but I replace your Xi with : (a - bi + 1) => which is the result of my inner sommation. My inner sommation is (because of OmG, he said that since a changes it should be in the parameters of the sum) : sum_j=0^(a - jb) 1 and then I get (a^2)/b + a/2 + a/b. But I agree with your complexity result because of the maximum rule.

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.