DSA Visualizer
GRID

Boundary Traversal

Top row, right column, bottom row backwards, left column upwards. The cells inside the ring are never touched.

The outer ring only: top, right, bottom, left. Inner cells are never touched.

0
1
2
3
0
1
2
3
4
1
5
6
7
8
2
9
10
11
12
Visited
Press play to start
Boundary order walks the outer ring only: top row, right column, bottom row backwards, left column upwards.
0
VisitedVisited
10
LeftRemaining
10
TotalTotal
1 / 12

Pseudocode

Boundary
1for j in 0 .. cols-1: visit(a[0][j])
2for i in 1 .. rows-1: visit(a[i][cols-1])
3if rows > 1: for j in cols-2 .. 0: visit(a[rows-1][j])
4if cols > 1: for i in rows-2 .. 1: visit(a[i][0])

Legend

Visiting nowNew directionVisited

Grid size

Want your own values? Edit this grid in the 2D Array Visualizer →

Tip: Space play/pause · ← → step

Learn boundary traversal

Boundary traversal visits only the outer ring of the grid: across the top row, down the right column, back along the bottom row and up the left column. The cells inside the ring are never touched, so it takes far fewer steps than a full traversal.

A traversal is just a rule for the order in which you visit cells. The grid does not change; only the path through it does. Compare it with row-major, column-major, snake, diagonal and spiral.

Every full traversal costs one step per cell, rows × cols, which is written O(rows · cols). Boundary is the exception: it visits only the outer ring.

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:

1 → 2 → 3 → 4 → 8 → 12 → 11 → 10 → 9 → 5

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 if guards 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.