49 #ifndef DSATUR_COLORING_HPP 50 #define DSATUR_COLORING_HPP 52 #include <boost/config.hpp> 53 #include <boost/graph/properties.hpp> 54 #include <boost/version.hpp> 55 #if BOOST_VERSION >= 104000 56 #include <boost/property_map/property_map.hpp> 58 #include <boost/property_map.hpp> 62 #include <boost/graph/graph_traits.hpp> 63 #include <boost/graph/visitors.hpp> 64 #include <boost/limits.hpp> 65 #include <boost/tuple/tuple.hpp> 87 template <
typename size_type,
typename VertexListGraph>
100 const VertexListGraph&
G;
103 template <
typename size_type,
typename VertexListGraph>
109 return degree(
vertex(v1,
G),
G) < v2_degree;
112 :
G(_G), v2_degree(degree(
vertex(_v2, _G), _G))
117 const VertexListGraph&
G;
121 template <
typename size_type>
127 explicit dsatur_d_list(size_type _data) : data(_data), prev(nullptr), next(nullptr)
132 template <
typename VertexListGraph,
typename ColorMap,
typename size_type>
137 using Vertex =
typename GraphTraits::vertex_descriptor;
143 const VertexListGraph&
G;
148 : num_node(_num_node),
150 vertex_to_satur(_num_node, 0),
151 vertex_to_iter(_num_node),
152 color_set(_num_node),
156 std::vector<size_type> tmp_vertex_degree_ordering(_num_node);
157 for(size_type i = 0; i < _num_node; i++)
159 tmp_vertex_degree_ordering[i] = i;
161 std::stable_sort(tmp_vertex_degree_ordering.begin(), tmp_vertex_degree_ordering.end(),
164 for(size_type i = 0; i < _num_node; i++)
166 size_type curr_vert = tmp_vertex_degree_ordering[i];
170 last = satur_to_list[0] = dobj;
178 vertex_to_iter[curr_vert] = dobj;
184 size_type maxsatur = 0;
185 size_type maxclr = 0;
186 for(size_type i = 0; i < num_node; i++)
189 while(!satur_to_list[maxsatur])
194 size_type v = satur_to_list[maxsatur]->data;
196 satur_to_list[maxsatur] = satur_to_list[maxsatur]->
next;
197 delete to_be_deleted;
198 if(satur_to_list[maxsatur])
200 satur_to_list[maxsatur]->
prev =
nullptr;
202 vertex_to_iter[v] =
nullptr;
205 while(color_set[v].find(color) != color_set[v].end())
209 color_set[v].insert(color);
214 put(CM,
vertex(v, G), color);
216 typename GraphTraits::adjacency_iterator vit, vend;
217 for(boost::tie(vit, vend) = adjacent_vertices(
vertex(v, G), G); vit != vend; ++vit)
219 size_type w =
get(vertex_index,
G, *vit);
220 if(vertex_to_iter[w])
223 if(color_set[w].find(color) == color_set[w].end())
226 color_set[w].insert(color);
229 size_type w_satur = vertex_to_satur[w];
231 if(vertex_to_be_moved->
prev ==
nullptr)
233 satur_to_list[w_satur] = vertex_to_be_moved->
next;
234 if(vertex_to_be_moved->
next)
236 satur_to_list[w_satur]->prev =
nullptr;
239 else if(vertex_to_be_moved->
next ==
nullptr)
241 vertex_to_be_moved->
prev->next =
nullptr;
245 vertex_to_be_moved->
prev->next = vertex_to_be_moved->
next;
246 vertex_to_be_moved->
next->prev = vertex_to_be_moved->
prev;
250 vertex_to_satur[w] = w_satur;
251 if(maxsatur < w_satur)
254 satur_to_list.resize(maxsatur + 1);
255 satur_to_list[maxsatur] =
nullptr;
259 if(satur_to_list[w_satur] ==
nullptr)
261 satur_to_list[w_satur] = vertex_to_be_moved;
262 vertex_to_be_moved->
prev =
nullptr;
263 vertex_to_be_moved->
next =
nullptr;
265 else if(DPF(satur_to_list[w_satur]->data))
267 vertex_to_be_moved->
prev =
nullptr;
268 vertex_to_be_moved->
next = satur_to_list[w_satur];
269 satur_to_list[w_satur]->prev = vertex_to_be_moved;
270 satur_to_list[w_satur] = vertex_to_be_moved;
275 while(iter->
next && !DPF(iter->
next->data))
280 vertex_to_be_moved->
next = iter->
next;
283 iter->
next->prev = vertex_to_be_moved;
285 iter->
next = vertex_to_be_moved;
286 vertex_to_be_moved->
prev = iter;
298 template <
typename VertexListGraph,
typename ColorMap>
299 typename property_traits<ColorMap>::value_type
dsatur_coloring(
const VertexListGraph&
G, ColorMap color)
301 using GraphTraits = graph_traits<VertexListGraph>;
302 using Vertex =
typename GraphTraits::vertex_descriptor;
303 using size_type =
typename property_traits<ColorMap>::value_type;
305 const size_type num_node = num_vertices(G);
This algorithm is to find coloring of a graph Algorithm: Let G = (V,E) be a graph with vertices v_1...
graph_traits< VertexListGraph > GraphTraits
dsatur_d_list(size_type _data)
std::vector< size_type > vertex_to_satur
std::vector< CustomUnorderedSet< size_type > > color_set
bool operator()(size_type v1) const
redefinition of set to manage ordered/unordered structures
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
const VertexListGraph & G
std::vector< dsatur_d_list< size_type > * > satur_to_list
std::vector< dsatur_d_list< size_type > * > vertex_to_iter
property_traits< ColorMap >::value_type dsatur_coloring(const VertexListGraph &G, ColorMap color)
coloring of a graph following the DSATUR heuristic (version1)
const VertexListGraph & G
dsatur_coloring_helper(const VertexListGraph &_G, ColorMap &_CM, const size_type _num_node)
bool operator()(size_type v1, size_type v2) const
dsatur_degree_predicate_functor(const VertexListGraph &_G, size_type &_v2)
const VertexListGraph & G
typename GraphTraits::vertex_descriptor Vertex
dsatur_degree_compare_functor(const VertexListGraph &_G)