liveBook · Manning

Backtracking

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

Backtracking search explores candidate solutions incrementally and reverses decisions when a partial candidate cannot lead to a solution.

Backtracking search explores one decision at a time, checks whether the partial assignment remains valid, and returns to the last decision point when the path fails. It is recursive depth-first search applied to spaces of candidate solutions. section 3.1

Permuting cities for the traveling salesman problem

A Swift permutation helper swaps an element into a fixed position, recursively explores the remaining positions, then swaps back. Applying it to five Vermont cities generates every route; closing each route into a cycle and summing dictionary-based distances produces a shortest path of 318 miles. section 8.2

Figure 3.1. Scheduling problems are a classic application of constraint-satisfaction frameworks.

Use the scheduling diagram to see how variables, domains, and constraints form a classic backtracking search problem. section 3.0

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Explains recursive state, variable selection, consistency checks, and restoration, while deferring performance heuristics beyond the simple search.
Examples
Demonstrates meeting scheduling, generic constraint frameworks, array permutations, and five-city Vermont routes.
What you can do after
Build it from scratch with Swift functions and generic types for constraint solving, permutation generation, and route evaluation.
How it connects
Connects constraint satisfaction, depth-first search, recursion, permutations, and the traveling salesman problem.

Also explained in

Compare how 6 books explain backtracking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.