The python wiki on time complexity lists a single intersection as O(min(len(s), len(t)) where s and t are sets with the sizes len(s) and len(t), respectively. (In English: the time is bounded by and linear in the size of the smaller set.)
Note: based on the comments below, this wiki entry had been be wrong if the argument passed is not a set. I've corrected the wiki entry.
If you have n sets (sets, not iterables), you'll do n-1 intersections and the time can be (n-1)O(len(s)) where s is the set with the smallest size.
Note that as you do an intersection the result may get smaller, so although O is the worst case, in practice, the time will be better than this.
However, looking at the specific code this idea of taking the min() only applies to a single pair of sets and doesn't extend to multiple sets. So in this case, we have to be pessimistic and take s as the set with the largest size.
Answer from rocky on Stack OverflowTime complexity of python set operations?
python - Intersection complexity - Stack Overflow
algorithms - Time complexity of set intersection - Computer Science Stack Exchange
What's the fastest algorithm and data structure for set operations like intersection?
What is the Time Complexity of Intersection Operations?
What are the Limitations of Set Intersection?
Is Set Intersection Commutative?
Videos
Like when getting common elements from two sets in python. What's the complexity in that case and how does it work? (Edited: so far found)
The data structure behind the set is a hash table where the typical performance is an amortized O(1) lookup and insertion.
The intersection algorithm loops exactly min(len(s1), len(s2)) times. It performs one lookup per loop and if there is a match performs an insertion. 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
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:
Average: O(min(len(s), len(t))
Worst case: O(len(s) * len(t))
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.
Your post contains two problems. I will only address the first.
There is a simple $O(n)$ algorithm for computing $A \cap B$, which involves an array of length $n$.
Another algorithm sorts $A \cup B$ to find the intersection in time $O(n\log n)$. In contrast to the previous algorithm, this algorithm can be implemented using only comparisons. There is a matching $\Omega(n\log n)$ lower bound in the algebraic decision tree model; see for example lecture notes of Otfried Cheong.
You canโt give the time complexity because a set is not a primitive data structure, so you need to know how it is represented.
Why would n be part of the input size? Let A = { 1,000,000,000 } and B = { 1, 1,000,000,000 }, for example.