Skip to content
Vamshi Jandhyala

Books · Nikoli

Chapter 7

Flow Free

Flow Free is the puzzle of disjoint paths. The grid holds a set of coloured dots paired up by colour: two yellows, two blues, two reds. The task is to draw, for each colour, an unbroken path through orthogonally adjacent grid cells joining its two dots, with the strict additional rule that every cell of the grid must lie on exactly one path. No path crosses itself, no two paths share a cell, no cell is left blank.

The puzzle is sold under the trademark Flow Free by Big Duck Games (20122012), but its lineage is older. Nikoli published essentially the same puzzle for years under the name Arukone (a contraction of aru “some” and kone “connection”), itself a relative of the long-standing Number Link puzzles by Walter Mott in 18971897. The polynomial-time complexity of the two-colour version is classical; with three or more colours the puzzle is NP-complete (Adcock et al., 20152015), making it the first in this book to be NP-complete by basic combinatorics rather than by sheer combinatorial scale.

The model in this chapter is, despite the complexity of the puzzle, strikingly compact: a colour-indicator variable per cell-colour pair, plus a degree constraint per cell-colour saying that endpoints have one same-colour neighbour and interior cells have two. The trick that makes it work is the strict “every cell must be filled” clause. Without it, paths could short-circuit through empty cells and the model would need a connectivity layer; with it, the degree constraints alone almost suffice, and a well-formed puzzle resolves uniquely.

Rules and a small instance

A Flow Free puzzle is an n×nn \times n grid with KK colours. Each colour k{1,,K}k \in \{1, \ldots, K\} is assigned exactly two cells, its endpoints; remaining cells are unmarked.

  1. Each unmarked cell receives exactly one colour from {1,,K}\{1, \ldots, K\}.

  2. For each colour kk, the cells coloured kk form a single path between the two endpoints of kk. Equivalently: each endpoint of kk has exactly one orthogonal neighbour of colour kk, and every other cell of colour kk has exactly two.

  3. Every cell is covered by some colour (the fill property).

Figure 7.1 is a 5×55 \times 5 Flow Free with four colours. The endpoints are drawn as small filled disks labelled with the colour number; the puzzle is to thread a path of each colour from dot to dot, covering every cell exactly once.

A 5×55 \times 5 Flow Free with four colours. Endpoint dots show the cells to connect; every empty cell must end up on exactly one colour’s path.
The unique solution. Each path connects its two endpoints; together they cover all twenty-five cells.

The programming model

For each cell (r,c)(r, c) and each colour kk, introduce a Boolean variable xr,c,k{0,1}x_{r,c,k} \in \{0, 1\} that is 11 iff cell (r,c)(r, c) is coloured kk. The model has three families of constraints.

  1. Single colour per cell. For each cell, kxr,c,k=1\sum_{k} x_{r,c,k} = 1.

  2. Endpoints fixed. For each pair of endpoints {e1,e2}\{e_1, e_2\} of colour kk, set xe1,k=1x_{e_1, k} = 1 and xe2,k=1x_{e_2, k} = 1.

  3. Degree. For each cell (r,c)(r, c) and each colour kk, if xr,c,k=1x_{r,c,k} = 1 then the count of kk-coloured orthogonal neighbours equals 11 (when (r,c)(r, c) is an endpoint of colour kk) or 22 (otherwise).

The degree constraint is reified: (r,c)(r,c)xr,c,k=tr,c,k\sum_{(r', c') \sim (r, c)} x_{r', c', k} = t_{r,c,k} holds only when xr,c,k=1x_{r,c,k} = 1, where tr,c,k{1,2}t_{r,c,k} \in \{1, 2\} depending on endpoint status. CP-SAT expresses this with OnlyEnforceIf; no further machinery is needed.

A note on connectivity

The degree-and-fill conditions are necessary for a valid solution but not strictly sufficient: a colour could in principle form a path between its endpoints plus a separate disjoint cycle, and every cell would still have the right degree. In practice, a well-formed Flow Free puzzle has a unique grid-filling solution and the unique solution has no separate cycles; the degree model returns it. For adversarial inputs one would add a connectivity constraint (a multi-commodity flow on the cell graph, or a position-along-the-path variable for each cell). For every puzzle in this chapter the degree model alone suffices, and we verify the result by checking each colour’s induced subgraph is connected.

A solver in twenty lines

from ortools.sat.python import cp_model

def nbrs(r, c, n):
    for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
        if 0 <= r+dr < n and 0 <= c+dc < n:
            yield (r+dr, c+dc)

def solve_flow(n, eps):
    """eps: dict {colour: [(r1, c1), (r2, c2)]}."""
    K = sorted(eps)
    m = cp_model.CpModel()
    x = {(r, c, k): m.NewBoolVar("")
         for r in range(n) for c in range(n) for k in K}
    for r in range(n):
        for c in range(n):
            m.Add(sum(x[(r, c, k)] for k in K) == 1)
            for k in K:
                if (r, c) in eps[k]:
                    m.Add(x[(r, c, k)] == 1)
    for r in range(n):
        for c in range(n):
            for k in K:
                same = [x[(nr, nc, k)]
                        for nr, nc in nbrs(r, c, n)]
                t = 1 if (r, c) in eps[k] else 2
                m.Add(sum(same) == t).OnlyEnforceIf(
                    x[(r, c, k)])
    s = cp_model.CpSolver()
    s.Solve(m)
    return [[next(k for k in K
                  if s.Value(x[(r, c, k)]))
             for c in range(n)] for r in range(n)]

The toy solves in about 55 milliseconds.

A larger instance: Arukone 300

Figure 7.3 is puzzle 300300 from the janko.at Arukone archive, a 15×1515 \times 15 instance by ARA3 with ten colour pairs. Two of the ten pairs sit close together (the green pair occupies cells (2,4)(2, 4) and (2,10)(2, 10), on the same row); the remaining eight thread across the grid. The same solver code, called on this instance, returns the unique completion in about 9090 milliseconds.

Arukone 300300 from janko.at (ARA3), 15×1515 \times 15 with ten colour pairs.
The unique solution. Path 99 snakes around the border, while 44 and 11 make long internal detours.

Sources. The Numberlink puzzle was first published by Walter Mott in Tit-Bits magazine in 18971897; Nikoli rebranded a version of it as Arukone in the early 19801980s. The Flow Free mobile game by Big Duck Games (20122012) revived the puzzle in its strict-fill form. Adcock, Demaine, Demaine, O’Brien, Reidl and Sullivan, Zig-zag Numberlink is NP-complete, Journal of Information Processing 2323 (20152015), 239239245245, established NP-completeness for the version with three or more colour pairs and the all-cells-covered constraint. The hard puzzle in Figure 7.3 is Arukone 300300 from janko.at, attributed to ARA3 (numberlink.ara3.net).