The Vertex coloring problem and bipartite graphs

Vertex-coloring problem
The Vertex coloring problem and bipartite graphs
Tyler Moore
CSE 3353, SMU, Dallas, TX
Lecture 8
Some slides created by or adapted from Dr. Kevin Wayne. For more information see
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
2 / 16
Vertex-coloring problem
Applications of the vertex-coloring problem
Apart from working at National Geographic, when might you
encounter a vertex-coloring problem?
The vertex-coloring problem seeks to assign a label (aka color) to
each vertex of a graph such that no edge links any two vertices of the
same color
Trivial solution: assign each vertex a different color
However, goal is usually to use as few colors as possible
3 / 16
Vertex-coloring problems arise in scheduling problems, where access
to shared resources must be coordinated
Example: register allocation by compilers
Variables are used for fixed timespan (after initialization, before final
use)
Two variables with intersecting lifespans can’t be put in the same
register
We can build a graph with variables assigned to vertices and edges
drawn between vertices if the variables’ lifespan intersects
Color the graph, and assign variables to the same register if their
vertices have the same color
4 / 16
Vertex-coloring problem special case: two colors
���������������������
Many graph problems become:
A bipartite graph is an undirected graph
whose vertices can be divided into disjoint
sets U and V such that every edge
connects a vertex in U to one in V .
U
�Easier if the underlying graph is bipartite (matching).
�Tractable if the underlying graph is bipartite (independent set).
V
Before attempting to design an algorithm, we need to understand structure
of bipartite graphs.
Bipartite graphs arise in matching
problems: matching workers to jobs,
matching kidney donors with recipients,
finding heterosexual mates
v2
v2
v3
v1
v4
If we can color a graph’s vertices using just
two colors, then we have a bipartite graph
v5
v6
v3
v4
v5
Problem: given a graph, find its
two-coloring or report that a two-coloring is
not possible
v6
v7
v1
v7
�������������������
��������������������
27
5 / 16
6 / 16
�������������������������������
����������������
Lemma. If a graph � is bipartite, it cannot contain an odd length cycle.
Lemma. Let � be a connected graph, and let ��������� be the layers produced
by BFS starting at node �. Exactly one of the following holds.
(i) No edge of � joins two nodes of the same layer, and � is bipartite.
Pf. Not possible to 2-color the odd cycle, let alone �.
(ii) An edge of � joins two nodes of the same layer, and � contains an
odd-length cycle (and hence is not bipartite).
���������
�������������
�������������
�����������������
L1
L2
���� ���
28
7 / 16
L3
L1
L2
L3
���������
29
8 / 16
�������������������
����������������
Property. Let � be a BFS tree of ����������, and let ������ be an edge of �.
Lemma. Let � be a connected graph, and let ��������� be the layers produced
Then, the level of � and � differ by at most 1.
by BFS starting at node �. Exactly one of the following holds.
(i) No edge of � joins two nodes of the same layer, and � is bipartite.
(ii) An edge of � joins two nodes of the same layer, and � contains an
odd-length cycle (and hence is not bipartite).
Pf. (i)
�Suppose no edge joins two nodes in adjacent layers.
�By previous lemma, this implies all edges join nodes on same level.
�Bipartition: red = nodes on odd levels, blue = nodes on even levels.
L0
L1
L2
L3
L2
L1
L3
���� ���
19
30
9 / 16
10 / 16
����������������
�������������������������������������
Lemma. Let � be a connected graph, and let ��������� be the layers produced
Corollary. A graph � is bipartite iff it contain no odd length cycle.
by BFS starting at node �. Exactly one of the following holds.
(i) No edge of � joins two nodes of the same layer, and � is bipartite.
(ii) An edge of � joins two nodes of the same layer, and � contains an
odd-length cycle (and hence is not bipartite).
Pf. (ii)
�Suppose ������ is an edge with �, � in same level ��.
�Let ��������������� lowest common ancestor.
�Let �� be level containing �.
�Consider cycle that takes edge from � to �,
z = lca(x, y)
5-cycle C
then path from � to �, then path from � to �.
�Its length is
����������������������������, which is odd. ▪
(x, y)
path from path from
y to z
z to x
31
11 / 16
���������
�������������
�������������
�����������������
32
12 / 16
Two-coloring algorithm example 1
2
Color the first vertex blue.
3
Do a breadth-first traversal. For each newly-discovered node, color it
the opposite of the parent (i.e., red if parent is blue)
Breadth-First Search Tree
a
a
1
If the traversal completes without any conflicting colors, then the
graph is bipartite.
e
f
g
13 / 16
Two-coloring algorithm example 2
a
a
4
e
f
e
8
6
c
b
5
d
7
f
g
14 / 16
3
1
2
Breadth-First Search Tree
c
e
d
Exercise: Check for bipartness
Undirected Graph
b
c
b
9
5
d
5
If the child node has already been discovered, check if the colors are
the same as the parent. If so, then the graph isn’t bipartite.
c
4
4
b
2
Suppose there are two colors: blue and red.
Undirected Graph
3
1
6
Two-coloring algorithm
d
7
f
9
CONFLICT
g
g
15 / 16
16 / 16