Masterclass: Graph Theory & The Six-Color Theorem
Welcome to class! Today, we are going to unravel a foundational result in Graph Theory: The Six-Color Theorem.
Imagine you are the Chief Royal Cartographer for a sprawling fantasy empire. The Emperor demands a new map of all provinces, but with one crucial condition: no two neighboring provinces that share a border can be painted the same color, or citizens might confuse where one territory ends and another begins.
Buying specialized ink is expensive, so the Emperor demands you use as few colors as possible. How many colors do you need to guarantee you'll never run out, no matter how complex the map gets?
By the end of this session, you will not only prove mathematically that six colors are always enough for any flat map, but you will also write a clean algorithm to compute this coloring programmatically.
1. Setting the Stage: Maps, Graphs, and Rules
To tackle this problem like a computer scientist, we must abstract away the geographic fluff (mountains, rivers, coastline squiggles) and distill the map down to pure structural relationships.
Step 1: Translate the Map to a Graph
- Provinces become Vertices (nodes).
- Shared Borders become Edges (connections).
Because a traditional world map is drawn flat on a sheet of paper without borders overlapping via "3D bridges" or tunnels, its corresponding graph is Planar.
A graph is Planar if it can be drawn on a 2D plane such that no two edges cross each other.
graph TD
%% Planar Graph Representation of 4 neighboring regions
subgraph Map Topology
A[Region A] --- B[Region B]
A --- C[Region C]
B --- C
B --- D[Region D]
C --- D
end
Step 2: Define Proper Graph Coloring
Our goal is proper vertex coloring: assign a color to every single vertex such that no two connected vertices share the same color.
The absolute minimum number of colors needed to properly color a graph is called its Chromatic Number, denoted as .
The Six-Color Theorem Statement: If is a simple planar graph, then .
In plain English: No matter how convoluted your flat map is, you will never need more than six colors.
2. The Secret Weapon: The "Degree " Lemma
To prove this theorem, we need a secret mathematical weapon.
In graph theory, the Degree of a vertex is simply the total number of edges connected to it (i.e., its direct neighbors).
Here is a bold claim: Every simple planar graph has at least one vertex with a degree of 5 or less.
Why must this be true? Let's prove it rigorously using basic geometry and algebra.
The Math Behind the Secret Weapon
For any connected planar graph with vertices, edges, and faces (the enclosed regions created by edges), Euler's Formula tells us:
Now, think about the boundary of each face. Every face is bounded by at least 3 edges. Furthermore, each edge borders exactly 2 faces. This gives us the inequality:
Substitute this face inequality back into Euler's Formula:
This is a critical property of planar graphs: The total number of edges is always strictly bounded by roughly three times the number of vertices.
Now let's compute the Average Degree of all vertices in our graph. The sum of all degrees in a graph equals (since every edge connects two endpoints):
Substitute our upper bound for :
Because is positive, is strictly less than 6!
The Conclusion: If the average degree of all vertices in a planar graph is strictly less than 6, it is mathematically impossible for every single vertex to have a degree of 6 or higher. Therefore, at least one vertex MUST have a degree of 5 or less.
3. The Grand Proof: Mathematical Induction
Now that we possess our secret weapon, we can prove the Six-Color Theorem using Mathematical Induction (the ultimate domino effect of computer science proofs).
Analogy: The Stack of Plates
Imagine building a stack of plates by removing vertices one by one until the graph is empty, and then rebuilding the graph while applying colors step by step.
stateDiagram-v2
[*] --> OriginalGraph: Start with Planar Graph G
OriginalGraph --> ShrinkGraph: Find vertex 'v' with deg(v) <= 5 and remove it
ShrinkGraph --> OriginalGraph: Repeat until graph has <= 6 vertices
ShrinkGraph --> BaseCase: Graph has <= 6 vertices
BaseCase --> ColorStack: Color base case with distinct colors
ColorStack --> Rebuild: Pop 'v' back into the graph
Rebuild --> ColorAssignment: Assign 'v' a color NOT used by its (<= 5) neighbors
ColorAssignment --> Rebuild: Repeat until full graph restored
ColorAssignment --> [*]: Graph Successfully 6-Colored!
The Proof Steps
Base Case: If a planar graph has 6 or fewer vertices, the theorem holds trivially. You can simply assign a unique color to each vertex using at most 6 colors.
Inductive Hypothesis: Assume that all simple planar graphs with vertices can be properly colored using at most 6 colors.
Inductive Step: Consider a planar graph with vertices.
- By our Lemma, must contain at least one vertex with .
- Temporarily remove and all its connected edges from . This leaves a smaller graph with vertices.
- By our Inductive Hypothesis, can be properly colored using at most 6 colors.
- Now, place back into the graph!
- Since is connected to at most 5 neighbors, those neighbors can consume at most 5 distinct colors.
- We have 6 colors in our palette. Therefore, there is always at least one color left over that is not used by any of 's neighbors.
- Assign that remaining color to .
By induction, every simple planar graph can be colored with 6 colors or fewer.
4. Algorithmic Implementation
Let's convert this mathematical proof directly into executable Python code.
We will model the reduction process using a Stack Data Structure. We iteratively find vertices with degree , pop them off the graph into our stack, and then pop them back out to assign colors safely.
class PlanarGraph: def __init__(self, num_vertices): self.V = num_vertices # Adjacency list: map vertex ID to set of neighboring vertex IDs self.adj = {i: set() for i in range(num_vertices)} def add_edge(self, u, v): self.adj[u].add(v) self.adj[v].add(u) def six_color(self): # Palette of available colors (indexed 0 to 5) PALETTE = [0, 1, 2, 3, 4, 5] # Keep track of working degrees and active vertices active_vertices = set(range(self.V)) adj_copy = {v: set(neighbors) for v, neighbors in self.adj.items()} # Stack to record vertex removal order reduction_stack = [] # STEP 1: Deconstruct graph by removing vertices of degree <= 5 while active_vertices: # Find a vertex with degree <= 5 among active vertices deg_less_than_6_vertex = None for v in active_vertices: if len(adj_copy[v]) <= 5: deg_less_than_6_vertex = v break if deg_less_than_6_vertex is None: raise ValueError("Graph is not planar! No vertex with degree <= 5 found.") # Pop vertex from active set and push to stack v = deg_less_than_6_vertex active_vertices.remove(v) reduction_stack.append(v) # Remove 'v' from its neighbors' sets for neighbor in adj_copy[v]: adj_copy[neighbor].remove(v) adj_copy[v].clear() # STEP 2: Reconstruct and Color color_map = {} while reduction_stack: v = reduction_stack.pop() # Find colors used by already-colored neighbors neighbor_colors = set() for neighbor in self.adj[v]: if neighbor in color_map: neighbor_colors.add(color_map[neighbor]) # Pick the first color from PALETTE not used by any neighbor for color in PALETTE: if color not in neighbor_colors: color_map[v] = color break return color_map# --- Example Usage ---if __name__ == "__main__": # Create a small planar graph g = PlanarGraph(7) edges = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (5, 6), (4, 6)] for u, v in edges: g.add_edge(u, v) coloring = g.six_color() print("Vertex Color Assignment:") for vertex, color in sorted(coloring.items()): print(f"Vertex {vertex} -> Color {color}")Time Complexity Breakdown
- Finding degree vertices: Using an efficient queue or bucket array, we can find and eliminate degree vertices in time.
- Coloring Phase: Popping vertices and checking up to 5 neighbors takes operations.
- Total Time Complexity:, which is linear time!
5. Summary & What Comes Next
You have successfully walked the path from a practical map-coloring problem to a full linear-time algorithm:
- Abstraction: Maps become Planar Graphs where edges do not cross.
- Euler's Identity (): Proves that edge density is capped ().
- The Key Lemma: Every simple planar graph has a vertex with degree 5 or less.
- Induction & Recursion: Removing and re-inserting that low-degree vertex guarantees we never run out of options from a 6-color palette.
Food for Thought: Can we do better than 6 colors? You might have heard of the famous Four-Color Theorem, which states that any planar graph can actually be colored with just 4 colors. However, proving the 4-color theorem requires checking over 1,500 complex configuration cases using computers! The 6-Color Theorem, on the other hand, gives us an elegant, human-understandable proof using pure computer science fundamentals.