Professional Documents
Culture Documents
algorithm
Andrew W. Appel Princeton University, 2016
f
That is, any nodes
connected by an edge e
must have different j b m
k
colors.
d c
h
g 2
Alfred B. Kempe, 1849-1922
In 1879, tried to prove the 4-color theorem:
every planar graph can be colored using at
most 4 colors.
5
From 6-coloring to 5-coloring
That was Kempe’s simplest algorithm, to 6-color
a planar graph; or in general, to K-color a graph
in class C, such that (1) every graph in class C has
a node of degree <K, and (2) removing a node
from a graph in class C gives you another graph
in class C.
j b m
k
d c
h
Stack:
12
Example: 3-color this graph
j b m
k
d c
h
g This node
has degree < 3 ;
remove it!
Stack:
13
Example: 3-color this graph
j b m
k
d c
h
g
Push node c on
Stack: c
the stack 14
Example: 3-color this graph
Removing c
lowers the degree f
of nodes b and m;
that will be helpful later!
e
j b m
k
d c
h
Stack: c
15
Example: 3-color this graph
e
This node
has degree < 3 ; j b m
remove it! k
d
h
Stack: c
16
Example: 3-color this graph
e
This node
has degree < 3 ; j b m
remove it! k
d
h
Stack: h c
17
Example: 3-color this graph
j b m
This node k
has degree < 3 ; d
remove it!
Stack: h c
18
Example: 3-color this graph
No node has degree < 3
j b m
k
d
Stack: g h c
19
Example: 3-color this graph
No node has degree < 3
j b m
k
d
Stack: k g h c
20
Example: 3-color this graph
j b m
d
This node
has degree < 3 ;
remove it!
Stack: k g h c
21
Example: 3-color this graph
j b m
This node
has degree < 3 ;
remove it!
Stack: d k g h c
22
Example: 3-color this graph
This node f
has degree < 3 ;
remove it! e
b m
Stack: j d k g h c
23
Example: 3-color this graph
e
This node
has degree < 3 ;
remove it! b m
Stack: f j d k g h c
24
Example: 3-color this graph
This node b m
has degree < 3 ;
remove it!
Stack: e f j d k g h c
25
Example: 3-color this graph
This node m
has degree < 3 ;
remove it!
Stack: b e f j d k g h c
26
Example: 3-color this graph
Stack: m b e f j d k g h c
27
Now, color the nodes in stack order
Find a color for this
node that’s not already
used in an adjacent node
f
j b m
k
d c
h
Stack: m b e f j d k g h c
28
Now, color the nodes in stack order
j b m
k
d c
h
Stack: m b e f j d k g h c
29
Now, color the nodes in stack order
j b m
k
d c
h
Stack: m b e f j d k g h c
30
Now, color the nodes in stack order
j b m
k
d c
h
Stack: m b e f j d k g h c
31
Now, color the nodes in stack order
j b m
k
d c
h
Stack: m b e f j d k g h c
32
Now, color the nodes in stack order
j b m
k
d c
h
Stack: m b e f j d k g h c
33
Now, color the nodes in stack order
We’re about to color node k.
This was the only one that was
degree ≥ 3 when we removed it.
Hence, it is not guaranteed that f
we can find a color for it now.
Stack: m b e f j d k g h c
34
Now, color the nodes in stack order
j b m
Find a color for this k
node that’s not already
used in an adjacent node d c
h
Stack: m b e f j d k g h c
35
Now, color the nodes in stack order
Stack: m b e f j d k g h c
36
Now, color the nodes in stack order
j b m
k
d c
h
g
Find a color for this
node that’s not already
Stack: m b e f j d k g h c
used in an adjacent node 37
Now, color the nodes in stack order
Why did this work?
Because (usually) when we removed
each node, at that time it had degree < 3. f
So when we put it back, it’s adjacent
to at most 2 already-colored nodes.
e
j b m
k
d c
h
Stack: m b e f j d k g h c
38
Two-phase algorithm:
Phase 1: list the nodes in some order (“the stack”)
j b m
k
d c
h
Stack: m b e d k j f h g c
39
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order, instead of the
“remove nodes of low-degree” e
order?
j b m
k
d c
h
Coloring order:
40
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order, instead of the
“remove nodes of low-degree” e
order?
j b m
k
Just for fun, let’s use
d c
alphabetical order. h
Coloring order: b c d e f g h j k m
41
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
k
d c
h
Coloring order: b c d e f g h j k m
42
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
k
d c
h
Coloring order: b c d e f g h j k m
43
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
k
d c
h
Coloring order: b c d e f g h j k m
44
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
k
d c
h
Coloring order: b c d e f g h j k m
45
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
k
d c
h
Coloring order: b c d e f g h j k m
46
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
k
d c
h
Coloring order: b c d e f g h j k m
47
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
k
d c
h
Coloring order: b c d e f g h j k m
48
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
k
d c
h
Coloring order: b c d e f g h j k m
49
Two phase algorithm
Phase 1: list the nodes in some order (“the stack”)
Phase 2: color the nodes in stack order
f
What if we use some other
coloring order?
e
j b m
No color available k
for node k, just d c
leave it uncolored h
Coloring order: b c d e f g h j k m
50
Two phase algorithm
This is a correct partial
coloring of the graph! f
It’s not as good as the other one,
but it is correct. e
(In a register-allocation j b m
application, variable k k
would not be assigned a d c
register, but would be h
spilled to the stack frame.)
g
Coloring order: b c d e f g h j k m
51
Two phase algorithm
Moral: The two-phase algorithm is correct
no matter what ordering you choose.
f
In phase 1, not necessary to use
Kempe’s algorithm,
although that may give e
better results.
j b m
k
d c
h
Coloring order: b c d e f g h j k m
52
Implications for program verification
53
Representing graphs in a functional program
This is a e
set of nodes j
b m
k
Graph is a finite function
from node to set-of-nodes: h d c
[ b↦{c,e,k,m}, c↦{b,m}, d↦{j,k,m},
e↦{b,f,j,m}, f↦{e,j,m}, g↦{h,j,k}, g
h↦{g,j}, j↦{d,e,f,g,h,k}, k↦{b,d,g,j},
m↦{b,c,d,e,f} ]
54
Undirected graphs
Graph coloring is done on undirected graphs.
undirected(G) :=
e
∀𝑥,𝑦. 𝑦 ϵ G(𝑥) ⇒ 𝑥 ϵ G(𝑦)
j
b m
k
Graph is a finite function
from node to set-of-nodes: h d c
[ b↦{c,e,k,m}, c↦{b,m}, d↦{j,k,m},
e↦{b,f,j,m}, f↦{e,j,m}, g↦{h,j,k}, g
h↦{g,j}, j↦{d,e,f,g,h,k}, k↦{b,d,g,j},
m↦{b,c,d,e,f} ]
55
Sets and maps
We can use Coq’s FSets and FMaps libraries
to implement (efficient) functional sets and functional maps
over small-integer keys:
f
e
First, remove c j
from finite-map G b m
k
h d c
g
[ b↦{c,e,k,m}, c↦{b,m}, d↦{j,k,m},
e↦{b,f,j,m}, f↦{e,j,m}, g↦{h,j,k},
h↦{g,j}, j↦{d,e,f,g,h,k}, k↦{b,d,g,j}, 57
m↦{b,c,d,e,f} ]
Removing a node from a graph
Definition remove_node (c: node) (G: graph) : graph :=
M.map (S.remove c) (M.remove c G).
f
e
First, remove c j
from finite-map G b m
k
h d
This leaves some
dangling edges
g
[ b↦{c,e,k,m}, c↦{b,m}, d↦{j,k,m},
e↦{b,f,j,m}, f↦{e,j,m}, g↦{h,j,k},
h↦{g,j}, j↦{d,e,f,g,h,k}, k↦{b,d,g,j}, 58
m↦{b,c,d,e,f} ]
Removing a node from a graph
Definition remove_node (c: node) (G: graph) : graph :=
M.map (S.remove c) (M.remove c G).
f
e
First, remove c j
from finite-map G b m
k
h d
This leaves some
dangling edges
g
remove the [ b↦{c,e,k,m}, c↦{b,m}, d↦{j,k,m},
dangling edges e↦{b,f,j,m}, f↦{e,j,m}, g↦{h,j,k},
h↦{g,j}, j↦{d,e,f,g,h,k}, k↦{b,d,g,j}, 59
m↦{b,c,d,e,f} ]
Testing whether a node is low-degree
Definition low_deg (K: nat) (n: node) (adj: nodeset) : bool :=
S.cardinal adj <? K.
f
Example: K=3, n=d, adj={j,k,m}
e
(S.cardinal {j,k,m} <? 3) is false. j
b m
k
h d c
g
[ b↦{c,e,k,m}, c↦{b,m}, d↦{j,k,m},
e↦{b,f,j,m}, f↦{e,j,m}, g↦{h,j,k},
h↦{g,j}, j↦{d,e,f,g,h,k}, k↦{b,d,g,j}, 60
m↦{b,c,d,e,f} ]
The fold function on a finite map
M.fold: ∀ A B : Type, (M.elt → A → B → B) → M.t A → B → B
(subset_nodes (low_deg K) G)
63
Recursive functions in Coq
Function select (K: nat) (G: graph) {measure M.cardinal G} : list node :=
match S.choose (subset_nodes (low_deg K) G) with
| Some n ⇒ n :: select K (remove_node n G)
| None ⇒ nil
end.
Proof. apply select_terminates.
Defined.
Lemma select_terminates:
∀ (K: nat) (G : graph) (n : node),
S.choose (subset_nodes (low_deg K) G) = Some n →
M.cardinal (remove_node n G) < M.cardinal G.
65
Color palette
palette: S.t
A “palette” is a set of colors
66
Phase 2 of the algorithm
e
j
k b m
Find a color for this
h d c
node that’s not already
used in an adjacent node
g 67
Phase 2 of the algorithm
Definition coloring := M.t node.
Definition color1 (palette: S.t) (g: graph) (n: node) (f: coloring) : coloring :=
match S.choose (S.diff palette (colors_of f (adj g n))) with
| Some c => M.add n c f
| None => f e
end. j
k b m
Find a color for this
h d c
node that’s not already
used in an adjacent node
g 68
The entire program
Definition color (palette: S.t) (G: graph) : coloring :=
fold_right (color1 palette G) (M.empty _) (select (S.cardinal palette) G).
Phase 2 Phase 1
e
j
k b m
h d c
g 69
PROVING THE PROGRAM
CORRECT
70
Specification of correctness
coloring : Type := “finite function from node to color”
Correctness:
If 𝑓 is a coloring for G, that is, color palette G = 𝑓,
then (1) if 𝑓(𝑖) = Some 𝑐 then 𝑐 ϵ palette
and (2) if 𝑗ϵG(𝑖) and 𝑓(𝑖) = Some 𝑐 and 𝑓(𝑗) = Some 𝑑 then 𝑐≠𝑑
71
Theorem
Definition coloring_ok (palette: S.t) (g: graph) (f: coloring) :=
∀ i j,
S.In j (adj g i) →
(forall ci, M.find i f = Some ci → S.In ci palette) /\
(forall ci cj, M.find i f = Some ci → M.find j f = Some cj → ci<>cj).
Theorem color_correct:
∀ palette g,
no_selfloop g →
undirected g →
coloring_ok palette g (color palette g).
72