SMALLTALK LAB
Euler Trails in Squeak: Use Every Edge Exactly Once
Build Hierholzer’s stack algorithm for an undirected multigraph, preserve parallel-edge identity, test the existence conditions, and independently validate the returned trail.
Before you begin
Open a fresh Workspace. Select one complete checkpoint and choose Print it. All eight expressions declare their own variables and run independently. Vertex numbers are one-based; the main example maps 1–4 to A–D. No packages are needed. The downloadable Morphic board shows five computed snapshots of traversal and backtracking.
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 show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.
01 - Give every edge its own identity
Vertices 1, 2, 3, 4 stand for A, B, C, D. Each array position is a unique undirected edge ID. Edges 2 and 3 both connect A and B; edges 4 and 5 both connect A and C. A Set of endpoint pairs would lose multiplicity. Euler trails use every edge exactly once, not every vertex exactly once.
| edges |
edges := #((1 4) (1 2) (2 1) (1 3) (3 1)).
{edges size. (edges at: 2). (edges at: 3)}Expected: #(5 #(1 2) #(2 1))

Try it: Reverse the endpoints of edge 3. It remains a distinct edge because its index is still 3.
02 - Count degrees, including both ends of a loop
An undirected self-loop contributes two to its vertex degree, despite being just one edge to consume. This separate tiny multigraph has two parallel A–B edges and one A loop. Increment each endpoint, even when both endpoints are equal.
| edges degree |
edges := #((1 2) (1 2) (1 1)).
degree := Array new: 2 withAll: 0.
edges do: [:pair |
degree at: pair first put: (degree at: pair first) + 1.
degree at: pair second put: (degree at: pair second) + 1].
{degree. degree sum. edges size * 2}Expected: #(#(4 2) 6 6)

Try it: The degree sum must equal twice the number of edges, including loops and parallel edges.
03 - Choose the correct start from the odd degrees
For a connected edge-bearing component, zero odd-degree vertices means an Euler circuit; exactly two means an open Euler trail. With two odd vertices, start at either one and finish at the other. With zero, start at any vertex of positive degree. This graph has odd vertices A and D.
| edges degree odd |
edges := #((1 4) (1 2) (2 1) (1 3) (3 1)).
degree := Array new: 4 withAll: 0.
edges do: [:pair |
degree at: pair first put: (degree at: pair first) + 1.
degree at: pair second put: (degree at: pair second) + 1].
odd := (1 to: 4) select: [:vertex | (degree at: vertex) odd].
{degree. odd asArray. odd first}Expected: #(#(5 2 2 1) #(1 4) 1)

Try it: Starting at B would violate the required endpoint choice. Degree parity alone is not a connectivity test.
04 - Reject disconnected edge-bearing components
Two isolated triangles have even degrees everywhere but cannot share one trail. Flood from vertex 1 across undirected edges, then require every vertex of positive degree to be reached. Isolated vertices with degree zero do not obstruct an Euler trail over the edges.
| edges degree reached pending active |
edges := #((1 2) (2 3) (3 1) (4 5) (5 6) (6 4)).
degree := Array new: 6 withAll: 0.
edges do: [:pair |
degree at: pair first put: (degree at: pair first) + 1.
degree at: pair second put: (degree at: pair second) + 1].
reached := Set with: 1.
pending := OrderedCollection with: 1.
[pending notEmpty] whileTrue: [ | current |
current := pending removeLast.
edges do: [:pair |
(pair includes: current) ifTrue: [
pair do: [:neighbor |
(reached includes: neighbor) ifFalse: [
reached add: neighbor. pending add: neighbor]]]]].
active := (1 to: 6) select: [:vertex | (degree at: vertex) > 0].
{(degree allSatisfy: [:count | count even]).
(active allSatisfy: [:vertex | reached includes: vertex]). reached size}Expected: #(true false 3)

Try it: Connect the two triangles by one edge. Connectivity becomes true and the bridge endpoints become the two odd vertices.
05 - Follow unused edges with a stack
Hierholzer follows unused edges until it cannot continue. Our deterministic scan chooses the smallest unused incident ID. The first move is A to D along edge 1, a dead end. Do not declare failure: pop D into the reverse answer, then continue from A. A completed suffix is discovered before the rest of the trail.
| edges used stack chosen pair |
edges := #((1 4) (1 2) (2 1) (1 3) (3 1)).
used := Array new: edges size withAll: false.
stack := OrderedCollection with: 1.
chosen := (1 to: edges size) detect: [:index |
((used at: index) not) and: [(edges at: index) includes: stack last]].
used at: chosen put: true.
pair := edges at: chosen.
stack add: (pair first = stack last ifTrue: [pair second] ifFalse: [pair first]).
{stack asArray. chosen. ((1 to: edges size) anySatisfy: [:index |
((used at: index) not) and: [(edges at: index) includes: stack last]])}Expected: #(#(1 4) 1 false)

Try it: The traversal stack is not yet the answer. D must be the final vertex, not the second vertex of the completed trail.
06 - Backtrack and reverse the completed trail
Keep an incoming edge ID alongside each stack entry; 0 is the root sentinel. When a vertex has no unused incident edge, append it and its incoming ID to reverse output. Reverse both collections at the end. This is the stack form of Hierholzer: dead-end suffixes and closed detours are spliced without manually inserting into a route. The graph here already satisfies the degree and connectivity conditions.
| edges used stack incoming reverseVertices reverseEdges route routeEdges |
edges := #((1 4) (1 2) (2 1) (1 3) (3 1)).
used := Array new: edges size withAll: false.
stack := OrderedCollection with: 1.
incoming := OrderedCollection with: 0.
reverseVertices := OrderedCollection new.
reverseEdges := OrderedCollection new.
[stack notEmpty] whileTrue: [ | vertex chosen pair next edgeId |
vertex := stack last.
chosen := (1 to: edges size) detect: [:index |
((used at: index) not) and: [(edges at: index) includes: vertex]]
ifNone: [0].
chosen = 0
ifTrue: [
reverseVertices add: stack removeLast.
edgeId := incoming removeLast.
edgeId > 0 ifTrue: [reverseEdges add: edgeId]]
ifFalse: [
used at: chosen put: true.
pair := edges at: chosen.
next := pair first = vertex ifTrue: [pair second] ifFalse: [pair first].
stack add: next.
incoming add: chosen]].
route := reverseVertices reversed asArray.
routeEdges := reverseEdges reversed asArray.
{route. routeEdges. (used allSatisfy: [:flag | flag])}Expected: #(#(1 2 1 3 1 4) #(2 3 4 5 1) true)

Try it: Edge 1 was consumed first but appears last in the answer. Repeated vertices are legal; repeated edge IDs are not.
07 - Validate IDs and adjacency, not just length
A route of E + 1 vertices is necessary but insufficient. Check that the edge-ID sequence has exactly E entries, that all IDs are unique and in range, and that each ID connects its two neighboring route vertices in either orientation. Together these checks certify that every original edge appears once. Keeping IDs makes validation unambiguous even with parallel edges.
| edges used stack incoming reverseVertices reverseEdges route routeEdges valid |
edges := #((1 4) (1 2) (2 1) (1 3) (3 1)).
used := Array new: edges size withAll: false.
stack := OrderedCollection with: 1.
incoming := OrderedCollection with: 0.
reverseVertices := OrderedCollection new.
reverseEdges := OrderedCollection new.
[stack notEmpty] whileTrue: [ | vertex chosen pair next edgeId |
vertex := stack last.
chosen := (1 to: edges size) detect: [:index |
((used at: index) not) and: [(edges at: index) includes: vertex]]
ifNone: [0].
chosen = 0
ifTrue: [
reverseVertices add: stack removeLast.
edgeId := incoming removeLast.
edgeId > 0 ifTrue: [reverseEdges add: edgeId]]
ifFalse: [
used at: chosen put: true.
pair := edges at: chosen.
next := pair first = vertex ifTrue: [pair second] ifFalse: [pair first].
stack add: next.
incoming add: chosen]].
route := reverseVertices reversed asArray.
routeEdges := reverseEdges reversed asArray.
valid := route size = (edges size + 1)
and: [routeEdges size = edges size
and: [routeEdges asSet size = edges size
and: [routeEdges allSatisfy: [:edgeId | edgeId between: 1 and: edges size]]]].
valid ifTrue: [
1 to: routeEdges size do: [:position | | pair from to |
pair := edges at: (routeEdges at: position).
from := route at: position.
to := route at: position + 1.
(((pair first = from) and: [pair second = to])
or: [(pair first = to) and: [pair second = from]])
ifFalse: [valid := false]]].
{valid. route first. route last}Expected: #(true 1 4)

Try it: Try replacing routeEdges with #(2 2 4 5 1) before validation. The duplicate ID must make valid false.
08 - Catch a plausible-looking but broken answer
A list can have the right length and contain all edge IDs yet mismatch the vertices. Here we compute the real route and then deliberately corrupt its second vertex to D. The independent validator rejects it. The teaching implementation scans the full edge list for each stack action, so it takes O(E squared) time and O(V + E) storage with preprocessing. Per-vertex adjacency lists and cursors reduce Hierholzer to O(V + E).
| edges used stack incoming reverseVertices reverseEdges route routeEdges valid |
edges := #((1 4) (1 2) (2 1) (1 3) (3 1)).
used := Array new: edges size withAll: false.
stack := OrderedCollection with: 1.
incoming := OrderedCollection with: 0.
reverseVertices := OrderedCollection new.
reverseEdges := OrderedCollection new.
[stack notEmpty] whileTrue: [ | vertex chosen pair next edgeId |
vertex := stack last.
chosen := (1 to: edges size) detect: [:index |
((used at: index) not) and: [(edges at: index) includes: vertex]]
ifNone: [0].
chosen = 0
ifTrue: [
reverseVertices add: stack removeLast.
edgeId := incoming removeLast.
edgeId > 0 ifTrue: [reverseEdges add: edgeId]]
ifFalse: [
used at: chosen put: true.
pair := edges at: chosen.
next := pair first = vertex ifTrue: [pair second] ifFalse: [pair first].
stack add: next.
incoming add: chosen]].
route := reverseVertices reversed asArray.
routeEdges := reverseEdges reversed asArray.
route at: 2 put: 4.
valid := route size = (edges size + 1)
and: [routeEdges size = edges size
and: [routeEdges asSet size = edges size
and: [routeEdges allSatisfy: [:edgeId | edgeId between: 1 and: edges size]]]].
valid ifTrue: [
1 to: routeEdges size do: [:position | | pair from to |
pair := edges at: (routeEdges at: position).
from := route at: position.
to := route at: position + 1.
(((pair first = from) and: [pair second = to])
or: [(pair first = to) and: [pair second = from]])
ifFalse: [valid := false]]].
{route size = (edges size + 1). routeEdges asSet size = edges size. valid}Expected: #(true true false)

Try it: This validator checks a proposed output, not why an input failed. A reusable solver should report bad vertex IDs, disconnected edges, or an odd-degree count other than zero or two before traversing.
Watch the computed states

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.
Reason about correctness and limits
Euler trails cover edges; Hamiltonian paths cover vertices. They are different problems.
Store edge identity separately from endpoint values. Parallel edges and self-loops must remain distinct.
Degree parity is not enough: all nonzero-degree vertices must be in one connected component. Isolated vertices may be ignored.
For two odd-degree vertices, choose an odd start. For an Euler circuit choose a nonisolated start.
A self-loop contributes two to degree but is consumed once; endpoint membership still finds it.
The full solver checkpoints use a known valid input, not a general input-validation API. Validate endpoint ranges and existence conditions before adapting them.
For an empty edge set, choose and document a convention: on a nonempty vertex set, a one-vertex route with no edge IDs is a valid empty trail. An empty vertex set needs a separately defined result.
The simple full-edge scan is quadratic, not the linear-time adjacency-list implementation. This undirected algorithm does not directly implement the directed degree conditions.
Three practice challenges
Close the open trail
Add a second edge between A and D. Predict the odd vertices and the final endpoint when starting at A.
Show solution
A and D each gain one degree, so all degrees become even. The connected graph admits a circuit starting and ending at A. Keep the new edge as its own sixth ID and rerun the validator.
Add a loop without changing parity
Append #(2 2) as a new edge. What changes in degrees and output length?
Show solution
B gains degree two, so A and D remain the two odd vertices. Six edges require seven route vertices. The loop contributes one edge ID and one adjacent B,B pair to the trail.
Make the traversal linear
Replace repeated full-edge scans with per-vertex incident-ID lists. How do you avoid reusing an undirected edge?
Show solution
Append each edge ID to both endpoint lists and retain one shared used array. Maintain a cursor per vertex, skipping IDs already used; each incidence is examined at most once. A self-loop may appear twice in its vertex list but the shared flag prevents a second traversal. With degree and connectivity preprocessing this gives O(V + E) time.
Continue learning
Bipartite Graphs | Union Find | Collections
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.