What is the time-complexity of this equality operator between two containers?

Viewed 114

I'm testing my understanding of complexity and want to verify my answer.

I have an equality operator between two containers of the same type. My algorithm iterates over the lhs (aka this) and tests for item containment in rhs. Subsequently, the algorithm iterates over the rhs and tests for item containment in lhs (aka this). At any time, if item is not in the other contain the algorithm breaks with a false.

So, I think that the minimum complexity is constant time O(1), since when lhs and rhs are different sizes the result is false.

I think that the maximum complexity is O(2n^2) for the edge case when the test for containment is always last for every element of both lhs and rhs iterations.

Is this right? Are there nuances that I have missed or misunderstood?

Here's an implementation of my algorithm. They are custom types but the algorithm should be readable.

    bool LibrdfModel::operator==(LibrdfModel &rhs) {
        /**
         * Note on complexity - this algorithm is quite expensive.
         * First, if not same size, then return false. So the rest is assuming the size of lhs and rhs are equal.
         * iterate over lhs. For each of the n elements element we search through n rhs --> n^2
         * Then we iterate over rhs. For each of the n elements we search through n lhs elements --> n^2
         * So we have O(2n^2)
         */

        // we first try comparing size. If they are not equal, then the models are not equal
        if (size() != rhs.size())
            return false;

        // if they are the same size, we need a more expensive operation to compare the models
        // No equals operator exists for a model so we use this strategy:
        // Convert this and that to a stream. Iterate over each stream
        // checking if all statements in this are in that and all
        // statements in that are in this. Then this == that.
        bool all_this_in_rhs = true;
        bool all_rhs_in_this = true;
        LibrdfStream this_stream = toStream();
        {
            int count = 0;

            while (!this_stream.end()) {
                LibrdfStatement statement = this_stream.getStatement();
                // check statement is in other model
                bool contains_statement = rhs.containsStatement(statement);
                if (!contains_statement) {
                    all_this_in_rhs = false;
                    break;
                }
                this_stream.next();
                count++;
            }
        }

        LibrdfStream rhs_stream = rhs.toStream();
        {
            int count = 0;
            while (!rhs_stream.end()) {
                LibrdfStatement statement = rhs_stream.getStatement();
                // check statement is in other model
                bool contains_statement = rhs.containsStatement(statement);
                if (!contains_statement) {
                    all_rhs_in_this = false;
                    break;
                }
                rhs_stream.next();
                count++;
            }
        }
        return all_this_in_rhs && all_rhs_in_this;
    }

0 Answers
Related