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 βž” disciplineImplementationCosts (the discriminators)
Stack (ADT) βž” LIFO, top onlyArrayStackall ops ; fixed capacity or amortised grow; wasted slack
LinkStackall ops ; never full; one pointer/node overhead β€” crossover β‰ˆ half-full array
Queue (ADT) βž” FIFO, two moving endsLinearQueue but leaks space (front creeps)
CircularQueuemod-arithmetic ring, , no waste
LinkQueuefront+rear pointers, , unbounded
List (ADT) βž” any positionArrayList__getitem__ Β· insert/delete shift Β· append amortised
LinkListreach index = Β· relink at held node
Sorted List (ADT) βž” value orderSortedArrayListsearch (binary) Β· insert (shift) β€” both needs a balanced tree
Set (ADT) βž” membership + algebraArraySetany 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__ or StopIteration); per step, space, single-use, fail-fast on mutation. LinkListIterator = mutate-in-traversal. Generator Expression = lazy, memory, no len/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)

SortBestWorstSpaceStableThe 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().