简体   繁体   中英

Subset algorithm between two lists — why is the worst case when len(L1) = len(L2)?

I have an algorithm that determines whether a list L1 is a subset of a list L2 (ie whether all elements in L1 are in L2):

def isSubset(L1, L2):
    for e1 in L1:
        matched = False
        for e2 in L2:
            if e1 == e2:
                matched = True
                break
        if not matched:
            return False
    return True

In the notes of my course, it says that "the worst case occurs when len(L1) = len(L2)", but why is this?

My reasoning is: for a given L1 and L2, the worst case occurs when L1 is indeed a subset of L2 (in which case, for every L1, we have to go through every element in L2 to verify this).

If this is the case (ie L1 is a subset of L2), then it's going to take longer to verify that L1 is a member of, say, [1,7,2,3,5,8,9,20], than it is to verify that it's a member of, say, [2,5,3].

What is wrong with my reasoning?

Basically, when L1 is equal to L2 , all the elements of L1 have to be checked against L2 .

On the other hand, if L1 is not equal to L2 , either it is a subset of it, or it is not.

If it is, fewer checks will be necessary, since there will be fewer elements in L1 than if L1 was as long as L2 .

Else, as soon as an element not belonging to L2 is discovered in L1 , it is certain that L1 cannot be a subset of L2 .

On the other hand, if len(L1) == len(L2) but the first element of L1 is not in L2 , the execution will require only one loop over L2 .

Therefore, worst case => len(L1) == len(L2) , but the converse is not true.

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