PandA-2024.02
dsatur2_coloring.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
49 #ifndef DSATUR2_COLORING_HPP
50 #define DSATUR2_COLORING_HPP
51 
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>
57 #else
58 #include <boost/property_map.hpp>
59 #endif
60 #include <boost/graph/adjacency_list.hpp>
61 #include <boost/graph/graph_traits.hpp>
62 #include <boost/graph/visitors.hpp>
63 #include <boost/limits.hpp>
64 #include <boost/tuple/tuple.hpp>
65 #if BOOST_VERSION >= 106400
66 #include <boost/serialization/array_wrapper.hpp>
67 #endif
68 #include <algorithm>
69 #include <boost/numeric/ublas/matrix.hpp>
70 #include <cassert>
71 #include <vector>
72 
92 namespace boost
93 {
97  template <typename size_type>
99  {
100  public:
101  bool operator()(size_type i1, size_type i2) const
102  {
103  size_type c1 = ColorCount[i1], c2 = ColorCount[i2];
104  return !((c1 > c2) || ((c1 == c2) && (DegreeCount[i1] > DegreeCount[i2])));
105  }
106  dsatur2_heap_compare_functor(const std::vector<size_type>& _ColorCount,
107  const std::vector<size_type>& _DegreeCount)
108  : ColorCount(_ColorCount), DegreeCount(_DegreeCount)
109  {
110  }
111 
112  private:
113  const std::vector<size_type>& ColorCount;
114  const std::vector<size_type>& DegreeCount;
115  };
119  template <typename VertexListGraph, typename ColorMap, typename size_type>
121  {
122  private:
123  using GraphTraits = graph_traits<VertexListGraph>;
124  using Vertex = typename GraphTraits::vertex_descriptor;
125  const size_type num_node;
126  std::vector<bool> valid;
127  typename boost::numeric::ublas::matrix<bool> ColorAdj;
128  std::vector<size_type> ColorCount;
129  std::vector<size_type> DegreeCount;
130  const VertexListGraph& G;
131  ColorMap& CM;
132  size_type* heap_container;
134 
135  public:
136  dsatur2_coloring_helper(const VertexListGraph& _G, ColorMap& _CM, const size_type _num_node)
137  : num_node(_num_node),
138  valid(_num_node, true),
139  ColorAdj(_num_node, _num_node),
140  ColorCount(_num_node, 0),
141  DegreeCount(_num_node),
142  G(_G),
143  CM(_CM),
144  HCF(ColorCount, DegreeCount)
145  {
146  unsigned int i, iheap = 0;
147  heap_container = new size_type[_num_node];
148  for(i = 0; i < num_node; i++)
149  {
150  Vertex v = boost::vertex(i, G);
151  DegreeCount[i] = out_degree(v, _G);
152  heap_container[iheap] = i;
153  iheap++;
154  }
155  boost::numeric::ublas::noalias(ColorAdj) = boost::numeric::ublas::zero_matrix<bool>(_num_node, _num_node);
156  }
158  {
159  delete[] heap_container;
160  }
161  // no copy constructor
162  dsatur2_coloring_helper(const dsatur2_coloring_helper& inst) = delete;
163 
164  void AssignColor(size_type node, size_type color)
165  {
166  size_type node1;
167  put(CM, boost::vertex(node, G), color);
168  typename GraphTraits::adjacency_iterator v, vend;
169  for(boost::tie(v, vend) = adjacent_vertices(boost::vertex(node, G), G); v != vend; ++v)
170  {
171  node1 = get(vertex_index, G, *v);
172  if(!ColorAdj(node1, color))
173  {
174  ColorCount[node1]++;
175  }
176  ColorAdj(node1, color) = true;
177  DegreeCount[node1]--;
178  // assert(DegreeCount[node1] >= 0);
179  }
180  }
181 
182  size_type SeqColor()
183  {
184  size_type j, v;
185  size_type max_color = 0;
186 
187  for(size_type i = 0; i < num_node; i++)
188  {
190  std::make_heap(heap_container + i, heap_container + num_node, HCF);
191  v = heap_container[i];
192 
194  for(j = 0; j <= max_color; j++)
195  {
196  if(!ColorAdj(v, j))
197  {
198  AssignColor(v, j);
199  break;
200  }
201  }
202  if(j <= max_color)
203  {
204  continue;
205  }
207  max_color++;
208  put(CM, boost::vertex(v, G), max_color);
209  AssignColor(v, max_color);
210  }
211  return max_color + 1;
212  }
213  };
214 
218  template <typename VertexListGraph, typename ColorMap>
219  typename property_traits<ColorMap>::value_type dsatur2_coloring(const VertexListGraph& G, ColorMap color)
220  {
221  using size_type = typename property_traits<ColorMap>::value_type;
222 
223  const size_type num_node = num_vertices(G);
224  if(num_node == 0)
225  {
226  return 0;
227  }
229 
230  return MDCH.SeqColor();
231  }
232 } // namespace boost
233 
234 #endif
void AssignColor(size_type node, size_type color)
const std::vector< size_type > & ColorCount
helper used to color the graph.
boost::numeric::ublas::matrix< bool > ColorAdj
TVMArray c2[1]
This algorithm is to find coloring of a graph Algorithm: Let G = (V,E) be a graph with vertices v_1...
TVMArray c1[1]
property_traits< ColorMap >::value_type dsatur2_coloring(const VertexListGraph &G, ColorMap color)
coloring of a graph following the DSATUR heuristic (version2)
std::vector< size_type > DegreeCount
dsatur2_heap_compare_functor(const std::vector< size_type > &_ColorCount, const std::vector< size_type > &_DegreeCount)
typename GraphTraits::vertex_descriptor Vertex
dsatur2_heap_compare_functor< size_type > HCF
graph_traits< VertexListGraph > GraphTraits
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
dsatur2_coloring_helper(const VertexListGraph &_G, ColorMap &_CM, const size_type _num_node)
bool operator()(size_type i1, size_type i2) const
const std::vector< size_type > & DegreeCount
std::vector< size_type > ColorCount

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13