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?
The intersection algorithm always runs at O(min(len(s1), len(s2))).
In pure Python, it looks like this:
[Answer to the question in the additional edit] The data structure behind sets is a hash table.
Set intersection of two sets of sizes
m,n
can be achieved withO(max{m,n} * log(min{m,n}))
in the following way: Assumem << n
The loop in step 3 will run for
n/m
iterations and each iteration will takeO(m*logm)
, so you will have time complexity ofO(nlogm)
for m << n.I think that's the best lower bound that exists
The answer appears to be a search engine query away. You can also use this direct link to the Time Complexity page at python.org. Quick summary:
EDIT: As Raymond points out below, the "worst case" scenario isn't likely to occur. I included it originally to be thorough, and I'm leaving it to provide context for the discussion below, but I think Raymond's right.