Limbo 3.5.4
Loading...
Searching...
No Matches
ChromaticNumber.h
Go to the documentation of this file.
1
7
8#ifndef LIMBO_ALGORITHMS_COLORING_CHROMATICNUMBER
9#define LIMBO_ALGORITHMS_COLORING_CHROMATICNUMBER
10
11#include <iostream>
12#include <vector>
13#include <set>
14#include <boost/graph/graph_concepts.hpp>
15#include <boost/graph/subgraph.hpp>
17
18using std::vector;
19using std::set;
20using std::cout;
21using std::endl;
22
24namespace limbo
25{
27namespace algorithms
28{
30namespace coloring
31{
32
51template <typename GraphType>
53{
54 public:
56 typedef GraphType graph_type;
57 typedef boost::subgraph<graph_type> subgraph_type;
58 typedef typename boost::graph_traits<graph_type>::vertex_descriptor graph_vertex_type;
60
64 {
66
69 mis_visitor_type(vector<set<graph_vertex_type> >& mMisNode_) : mMisNode(mMisNode_) {}
73
77 template <typename MisType>
78 void mis(MisType const& is)
79 {
80 // only record maximal independent sets
81 if (!mMisNode.empty())
82 {
83 if (mMisNode.front().size() < is.size())
84 mMisNode.clear();
85 else if (mMisNode.front().size() > is.size())
86 return;
87 }
88
89 mMisNode.push_back(set<graph_vertex_type>());
90 for (typename MisType::const_iterator it = is.begin();
91 it != is.end(); ++it)
92 mMisNode.back().insert(*it);
93 }
94 };
95
98 int operator()(subgraph_type g) const
99 {
100 return chromatic_number(g);
101 }
102
103 protected:
110 int chromatic_number(subgraph_type& g) const
111 {
112 int cn = boost::num_vertices(g); // initial chromatic number
113
114 // stop recursion
115 if (cn <= 1) return cn;
116 else if (cn == 2)
117 {
118 BGL_FORALL_EDGES_T(e, g, subgraph_type)
119 {
120 // the only two vertices are connected
121 if (boost::source(e, g) != boost::target(e, g))
122 return 2;
123 }
124 // they are not connected
125 return 1;
126 }
127
130
131#ifdef DEBUG_CHROMATICNUMBER
132#if 0
133 typename boost::property_map<subgraph_type, boost::vertex_index_t>::type vertex_index_map = boost::get(boost::vertex_index, g);
134
135 for (typename vector<set<graph_vertex_type> >::const_iterator it1 = mMisNode.begin();
136 it1 != mMisNode.end(); ++it1)
137 {
138 set<graph_vertex_type> const& sMisNode = *it1;
139
140 for (typename set<graph_vertex_type>::const_iterator it2 = sMisNode.begin();
141 it2 != sMisNode.end(); ++it2)
142 cout << vertex_index_map[*it2] << " ";
143 cout << endl;
144 }
145#endif
146#endif
147
148 for (typename vector<set<graph_vertex_type> >::const_iterator it1 = mMisNode.begin();
149 it1 != mMisNode.end(); ++it1)
150 {
151 set<graph_vertex_type> const& sMisNode = *it1;
152 subgraph_type& g_s = g.create_subgraph(); // subgraph, G \ I
153
154 BGL_FORALL_VERTICES_T(v, g, subgraph_type)
155 {
156 if (!sMisNode.count(v))
157 boost::add_vertex(v, g_s);
158 }
159
160#ifdef DEBUG_CHROMATICNUMBER
161 //boost::print_graph(g_s, vertex_index_map);
162#endif
163 // get chromatic number of complementary MIS graph
164 int comp_cn = chromatic_number(g_s);
165
166 if (cn > comp_cn) cn = comp_cn;
167
168 // the assumption is that all mis have the same size
169 //
170 // if current graph has more than 1 vertices
171 // and we only need 1 color, that is already smallest number of colors
172 // so we can exit early
173 if (boost::num_vertices(g_s) == 0
174 || (boost::num_vertices(g_s) > 0 && cn == 1))
175 break;
176 }
177
178 return cn+1;
179 }
180};
181
182} // namespace coloring
183} // namespace algorithms
184} // namespace limbo
185
186#endif
solve maximum independent sets with maximum cliques
namespace for Limbo.Algorithms.Coloring
namespace for Limbo.algorithms
void max_independent_set(GraphType const &g, VisitorType vis, AlgorithmType const &)
namespace for Limbo
mis_visitor_type(vector< set< graph_vertex_type > > &mMisNode_)
vector< set< graph_vertex_type > > & mMisNode
bind mis nodes