SMALLTALK LAB
Seam Carving in Squeak: Find and Remove a Low-Energy Path
Remove one column’s worth of cells without taking a straight column. Compute the cheapest connected seam in a small synthetic energy grid.
Before you begin
Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.
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 - Describe the choices
A vertical seam has one cell per row. Its next column may move left by one, stay put, or move right by one. From column 3, the previous row’s allowed columns are 2, 3, and 4.
| costs c |
costs := #(6 1 5 8).
c := 3.
((c - 1) to: (c + 1)) collect: [:p | costs at: p]Expected: #(1 5 8)

Try it: For column 1, clip the range so column zero cannot be selected.
02 - Accumulate one row of costs
The cheapest way to enter each new cell is its fixed energy plus the smallest reachable total from the previous row. By using cumulative totals, we do not risk choosing a cheap local cell on an expensive path.
| old row |
old := #(6 1 5 8).
row := #(4 2 7 4).
(1 to: row size) collect: [:c | | lo hi |
lo := (c - 1) max: 1. hi := (c + 1) min: row size.
(row at: c) + (old copyFrom: lo to: hi) min]Expected: #(5 3 8 9)

Try it: For column 2, the best predecessor cost is 1 and the new cell costs 2, giving 3.
03 - Remember predecessors and reconstruct
Store the winning predecessor in each row as you accumulate costs. Choose the cheapest bottom cell, then follow its saved predecessors upward. When costs tie, choosing the leftmost predecessor makes the outcome repeatable.
| energy trace |
energy := #((6 1 5 8) (4 2 7 4) (9 9 1 9) (5 4 6 0)).
trace := [:grid | | height width costs parents route best |
height := grid size. width := grid first size.
costs := grid first copy.
parents := Array new: height.
2 to: height do: [:r | | next from |
next := Array new: width. from := Array new: width.
1 to: width do: [:c | | pick |
pick := (c - 1) max: 1.
pick + 1 to: ((c + 1) min: width) do: [:p |
(costs at: p) < (costs at: pick) ifTrue: [pick := p]].
from at: c put: pick.
next at: c put: ((costs at: pick) + ((grid at: r) at: c))].
parents at: r put: from. costs := next].
best := 1.
2 to: width do: [:c |
(costs at: c) < (costs at: best) ifTrue: [best := c]].
route := Array new: height.
route at: height put: best.
height to: 2 by: -1 do: [:r |
route at: r - 1 put: ((parents at: r) at: (route at: r))].
{route. costs at: best}].
trace value: energyExpected: #((2 2 3 4) 4)

Try it: The winning path bends right twice. Adjacent columns never differ by more than one.
04 - Check the seam’s actual cost
The chosen cell values are 1, 2, 1, and 0, which sum to 4. This verifies that path coordinates and the advertised cost agree.
| energy trace path total |
energy := #((6 1 5 8) (4 2 7 4) (9 9 1 9) (5 4 6 0)).
trace := [:grid | | height width costs parents route best |
height := grid size. width := grid first size.
costs := grid first copy.
parents := Array new: height.
2 to: height do: [:r | | next from |
next := Array new: width. from := Array new: width.
1 to: width do: [:c | | pick |
pick := (c - 1) max: 1.
pick + 1 to: ((c + 1) min: width) do: [:p |
(costs at: p) < (costs at: pick) ifTrue: [pick := p]].
from at: c put: pick.
next at: c put: ((costs at: pick) + ((grid at: r) at: c))].
parents at: r put: from. costs := next].
best := 1.
2 to: width do: [:c |
(costs at: c) < (costs at: best) ifTrue: [best := c]].
route := Array new: height.
route at: height put: best.
height to: 2 by: -1 do: [:r |
route at: r - 1 put: ((parents at: r) at: (route at: r))].
{route. costs at: best}].
path := (trace value: energy) first.
total := (1 to: path size) inject: 0 into: [:sum :r |
sum + ((energy at: r) at: (path at: r))].
{path. total. total = (trace value: energy) second}Expected: #((2 2 3 4) 4 true)

Try it: Inspect the marked cells in the native replay before checking the sum.
05 - Remove exactly one cell per row
Remove the selected column from each row and keep all other values in order. On an image, you would remove the corresponding pixels, not the energy values. We show numbers to make the operation inspectable.
| energy trace path |
energy := #((6 1 5 8) (4 2 7 4) (9 9 1 9) (5 4 6 0)).
trace := [:grid | | height width costs parents route best |
height := grid size. width := grid first size.
costs := grid first copy.
parents := Array new: height.
2 to: height do: [:r | | next from |
next := Array new: width. from := Array new: width.
1 to: width do: [:c | | pick |
pick := (c - 1) max: 1.
pick + 1 to: ((c + 1) min: width) do: [:p |
(costs at: p) < (costs at: pick) ifTrue: [pick := p]].
from at: c put: pick.
next at: c put: ((costs at: pick) + ((grid at: r) at: c))].
parents at: r put: from. costs := next].
best := 1.
2 to: width do: [:c |
(costs at: c) < (costs at: best) ifTrue: [best := c]].
route := Array new: height.
route at: height put: best.
height to: 2 by: -1 do: [:r |
route at: r - 1 put: ((parents at: r) at: (route at: r))].
{route. costs at: best}].
path := (trace value: energy) first.
(1 to: energy size) collect: [:r | | row |
row := energy at: r.
(((1 to: row size) reject: [:c | c = (path at: r)])
collect: [:c | row at: c]) asArray]Expected: #((6 5 8) (4 7 4) (9 9 9) (5 4 6))

Try it: The returned grid still has four rows; each row now has three cells.
06 - Make ties explicit
In a flat equal-cost grid there can be many equally good seams. The strict less-than comparison preserves the leftmost tie rather than pretending one path is better.
| energy trace |
energy := #((6 1 5 8) (4 2 7 4) (9 9 1 9) (5 4 6 0)).
trace := [:grid | | height width costs parents route best |
height := grid size. width := grid first size.
costs := grid first copy.
parents := Array new: height.
2 to: height do: [:r | | next from |
next := Array new: width. from := Array new: width.
1 to: width do: [:c | | pick |
pick := (c - 1) max: 1.
pick + 1 to: ((c + 1) min: width) do: [:p |
(costs at: p) < (costs at: pick) ifTrue: [pick := p]].
from at: c put: pick.
next at: c put: ((costs at: pick) + ((grid at: r) at: c))].
parents at: r put: from. costs := next].
best := 1.
2 to: width do: [:c |
(costs at: c) < (costs at: best) ifTrue: [best := c]].
route := Array new: height.
route at: height put: best.
height to: 2 by: -1 do: [:r |
route at: r - 1 put: ((parents at: r) at: (route at: r))].
{route. costs at: best}].
trace value: #((3 3 3) (2 2 2))Expected: #((1 1) 5)

Try it: Try a grid with just one column. Every row then uses column 1.
Replay the checkpoints

Download the native energy-grid demo. Do it in Workspace. Close the window, change shown := 1. to a number from 2 to 4, and run again.
Reason about correctness and limits
The grid contains synthetic fixed energies, not image intensities. A real implementation computes an energy from the pixels, removes the matching pixels, then recomputes the affected energy before another removal. This tutorial does only one seam.
The block assumes a nonempty rectangular grid with at least one column. Time is O(rows × columns); the prior-cost row is reused, but predecessor rows stay for backtracking.
Three practice challenges
Count an edge’s options
How many predecessor columns can the rightmost cell of a four-column grid use?
Show solution
Two: 3 and 4.
Check connectivity
Would columns 2, 4, 4, 4 form a valid seam?
Show solution
No. It jumps by two between the first and second rows.
Change the energy
Put a high cost in the currently selected first-row cell. What should you recompute?
Show solution
Re-run the cost accumulation and backtracking; changing one cost can change the best path through several rows.
Further reading
Avidan and Shamir’s original seam-carving paper defines content-aware image resizing and energy functions. This tutorial implements a small fixed-energy removal example.
Continue learning
Pathfinding | Flood Fill | First Point
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.