s LearnSqueak.com

SMALLTALK LAB

Find Graph Bridges in Squeak: Which Edge Disconnects Its Endpoints?

Build a bridge finder for small undirected graphs. Remove each edge in turn and test whether its endpoints can still reach each other, even when the graph starts out disconnected.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Squeak Workspace. An edge is a two-element array of its vertex symbols, and its position identifies that edge. Use Print it on each entire checkpoint, including its temporary 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 - Model a triangle with a tail

The three sides of the triangle supply alternate routes. The fourth edge connects the triangle to D; we suspect it is a bridge. Each edge is undirected, so (C D) permits travel both ways.

Independent Workspace expression - select all, then Print it
| edges tail |
edges := #((A B) (B C) (C A) (C D)).
tail := edges at: 4.
{tail first. tail second}

Expected: #(C D)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Model a triangle with a tail
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Look for a different route from A to B without using the first edge.

Download this checkpoint

02 - Grow a set of reachable vertices

One pass finds B from A, but the route from B to C appeared earlier in the array. To make the result independent of edge order, keep scanning until a whole pass discovers no new vertex.

Independent Workspace expression - select all, then Print it
| edges seen |
edges := #((B C) (A B)).
seen := Set with: #A.
edges do: [:edge |
 (seen includes: edge first) ifTrue: [seen add: edge second].
 (seen includes: edge second) ifTrue: [seen add: edge first]].
{seen includes: #B. seen includes: #C}

Expected: #(true false)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Grow a set of reachable vertices
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Swap the edge order and run again. Now both answers should be true.

Download this checkpoint

03 - Skip one edge during the search

With edge 1 removed, A reaches B through C. With edge 4 removed, C cannot reach D. The set only grows; once a pass adds no vertex, all remaining edges incident to reachable vertices have been accounted for.

Independent Workspace expression - select all, then Print it
| reachable edges |
reachable := [:edgeList :start :skip | | seen before |
 seen := Set with: start.
 [
  before := seen size.
  edgeList withIndexDo: [:edge :index |
   index = skip ifFalse: [
    (seen includes: edge first) ifTrue: [seen add: edge second].
    (seen includes: edge second) ifTrue: [seen add: edge first]]].
  seen size > before
 ] whileTrue.
 seen].
edges := #((A B) (B C) (C A) (C D)).
{(reachable value: edges value: #A value: 1) includes: #B.
 (reachable value: edges value: #C value: 4) includes: #D}

Expected: #(true false)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Skip one edge during the search
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Repeat the first check with edge 2 excluded and start at B. Can B still get to C?

Download this checkpoint

04 - Return the indices of the bridges

For each edge, start at one endpoint and search without that edge. If the other endpoint is missing from the reachable set, the deletion separates its original component. If the endpoints are still connected, any old route using the edge can use that alternate route instead.

Independent Workspace expression - select all, then Print it
| reachable bridges |
reachable := [:edgeList :start :skip | | seen before |
 seen := Set with: start.
 [
  before := seen size.
  edgeList withIndexDo: [:edge :index |
   index = skip ifFalse: [
    (seen includes: edge first) ifTrue: [seen add: edge second].
    (seen includes: edge second) ifTrue: [seen add: edge first]]].
  seen size > before
 ] whileTrue.
 seen].
bridges := [:edges |
 (1 to: edges size) select: [:i | | edge |
  edge := edges at: i.
  ((reachable value: edges value: edge first value: i)
    includes: edge second) not]].
(bridges value: #((A B) (B C) (C A) (C D))) asArray

Expected: #(4)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Return the indices of the bridges
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Add (D A) to close a second cycle. There should no longer be a bridge.

Download this checkpoint

05 - Test graphs that start disconnected or empty

A graph does not have to start out connected. In the first case no triangle edge is a bridge, but removing X–Y splits its own component. The empty graph has no candidate edges. Vertices already isolated need not appear in this edge list to determine which edges are bridges.

Independent Workspace expression - select all, then Print it
| reachable bridges |
reachable := [:edgeList :start :skip | | seen before |
 seen := Set with: start.
 [
  before := seen size.
  edgeList withIndexDo: [:edge :index |
   index = skip ifFalse: [
    (seen includes: edge first) ifTrue: [seen add: edge second].
    (seen includes: edge second) ifTrue: [seen add: edge first]]].
  seen size > before
 ] whileTrue.
 seen].
bridges := [:edges |
 (1 to: edges size) select: [:i | | edge |
  edge := edges at: i.
  ((reachable value: edges value: edge first value: i)
    includes: edge second) not]].
{(bridges value: #((A B) (B C) (C A) (X Y))) asArray.
 (bridges value: #()) asArray}

Expected: #(#(4) #())

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Test graphs that start disconnected or empty
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Add a separate two-edge path. Both of those new edges should be bridges.

Download this checkpoint

06 - Keep parallel edges distinct

The two A–B entries are separate routes. Skipping one index leaves the other intact. The B–C edge is the only bridge. C–C is a loop, and a vertex can already reach itself without traversing an edge.

Independent Workspace expression - select all, then Print it
| reachable bridges |
reachable := [:edgeList :start :skip | | seen before |
 seen := Set with: start.
 [
  before := seen size.
  edgeList withIndexDo: [:edge :index |
   index = skip ifFalse: [
    (seen includes: edge first) ifTrue: [seen add: edge second].
    (seen includes: edge second) ifTrue: [seen add: edge first]]].
  seen size > before
 ] whileTrue.
 seen].
bridges := [:edges |
 (1 to: edges size) select: [:i | | edge |
  edge := edges at: i.
  ((reachable value: edges value: edge first value: i)
    includes: edge second) not]].
(bridges value: #((A B) (A B) (B C) (C C))) asArray

Expected: #(3)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Keep parallel edges distinct
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: What happens if you start over with only one copy of (A B)? Both A–B and B–C should now be bridges.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Find Graph Bridges in Squeak: Which Edge Disconnects Its Endpoints? native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

Skip by index rather than deleting every matching pair. Otherwise parallel edges disappear together, which can turn an ordinary edge into a false bridge. Comparing endpoint reachability also avoids assuming the graph has exactly one component.

A cycle detector answers whether any cycle exists; this program identifies which individual edges are on no cycle in a simple undirected graph. Kruskal chooses a minimum-weight spanning forest; an edge selected by Kruskal is not automatically a bridge in the original graph.

This version trades speed for readable code. With V vertices and E edges it can make up to V full edge scans for each of E candidates, for O(V × E²) time in the worst case assuming typical constant-time Set operations. For large graphs, build adjacency lists and consider a depth-first low-link bridge algorithm instead.

Three practice challenges

Turn the tail into a cycle

Start with triangle A–B–C–A and a tail C–D–E. Add E–C. Which edge indices are bridges now?

Show solution

None. Every edge belongs to one of the two triangles.

Compare two components

Take edge list #((A B) (B C) (X Y) (Y Z) (Z X)). Which indices should be returned?

Show solution

1 and 2. The first component is a path with no alternative routes; the second is a triangle.

Reverse an edge

Replace (B C) with (C B) in a three-vertex path. Does the bridge result change?

Show solution

No. Our scan follows both directions, so both edges are still bridges.

Continue learning

Cycle Detection | Kruskal Spanning Tree | Bipartite Graphs

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.