How boundary traversal works
Trace the frame of a picture with your finger: along the top edge, down the right side, back along the bottom, up the left side to where you started. Boundary traversal does that on a grid. It touches only the outer ring of cells and leaves everything inside alone.
That makes it different from every other order on this site: it is not a way to visit all the cells. On a 3 × 4 grid it visits 10 of 12; on a 10 × 10 grid, 36 of 100. The counters in the visualizer show the total it is aiming for, not the size of the grid.
The whole difficulty is the corners. Each one belongs to two edges, and a careless version prints them twice. The trick is to give each edge one corner: the top row takes both of its corners, the right column starts one below, the bottom row starts one to the left, and the left column stops one above where the top row began.
The loop shape
Four loops in a row, no nesting. Top row: j from 0 to cols − 1 along row 0. Right column: i from 1 to rows − 1 down column cols − 1. Bottom row: j from cols − 2 down to 0. Left column: i from rows − 2 down to 1.
The last two loops are wrapped in if rows > 1 and if cols > 1. Set the sliders to one row and watch why: without the guard, the “bottom row” would be the top row read backwards.
A worked example: a 3 × 4 grid
With the cells numbered 1 to 12, the ring reads clockwise from the top-left corner:
Ten cells: 4 along the top, 2 down the right side, 3 back along the bottom, 1 up the left. The 6 and 7 in the middle are never visited and stay grey in the picture. Check the arithmetic: 2 × 3 + 2 × 4 − 4 = 10.
Where you'll meet it
- Borders and frames. Drawing a border around an image or a table, or checking that a board's edge is walls.
- Spiral traversal. The boundary is one ring of the spiral; get this right and the spiral follows.
- Grid problems. Flood fill from the edges, “cells that can reach the border”, and island-counting variants all start by walking the boundary.
- Interviews. “Print the boundary elements of a matrix” is a common warm-up precisely because of the corner and single-row traps.
Mistakes beginners make
- Printing the corners twice. Each edge must skip the corner the previous edge already took.
- Forgetting the single-row and single-column cases. The two
ifguards are not optional. - Counting rows × cols. The number of boundary cells is
2r + 2c − 4, so a loop that expects every cell will never finish.
Now keep going inwards until nothing is left: Spiral traversal →
Boundary traversal questions
What is boundary traversal of a matrix?
Visiting only the cells on the outer edge of a 2D array, usually clockwise: the whole top row, the right column below it, the bottom row from right to left, and the left column from bottom to top, stopping before the cell you started on.
How many cells are on the boundary?
2 × rows + 2 × cols − 4 when the grid has at least two rows and two columns, because the four corners would otherwise be counted twice. A 3 × 4 grid has 10 boundary cells. A single row or single column is all boundary, so the count is simply rows × cols.
Why do single-row and single-column grids need special care?
Because the bottom row is the top row and the left column is the right column. Without the two if checks, a 1 × 4 grid would be printed forwards and then backwards, visiting every cell twice.
How is boundary traversal related to spiral traversal?
The boundary is the first ring of a spiral. Spiral traversal walks the boundary, shrinks the four bounds by one, and walks the boundary of what is left, again and again. If you can write the boundary correctly, the spiral is a loop around it.