Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersection complexity

In Python you can get the intersection of two sets doing:

>>> s1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> s2 = {0, 3, 5, 6, 10}
>>> s1 & s2
set([3, 5, 6])
>>> s1.intersection(s2)
set([3, 5, 6])

Anybody knows the complexity of this intersection (&) algorithm?

EDIT: In addition, does anyone know what is the data structure behind a Python set?

like image 906
juliomalegria Avatar asked Nov 12 '11 04:11

juliomalegria


People also ask

What is the complexity of intersection?

The time complexity of set intersection in Python on a set with n elements and a set argument with m elements is O(min(n, m)) because you need to check for the smaller set whether each of its elements is a member of the larger set.

How does Python intersection work?

Python Set intersection() Method The intersection() method returns a set that contains the similarity between two or more sets. Meaning: The returned set contains only items that exist in both sets, or in all sets if the comparison is done with more than two sets.


1 Answers

The intersection algorithm always runs at O(min(len(s1), len(s2))).

In pure Python, it looks like this:

    def intersection(self, other):
        if len(self) <= len(other):
            little, big = self, other
        else:
            little, big = other, self
        result = set()
        for elem in little:
            if elem in big:
                result.add(elem)
        return result

[Answer to the question in the additional edit] The data structure behind sets is a hash table.

like image 138
Raymond Hettinger Avatar answered Oct 19 '22 18:10

Raymond Hettinger