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? 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 edge links any two vertices of the Example: register allocation by compilers same color Variables are used for fi xed timespan (after initialization, before fi nal Trivial solution: assign each vertex a di ff erent 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 / 16 4 / 16
Vertex-coloring problem special case: two colors ��������������������� Many graph problems become: A bipartite graph is an undirected graph U V � Easier if the underlying graph is bipartite (matching). whose vertices can be divided into disjoint � Tractable if the underlying graph is bipartite (independent set). sets U and V such that every edge Before attempting to design an algorithm, we need to understand structure connects a vertex in U to one in V . of bipartite graphs. Bipartite graphs arise in matching problems: matching workers to jobs, matching kidney donors with recipients, v 2 fi nding heterosexual mates v 2 v 3 v 1 v 4 If we can color a graph’s vertices using just v 3 v 6 v 5 v 4 two colors, then we have a bipartite graph v 5 Problem: given a graph, fi nd its v 6 v 7 v 1 v 7 two-coloring or report that a two-coloring is not possible ������������������� �������������������� 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. 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 1 L 2 L 3 L 1 L 2 L 3 ��������� ���� ��� 28 29 7 / 16 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. L 0 L 1 L 2 L 3 L 1 L 2 L 3 ���� ��� 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. 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 / 16 12 / 16
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 the fi rst vertex blue. 1 3 2 3 Do a breadth- fi rst traversal. For each newly-discovered node, color it c c the opposite of the parent (i.e., red if parent is blue) b d b d 5 7 4 If the child node has already been discovered, check if the colors are 4 6 the same as the parent. If so, then the graph isn’t bipartite. e e f f 5 If the traversal completes without any con fl icting colors, then the graph is bipartite. 9 g g 13 / 16 14 / 16 Two-coloring algorithm example 2 Exercise: Check for bipartness Undirected Graph Breadth-First Search Tree a a 1 3 2 c c b d b d 5 7 4 6 8 e e f f CONFLICT 9 g g 15 / 16 16 / 16
Recommend
More recommend