Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GreedyColoring.h
Go to the documentation of this file.
1 
14 #ifndef LIMBO_ALGORITHMS_COLORING_GREEDYCOLORING
15 #define LIMBO_ALGORITHMS_COLORING_GREEDYCOLORING
16 
17 #include <iostream>
18 #include <vector>
19 #include <set>
20 #include <map>
21 #include <boost/graph/graph_concepts.hpp>
22 using std::cout;
23 using std::endl;
24 using std::vector;
25 using std::set;
26 using std::map;
27 
29 namespace limbo
30 {
32 namespace algorithms
33 {
35 namespace coloring
36 {
37 
41 template <typename GraphType>
43 {
44  public:
46  typedef GraphType graph_type;
47  typedef typename boost::graph_traits<graph_type>::vertex_descriptor graph_vertex_type;
49 
53  {
54  public:
58 
62  int saturation_degree(graph_vertex_type const& v) const
63  {
64  set<int> sColor; // used colors
65  typename boost::graph_traits<graph_type>::out_edge_iterator ite, ite_end;
66  for (boost::tie(ite, ite_end) = boost::out_edges(v, m_dc.m_graph);
67  ite != ite_end; ++ite)
68  {
69  BOOST_AUTO(found, m_dc.m_mColor.find(boost::target(*ite, m_dc.m_graph)));
70  assert(found != m_dc.m_mColor.end());
71  int color = found->second;
72  if (color >= 0)
73  sColor.insert(color);
74  }
75 
76  return sColor.size();
77  }
82  bool operator()(graph_vertex_type const& v1, graph_vertex_type const& v2) const
83  {
84  return this->saturation_degree(v1) < this->saturation_degree(v2)
85  || boost::out_degree(v1, m_dc.m_graph) < boost::out_degree(v2, m_dc.m_graph);
86  }
87  protected:
88  DsatColoring const& m_dc;
89  };
90 
93  DsatColoring(graph_type const& g) : m_graph(g)
94  {
95  BGL_FORALL_VERTICES_T(v, g, graph_type)
96  {
97  m_mColor[v] = -1;
98  }
99  }
100 
103  map<graph_vertex_type, int> const& color_map() const {return m_mColor;}
107  int color(graph_vertex_type v) const
108  {
109  BOOST_AUTO(found, m_mColor.find(v));
110  if (found == m_mColor.end()) return -1;
111  else return found->second;
112  }
113 
117  {
118  return this->run();
119  }
120  protected:
123  int run()
124  {
125  vector<graph_vertex_type> vNode;
126  vNode.reserve(boost::num_vertices(m_graph));
127  BGL_FORALL_VERTICES_T(v, m_graph, graph_type)
128  {
129  vNode.push_back(v);
130  }
131 
132  int color_cnt = 0; // total number of colors used
133  // color assignment starts from 0 to color_cnt-1
134  while (!vNode.empty())
135  {
136  // choose a node
137  typename vector<graph_vertex_type>::iterator itv = std::max_element(vNode.begin(), vNode.end(), saturation_degree_type(*this));
138 
139  // assign color to current node
140  set<int> sUsedColor; // used colors
141  typename boost::graph_traits<graph_type>::out_edge_iterator ite, ite_end;
142  for (boost::tie(ite, ite_end) = boost::out_edges(*itv, m_graph);
143  ite != ite_end; ++ite)
144  {
145  int color = m_mColor[boost::target(*ite, m_graph)];
146  sUsedColor.insert(color);
147  }
148  for (int i = 0; i < color_cnt+1; ++i)
149  {
150  if (!sUsedColor.count(i))
151  {
152  m_mColor[*itv] = i;
153  break;
154  }
155  }
156  assert(m_mColor[*itv] >= 0);
157  color_cnt = std::max(color_cnt, 1+m_mColor[*itv]);
158 
159  // erase itv
160  *itv = vNode.back();
161  vNode.pop_back();
162  }
163 
164  return color_cnt;
165  }
166 
167  graph_type const& m_graph;
168  map<graph_vertex_type, int> m_mColor;
169 
170 };
171 
172 } // namespace coloring
173 } // namespace algorithms
174 } // namespace limbo
175 
176 #endif
map< graph_vertex_type, int > const & color_map() const
map< graph_vertex_type, int > m_mColor
color map
bool operator()(graph_vertex_type const &v1, graph_vertex_type const &v2) const
namespace for Limbo
Definition: GraphUtility.h:22
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
Definition: Math.h:61
DsatColoring const & m_dc
bind DsatColoring class
int color(graph_vertex_type v) const