-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. Graph-coloring of registers adapted from Stanford’s CS143

(Aiken, Treichler).

2 / 32

Vertex-coloring problem Applications of the vertex-coloring problem

Apart from working at National Geographic, when might you encounter a vertex-coloring problem? Vertex-coloring problems arise in scheduling problems, where access The vertex-coloring problem seeks to assign a label (aka color) to to shared resources must be coordinated each vertex of a graph such that no links any two vertices of the Example: register allocation by compilers same color Variables are used forfixed timespan (after initialization, beforefinal Trivial solution: assign each vertex a different color use) However, goal is usually to use as few colors as possible 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

3 / 32 4 / 32 Vertex-coloring problem special case: two colors ���������������������

Many graph problems become: A is an undirected graph UV �Easier if the underlying graph is bipartite (). whose vertices can be divided into disjoint �Tractable if the underlying graph is bipartite (independent set). setsU andV such that every edge connects a vertex inU to one inV. Before attempting to design an , we need to understand structure of bipartite graphs. Bipartite graphs arise in matching problems: matching workers to jobs, matching kidney donors with recipients, v2 v2 v3 finding heterosexual mates v1 If we can color a graph’s vertices using just v4 v v6 v5 v4 3

two colors, then we have a bipartite graph v5

Problem: given a graph,find its v6 v7 v1 two-coloring or report that a two-coloring is v7 not possible ������������������� ��������������������

27 5 / 32 6 / 32

������������������������������� ����������������

Lemma. If a graph � is bipartite, it cannot contain an odd length . Lemma. Let � be a connected graph, and let ��������� be the layers produced by BFS starting at node �. Exactly one of the following holds. Pf. Not possible to 2-color the odd cycle, let alone �. (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).

��������� ������������� ������������� �����������������

L L L L1 L2 L3 1 2 3

���� ��� ��������� 28 29 7 / 32 8 / 32 ������������������� ����������������

Property. Let � be a BFS 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 L1 L2 L3

���� ��� 19 30 9 / 32 10 / 32

���������������� �������������������������������������

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 ��������������� . z = lca(x, y) 5-cycle C Let be level containing . � �� � �Consider cycle that takes edge from � to �, 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 32 11 / 32 12 / 32 Two-coloring algorithm Two-coloring algorithm example 1

Undirected Graph Breadth-First Search Tree 1 Suppose there are two colors: blue and red. a a 2 Color thefirst vertex blue. 3 1

2 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) b c d b c d

4 4 5 7

If the child node has already been discovered, check if the colors are 6 the same as the parent. If so, then the graph isn’t bipartite. e f e f 5 If the traversal completes without any conflicting colors, then the

graph is bipartite. 9 g g

13 / 32 14 / 32

Two-coloring algorithm example 2 Exercise: Check for bipartness

Undirected Graph Breadth-First Search Tree a a 3 1

2

b c d b c d

4 5 7

6 8 e f e f CONFLICT

9 g g

15 / 32 16 / 32 Global OpVmizaVon Register Interference Graph

� Consider the following the following IL: � Use liveness analysis to compute a register interference graph (RIG) ��������� ������������������� � Each variable is a node in the RIG ������������� ���������� ���� � An edge exists between two nodes (variables) if: ��������� � at ANY point in program, both variables are live ���� ������������� � Directly connected nodes are variables that ������������������� cannot share a register ��������

Sean Treichler CS143 – Summer 2014 – Lecture 13 7 Sean Treichler CS143 – Summer 2014 – Lecture 13 46 17 / 32 18 / 32

RIG Example

b, w, z � A coloring of a graph is a assignment of colors to { } �� �� nodes: ������� � ������ such that node that share an edge have di fferent b, w, x, z b, x colors { } { } ����������� ������� �� �� b, x, y � A k-­‐coloring is a coloring that uses at most k { } different colors a, b, x, y ����������� { } ������ b, x, y { } �� �� � A k-­‐colorable graph is a graph for which there ������ exists at least one k-­‐coloring ;

Sean Treichler CS143 – Summer 2014 – Lecture 13 47 Sean Treichler CS143 – Summer 2014 – Lecture 13 48 19 / 32 20 / 32 Register AllocaVon via Coloring Our HeurisVc

� A k-­‐coloring of a RIG is a valid register � Start with full RIG allocaVon for k registers: � While graph is not empty: � Each color is a register � Select a node with minimum number of edges � Variables with the same color are never live at the � Remove node from graph, place on stack same Vme � While stack is not empty: � Pop node from stack, put back in graph � Graph coloring is a hard problem (NP-­‐hard) � Add back any edges to other nodes in graph � Pick a color for the node that doesn’t match any neighbor � Have to use heurisVcs � Pick a new color if necessary

Sean Treichler CS143 – Summer 2014 – Lecture 13 49 Sean Treichler CS143 – Summer 2014 – Lecture 13 50 21 / 32 22 / 32

Example Coloring Example Coloring

Graph: Stack: Graph: Stack:

�� �� �� ��

�� �� �� ��

�� �� �� �� ��

Sean Treichler CS143 – Summer 2014 – Lecture 13 51 Sean Treichler CS143 – Summer 2014 – Lecture 13 57 23 / 32 24 / 32 Example Coloring Example Coloring

Graph: Stack: Graph: Stack:

�� �� �� �� ��

�� �� �� �� ��

�� ��

�� ��

Sean Treichler CS143 – Summer 2014 – Lecture 13 58 Sean Treichler CS143 – Summer 2014 – Lecture 13 59 25 / 32 26 / 32

Example Coloring Example Coloring

Graph: Stack: Graph: Stack:

�� ��

�� �� �� �� �� �� ��

�� �� �� �� ��

Sean Treichler CS143 – Summer 2014 – Lecture 13 60 Sean Treichler CS143 – Summer 2014 – Lecture 13 61 27 / 32 28 / 32 Example Coloring Example Coloring

Graph: Stack: Graph: Stack:

�� �� �� ��

�� �� �� ��

�� �� �� �� �� ��

Sean Treichler CS143 – Summer 2014 – Lecture 13 62 Sean Treichler CS143 – Summer 2014 – Lecture 13 63 29 / 32 30 / 32

Example Coloring Example Coloring

Graph: Code: Graph: Code:

��������� ���������� �� �� �� �� ������������������� �������������������� ������������� �� ���� ������ ���������� ���������� ���� ���� �� �� �� �� ��������� �� ������� ���� ���� ������������� ��������������� ������������������� ��������������������� �� �� �������� �� �� ������

Sean Treichler CS143 – Summer 2014 – Lecture 13 65 Sean Treichler CS143 – Summer 2014 – Lecture 13 66 31 / 32 32 / 32