graph_coloring.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <Head>
  9. <Title>Graph Coloring Example</Title>
  10. <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
  11. ALINK="#ff0000">
  12. <IMG SRC="../../../boost.png"
  13. ALT="C++ Boost" width="277" height="86">
  14. <BR Clear>
  15. <H1>Graph Coloring</H1>
  16. The graph (or vertex) coloring problem, which involves assigning
  17. colors to vertices in a graph such that adjacenct vertices have
  18. distinct colors, arises in a number of scientific and engineering
  19. applications such as scheduling&nbsp;, register allocation&nbsp;,
  20. optimization&nbsp; and parallel numerical computation.
  21. <P>
  22. Mathmatically, a proper vertex coloring of an undirected graph
  23. <i>G=(V,E)</i> is a map <i>c: V -> S</i> such that <i>c(u) != c(v)</i>
  24. whenever there exists an edge <i>(u,v)</i> in <i>G</i>. The elements
  25. of set <i>S</i> are called the available colors. The problem is often
  26. to determine the minimum cardinality (the number of colors) of
  27. <i>S</i> for a given graph <i>G</i> or to ask whether it is able to
  28. color graph <i>G</i> with a certain number of colors. For example, how
  29. many color do we need to color the United States on a map in such a
  30. way that adjacent states have different color? A compiler needs to
  31. decide whether variables and temporaries could be allocated in fixed
  32. number of registers at some point. If a target machine has <i>K</i>
  33. registers, can a particular interference graph be colored with
  34. <i>K</i> colors? (Each vertex in the interference graph represents a
  35. temporary value; each edge indicates a pair of temporaries that cannot
  36. be assigned to the same register.)
  37. <P>
  38. Another example is in the estimation of sparse Jacobian matrix by
  39. differences in large scale nonlinear problems in optimization and
  40. differential equations. Given a nonlinear function <i>F</i>, the
  41. estimation of Jacobian matrix <i>J</i> can be obtained by estimating
  42. <i>Jd</i> for suitable choices of vector <i>d</i>. Curtis, Powell and
  43. Reid&nbsp;[<a href="bibliography.html#curtis74:_jacob">9</a>] observed that a group of columns of <i>J</i> can be
  44. determined by one evaluation of <i>Jd</i> if no two columns in this
  45. group have a nonzero in the same row position. Therefore, a question
  46. is emerged: what is the number of function evaluations need to compute
  47. approximate Jacobian matrix? As a matter of fact this question is the
  48. same as to compute the minimum numbers of colors for coloring a graph
  49. if we construct the graph in the following matter: A vertex represents
  50. a column of <i>J</i> and there is an edge if and only if the two
  51. column have a nonzero in the same row position.
  52. <P>
  53. However, coloring a general graph with the minimum number of colors
  54. (the cardinality of set <i>S</i>) is known to be an NP-complete
  55. problem&nbsp;[<a
  56. href="bibliography.html#garey79:computers-and-intractability">30</a>].
  57. One often relies on heuristics to find a solution. A widely-used
  58. general greedy based approach is starting from an ordered vertex
  59. enumeration <i>v<sub>1</sub>, ..., v<sub>n</sub></i> of <i>G</i>, to
  60. assign <i>v<sub>i</sub></i> to the smallest possible color for
  61. <i>i</i> from <i>1</i> to <i>n</i>. In section <a
  62. href="constructing_algorithms.html">Constructing graph
  63. algorithms with BGL</a> we have shown how to write this algorithm in
  64. the generic programming paradigm. However, the ordering of the
  65. vertices dramatically affects the coloring. The arbitrary order may
  66. perform very poorly while another ordering may produces an optimal
  67. coloring. Several ordering algorithms have been studied to help the
  68. greedy coloring heuristics including largest-first ordering,
  69. smallest-last ordering and incidence degree ordering.
  70. <P>
  71. In the BGL framework, the process of constructing/prototyping such a
  72. ordering is fairly easy because writing such a ordering follows the
  73. algorithm description closely. As an example, we present the
  74. smallest-last ordering algorithm.
  75. <P>
  76. The basic idea of the smallest-last ordering&nbsp;[<a
  77. href="bibliography.html#matula72:_graph_theory_computing">29</a>] is
  78. as follows: Assuming that the vertices <i>v<sub>k+1</sub>, ...,
  79. v<sub>n</sub></i> have been selected, choose <i>v<sub>k</sub></i> so
  80. that the degree of <i>v<sub>k</sub></i> in the subgraph induced by
  81. <i>V - {v<sub>k+1</sub>, ..., v<sub>n</sub>}</i> is minimal.
  82. <P>
  83. The algorithm uses a bucket sorter for the vertices in the graph where
  84. bucket is the degree. Two vertex property maps, <TT>degree</TT> and
  85. <TT>marker</TT>, are used in the algorithm. The former is to store
  86. degree of every vertex while the latter is to mark whether a vertex
  87. has been ordered or processed during traversing adjacent vertices. The
  88. ordering is stored in the <TT>order</TT>. The algorithm is as follows:
  89. <UL>
  90. <LI>put all vertices in the bucket sorter
  91. </LI>
  92. <LI>find the vertex <tt>node</tt> with smallest degree in the bucket sorter
  93. </LI>
  94. <LI>number <tt>node</tt> and traverse through its adjacent vertices to
  95. update its degree and the position in the bucket sorter.
  96. </LI>
  97. <LI>go to the step 2 until all vertices are numbered.
  98. </LI>
  99. </UL>
  100. <P>
  101. <PRE>
  102. namespace boost {
  103. template &lt;class VertexListGraph, class Order, class Degree,
  104. class Marker, class BucketSorter&gt;
  105. void
  106. smallest_last_ordering(const VertexListGraph&amp; G, Order order,
  107. Degree degree, Marker marker,
  108. BucketSorter&amp; degree_buckets) {
  109. typedef typename graph_traits&lt;VertexListGraph&gt; GraphTraits;
  110. typedef typename GraphTraits::vertex_descriptor Vertex;
  111. //typedef typename GraphTraits::size_type size_type;
  112. typedef size_t size_type;
  113. const size_type num = num_vertices(G);
  114. typename GraphTraits::vertex_iterator v, vend;
  115. for (boost::tie(v, vend) = vertices(G); v != vend; ++v) {
  116. put(marker, *v, num);
  117. put(degree, *v, out_degree(*v, G));
  118. degree_buckets.push(*v);
  119. }
  120. size_type minimum_degree = 1;
  121. size_type current_order = num - 1;
  122. while ( 1 ) {
  123. typedef typename BucketSorter::stack MDStack;
  124. MDStack minimum_degree_stack = degree_buckets[minimum_degree];
  125. while (minimum_degree_stack.empty())
  126. minimum_degree_stack = degree_buckets[++minimum_degree];
  127. Vertex node = minimum_degree_stack.top();
  128. put(order, current_order, node);
  129. if ( current_order == 0 ) //find all vertices
  130. break;
  131. minimum_degree_stack.pop();
  132. put(marker, node, 0); //node has been ordered.
  133. typename GraphTraits::adjacency_iterator v, vend;
  134. for (boost::tie(v,vend) = adjacent_vertices(node, G); v != vend; ++v)
  135. if ( get(marker, *v) &gt; current_order ) { //*v is unordered vertex
  136. put(marker, *v, current_order); //mark the columns adjacent to node
  137. //It is possible minimum degree goes down
  138. //Here we keep tracking it.
  139. put(degree, *v, get(degree, *v) - 1);
  140. minimum_degree = std::min(minimum_degree, get(degree, *v));
  141. //update the position of *v in the bucket sorter
  142. degree_buckets.update(*v);
  143. }
  144. current_order--;
  145. }
  146. //at this point, get(order, i) == vertex(i, g);
  147. }
  148. } // namespace boost
  149. </PRE>
  150. <br>
  151. <HR>
  152. <TABLE>
  153. <TR valign=top>
  154. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  155. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
  156. Indiana University (<A
  157. HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
  158. <A HREF="http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br>
  159. <A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>,
  160. Indiana University (<A
  161. HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
  162. </TD></TR></TABLE>
  163. </BODY>
  164. </HTML>