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
13 #include <vector>
14 #include <boost/graph/graph_traits.hpp>
15 #include <boost/tuple/tuple.hpp>
16 #include <boost/property_map/property_map.hpp>
17 #include <boost/limits.hpp>
18
19 #ifdef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
20 #include <iterator>
21 #endif
22
23 /* This algorithm is to find coloring of a graph
24
25 Algorithm:
26 Let G = (V,E) be a graph with vertices (somehow) ordered v_1, v_2, ...,
27 v_n. For k = 1, 2, ..., n the sequential algorithm assigns v_k to the
28 smallest possible color.
29
30 Reference:
31
32 Thomas F. Coleman and Jorge J. More, Estimation of sparse Jacobian
33 matrices and graph coloring problems. J. Numer. Anal. V20, P187-209, 1983
34
35 v_k is stored as o[k] here.
36
37 The color of the vertex v will be stored in color[v].
38 i.e., vertex v belongs to coloring color[v] */
39
40 namespace boost
41 {
42 template < class VertexListGraph, class OrderPA, class ColorMap >
sequential_vertex_coloring(const VertexListGraph & G,OrderPA order,ColorMap color)43 typename property_traits< ColorMap >::value_type sequential_vertex_coloring(
44 const VertexListGraph& G, OrderPA order, ColorMap color)
45 {
46 typedef graph_traits< VertexListGraph > GraphTraits;
47 typedef typename GraphTraits::vertex_descriptor Vertex;
48 typedef typename property_traits< ColorMap >::value_type size_type;
49
50 size_type max_color = 0;
51 const size_type V = num_vertices(G);
52
53 // We need to keep track of which colors are used by
54 // adjacent vertices. We do this by marking the colors
55 // that are used. The mark array contains the mark
56 // for each color. The length of mark is the
57 // number of vertices since the maximum possible number of colors
58 // is the number of vertices.
59 std::vector< size_type > mark(V,
60 std::numeric_limits< size_type >::max
61 BOOST_PREVENT_MACRO_SUBSTITUTION());
62
63 // Initialize colors
64 typename GraphTraits::vertex_iterator v, vend;
65 for (boost::tie(v, vend) = vertices(G); v != vend; ++v)
66 put(color, *v, V - 1);
67
68 // Determine the color for every vertex one by one
69 for (size_type i = 0; i < V; i++)
70 {
71 Vertex current = get(order, i);
72 typename GraphTraits::adjacency_iterator v, vend;
73
74 // Mark the colors of vertices adjacent to current.
75 // i can be the value for marking since i increases successively
76 for (boost::tie(v, vend) = adjacent_vertices(current, G); v != vend;
77 ++v)
78 mark[get(color, *v)] = i;
79
80 // Next step is to assign the smallest un-marked color
81 // to the current vertex.
82 size_type j = 0;
83
84 // Scan through all useable colors, find the smallest possible
85 // color that is not used by neighbors. Note that if mark[j]
86 // is equal to i, color j is used by one of the current vertex's
87 // neighbors.
88 while (j < max_color && mark[j] == i)
89 ++j;
90
91 if (j == max_color) // All colors are used up. Add one more color
92 ++max_color;
93
94 // At this point, j is the smallest possible color
95 put(color, current, j); // Save the color of vertex current
96 }
97
98 return max_color;
99 }
100
101 template < class VertexListGraph, class ColorMap >
sequential_vertex_coloring(const VertexListGraph & G,ColorMap color)102 typename property_traits< ColorMap >::value_type sequential_vertex_coloring(
103 const VertexListGraph& G, ColorMap color)
104 {
105 typedef typename graph_traits< VertexListGraph >::vertex_descriptor
106 vertex_descriptor;
107 typedef typename graph_traits< VertexListGraph >::vertex_iterator
108 vertex_iterator;
109
110 std::pair< vertex_iterator, vertex_iterator > v = vertices(G);
111 #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
112 std::vector< vertex_descriptor > order(v.first, v.second);
113 #else
114 std::vector< vertex_descriptor > order;
115 order.reserve(std::distance(v.first, v.second));
116 while (v.first != v.second)
117 order.push_back(*v.first++);
118 #endif
119 return sequential_vertex_coloring(G,
120 make_iterator_property_map(order.begin(), identity_property_map(),
121 graph_traits< VertexListGraph >::null_vertex()),
122 color);
123 }
124 }
125
126 #endif
127