31

I'm working through some practice problems where I'm given a target time complexity and space complexity. One of them gives a target time complexity of O(N+M). I'm having some trouble with the intuition of what an O(N+M) algorithm would look like. Does anyone have an example of an algorithm like this or can explain it clearly? Every example I try to think of seems like O(N*M) to me.

user137717
  • 2,005
  • 4
  • 25
  • 48
  • 1
    Can you post an example of a case with O(N+M)? – Javi Sep 11 '14 at 20:19
  • 1
    Yes, so we can figure out what N and M are in an specific case. All I can think about right now is something like: find the minimum of 2 vector, once of size N and the other one of size M, so that it would be O(M+N) – Javi Sep 11 '14 at 20:21
  • 1
    I'd like to avoid it if I can. The answer below is a good example and I don't want to post the question for fear of someone giving too much of the answer away. Thanks for your help! – user137717 Sep 11 '14 at 20:31
  • 1
    In case you want the formal definition: http://en.wikipedia.org/wiki/Big_O_notation#Multiple_variables – Tavian Barnes Sep 11 '14 at 20:50

6 Answers6

40

A simple example of algorithm that is O(m+n):

int sum(int[] nArr, int[] mArr) {
    int sum = 0;
    for(int i : nArr) {
        sum += i;
    }
    for(int i : mArr) {
        sum += i;
    }
    return sum;
}

To compute the sum, you need to go through all elements in nArr (size n) and all elements in mArr (size m), so the overall complexity is O(m+n)

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • 1
    Complexity-wise, that is impossible to do better :) – Jean Logeart Sep 11 '14 at 20:27
  • 2
    Let's say array nArr with size N is larger. Would it still be correct to say that this algorithm is O(N)? – user137717 Sep 11 '14 at 20:30
  • 5
    No this is not correct, unless ``m`` is constant or unless ``m`` depends on ``n`` such that ``m = O(n)`` – Jean Logeart Sep 11 '14 at 20:31
  • 2
    Is it correct to think about it as an O(n) algorithm followed by an O(m) algorithm and together they are O(n+m) and abstract this to an arbitrary number of arrays? – user137717 Sep 11 '14 at 20:41
  • 12
    Complexity annotation (such as `O`) describes the way an algorithm's performance (e.g. time or space) is dependent on the size of the input. It is "blind" to a specific input. When you write `O(m+n)` it means that the algorithm will take `O(m)` time when m>n and `O(n)` when m – Shaked Sep 11 '14 at 20:49
  • Shaked, I dont get it. Logically, it would be O(max(m,n)) if work on elements of both arrays is going at the same iteration process (loop). But if iteration of two, say, arrays going separately one after another, it becomes O(m+n), but if work on both arrays is done in the same loop, then it is O(max(m,n)). Am I right? – Sermilion Apr 25 '17 at 10:29
  • @Sermilion Yes you are. – ruohola May 18 '21 at 10:24
21

Quick and simple example of an O(n + m) algorithm:

for (i = 0; i < n; i++)
{
  // do something but don't loop or invoke recursive functions
  // only constant O(c) complexity is allowed: a simple series of commands
}

for (i = 0; i < m; i++)
{
  // idem
}

Complexity is commutative when added (O(n + m) == O(m + n)) this means you may invert the two for() without affecting complexity. Obviously, on an algorithmic level the inverted one MAY not be equivalent to the straight one.

As an additional help, here is an example of O(n * m) algorithm:

for (i = 0; i < n; i++)
{
  for (j = 0; j < m; j++)
  {
    // do something but don't loop or invoke recursive functions
    // only constant O(c) complexity is allowed: a simple series of commands
  }
}

Again, you may invert inside with outside loop without affecting complexity (O(n * m) == O(m * n)). The same obvious considerations apply.

The limitation on what you may put into the for() bodies is because the big-o notation constraints the upper bound. If it were a lower bound (little-o notation) you may have put more complex stuff in, but it could never get less than that.

pid
  • 11,472
  • 6
  • 34
  • 63
2

The intuition of this problem is that you have two unique variables n and m. Now imagine these two unique variables independently increasing, approaching infinity.

If this were an O(n) problem (i.e. BIG-O), the upward boundary of the complexity of this problem would be linear at least. You could say that O(n) = n^2. But an O(n) problem would never even get close to that n^2 limit as n (the input) approaches infinite.

Likewise, the behavior for m would be the same. O(m) can be m^2. But it is more accurate to say that O(m) = m. The complexities of these two problems are linear.


Now, if you just do O(n+m), is that really n^2? It shouldn't be. Even if n=m, the sum would be 2n or 2m. The complexity of this problem is still linear because the size of the output is still proportional to the inputs n and m. Therefore, the most precise answer to this problem would be O(n+m) = n+m.

Caleb Faruki
  • 2,577
  • 3
  • 30
  • 54
2

So, in order to expand the other replies, I will try to add a example of such problems to help you understand:

  • Find a min/max in a N sized array, and then look for this value in an M sized array. Since you need to perform first min/max search, you cannot do it at once.

For instance, summing up the elements of 2 vectors can be done in O(M+N), but it can be thought as O(N) (assuming N>M) or O(M) (if M>N).

Javi
  • 3,440
  • 5
  • 29
  • 43
2

All Above Answers illustrate how O(n+m) works but I would like to look at it from a different view by knowing what O(nm), what is the difference between O(n+m) and O(nm) the main difference is when multiplying an n number by m number it means that n will happen for m times or will try it for m times, for example, the below code is O(n*m) because n will happen m times for n times

for(int i=0; i < n;i++){
  for(int j=0; j < m;j++){
  //some_code
  }
}
Mort
  • 1,411
  • 5
  • 19
  • 51
m.mashaly
  • 81
  • 6
1

One instructive example which does something non-trivial is to take two sorted arrays of size M and N and output a new sorted array with all of these elements. This is the basis of merge-sort and will take O(M+N) comparisons.

You can find an example anywhere or do it yourself.

user1952500
  • 6,611
  • 3
  • 24
  • 37