why is this o(n) three-way set disjointness algorithm slower than then o(n^3) version?
<p>O(n) because converting list to set is O(n) time, getting intersection is O(n) time and len is O(n)</p>

<pre><code>def disjoint3c(A, B, C):
    """Return True if there is no element common to all three lists."""
    return len(set(A) &amp; set(B) &amp; set(C)) == 0
</code></pre>

<p>or similarly, should be clearly O(N)</p>

<pre><code>def set_disjoint_medium (a, b, c):
    a, b, c = set(a), set(b), set(c)
    for elem in a:
        if elem in b and elem in c:
            return False
    return True
</code></pre>

<p>yet this O(n^3) code:</p>

<pre><code>def set_disjoint_slowest (a, b, c):
    for e1 in a:
        for e2 in b:
            for e3 in c:
                if e1 == e2 == e3:
                    return False
    return True
</code></pre>

<p>runs faster</p>

<p>see time where algorithm one is the n^3, and algorithm three is the O(n) set code... algorithm two is actually n^2 where we optimize algorithm one by checking for disjointness before the third loop starts</p>

<pre><code>Size Input (n):  10000

Algorithm One: 0.014993906021118164

Algorithm Two: 0.013481855392456055

Algorithm Three: 0.01955580711364746

Size Input (n):  100000

Algorithm One: 0.15916991233825684

Algorithm Two: 0.1279449462890625

Algorithm Three: 0.18677806854248047

Size Input (n):  1000000

Algorithm One: 1.581618070602417

Algorithm Two: 1.146049976348877

Algorithm Three: 1.8179030418395996
</code></pre>

<p>The comments made clarifications about the Big-Oh notations. So I will just start with testing the code.</p>

<p>Here is the setup I used for testing the speed of the code.</p>

<pre><code>import random

# Collapsed these because already known
def disjoint3c(A, B, C):
def set_disjoint_medium (a, b, c):
def set_disjoint_slowest (a, b, c):

a = [random.randrange(100) for i in xrange(10000)]
b = [random.randrange(100) for i in xrange(10000)]
c = [random.randrange(100) for i in xrange(10000)]

# Ran timeit.
# Results with timeit module.
1-) 0.00635750419422
2-) 0.0061145967287
3-) 0.0487953200969
</code></pre>

<p>Now to the results, as you see, the <code>O(n^3)</code> solution runs <strong>8</strong> times slower than the other solutions. But this is still fast for such an algorithm(Even faster in your test). <strong>Why this happens ?</strong></p>

<p>Because medium and slowest solutions you used, finishes the execution of the code <strong>as soon as a common element is detected</strong>. So the full complexity of the code is not realized. It breaks as soon as it finds an answer. Why the slowest solution ran almost as fast as the other ones in your test ? Probably because it finds the answer closer to the beginning of the lists.</p>

<p>To test this, you could create the lists like this. Try this yourself.</p>

<pre><code>a = range(1000)
b = range(1000, 2000)
c = range(2000, 3000)
</code></pre>

<p>Now the real difference between the times will be obvious because the slowest solution will have to run until it finishes all iterations, because there is no common element.</p>

<p>So it is a situation of <strong>Worst case</strong> and <strong>Best case</strong> performance.</p>

<p><strong>Not a part of the question edit:</strong> So, what if you want to retain the speed of finding early common occurances, but also don't want to increase complexity. I made a rough solution for that, maybe more experienced users can suggest faster code.</p>

<pre><code>def mysol(a, b, c):
    store = [set(), set(), set()]

    # zip_longest for Python3, not izip_longest.
    for i, j, k in itertools.izip_longest(a, b, c):
        if i: store[0].add(i)
        if j: store[1].add(j)
        if k: store[2].add(k)

        if (i in store[1] and i in store[2]) or (j in store[0] and i in store[2]) or (k in store[0] and i in store[1]):
            return False
    return True
</code></pre>

<p>What is basically being done in this code is, you avoid converting all the lists to sets in the beginning. Rather, iterate through all lists at the same time, add elements to sets, check for common occurances. So now, you keep the speed of finding an early solution, but it is still slow for the worst case that I shown.</p>

<p>For the speeds, this runs 3-4 times slower than your first two solutions in the worst case. But runs 4-10 times faster than those solutions in randomized lists.</p>

python algorithm complexity-theory