sequential_vertex_coloring.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Copyright 2004 The Trustees of Indiana University
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. #ifndef BOOST_GRAPH_SEQUENTIAL_VERTEX_COLORING_HPP
  11. #define BOOST_GRAPH_SEQUENTIAL_VERTEX_COLORING_HPP
  12. #include <vector>
  13. #include <boost/graph/graph_traits.hpp>
  14. #include <boost/tuple/tuple.hpp>
  15. #include <boost/property_map/property_map.hpp>
  16. #include <boost/limits.hpp>
  17. #ifdef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
  18. # include <iterator>
  19. #endif
  20. /* This algorithm is to find coloring of a graph
  21. Algorithm:
  22. Let G = (V,E) be a graph with vertices (somehow) ordered v_1, v_2, ...,
  23. v_n. For k = 1, 2, ..., n the sequential algorithm assigns v_k to the
  24. smallest possible color.
  25. Reference:
  26. Thomas F. Coleman and Jorge J. More, Estimation of sparse Jacobian
  27. matrices and graph coloring problems. J. Numer. Anal. V20, P187-209, 1983
  28. v_k is stored as o[k] here.
  29. The color of the vertex v will be stored in color[v].
  30. i.e., vertex v belongs to coloring color[v] */
  31. namespace boost {
  32. template <class VertexListGraph, class OrderPA, class ColorMap>
  33. typename property_traits<ColorMap>::value_type
  34. sequential_vertex_coloring(const VertexListGraph& G, OrderPA order,
  35. ColorMap color)
  36. {
  37. typedef graph_traits<VertexListGraph> GraphTraits;
  38. typedef typename GraphTraits::vertex_descriptor Vertex;
  39. typedef typename property_traits<ColorMap>::value_type size_type;
  40. size_type max_color = 0;
  41. const size_type V = num_vertices(G);
  42. // We need to keep track of which colors are used by
  43. // adjacent vertices. We do this by marking the colors
  44. // that are used. The mark array contains the mark
  45. // for each color. The length of mark is the
  46. // number of vertices since the maximum possible number of colors
  47. // is the number of vertices.
  48. std::vector<size_type> mark(V,
  49. std::numeric_limits<size_type>::max BOOST_PREVENT_MACRO_SUBSTITUTION());
  50. //Initialize colors
  51. typename GraphTraits::vertex_iterator v, vend;
  52. for (boost::tie(v, vend) = vertices(G); v != vend; ++v)
  53. put(color, *v, V-1);
  54. //Determine the color for every vertex one by one
  55. for ( size_type i = 0; i < V; i++) {
  56. Vertex current = get(order,i);
  57. typename GraphTraits::adjacency_iterator v, vend;
  58. //Mark the colors of vertices adjacent to current.
  59. //i can be the value for marking since i increases successively
  60. for (boost::tie(v,vend) = adjacent_vertices(current, G); v != vend; ++v)
  61. mark[get(color,*v)] = i;
  62. //Next step is to assign the smallest un-marked color
  63. //to the current vertex.
  64. size_type j = 0;
  65. //Scan through all useable colors, find the smallest possible
  66. //color that is not used by neighbors. Note that if mark[j]
  67. //is equal to i, color j is used by one of the current vertex's
  68. //neighbors.
  69. while ( j < max_color && mark[j] == i )
  70. ++j;
  71. if ( j == max_color ) //All colors are used up. Add one more color
  72. ++max_color;
  73. //At this point, j is the smallest possible color
  74. put(color, current, j); //Save the color of vertex current
  75. }
  76. return max_color;
  77. }
  78. template<class VertexListGraph, class ColorMap>
  79. typename property_traits<ColorMap>::value_type
  80. sequential_vertex_coloring(const VertexListGraph& G, ColorMap color)
  81. {
  82. typedef typename graph_traits<VertexListGraph>::vertex_descriptor
  83. vertex_descriptor;
  84. typedef typename graph_traits<VertexListGraph>::vertex_iterator
  85. vertex_iterator;
  86. std::pair<vertex_iterator, vertex_iterator> v = vertices(G);
  87. #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
  88. std::vector<vertex_descriptor> order(v.first, v.second);
  89. #else
  90. std::vector<vertex_descriptor> order;
  91. order.reserve(std::distance(v.first, v.second));
  92. while (v.first != v.second) order.push_back(*v.first++);
  93. #endif
  94. return sequential_vertex_coloring
  95. (G,
  96. make_iterator_property_map
  97. (order.begin(), identity_property_map(),
  98. graph_traits<VertexListGraph>::null_vertex()),
  99. color);
  100. }
  101. }
  102. #endif