Graph Modeling & Algorithms

Many scheduling, dependency, routing and state-transition problems are graphs before they are algorithms. Correct modeling and assumption checks matter more than recalling a traversal name.

Section 3 of 5 · 9 units · about 33 hours

0/9

By the end you can Convert two unfamiliar narratives into graphs, implement the selected algorithms with certificates, and reject one wrong method using a minimized counterexample.

Units

Build it

Mini project · about 10 to 14 hours

Union-find, two narratives as graphs and a refuted method

Build a tested union-find and turn two unfamiliar narratives into graphs whose algorithms return checkable certificates. Refuting one plausible method with a minimized counterexample trains the modeling judgment the capstone needs.

  1. 1Implement union-find with union by rank and path compression. State its invariants and amortized cost, and test it with a Hypothesis state machine against a component-labeling oracle over the unions so far.
  2. 2Model the AlgoMaster Alien Dictionary narrative as a graph of letters and first-difference edges, treating a longer word before its prefix as invalid. Ship a certificate with every answer: a forward-edge check for an order, or the cycle or bad pair for an empty one.
  3. 3Test the dictionary solver on generated inputs: draw a random alphabet order, generate words sorted by it, and check outputs against the extracted edges, since several orders can be valid. Add cyclic and bad-prefix cases.
  4. 4Model LC 1631 Path With Minimum Effort as cells joined by edges weighted by height difference, with path cost as the maximum edge. Solve it with a Dijkstra variant that returns the path and a cut certificate showing the target unreachable below the answer.
  5. 5Solve LC 1631 a second way with union-find, adding edges in increasing weight until the corners connect. Assert both methods agree on random grids.
  6. 6Refute a right-and-down-only dynamic program for LC 1631 with Hypothesis against an exhaustive path search, keeping the shrunk counterexample. State the movement assumption that fails.
What you hand in
  • The union-find module with invariants, cost notes and the state machine.
  • The Alien Dictionary solver with its certificate checker and generated tests.
  • Two LC 1631 solutions with the cut certificate, the agreement test and the refutation write-up.
How to check it
How it's graded

Union-find correctness

Pass: The state machine agrees with the component oracle, and the rank-size invariant holds after every step.

Great: Tree heights are measured with and without path compression on an adversarial union order and compared against the bound.

Graph modeling

Pass: Each narrative has a written model of vertices, edges, weights and the question asked, stated before code.

Great: The write-up names one modeling alternative that fails and why.

Answer certificates

Pass: Every answer ships with a checker that validates it without trusting the solver.

Great: The checker catches a seeded solver bug that the worked examples miss.

Method refutation

Pass: The restricted dynamic program is refuted with a minimized counterexample from the exhaustive search.

Great: The write-up states the failed movement assumption and why a shortest-path method repairs it.

Resources

  • DSA Handbook - graphsHandbook Academy

    Study all of Part 8 in order. For each algorithm, write the representation, invariant, forbidden input assumptions and independently checkable output before reading the implementation.

  • AlgoMaster DSA - graph sliceAlgoMaster

    Attempt Number of Islands, Course Schedule II and Network Delay Time cold. They cover traversal, dependency modeling and weighted shortest paths; add one narrative problem only if graph modeling remains weak.

  • Algorithms - graph chaptersJeff Erickson

    Use the basic graph, depth-first search, minimum spanning tree, shortest path and flow chapters, and lecture note 11 Disjoint Sets for union-find, for correctness proofs and counterexamples to tempting wrong algorithms.

  • Hypothesis - generated graph testsHypothesis

    Generate small sparse, dense, disconnected, cyclic, parallel-edge and weighted graphs. Compare optimized results with exhaustive or deliberately simple references and retain shrunk failures.

  • NeetCode - unseen graph problemsNeetCode

    Use two unseen story problems without graph tags. Grade the model, assumptions, certificate and tests as well as the final answer.