FIT1008 Unit Cheatsheet
Context: FIT1008_MOC Β· the WHOLE unit in one re-read β complexity (W1) β ADTs (W2β5) β recursion + sorts (W6β7) β trees, heaps, hashing (W8β11). Mid-sem test = W1β5; exam = everything, weighted to W6β11.
Quick Revision
- π― Objective: every exam question reduces to β state the invariant, pick the implementation, justify with a Best/Worst complexity bound.
- β‘ Key Constraint: array vs linked are MIRROR images β random access vs structural edit; every ADT decision question is this trade-off wearing a costume.
1οΈβ£ Foundations & Complexity (W1)
- Algorithm β finite, well-defined, halts, correct I/O; total correctness = invariant (partial) + variant (termination). Invariant proof = initialization β maintenance β termination.
- Problem vs algorithm bounds β lower bound belongs to the PROBLEM (comparison sorting β no algorithm beats it); upper bound is exhibited by a specific algorithm; matching them proves optimality.
- Big-O algebra β keep dominant term, drop constants; = upper, = lower, ; unqualified βcomplexityβ = worst case. Ladder: ; polynomial vs exponential = the tractability frontier.
- Arithmetic series β β WHY a shrinking nested loop is quadratic, not linear.
- Search β linear , works on any iterable, no precondition Β· binary , needs sorted + random access (never on a LinkList).
2οΈβ£ ADT Master Table (W2β5)
ADT = contract (values + operations + invariant), implementation fixes the cost. Same interface, opposite cost profiles β the exam question is always βwhich implementation for THIS workloadβ.
| ADT β discipline | Implementation | Costs (the discriminators) |
|---|---|---|
| Stack (ADT) β LIFO, top only | ArrayStack | all ops ; fixed capacity or amortised grow; wasted slack |
| LinkStack | all ops ; never full; one pointer/node overhead β crossover β half-full array | |
| Queue (ADT) β FIFO, two moving ends | LinearQueue | but leaks space (front creeps) |
| CircularQueue | mod-arithmetic ring, , no waste | |
| LinkQueue | front+rear pointers, , unbounded | |
| List (ADT) β any position | ArrayList | __getitem__ Β· insert/delete shift Β· append amortised |
| LinkList | reach index = Β· relink at held node | |
| Sorted List (ADT) β value order | SortedArrayList | search (binary) Β· insert (shift) β both needs a balanced tree |
| Set (ADT) β membership + algebra | ArraySet | any type, scans |
| BVSet (Bit Vector) | ints only; ops, word-parallel ; cost scales with universe, not count |
- Dynamic resizing β grow by a constant factor βΉ append amortised (single append can be ); additive growth fails β total.
- Slicing β Python slice copies ( time+space); NumPy slices are views.
- Iterators (W5) β Iterable
__iter__returns a fresh Iterator (__next__orStopIteration); per step, space, single-use, fail-fast on mutation. LinkListIterator = mutate-in-traversal. Generator Expression = lazy, memory, nolen/indexing.
3οΈβ£ Recursion (W6)
- Anatomy β base case + recursive call + convergence to base + combine; correctness by induction, cost by recurrence.
- Stack hazard β frames, no Python TCO βΉ overflow; fix forward with an accumulator, or backward with an explicit Stack (ADT).
- Tower of Hanoi β move aside Β· move bottom Β· restack βΉ exactly moves , provably optimal; stack depth only .
- Divide and Conquer depth rule β balanced halves β ; lopsided split β ; single-half recurse β .
4οΈβ£ Sorting Suite (W1 basics + W7 recursive + W9 heapsort)
| Sort | Best | Worst | Space | Stable | The one thing to say |
|---|---|---|---|---|---|
| Bubble | (adaptive) | β | swap-heavy; early-exit flag gives the best | ||
| Selection | β | only swaps β never adaptive | |||
| Insertion | (adaptive) | β | online; best on nearly-sorted | ||
| Merge | scratch | β (ties left-first) | trivial split / heavy combine; guaranteed every case | ||
| Quick | bad pivots | stack | β | heavy split / trivial combine; in-place, smallest constants; pivot is the whole game | |
| Heap | β | selection sort with a fast find_max; guaranteed + in-place, cache-unfriendly |
- Correctness = permutation + ordering β both clauses, or the answer is incomplete.
5οΈβ£ Trees, Heaps, Hashing (W8β11)
- Tree β connected + acyclic, nodes βΉ edges; every structural op is β balanced, degenerate. Traversals (pre/in/post DFS + level BFS) all .
- Binary Search Tree (BST) β invariant left < node < right; search = halving; insert = return-and-relink; delete = 3 cases via in-order successor. Sorted input βΉ degenerate βstickβ . vs hash table: but ordered (range/successor queries).
- Heap β complete (height , array-backed: children of at ) + heap-order (parent β₯ child).
addβ rise;get_maxβ sink; peek ; bottom-up build , NOT . Only min/max β no arbitrary search. - Priority Queue (ADT) β every linear implementation has one stuck op; only heap/balanced tree gets both add and get_max to . FIFO queue = PQ where priority = waiting time.
- Dictionary (ADT) β keyed lookup: hash-backed expected, unordered Β· tree-backed , ordered.
- Hash Table β key β index; expected , worst ; hinges on uniform hash + bounded load factor (rehash trigger). Collision resolution: chaining vs open addressing/linear probing (clustering hazard).
β οΈ Top Cross-Unit Traps
- π‘ β insertβ needs the node in hand β LinkList insert-at-index is walk + relink = .
- π‘ Amortised β every-call β one append may cost ; the SEQUENCE averages β say βamortisedβ explicitly.
- π‘ Binary search on a LinkList β invalid β no random access; the dies in the walk.
- π‘ Heap build β writing for bottom-up heapify is the classic W9 deduction.
- π‘ Invariant proves partial correctness only β termination needs a separate variant (strictly decreasing, non-negative).
- π‘ No magic methods in answers β Domain A rule: raw index/pointer code, never
.sort()/min()/sum().