PandA-2024.02
degree_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  */
46 #ifndef DEGREE_COLORING_HPP
47 #define DEGREE_COLORING_HPP
48 
49 #include <boost/config.hpp>
50 #include <boost/graph/properties.hpp>
51 #include <boost/version.hpp>
52 #if BOOST_VERSION >= 104000
53 #include <boost/property_map/property_map.hpp>
54 #else
55 #include <boost/property_map.hpp>
56 #endif
57 #include "custom_set.hpp"
58 #include <algorithm>
59 #include <boost/graph/graph_traits.hpp>
60 #include <boost/graph/visitors.hpp>
61 #include <boost/limits.hpp>
62 #include <boost/tuple/tuple.hpp>
63 #include <vector>
64 
76 namespace boost
77 {
81  template <typename VertexListGraph>
83  {
84  public:
85  template <typename Vertex>
86  bool operator()(Vertex v1, Vertex v2) const
87  {
88  return out_degree(v1, G) < out_degree(v2, G);
89  }
90  explicit degree_compare_functor(const VertexListGraph& _G) : G(_G)
91  {
92  }
93 
94  private:
95  const VertexListGraph& G;
96  };
97 
101  template <class VertexListGraph, class ColorMap>
102  typename property_traits<ColorMap>::value_type degree_coloring(const VertexListGraph& G, ColorMap color)
103  {
104  using GraphTraits = graph_traits<VertexListGraph>;
105  using Vertex = typename GraphTraits::vertex_descriptor;
106  using size_type = typename property_traits<ColorMap>::value_type;
107 
108  size_type max_color = 0;
109  const size_type V = num_vertices(G);
110  if(V == 0)
111  {
112  return 0;
113  }
114 
115  // We need to keep track of which colors are used by
116  // adjacent vertices. We do this by marking the colors
117  // that are used. The mark array contains the mark
118  // for each color. The length of mark is the
119  // number of vertices since the maximum possible number of colors
120  // is the number of vertices.
121  std::vector<size_type> mark(V, std::numeric_limits<size_type>::max BOOST_PREVENT_MACRO_SUBSTITUTION());
122  unsigned iheap = 0, heapsize;
123  // Initialize colors
124  typename GraphTraits::vertex_iterator v, vend;
125  for(boost::tie(v, vend) = vertices(G); v != vend; ++v)
126  {
127  put(color, *v, V - 1);
128  iheap++;
129  }
130  heapsize = iheap;
131  Vertex* heap_container;
132  heap_container = new size_type[iheap];
133  for(boost::tie(v, vend) = vertices(G); v != vend; ++v)
134  {
135  --iheap;
136  heap_container[iheap] = *v;
137  }
139  std::make_heap(heap_container, heap_container + heapsize, HCF);
140 
141  // Determine the color for every vertex one by one
142  for(size_type i = 0; i < heapsize; ++i)
143  {
144  Vertex current = heap_container[0];
145  std::pop_heap(heap_container, heap_container + heapsize - i, HCF);
146  typename GraphTraits::adjacency_iterator v1, v1end;
147 
148  // Mark the colors of vertices adjacent to current.
149  // i can be the value for marking since i increases successively
150  for(boost::tie(v1, v1end) = adjacent_vertices(current, G); v1 != v1end; ++v1)
151  {
152  mark[get(color, *v1)] = i;
153  }
154 
155  // Next step is to assign the smallest un-marked color
156  // to the current vertex.
157  size_type j = 0;
158 
159  // Scan through all useable colors, find the smallest possible
160  // color that is not used by neighbors. Note that if mark[j]
161  // is equal to i, color j is used by one of the current vertex's
162  // neighbors.
163  while(j < max_color && mark[j] == i)
164  {
165  ++j;
166  }
167 
168  if(j == max_color)
169  { // All colors are used up. Add one more color
170  ++max_color;
171  }
172 
173  // At this point, j is the smallest possible color
174  put(color, current, j); // Save the color of vertex current
175  }
176  delete[] heap_container;
177  return max_color;
178  }
179 
180 } // namespace boost
181 
182 #endif
bool operator()(Vertex v1, Vertex v2) const
This algorithm is to find coloring of a graph Algorithm: Let G = (V,E) be a graph with vertices v_1...
#define max
Definition: backprop.h:17
redefinition of set to manage ordered/unordered structures
degree_compare_functor(const VertexListGraph &_G)
const VertexListGraph & G
property_traits< ColorMap >::value_type degree_coloring(const VertexListGraph &G, ColorMap color)
coloring of a graph following the descandant degree of vertices
degree compare functor

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