Limbo
3.5.4
Toggle main menu visibility
Loading...
Searching...
No Matches
limbo
algorithms
coloring
GreedyColoring.h
Go to the documentation of this file.
1
13
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>
42
class
DsatColoring
43
{
44
public
:
46
typedef
GraphType graph_type;
47
typedef
typename
boost::graph_traits<graph_type>::vertex_descriptor graph_vertex_type;
49
52
class
saturation_degree_type
53
{
54
public
:
57
saturation_degree_type
(
DsatColoring
const
& dc) :
m_dc
(dc) {}
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
}
78
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
116
int
operator()
()
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
limbo::algorithms::coloring::DsatColoring::saturation_degree_type
Definition
GreedyColoring.h:53
limbo::algorithms::coloring::DsatColoring::saturation_degree_type::operator()
bool operator()(graph_vertex_type const &v1, graph_vertex_type const &v2) const
Definition
GreedyColoring.h:82
limbo::algorithms::coloring::DsatColoring::saturation_degree_type::m_dc
DsatColoring const & m_dc
bind DsatColoring class
Definition
GreedyColoring.h:88
limbo::algorithms::coloring::DsatColoring::saturation_degree_type::saturation_degree_type
saturation_degree_type(DsatColoring const &dc)
Definition
GreedyColoring.h:57
limbo::algorithms::coloring::DsatColoring::saturation_degree_type::saturation_degree
int saturation_degree(graph_vertex_type const &v) const
Definition
GreedyColoring.h:62
limbo::algorithms::coloring::DsatColoring::operator()
int operator()()
Definition
GreedyColoring.h:116
limbo::algorithms::coloring::DsatColoring::run
int run()
Definition
GreedyColoring.h:123
limbo::algorithms::coloring::DsatColoring::color_map
map< graph_vertex_type, int > const & color_map() const
Definition
GreedyColoring.h:103
limbo::algorithms::coloring::DsatColoring::m_graph
graph_type const & m_graph
graph
Definition
GreedyColoring.h:167
limbo::algorithms::coloring::DsatColoring::m_mColor
map< graph_vertex_type, int > m_mColor
color map
Definition
GreedyColoring.h:168
limbo::algorithms::coloring::DsatColoring::DsatColoring
DsatColoring(graph_type const &g)
Definition
GreedyColoring.h:93
limbo::algorithms::coloring::DsatColoring::color
int color(graph_vertex_type v) const
Definition
GreedyColoring.h:107
limbo::algorithms::coloring::vector
limbo::algorithms::coloring
namespace for Limbo.Algorithms.Coloring
Definition
BacktrackColoring.h:22
limbo::algorithms
namespace for Limbo.algorithms
Definition
GraphUtility.h:26
limbo
namespace for Limbo
Definition
GraphUtility.h:23
Generated on
for Limbo by
1.17.0