Set (ADT)

Context: FIT1008_MOC · an unordered, duplicate-free Abstract Data Type (ADT) · backbone clustering ArraySet (any type) and BVSet (integers, ) · contract via Abstract Base Class

Quick Revision

  • 🎯 Objective: membership + algebra (∪,∩,∖) over unordered, duplicate-free elements ➔ a characteristic function .
  • 📦 Core Components: Contractadd/remove/__contains__/union | ArraySet ➔ any type, scan | BVSet ➔ integers only, word-parallel.
  • ⚡ Key Constraint: same interface, opposite cost profiles ➔ the implementation, not the ADT, sets complexity (ArraySet vs BVSet ).

📝 Core

1. Set Contract (Membership + Algebra)

  • Set rulesunordered () + duplicate-free (re-add is a no-op).
  • Operationsadd/remove/__contains__ + union / intersection / difference (≙ OR/AND/AND-NOT).
  • Characteristic functionbit-vector makes it literal; Bloom filter approximates it (one-sided error).

2. ArraySet (Array-Backed, Any Type)

  • Storage substrate ➔ fixed Array (Data Structure) + size; arrival order (irrelevant to a set).
  • Scan cost ➔ every membership op scans ➔ ; add is (duplicate check dominates).
  • Swap-with-last delete — valid because unordered (a list cannot).

3. BVSet (Bit-Vector, Integers Only)

  • Storage substrate ➔ one arbitrary-precision int elems (Bit Vector); item ⟺ bit ; never full.
  • Word-parallel algebra ➔ member/add/remove + |/&/& ~ all per word.
  • Type/size boundarypositive integers only; __len__ = popcount (driven by largest value).

⚙️ Core Implementation

🔹 ArraySet — linear scan, swap-with-last delete

🔹 BVSet — word-parallel bit algebra

⚖️ Core Decision Matrix

Variant / StrategyTrigger Conditionmember / add / removeunion / ∩ / ∖__len__Element types
ArraySet (unsorted)Arbitrary types, small setsany
BVSet (bit-vector)Dense small-int sets, heavy algebra word-parallelints only
sorted arrayMembership-heavy, ordered / mergecomparable
hash setGeneral-purpose point queries expected expectedhashable
balanced BSTOrdered iteration / rangecomparable

When It Flips: ArraySet and BVSet are exact inverses (any-type/-size vs ints-only/-membership). BVSet's union is really — a ~64× constant-factor speedup, valid only for integer universes that aren't enormous/sparse.

📊 Exam Execution Trace

Manual Execution Trace

BVSet: add 1, add 3, add 4; union {2,3}

Step / StateTrigger Opelems (binary, bit )Set Payload
0 (Init)init0000
1add 10001
2add 30101
3add 41101
4∪ 01101101 | 0110 = 1111

Applied Exercise

Problem: Explain why ArraySet add is but its remove is . Derivation Proof / Hand-Calculation Walkthrough:

Final Extracted Output: add (no-duplicates check), remove (swap-with-last in an unordered set).

🧠 Active Recall