PandA-2024.02
dsatur_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 DSATUR_COLORING_HPP
50 #define DSATUR_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 "custom_set.hpp"
61 #include <algorithm>
62 #include <boost/graph/graph_traits.hpp>
63 #include <boost/graph/visitors.hpp>
64 #include <boost/limits.hpp>
65 #include <boost/tuple/tuple.hpp>
66 #include <vector>
67 
85 namespace boost
86 {
87  template <typename size_type, typename VertexListGraph>
89  {
90  public:
91  bool operator()(size_type v1, size_type v2) const
92  {
93  return degree(vertex(v1, G), G) >= degree(vertex(v2, G), G);
94  }
95  explicit dsatur_degree_compare_functor(const VertexListGraph& _G) : G(_G)
96  {
97  }
98 
99  private:
100  const VertexListGraph& G;
101  };
102 
103  template <typename size_type, typename VertexListGraph>
105  {
106  public:
107  bool operator()(size_type v1) const
108  {
109  return degree(vertex(v1, G), G) < v2_degree;
110  }
111  dsatur_degree_predicate_functor(const VertexListGraph& _G, size_type& _v2)
112  : G(_G), v2_degree(degree(vertex(_v2, _G), _G))
113  {
114  }
115 
116  private:
117  const VertexListGraph& G;
118  size_type v2_degree;
119  };
120 
121  template <typename size_type>
123  {
124  size_type data;
127  explicit dsatur_d_list(size_type _data) : data(_data), prev(nullptr), next(nullptr)
128  {
129  }
130  };
131 
132  template <typename VertexListGraph, typename ColorMap, typename size_type>
134  {
135  private:
136  using GraphTraits = graph_traits<VertexListGraph>;
137  using Vertex = typename GraphTraits::vertex_descriptor;
138  const size_type num_node;
139  std::vector<dsatur_d_list<size_type>*> satur_to_list;
140  std::vector<size_type> vertex_to_satur;
141  typename std::vector<dsatur_d_list<size_type>*> vertex_to_iter;
142  std::vector<CustomUnorderedSet<size_type>> color_set;
143  const VertexListGraph& G;
144  ColorMap& CM;
145 
146  public:
147  dsatur_coloring_helper(const VertexListGraph& _G, ColorMap& _CM, const size_type _num_node)
148  : num_node(_num_node),
149  satur_to_list(1),
150  vertex_to_satur(_num_node, 0),
151  vertex_to_iter(_num_node),
152  color_set(_num_node),
153  G(_G),
154  CM(_CM)
155  {
156  std::vector<size_type> tmp_vertex_degree_ordering(_num_node);
157  for(size_type i = 0; i < _num_node; i++)
158  {
159  tmp_vertex_degree_ordering[i] = i;
160  }
161  std::stable_sort(tmp_vertex_degree_ordering.begin(), tmp_vertex_degree_ordering.end(),
163  dsatur_d_list<size_type>* last = nullptr;
164  for(size_type i = 0; i < _num_node; i++)
165  {
166  size_type curr_vert = tmp_vertex_degree_ordering[i];
167  auto* dobj = new dsatur_d_list<size_type>(curr_vert);
168  if(!last)
169  {
170  last = satur_to_list[0] = dobj;
171  }
172  else
173  {
174  last->next = dobj;
175  dobj->prev = last;
176  last = dobj;
177  }
178  vertex_to_iter[curr_vert] = dobj;
179  }
180  }
181 
182  size_type SeqColor()
183  {
184  size_type maxsatur = 0;
185  size_type maxclr = 0;
186  for(size_type i = 0; i < num_node; i++)
187  {
189  while(!satur_to_list[maxsatur])
190  {
191  maxsatur--;
192  }
194  size_type v = satur_to_list[maxsatur]->data;
195  dsatur_d_list<size_type>* to_be_deleted = satur_to_list[maxsatur];
196  satur_to_list[maxsatur] = satur_to_list[maxsatur]->next;
197  delete to_be_deleted;
198  if(satur_to_list[maxsatur])
199  {
200  satur_to_list[maxsatur]->prev = nullptr;
201  }
202  vertex_to_iter[v] = nullptr;
204  size_type color = 0;
205  while(color_set[v].find(color) != color_set[v].end())
206  {
207  color++;
208  }
209  color_set[v].insert(color);
210  if(maxclr < color)
211  {
212  maxclr = color;
213  }
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)
218  {
219  size_type w = get(vertex_index, G, *vit);
220  if(vertex_to_iter[w])
221  {
223  if(color_set[w].find(color) == color_set[w].end())
224  {
226  color_set[w].insert(color);
227 
229  size_type w_satur = vertex_to_satur[w];
230  dsatur_d_list<size_type>* vertex_to_be_moved = vertex_to_iter[w];
231  if(vertex_to_be_moved->prev == nullptr)
232  {
233  satur_to_list[w_satur] = vertex_to_be_moved->next;
234  if(vertex_to_be_moved->next)
235  {
236  satur_to_list[w_satur]->prev = nullptr;
237  }
238  }
239  else if(vertex_to_be_moved->next == nullptr)
240  {
241  vertex_to_be_moved->prev->next = nullptr;
242  }
243  else
244  {
245  vertex_to_be_moved->prev->next = vertex_to_be_moved->next;
246  vertex_to_be_moved->next->prev = vertex_to_be_moved->prev;
247  }
249  w_satur++;
250  vertex_to_satur[w] = w_satur;
251  if(maxsatur < w_satur)
252  {
253  maxsatur = w_satur;
254  satur_to_list.resize(maxsatur + 1);
255  satur_to_list[maxsatur] = nullptr;
256  }
259  if(satur_to_list[w_satur] == nullptr)
260  {
261  satur_to_list[w_satur] = vertex_to_be_moved;
262  vertex_to_be_moved->prev = nullptr;
263  vertex_to_be_moved->next = nullptr;
264  }
265  else if(DPF(satur_to_list[w_satur]->data))
266  {
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;
271  }
272  else
273  {
274  dsatur_d_list<size_type>* iter = satur_to_list[w_satur];
275  while(iter->next && !DPF(iter->next->data))
276  {
277  iter = iter->next;
278  }
279 
280  vertex_to_be_moved->next = iter->next;
281  if(iter->next)
282  {
283  iter->next->prev = vertex_to_be_moved;
284  }
285  iter->next = vertex_to_be_moved;
286  vertex_to_be_moved->prev = iter;
287  }
288  }
289  }
290  }
291  }
292  return maxclr + 1;
293  }
294  };
298  template <typename VertexListGraph, typename ColorMap>
299  typename property_traits<ColorMap>::value_type dsatur_coloring(const VertexListGraph& G, ColorMap color)
300  {
301  using GraphTraits = graph_traits<VertexListGraph>;
302  using Vertex = typename GraphTraits::vertex_descriptor;
303  using size_type = typename property_traits<ColorMap>::value_type;
304 
305  const size_type num_node = num_vertices(G);
306  if(num_node == 0)
307  {
308  return 0;
309  }
311 
312  return DCH.SeqColor();
313  }
314 } // namespace boost
315 
316 #endif
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
redefinition of set to manage ordered/unordered structures
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
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)
typename GraphTraits::vertex_descriptor Vertex
dsatur_degree_compare_functor(const VertexListGraph &_G)

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