SMALLTALK LAB
Find strongly connected components in Squeak
Use two depth-first passes to separate a directed graph into groups whose vertices can all reach one another.
Before you begin
Use native Squeak 6.1. Each array row lists outgoing neighbors of one numbered vertex; vertex labels are one-based integers from 1 through the number of rows.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.
01 - Read the outgoing edges
The third row names the destinations 1 and 4. A vertex with no outgoing edges still has a row.
| graph |
graph := #((2) (3) (1 4) (5) (4 6) ()).
{graph at: 3. graph at: 6}Expected: #((1 4) ())

Try it: Can vertex 6 immediately reach any other vertex? No.
02 - Reverse all arrows
For each u→v, append u to incoming row v. The reverse graph is built separately and keeps the same vertex labels.
| reverse |
reverse := [:graph | | incoming |
incoming := (1 to: graph size) collect: [:unused | OrderedCollection new].
graph withIndexDo: [:neighbors :u | neighbors do: [:v |
(incoming at: v) addLast: u]].
(incoming collect: [:row | row asArray]) asArray].
reverse value: #((2) (3) (1 4) (5) (4 6) ())Expected: #((3) (1) (2) (3 5) (4) (5))

Try it: Why does reverse row 4 contain both 3 and 5? Both had arrows to 4.
03 - Record when DFS finishes a vertex
Append a vertex only when recursion has inspected all outgoing neighbors. A visited set prevents cycles from recursing forever.
| finish |
finish := [:graph | | seen post dfs |
seen := Set new. post := OrderedCollection new.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. (graph at: u) do: [:v | dfs value: v].
post addLast: u]].
1 to: graph size do: [:u | dfs value: u].
post asArray].
finish value: #((2) (3) (1 4) (5) (4 6) ())Expected: #(6 5 4 3 2 1)

Try it: Why is 6 first in this finish list? It has no outgoing edges.
04 - Visit the reversed graph in reverse finish order
Start with the latest finisher. Each new DFS in the reversed graph finds one component; the shared visited set prevents reassigning a vertex. Sort within a group for display only.
| reverse finish partition |
reverse := [:graph | | incoming |
incoming := (1 to: graph size) collect: [:unused | OrderedCollection new].
graph withIndexDo: [:neighbors :u | neighbors do: [:v |
(incoming at: v) addLast: u]].
(incoming collect: [:row | row asArray]) asArray].
finish := [:graph | | seen post dfs |
seen := Set new. post := OrderedCollection new.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. (graph at: u) do: [:v | dfs value: v].
post addLast: u]].
1 to: graph size do: [:u | dfs value: u].
post asArray].
partition := [:graph | | seen groups transposed dfs current |
seen := Set new. groups := OrderedCollection new.
transposed := reverse value: graph.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. current add: u.
(transposed at: u) do: [:v | dfs value: v]]].
(finish value: graph) reverseDo: [:u |
(seen includes: u) ifFalse: [
current := OrderedCollection new. dfs value: u.
groups addLast: current asSortedCollection asArray]].
groups asArray].
partition value: #((2) (3) (1 4) (5) (4 6) ())Expected: #((1 2 3) (4 5) (6))

Try it: Arrow 3→4 connects two components; there is no way back to 3.
05 - Do not mistake a one-way chain for a component
Reachability goes in only one direction along the chain. Distinct vertices cannot reach each other both ways.
| reverse finish partition |
reverse := [:graph | | incoming |
incoming := (1 to: graph size) collect: [:unused | OrderedCollection new].
graph withIndexDo: [:neighbors :u | neighbors do: [:v |
(incoming at: v) addLast: u]].
(incoming collect: [:row | row asArray]) asArray].
finish := [:graph | | seen post dfs |
seen := Set new. post := OrderedCollection new.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. (graph at: u) do: [:v | dfs value: v].
post addLast: u]].
1 to: graph size do: [:u | dfs value: u].
post asArray].
partition := [:graph | | seen groups transposed dfs current |
seen := Set new. groups := OrderedCollection new.
transposed := reverse value: graph.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. current add: u.
(transposed at: u) do: [:v | dfs value: v]]].
(finish value: graph) reverseDo: [:u |
(seen includes: u) ifFalse: [
current := OrderedCollection new. dfs value: u.
groups addLast: current asSortedCollection asArray]].
groups asArray].
partition value: #((2) (3) ())Expected: #((1) (2) (3))

Try it: What component does the isolated final vertex form? A group of one.
06 - Handle an isolated loop and an empty graph
A self edge does not join distinct vertices. On empty input there is nothing to visit, so the answer is an empty array.
| reverse finish partition |
reverse := [:graph | | incoming |
incoming := (1 to: graph size) collect: [:unused | OrderedCollection new].
graph withIndexDo: [:neighbors :u | neighbors do: [:v |
(incoming at: v) addLast: u]].
(incoming collect: [:row | row asArray]) asArray].
finish := [:graph | | seen post dfs |
seen := Set new. post := OrderedCollection new.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. (graph at: u) do: [:v | dfs value: v].
post addLast: u]].
1 to: graph size do: [:u | dfs value: u].
post asArray].
partition := [:graph | | seen groups transposed dfs current |
seen := Set new. groups := OrderedCollection new.
transposed := reverse value: graph.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. current add: u.
(transposed at: u) do: [:v | dfs value: v]]].
(finish value: graph) reverseDo: [:u |
(seen includes: u) ifFalse: [
current := OrderedCollection new. dfs value: u.
groups addLast: current asSortedCollection asArray]].
groups asArray].
{partition value: #((1) ()). partition value: #()}Expected: #(((2) (1)) ())

Try it: The first graph visits isolated row 2 before row 1 in the reverse finish pass.
07 - Close the missing return path
Adding 6→1 provides a return path for the single-direction connections. All six vertices now share one component.
| reverse finish partition |
reverse := [:graph | | incoming |
incoming := (1 to: graph size) collect: [:unused | OrderedCollection new].
graph withIndexDo: [:neighbors :u | neighbors do: [:v |
(incoming at: v) addLast: u]].
(incoming collect: [:row | row asArray]) asArray].
finish := [:graph | | seen post dfs |
seen := Set new. post := OrderedCollection new.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. (graph at: u) do: [:v | dfs value: v].
post addLast: u]].
1 to: graph size do: [:u | dfs value: u].
post asArray].
partition := [:graph | | seen groups transposed dfs current |
seen := Set new. groups := OrderedCollection new.
transposed := reverse value: graph.
dfs := [:u | (seen includes: u) ifFalse: [
seen add: u. current add: u.
(transposed at: u) do: [:v | dfs value: v]]].
(finish value: graph) reverseDo: [:u |
(seen includes: u) ifFalse: [
current := OrderedCollection new. dfs value: u.
groups addLast: current asSortedCollection asArray]].
groups asArray].
partition value: #((2) (3) (1 4) (5) (4 6) (1))Expected: #((1 2 3 4 5 6))

Try it: Removing 6→1 restores the three groups from the first graph.
Replay the checkpoints

Reason about correctness and limits
The first pass records finishing order, not discovery order. Use the reversed graph in the second pass, with a fresh visited set shared across its searches.
Contract: each edge endpoint is an integer from 1 through the row count. Start with small graphs because recursive DFS can exhaust the stack on very long paths.
The two graph passes and transpose take O(V + E) time and space under ordinary set operations. Sorting members for display adds at most O(V log V) time in this implementation; remove that sort to retain linear traversal time. Group order is discovery order in the second pass, not a canonical sort.
Three practice challenges
Trace an edge reversal
In the sample, where does original edge 5→6 appear in the reverse graph?
Show solution
It appears as neighbor 5 in row 6.
Separate two-way from one-way
Apply the partition to #((2) (1 3) ()).
Show solution
Vertices 1 and 2 form a component; vertex 3 forms its own.
Add a return link
Change the previous example so that all three vertices share a component.
Show solution
Put 1 in row 3. The edge 3→1 closes a directed cycle.
Reference
Micha Sharir: A strong-connectivity algorithm and its applications in data flow analysis
Continue learning
Graph Bridges | Union Find | Dependency Order
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.