49 #ifndef MAXCLIQUE_DSATUR_COLORING_HPP 50 #define MAXCLIQUE_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> 60 #include <boost/graph/graph_traits.hpp> 61 #include <boost/graph/visitors.hpp> 62 #include <boost/limits.hpp> 63 #if BOOST_VERSION >= 106400 64 #include <boost/serialization/array_wrapper.hpp> 67 #include <boost/numeric/ublas/matrix.hpp> 72 #include <boost/graph/filtered_graph.hpp> 96 template <
typename size_type>
114 template <
class VertexListGraph,
class ColorMap>
115 typename property_traits<ColorMap>::value_type
unsorted_coloring(
const VertexListGraph& G, ColorMap color)
117 using GraphTraits = graph_traits<VertexListGraph>;
118 using Vertex =
typename GraphTraits::vertex_descriptor;
119 using size_type =
typename property_traits<ColorMap>::value_type;
121 size_type max_color = 0;
122 const size_type V = num_vertices(G);
133 typename GraphTraits::vertex_iterator v, vend;
134 for(boost::tie(v, vend) = vertices(G); v != vend; ++v)
136 put(color, *v, V - 1);
141 for(boost::tie(v, vend) = vertices(G); v != vend; ++v)
144 typename GraphTraits::adjacency_iterator v1, vend1;
148 for(boost::tie(v1, vend1) = adjacent_vertices(current, G); v1 != vend1; ++v1)
150 mark[
get(color, *v1)] = i;
161 while(j < max_color && mark[j] == i)
172 put(color, current, j);
181 template <
typename SET_container>
195 void init(SET_container* _support)
199 template <
typename Vertex>
203 return support->find(v) != support->end();
208 template <
typename VertexListGraph,
typename ColorMap,
typename size_type,
typename SET_container>
213 using Vertex =
typename GraphTraits::vertex_descriptor;
219 typename boost::numeric::ublas::matrix<size_type>
ColorAdj;
223 const VertexListGraph&
G;
232 using Induced_Graph = filtered_graph<VertexListGraph, keep_all, select_vertex<SET_container>>;
235 using IG_Vertex =
typename IG_GraphTraits::vertex_descriptor;
240 : num_node(_num_node),
241 BestColoring(_num_node + 1),
242 ColorClass(_num_node, _num_node - 1),
243 valid(_num_node,
true),
244 ColorAdj(_num_node, _num_node),
245 ColorCount(_num_node, 0),
246 DegreeCount(_num_node),
250 HCF(ColorCount, DegreeCount),
253 FG(G, keep_all(), filter)
255 unsigned int i, iheap = 0;
256 heap_container =
new size_type[_num_node - clique_size];
257 for(i = 0; i < num_node; i++)
261 DegreeCount[i] = degree(v, G);
262 heap_container[iheap] = i;
265 boost::numeric::ublas::noalias(ColorAdj) = boost::numeric::ublas::zero_matrix<size_type>(_num_node, _num_node);
269 delete[] heap_container;
276 SET_container GreedySupport, G1_support;
277 typename IG_GraphTraits::vertex_iterator vstart, v, vend;
278 boost::tie(vstart, vend) = vertices(FG);
282 GreedySupport.insert(*v);
285 std::swap(GreedySupport, support);
290 size_type maxdegree = out_degree(*v, FG);
295 size_type tmpdegree = out_degree(*v, FG);
296 if(tmpdegree > maxdegree)
298 maxdegree = tmpdegree;
303 BestClique.insert(selected);
304 typename IG_GraphTraits::adjacency_iterator v1, v1end;
305 boost::tie(v1, v1end) = adjacent_vertices(selected, FG);
306 G1_support.insert(v1, v1end);
307 support = G1_support;
309 boost::tie(vstart, vend) = vertices(FG);
310 }
while(vstart != vend);
311 std::swap(GreedySupport, support);
312 return BestClique.size();
316 typename IG_GraphTraits::vertex_iterator vstart, v, vend;
317 boost::tie(vstart, vend) = vertices(FG);
320 if(BestClique.size() < C.size())
328 std::vector<size_type> color_vec(num_node);
329 ColorMap color(&color_vec.front(),
get(vertex_index, FG));
332 if(support.size() == num_node && k < BestColoring)
335 for(size_type i = 0; i < num_node; ++i)
341 ub = ub <= k + C.size() ? ub : k + C.size();
342 if(ub <= BestClique.size())
344 return BestClique.size();
350 size_type FG_cardinality = 1;
351 size_type mindegree = out_degree(*v, FG);
352 size_type maxdegree = mindegree;
356 size_type tmpdegree = out_degree(*v, FG);
357 if(tmpdegree > maxdegree)
359 maxdegree = tmpdegree;
362 if(tmpdegree < mindegree)
364 mindegree = tmpdegree;
370 if(FG_cardinality + C.size() <= BestClique.size())
372 return BestClique.size();
375 if(mindegree + 1 == FG_cardinality)
377 if(BestClique.size() < C.size() + FG_cardinality)
380 BestClique.insert(vstart, vend);
383 return BestClique.size();
386 SET_container G1_support;
387 typename IG_GraphTraits::adjacency_iterator v1, v1end;
388 boost::tie(v1, v1end) = adjacent_vertices(selected, FG);
389 G1_support.insert(v1, v1end);
390 std::swap(G1_support, support);
392 size_type sizeG1 = MaxCliqueRec(ub);
394 std::swap(G1_support, support);
399 support.erase(selected);
400 size_type sizeG0 = MaxCliqueRec(ub);
401 support.insert(selected);
408 for(i = 0; i < num_node; i++)
410 put(CM,
vertex(i, G), ColorClass[i]);
418 ColorClass[node] = color;
419 typename GraphTraits::adjacency_iterator v, vend;
420 for(boost::tie(v, vend) = adjacent_vertices(
vertex(node, G), G); v != vend; ++v)
422 node1 =
get(vertex_index, G, *v);
423 if(ColorAdj(node1, color) == 0)
427 ColorAdj(node1, color) += 1;
428 DegreeCount[node1]--;
429 assert(DegreeCount[node1] >= 0);
436 ColorClass[node] = 0;
437 typename GraphTraits::adjacency_iterator v, vend;
438 for(boost::tie(v, vend) = adjacent_vertices(
vertex(node, G), G); v != vend; ++v)
440 node1 =
get(vertex_index, G, *v);
441 assert(ColorAdj(node1, color) != 0);
442 ColorAdj(node1, color) -= 1;
443 if(ColorAdj(node1, color) == 0)
447 DegreeCount[node1]++;
453 size_type j, new_val, place;
457 if(current_color + 1 >= BestColoring)
459 return (current_color + 1);
461 if(BestColoring <= lb)
463 return (BestColoring);
468 return (current_color + 1);
473 std::make_heap(heap_container + i - clique_size, heap_container + num_node - clique_size, HCF);
474 place = heap_container[i];
477 for(j = 0; j <= current_color; j++)
479 if(ColorAdj(place, j) == 0)
481 AssignColor(place, j);
482 new_val = SeqColorRec(i + 1, current_color);
483 if(new_val < BestColoring)
485 BestColoring = new_val;
488 RemoveColor(place, j);
489 if(BestColoring <= current_color)
491 return (BestColoring);
495 if(current_color + 2 < BestColoring)
497 AssignColor(place, current_color + 1);
498 new_val = SeqColorRec(i + 1, current_color + 1);
499 if(new_val < BestColoring)
501 BestColoring = new_val;
505 RemoveColor(place, current_color + 1);
507 return (BestColoring);
511 template <
typename VertexListGraph,
typename ColorMap>
514 using GraphTraits = graph_traits<VertexListGraph>;
515 using Vertex =
typename GraphTraits::vertex_descriptor;
516 using size_type =
typename property_traits<ColorMap>::value_type;
518 const size_type num_node = num_vertices(G);
523 size_type lb = 0, val;
graph_traits< VertexListGraph > GraphTraits
property_traits< ColorMap >::value_type unsorted_coloring(const VertexListGraph &G, ColorMap color)
const VertexListGraph & G
This algorithm is to find coloring of a graph Algorithm: Let G = (V,E) be a graph with vertices v_1...
heap_compare_functor(const std::vector< size_type > &_ColorCount, const std::vector< size_type > &_DegreeCount)
size_type MaxCliqueGreedy()
const unsigned int clique_size
maxclique_dsatur_coloring_helper(const VertexListGraph &_G, ColorMap &_CM, const size_type _num_node, size_type &_lb)
graph_traits< Induced_Graph > IG_GraphTraits
size_type SeqColorRec(size_type i, size_type current_color)
void init(SET_container *_support)
boost::numeric::ublas::matrix< size_type > ColorAdj
void AssignColor(size_type node, size_type color)
std::vector< size_type > ColorClass
std::vector< size_type > ColorCount
size_type * heap_container
bool operator()(const Vertex &v) const
filtered_graph< VertexListGraph, keep_all, select_vertex< SET_container > > Induced_Graph
void RemoveColor(size_type node, size_type color)
std::vector< size_type > DegreeCount
property_traits< ColorMap >::value_type maxclique_dsatur_coloring(const VertexListGraph &G, ColorMap color)
static const uint32_t k[]
redefinition of set to manage ordered/unordered structures
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Predicate functor object used to select the proper set of vertices.
typename IG_GraphTraits::vertex_descriptor IG_Vertex
std::vector< bool > valid
bool operator()(size_type i1, size_type i2) const
const std::vector< size_type > & DegreeCount
~maxclique_dsatur_coloring_helper()
heap_compare_functor< size_type > HCF
select_vertex(const select_vertex &sv)
const std::vector< size_type > & ColorCount
select_vertex< SET_container > filter
size_type MaxCliqueRec(size_type ub)
typename GraphTraits::vertex_descriptor Vertex
select_vertex(SET_container *_support)