s LearnSqueak.com

GRAPHS · DEPENDENCIES

Topological ordering: schedule prerequisites

Build a native Squeak dependency planner with ready queues, indegree counts, deterministic ordering and explicit cycle detection.

8 native experiments10 SUnit testsComplete source

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.

  1. 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.
  2. 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.
  3. 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.

LSDependencyPlan method · read in Browser
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 new
Same Workspace · select and Do it
view := 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.

NATIVE SQUEAK CAPTURE
Native Squeak Topological ordering: schedule prerequisites, experiment 1: Count unmet prerequisites
Genuine Squeak Morphic window after experiment 1.

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.

LSDependencyPlan method · read in Browser
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]]].
 ^ node
Same Workspace · select and Do it
view 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.

NATIVE SQUEAK CAPTURE
Native Squeak Topological ordering: schedule prerequisites, experiment 2: Emit one ready task
Genuine Squeak Morphic window after experiment 2.

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.

LSDependencyPlan method · read in Browser
remainingCounts
 ^ nodes collect: [:node | node, ': ', (indegrees at: node) printString]
Same Workspace · select and Do it
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.

NATIVE SQUEAK CAPTURE
Native Squeak Topological ordering: schedule prerequisites, experiment 3: Wait for every prerequisite
Genuine Squeak Morphic window after experiment 3.

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.

LSDependencyPlan method · read in Browser
ready
 ^ ready asArray
Same Workspace · select and Do it
view 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.

NATIVE SQUEAK CAPTURE
Native Squeak Topological ordering: schedule prerequisites, experiment 4: Unlock the shared dependent
Genuine Squeak Morphic window after experiment 4.

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.

LSDependencyPlan method · read in Browser
edges
 ^ edges collect: [:edge | edge copy]
Same Workspace · select and Do it
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.

NATIVE SQUEAK CAPTURE
Native Squeak Topological ordering: schedule prerequisites, experiment 5: Continue to the final dependency
Genuine Squeak Morphic window after experiment 5.

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.

LSDependencyPlan method · read in Browser
isComplete
 ^ order size = nodes size
Same Workspace · select and Do it
view 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.

NATIVE SQUEAK CAPTURE
Native Squeak Topological ordering: schedule prerequisites, experiment 6: Recognize a complete ordering
Genuine Squeak Morphic window after experiment 6.

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.

LSDependencyPlan method · read in Browser
finish
 [ready isEmpty] whileFalse: [self next].
 ^ self isComplete
Same Workspace · select and Do it
view 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.

NATIVE SQUEAK CAPTURE
Native Squeak Topological ordering: schedule prerequisites, experiment 7: Stop without inventing more work
Genuine Squeak Morphic window after experiment 7.

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.

LSDependencyPlan method · read in Browser
isBlocked
 ^ ready isEmpty and: [self isComplete not]
Same Workspace · select and Do it
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.

NATIVE SQUEAK CAPTURE
Native Squeak Topological ordering: schedule prerequisites, experiment 8: Detect a cycle, not a valid schedule
Genuine Squeak Morphic window after experiment 8.

Replay the native experiments

NATIVE SQUEAK CAPTURE
Topological ordering: schedule prerequisites before the experiment sequence
Eight genuine native window captures after the scripted lesson commands, not a mouse recording or interpolated animation.

Verify, extend and know the limits

File in the tests first · select and Print it
LSDependencyTest suite run

Expected: 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.