SMALLTALK LAB
PageRank in Squeak: Follow Probability Through a Tiny Web
Turn four linked pages into a probability experiment. Repair dangling nodes, add teleportation, and watch iteration settle while checking every unit of probability.
Before you begin
Open a Squeak Workspace. Select one complete checkpoint, including its temporary declarations, and choose Print it. All eight expressions are independent and need no packages or network access. Pages are numbered 1 through 4. Outputs use integers in ten-thousandths or Boolean tolerance checks so small Float formatting differences do not affect verification.
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 links, not scores
Our four pages are A, B, C, D, stored at positions 1 through 4. A links to B and C; B links to C; C links to A. D has no outgoing links and is called dangling. These arrays are outgoing adjacency lists, not incoming neighbors. Every target is a valid one-based page index. Treat links as unweighted and unique within each list.
| links |
links := #(#(2 3) #(3) #(1) #()).
{links collect: [:outgoing | outgoing size].
(1 to: links size) select: [:page | (links at: page) isEmpty]}Expected: #(#(2 1 1 0) #(4))

Try it: Trace A to C using array positions. D exists even though nobody links to it.
02 - Start with one unit of probability
PageRank models the location of a random surfer after many moves, not the count of incoming links. Begin with equal probability on all four pages. Here 1 / 4 is exact rational arithmetic. A normalized probability vector is nonnegative and sums to one. Equal starting ranks do not mean the final ranks must be equal.
| ranks |
ranks := Array new: 4 withAll: 1 / 4.
{ranks collect: [:amount | (amount * 10000) rounded]. ranks sum = 1}Expected: #(#(2500 2500 2500 2500) true)

Try it: The displayed integers are ten-thousandths, not percentages. Why does every page start at 2500?
03 - Redistribute a dangling page
A surfer on D has no link to follow. Replace its missing transition with a uniform choice among all four pages, including D itself. Without this repair, one quarter of the initial mass disappears. The undamped link-following step below returns exact fractions internally. The result is the repaired transition before teleportation.
| links ranks next |
links := #(#(2 3) #(3) #(1) #()).
ranks := Array new: 4 withAll: 1 / 4.
next := Array new: 4 withAll: 0.
1 to: 4 do: [:source | | targets share |
targets := links at: source.
targets isEmpty ifTrue: [targets := #(1 2 3 4)].
share := (ranks at: source) / targets size.
targets do: [:target | next at: target put: (next at: target) + share]].
{next collect: [:amount | (amount * 10000) rounded]. next sum = 1}Expected: #(#(3125 1875 4375 625) true)

Try it: D contributes 1/16 to every page. Explain the other 3/8 received by C.
04 - Mix links with teleportation
With damping d = 0.85, follow the repaired link transition with probability 0.85 and choose a uniform page with probability 0.15. Each page gets a teleportation base of 0.15/4. Use 17/20 here to make the calculation exact; later iterations use Floats. The complete update is new[j] = (1-d)/N + d*danglingMass/N + the incoming link shares.
| repaired damping next |
repaired := {5 / 16. 3 / 16. 7 / 16. 1 / 16}.
damping := 17 / 20.
next := repaired collect: [:amount |
((1 - damping) / 4) + (damping * amount)].
{next collect: [:amount | (amount * 10000) rounded]. next sum = 1}Expected: #(#(3031 1969 4094 906) true)

Try it: The rounded display adds to 10000 here, but independently rounded entries need not always do so. Test mass before rounding.
05 - Iterate with separate old and new arrays
The advance block performs a complete synchronous update. It reads only old and writes only fresh, so array order cannot let a page forward newly received probability in the same round. Dangling mass is gathered first and distributed with the base. Two rounds already change the apparent leader: A overtakes C temporarily. One round is not a ranking at equilibrium.
| links damping ranks advance |
links := #(#(2 3) #(3) #(1) #()).
damping := 0.85.
ranks := Array new: 4 withAll: 0.25.
advance := [:old | | fresh dangling |
dangling := 0.0.
1 to: links size do: [:source |
(links at: source) isEmpty ifTrue: [
dangling := dangling + (old at: source)]].
fresh := Array new: links size
withAll: ((1.0 - damping) + (damping * dangling)) / links size.
1 to: links size do: [:source | | outgoing share |
outgoing := links at: source.
outgoing isEmpty ifFalse: [
share := damping * (old at: source) / outgoing size.
outgoing do: [:target |
fresh at: target put: (fresh at: target) + share]]].
fresh].
2 timesRepeat: [ranks := advance value: ranks].
ranks collect: [:amount | (amount * 10000) rounded]Expected: #(4047 1856 3529 568)

Try it: Compare the first two rounds. An oscillation in which page leads does not by itself mean the algorithm diverges.
06 - Audit mass at every iteration
Floating-point sums can differ from one by tiny rounding errors, so test within a tolerance instead of comparing floats for equality. This audit checks all 100 completed rounds, not only the last one. A normalized start and a stochastic transition preserve total probability mathematically. Do not routinely renormalize a broken implementation: doing so can conceal a missing dangling contribution.
| links damping ranks advance allValid |
links := #(#(2 3) #(3) #(1) #()).
damping := 0.85.
ranks := Array new: 4 withAll: 0.25.
advance := [:old | | fresh dangling |
dangling := 0.0.
1 to: links size do: [:source |
(links at: source) isEmpty ifTrue: [
dangling := dangling + (old at: source)]].
fresh := Array new: links size
withAll: ((1.0 - damping) + (damping * dangling)) / links size.
1 to: links size do: [:source | | outgoing share |
outgoing := links at: source.
outgoing isEmpty ifFalse: [
share := damping * (old at: source) / outgoing size.
outgoing do: [:target |
fresh at: target put: (fresh at: target) + share]]].
fresh].
allValid := true.
100 timesRepeat: [
ranks := advance value: ranks.
allValid := allValid and: [
(ranks sum - 1.0) abs < 1.0e-12 and: [
ranks allSatisfy: [:amount | amount >= 0.0]]]].
{allValid. ranks collect: [:amount | (amount * 10000) rounded]}Expected: #(true #(3693 2046 3785 476))

Try it: Temporarily remove dangling from the base. The mass check should fail. Restore it rather than hiding the error with rescaling.
07 - Stop on movement, with a safety cap
Measure movement with the L1 distance: sum the absolute coordinate changes between successive vectors. Stop when it falls below 1e-10, or after 200 rounds to avoid an accidental infinite loop. For this fixed transition and 0 <= d < 1, uniform teleportation gives a unique stationary vector and contracts L1 distance by at most d. A small successive-step distance is evidence of convergence, not exact equality with the limit.
| links damping ranks advance next delta iterations |
links := #(#(2 3) #(3) #(1) #()).
damping := 0.85.
ranks := Array new: 4 withAll: 0.25.
advance := [:old | | fresh dangling |
dangling := 0.0.
1 to: links size do: [:source |
(links at: source) isEmpty ifTrue: [
dangling := dangling + (old at: source)]].
fresh := Array new: links size
withAll: ((1.0 - damping) + (damping * dangling)) / links size.
1 to: links size do: [:source | | outgoing share |
outgoing := links at: source.
outgoing isEmpty ifFalse: [
share := damping * (old at: source) / outgoing size.
outgoing do: [:target |
fresh at: target put: (fresh at: target) + share]]].
fresh].
delta := 1.0. iterations := 0.
[delta > 1.0e-10 and: [iterations < 200]] whileTrue: [
next := advance value: ranks.
delta := ((1 to: ranks size) collect: [:page |
((next at: page) - (ranks at: page)) abs]) sum.
ranks := next. iterations := iterations + 1].
{delta <= 1.0e-10. iterations < 200.
ranks collect: [:amount | (amount * 10000) rounded]}Expected: #(true true #(3693 2046 3785 476))

Try it: Do not use the rounded display as the stopping rule. Rounded ranks can look stable while their underlying probabilities still move.
08 - Check a stationary vector independently
The converged values should approximately reproduce themselves under one more update. Also test the boundary d = 0: links then have no influence, so even a highly uneven starting vector becomes uniform immediately. D retains positive rank without an incoming link because teleportation and dangling redistribution can reach it. PageRank measures this model of link prestige, not truth, quality, or an objective recommendation.
| links damping ranks advance residual uniform |
links := #(#(2 3) #(3) #(1) #()).
damping := 0.85.
ranks := Array new: 4 withAll: 0.25.
advance := [:old | | fresh dangling |
dangling := 0.0.
1 to: links size do: [:source |
(links at: source) isEmpty ifTrue: [
dangling := dangling + (old at: source)]].
fresh := Array new: links size
withAll: ((1.0 - damping) + (damping * dangling)) / links size.
1 to: links size do: [:source | | outgoing share |
outgoing := links at: source.
outgoing isEmpty ifFalse: [
share := damping * (old at: source) / outgoing size.
outgoing do: [:target |
fresh at: target put: (fresh at: target) + share]]].
fresh].
100 timesRepeat: [ranks := advance value: ranks].
residual := ((advance value: ranks) with: ranks collect: [:after :before |
(after - before) abs]) sum.
damping := 0.0.
uniform := advance value: #(1.0 0.0 0.0 0.0).
{residual < 1.0e-12.
uniform collect: [:amount | (amount * 10000) rounded].
(ranks at: 3) > (ranks at: 1)}Expected: #(true #(2500 2500 2500 2500) true)

Try it: Explain why a zero incoming-link count does not force rank zero. Why would damping = 1 remove the general convergence guarantee?
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
Outgoing adjacency lists describe where a page sends rank, not where it receives rank.
Divide a source contribution by its outgoing degree, not the target degree.
A dangling node must redistribute all its link-following mass; it is not deleted.
Keep old and new arrays separate. Updating ranks in place changes the algorithm.
Teleportation adds (1-d)/N per page, not (1-d) per page.
Do not test Float mass or convergence with exact equality, or round inside the iteration.
Keep 0 <= damping < 1 for the general unique-limit guarantee used here. Damping 1 can cycle or have multiple stationary distributions.
For larger graphs, keep adjacency lists sparse. One round costs O(N + E) time and O(N) additional storage, with graph storage O(N + E).
Three practice challenges
An all-dangling web
Replace all four link lists with empty arrays. Starting from #(1.0 0.0 0.0 0.0), what should one update return?
Show solution
Uniform #(0.25 0.25 0.25 0.25), or #(2500 2500 2500 2500) in the display. All mass is dangling, so the base is ((1-d) + d*1)/4 = 1/4. Never divide by a zero degree.
A two-page trap
Use A -> B and B -> A, start entirely at A, and remove teleportation with d = 1. What happens? What changes at d = 0.85?
Show solution
At d = 1 the vector alternates #(1 0) and #(0 1) forever. With d = 0.85, teleportation gives both pages positive probability each round and the alternating error shrinks toward #(0.5 0.5). Change the initialization and graph size together.
Bound the remaining error
If the L1 change between consecutive iterates is at most 1e-10 and d = 0.85, give an upper bound on the distance from the newest iterate to the exact stationary vector, ignoring floating-point error.
Show solution
Later changes are bounded by d*delta, d squared*delta, and so on. Their sum is d*delta/(1-d), at most about 5.67e-10. This is an L1 bound under the fixed stochastic transition assumption, not a claim that every printed digit is exact.
Continue learning
Probability Lab | Matrix Power | Pathfinding | Fractions
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.