nitiger
nitiger

Reputation: 359

Big-O Notation of multiple statements

Suppose there are multiple functions with certain big o notations, anything O(N), O(N^2), etc. If you have a code fragment such as.

 f1(x);
 f2(x);
 f3(x);

Are all the big O notations added together or multiplied? Any explanation as to why either would be correct - addition or multiplication?

Upvotes: 1

Views: 280

Answers (2)

aschepler
aschepler

Reputation: 72311

Neither. You would take the maximum.

Calling the larger piece of code g... If for example O(f2) >= O(f1) and O(f2) >= O(f3), then the complexity of g is <= 3 * O(f2) = O(f2).

Upvotes: 3

nneonneo
nneonneo

Reputation: 179422

Assuming they don't modify x, then the O notations are added together, since the time taken to do all three is the sum of the time taken to do each individually.

Upvotes: 0

Related Questions