简体   繁体   中英

What is the complexity evaluation of O(m * n) + O((m + n) * log(m + n))

I have following Python code, a and b are lists(I know it isn't best way of getting intersections):

def get_intersection(a, b):
    a = set(a)
    b = set(b)
    c = sorted(list(a&b))
    return c

Let's call len(a) - m and len(b) - n, where is no additional information about a and b. Then time complexity of given code is O(m) + O(n) + O(m * n) + O((m + n) * log(m + n)).

I definitely can shorten O(m) and O(n), because they are much less than O(m * n), but what should I do with O((m + n) * log(m + n))?

How do i compare O(m * n) and O((m + n) * log(m + n))? Should I keep O((m + n) * log(m + n)) in final evaluation?

You can treat the total input size as n ; it doesn't really matter which argument contributes what to that total. (The two extremes are when one or the other argument is empty; moving items from one argument to the other doesn't change the overall amount of work you'll be doing.)

As such, both set(a) and set(b) are O(n) operations.

a & b is also O(n); you don't need to compare every element of a to every element of b to compute the intersection, because sets are hash-based. You basically just make O(n) constant-time lookups. (I am ignoring the horrific corner case that makes set lookup linear. If you data has a hash function that doesn't map every item to the same value, you won't hit the worst case.)

sorted(a&b) (no need to create a list first, but that's also just an O(n) operation) takes O(n lg n).

Because each of the preceding operations is performed in sequence, the total complexity of get_intersection is O(n lg n).

I don't think that you can simplify the expression.

Indeed, if you set m to a constant value, say 5 , you have a complexity

5n + (n+5)log(n+5) = O(n log(n))

and the first term is absorbed.

But if you set m = n ,

n² + 2n log(2n) = O(n²)

and this time the second term is absorbed. Hence all you can write is

O(mn + (m+n) log(m+n)).

With a change of variable such that s is the sum and p the product,

O(p + s log(s)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM