liveBook · Manning

Chapter 8: Coloring graphs with backtracking search

What you will learn in chapter 8 of Fabulous Adventures in Data Structures and Algorithms

Introduces graph coloring as an adjacency-constrained assignment problem and previews using immutable data structures and backtracking search to solve it and related problems.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces graph coloring as an adjacency-constrained assignment problem and previews using immutable data structures and backtracking search to solve it and related problems.

Key terms: graph coloring, node, edge, adjacent nodes, vertex, backtracking search algorithm, guess, solution, general-purpose engine, immutable data structures

8.1 Coloring South America

This section introduces map coloring, explains why real maps can need extra constraints, and models the problem with an immutable undirected graph.

Key terms: map coloring, adjacent regions, sharing a border, four-color theorem, plane, sphere, noncontiguous political regions, French Guiana, abstract data type, immutable undirected graph

8.2 An immutable multidictionary

This section chooses and implements an immutable multidictionary as the adjacency representation needed for immutable graphs.

Key terms: incidence matrix, adjacency matrix, adjacency dictionary, multidictionary, immutable graph, ImmutableDictionary, immutable hash set, System.Collections.Immutable, IsEmpty, ContainsKey

8.3 An immutable undirected graph

This section defines an immutable undirected graph as a graph-oriented wrapper around an immutable multidictionary that enforces graph invariants and supports nodes, edges, and cliques.

Key terms: ImGraph<N>, ImMulti<N,N>, invariants, nodes, edges, adjacency matrix, node, edge, adjacent nodes, adjacency set

8.4 Coloring simple graphs

This section develops a recursive graph-coloring algorithm that removes low-degree nodes, colors the remaining graph, and restores the nodes with available colors.

Key terms: graph coloring, adjacent nodes, degree, degree-zero node, empty graph, base case, degree less than c, available color, immutable dictionary, O(n+e)

8.5 Solving Sudokus with backtracking search

This section models Sudoku as graph coloring and introduces recursive backtracking as the stronger search needed when simple graph coloring cannot proceed.

Key terms: graph coloring, nodes, edges, colors, Sudoku, clique, degree 20, nine colors, simple coloring algorithm, backtracking

8.5.1 Graph coloring is NP-complete

This section defines NP-completeness, shows why graph coloring belongs to that class, and explains why a complete backtracking solver has exponential worst-case time.

Key terms: polynomial performance, exponential, O(n^c), O(c^n), NP-complete, polynomial time, yes-or-no questions, verification, complete, graph coloring

8.5.2 Implementing a general backtracker

This section builds a reusable backtracking solver by representing possible assignments as attempts, reducing them without guesses, and recursively exploring the remaining choices to solve Sudoku.

Key terms: attempt, multidictionary, solution, broken attempt, reducer, ReduceOnce, Reduce, possible colors, neighbors, backtracker

8.5.3 How could we improve?

This section identifies several ways a graph-coloring reducer could prune Sudoku search faster and explains why richer Sudoku-specific deductions can greatly reduce backtracking.

Key terms: reducer, broken attempt, remaining possible colors, backtracker, ReduceOnce, single colors, adjacent nodes, reductions, clique, available colors

8.5.4 Backtracking and the Cartesian product

This section reframes backtracking as Cartesian-product enumeration that prunes impossible partial solutions early through reduction.

Key terms: Cartesian product, SelectMany, backtracking search, possible solutions, reduction step, constraints, broken attempts, space of possible solutions

8.6 Scheduling problems are graph-coloring problems

This section shows how real scheduling and register-allocation constraints can be modeled and solved as graph-coloring problems.

Key terms: graph coloring, nodes, adjacent, colors, scheduling problems, gates, airplanes, classrooms, time slots, adjacencies

8.7 Summary

This section recaps graph coloring, recursive and backtracking approaches to solving it, and the problem's general computational difficulty.

Where this is used later

Concepts explained in this chapter

← Chapter 7: First abstract nonsense interlude: Category theory Chapter 9: Greedy iterative pretty printing →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.