AlgoViz

Matrix DFS

medium 20 min

A 2D grid is a graph in disguise: every cell is a node and its up, down, left and right neighbours are its edges.

Where it shows up

The same pattern appears across everyday software.

  • Paint bucket in image editors

    The fill tool in Paint, Photoshop or GIMP is flood fill: start at the clicked pixel and recolour every connected pixel of the same (or similar, within a tolerance) colour. Production versions use an explicit stack or scanline filling, because a large image would overflow the call stack.

  • Connected component labelling in vision

    Medical imaging, OCR and satellite analysis threshold an image to black and white, then label each connected blob of pixels: count the cells on a slide, the letters on a page, the lakes on a map. It is number of islands on millions of pixels, with 4- or 8-connectivity chosen per task.

  • Flood and terrain modelling

    GIS tools find which areas a rising river reaches by filling outward from the breach across cells lower than the water level, with ridges and levees as walls. Hydrology software uses the same idea to delineate watersheds on elevation grids.

  • Game maps and rules

    Minesweeper reveals a region of empty squares with a flood fill (8-directional). Go removes a group of stones when DFS over same-coloured neighbours finds no empty point. Strategy games check which tiles a unit can reach, and level designers verify every room in a generated dungeon is connected.

  • Robots and maze solving

    A warehouse robot on a grid map asks whether its target shelf is reachable at all before planning a route; a DFS reachability check answers that cheaply. Micromouse maze robots and puzzle solvers explore depth-first with backtracking.

  • Circuit board and chip checks

    Design-rule checkers treat a PCB or chip layout as a grid of copper and gaps and flood fill each net to confirm that pins meant to be connected are, and that no two nets accidentally touch.