GRAPHS · DEPENDENCIES
Topological ordering: schedule prerequisites
Build a native Squeak dependency planner with ready queues, indegree counts, deterministic ordering and explicit cycle detection.
Install and open the native lab
Some tasks must happen before others. Design comes before art and code; both art and code must finish before testing; testing precedes shipping. A topological order arranges tasks so every prerequisite appears before its dependent. Unlike shortest-path search or undirected union-find, the question is about a valid ordering of directed constraints, not distance or connectivity.
- Use a copy of Squeak 6.1 with its matching changes and sources files. This is a native-only workshop; these classes are not installed in the SqueakJS playground.
- Download dependency-order.st. Select the file in Squeak’s File List and choose fileIn. Do not paste the entire chunk-format file into a Workspace.
- Keep one Workspace open for the numbered commands. Read method definitions in the Browser, not as Workspace expressions. The complete model and view are installed before you start; the steps explain and exercise that source.
Prerequisites: classes and objects, the related foundations lesson, and SUnit basics.
SUnit tests · Complete source kit
Keep the fixed-layout native window at least 980 × 620 pixels. No network, file saving or timers are used by the app. Close the window to clean up; Reset returns the view to its initial experiment.
01 · Count unmet prerequisites
This fixed teaching graph has five named tasks and five directed edges. Each edge is written prerequisite first, dependent second. reset counts incoming edges and adds every zero-count task to a ready queue. At the beginning, only design has no prerequisite.
reset
indegrees := Dictionary new. nodes do: [:node | indegrees at: node put: 0].
edges do: [:edge | indegrees at: edge second put: (indegrees at: edge second) + 1].
ready := OrderedCollection new.
nodes do: [:node | (indegrees at: node) = 0 ifTrue: [ready add: node]].
order := OrderedCollection newview := LSDependencyView open.Expected: Ready is #('design'); Order is empty. Counts are 0, 1, 1, 2, 1 in the displayed task order.
Inspect model nodes and edges in the Browser. The order of the nodes and edges is intentional, making the lesson reproducible. The code accepts a fixed graph rather than pretending to be a general graph editor. All endpoints belong to the node list.

02 · Emit one ready task
next removes the first ready task and appends it to the result. It then scans edges leaving that task and subtracts one from each dependent’s count. A dependent enters the queue only when its count reaches zero.
next
| node |
ready isEmpty ifTrue: [^ nil].
node := ready removeFirst. order add: node.
edges do: [:edge | edge first = node ifTrue: [
indegrees at: edge second put: (indegrees at: edge second) - 1.
(indegrees at: edge second) = 0 ifTrue: [ready add: edge second]]].
^ nodeview next.Expected: design is emitted. Both art and code are now ready.
The view’s Next experiment button calls this model operation. Printing view model next yourself also advances the model, but will take you off the pictured sequence. Use Reset before continuing the numbered experiments if you do that.

03 · Wait for every prerequisite
art is next because it was enqueued first. Its completion reduces test’s unmet count from two to one. test must not become ready yet: code still needs to happen. This is an AND relationship between prerequisites, not an either-or choice.
remainingCounts
^ nodes collect: [:node | node, ': ', (indegrees at: node) printString]view next.Expected: Order is #('design' 'art'); only code is ready. test still has one unmet prerequisite.
Trace the dictionary entry for test before and after next. The count reflects edges from tasks not yet emitted. A zero shown beside an already emitted task does not mean it should be scheduled again; the queue and emitted result are separate state.

04 · Unlock the shared dependent
After code is emitted, test’s count reaches zero and it is appended to ready exactly once. Kahn’s algorithm repeatedly performs this same local rule until no ready tasks remain. The final result need not be unique.
ready
^ ready asArrayview next.Expected: Order is #('design' 'art' 'code'); test becomes ready.
The alternative order design, code, art, test, ship also respects the edges. This demo chooses FIFO order and a fixed edge order rather than alphabetical sorting. Scheduling policy can affect which valid order you obtain without changing the prerequisite rules.

05 · Continue to the final dependency
Emitting test decrements ship’s count to zero. The graph itself is preserved: only the separate indegree dictionary, ready queue and output collection change. That makes reset straightforward and lets the view keep displaying the original constraints.
edges
^ edges collect: [:edge | edge copy]view next.Expected: test joins the order and ship is the sole ready task.
Print view model edges. It still contains all five original edges. The accessor copies each pair, preventing a caller from changing endpoint slots in the internal graph. This is a small static teaching model; it does not support graph edits while a run is in progress.

06 · Recognize a complete ordering
Completion is defined by the number of emitted tasks, not merely by an empty queue. Here all five tasks have been emitted, so the graph has a valid topological order. The SUnit test checks every edge against its endpoint positions in that order.
isComplete
^ order size = nodes sizeview next.Expected: The order is design, art, code, test, ship. Complete is true and Blocked is false.
Read testAllEdges in the test class: for each prerequisite/dependent pair, the prerequisite index must be smaller. That property is more general than asserting one particular sequence, because another queue policy could choose a different valid order.

07 · Stop without inventing more work
When ready is empty, next returns nil without changing the result. finish is the automatic equivalent of pressing Next until ready empties. It answers whether all tasks were emitted, allowing callers to distinguish success from a cycle-blocked run.
finish
[ready isEmpty] whileFalse: [self next].
^ self isCompleteview next.Expected: The completed result is unchanged after an extra next request.
Print view model finish and expect true. Do not interpret nil from next alone as success. An incomplete graph with a cycle also runs out of ready tasks. A real application should report both the emitted prefix and the blocked remainder rather than executing an incomplete schedule blindly.

08 · Detect a cycle, not a valid schedule
The final experiment adds ship → design and restarts the run. Now every task has an incoming dependency and there is no starting task. isBlocked combines an empty ready queue with an incomplete result. No valid topological ordering exists for this graph.
isBlocked
^ ready isEmpty and: [self isComplete not]view next.Expected: Ready and Order are empty. Complete is false; Blocked is true.
Print view model finish and expect false. The blocked set can include tasks downstream of a cycle, not only cycle members; this method does not reconstruct a cycle path. Reset on the VIEW creates a fresh acyclic model. model reset alone retains the deliberately added cycle.

Replay the native experiments

Verify, extend and know the limits
LSDependencyTest suite runExpected: 10 tests, 10 passes, 0 failures and 0 errors. File the test source in through the File List before evaluating the suite. Tests were run in Squeak 6.1 build 23976.
This implementation scans the full edge array on each emitted node, so a complete run costs O(VE + V) in this representation, not the usual O(V + E) bound for adjacency lists. Build an outgoing-neighbors dictionary once to avoid that scan, then preserve the edge-order property tests. A general planner also needs duplicate-edge policy, endpoint validation and a clear response to edits. It should not run arbitrary real-world tasks merely because the teaching queue says they are ready.
University of Illinois: topological sorting lecture notes provides background for the underlying idea; this Smalltalk implementation and tutorial are original. Official Squeak documentation covers the environment and language.
Continue with Prefix tries: build an autocomplete index · Affine transforms: move shapes with six numbers.