Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The complextiy of Python issubset()

Given two sets A and B and their length: a=len(A) and b=len(B) where a>=b. What is the complextiy of Python 2.7's issubset() function, ie, B.issubset(A)? There are two conflicting answers I can find from the Internet:

1, O(a) or O(b)

found from:https://wiki.python.org/moin/TimeComplexity and bit.ly/1AWB1QU

(Sorry that I can not post more http links so I have to use shorten url instead.)

I downloaded the source code from Python offical website and found that:

def issubset(self, other):
    """Report whether another set contains this set."""
    self._binary_sanity_check(other)
    if len(self) > len(other):  # Fast check for obvious cases
        return False
    for elt in ifilterfalse(other._data.__contains__, self):
        return False
    return True

there is only loop here.

2, O(a*b)

found from: bit.ly/1Ac7geK

I also found some codes look like source codes of Python from: bit.ly/1CO9HXa as following:

def issubset(self, other):
    for e in self.dict.keys():
        if e not in other:
            return False
        return True

there are two loop here.

So which one is right? Could someone give me a detailed answer about the difference between the above two explanations? Great thanks in advance.

like image 911
Jasonhz Avatar asked Dec 28 '14 06:12

Jasonhz


People also ask

What is Issubset in Python?

The issubset() method returns True if all items in the set exists in the specified set, otherwise it retuns False.

What is the time complexity of set () in Python?

Time Complexity of this is O(len(s1) + len(s2)) where s1 and s2 are two sets whose union needs to be done. Intersection:- This can be done through intersection() or & operator. Common Elements are selected. They are similar to iteration over the Hash lists and combining the same values on both the Table.

What is Issubset and Issuperset in Python?

Introduction to Python issuperset method Suppose that you have two sets: A and B. Set A is a superset of set B if all elements of set B are elements of set A. If set A is a superset of set B, then set B is a subset of set A. To check if a set is a subset of another, you use the issubset() method.

Is subset function in set in Python?

Python set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present in Python.


1 Answers

The complexity of B.issubset(A) is O(len(B)), assuming that e in A is constant-time.

This a reasonable assumption generally, but can be easily violated with a bad hash function. If, for example, all elements of A had the same hash code, the time complexity of B.issubset(A) would deteriorate to O(len(B) * len(A)).

In your second code snippet, the complexity is the same as above. If you look closely, there is only one loop; the other is an if statement (if e not in other:).

like image 68
NPE Avatar answered Oct 03 '22 00:10

NPE