11 #include <boost/graph/graphviz.hpp>
12 #include <boost/graph/graph_utility.hpp>
13 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/graph/undirected_graph.hpp>
16 #include <boost/graph/erdos_renyi_generator.hpp>
17 #include <boost/random/mersenne_twister.hpp>
18 #include <boost/graph/random.hpp>
19 #include <boost/graph/iteration_macros.hpp>
21 #include <boost/version.hpp>
22 #if BOOST_VERSION <= 14601
23 #include <boost/graph/detail/is_same.hpp>
25 #include <boost/type_traits/is_same.hpp>
34 using namespace boost;
39 typedef adjacency_list<vecS, vecS, undirectedS,
40 property<vertex_index_t, std::size_t, property<vertex_color_t, int> >,
41 property<edge_index_t, std::size_t, property<edge_weight_t, double> >,
42 property<graph_name_t, string> > graph_type;
43 typedef subgraph<graph_type> subgraph_type;
44 typedef property<vertex_index_t, std::size_t> VertexId;
45 typedef property<edge_index_t, std::size_t> EdgeID;
46 typedef graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
47 typedef graph_traits<graph_type>::edge_descriptor edge_descriptor;
48 typedef property_map<graph_type, edge_weight_t>::type edge_weight_map_type;
49 typedef property_map<graph_type, vertex_color_t>::type vertex_color_map_type;
56 vertex_descriptor a = boost::add_vertex(g);
57 vertex_descriptor b = boost::add_vertex(g);
58 vertex_descriptor c = boost::add_vertex(g);
59 vertex_descriptor d = boost::add_vertex(g);
60 vertex_descriptor e = boost::add_vertex(g);
61 boost::add_edge(a, b, g);
62 boost::add_edge(a, c, g);
63 boost::add_edge(a, d, g);
64 boost::add_edge(a, e, g);
65 boost::add_edge(b, c, g);
66 boost::add_edge(b, e, g);
67 boost::add_edge(c, d, g);
68 boost::add_edge(d, e, g);
70 BOOST_AUTO(edge_weight_map,
get(edge_weight, g));
71 graph_traits<graph_type>::edge_iterator eit, eit_end;
72 for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit)
74 if (source(*eit, g) != a || target(*eit, g) != d)
75 edge_weight_map[*eit] = 1;
77 edge_weight_map[*eit] = -1;
94 std::vector<vertex_descriptor> vertex_set;
95 std::vector< std::pair<vertex_descriptor, vertex_descriptor> > edge_set;
96 generate_random_graph(g, N, N * 2, gen,
97 std::back_inserter(vertex_set),
98 std::back_inserter(edge_set));
99 BOOST_AUTO(edge_weight_map,
get(edge_weight, g));
101 graph_traits<graph_type>::edge_iterator eit, eit_end;
102 for (tie(eit, eit_end) = edges(g); eit != eit_end; ++eit, ++i)
106 edge_weight_map[*eit] = -1;
109 edge_weight_map[*eit] = 1;
124 ifstream in (filename.c_str());
128 typedef adjacency_list<vecS, vecS, undirectedS,
129 property<vertex_index_t, std::size_t, property<vertex_color_t, int, property<vertex_name_t, std::size_t> > >,
130 property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
131 property<graph_name_t, string> > tmp_graph_type;
133 dynamic_properties tmpdp;
134 tmpdp.property(
"node_id",
get(vertex_name, tmpg));
135 tmpdp.property(
"label",
get(vertex_name, tmpg));
136 tmpdp.property(
"weight",
get(edge_weight, tmpg));
137 tmpdp.property(
"label",
get(edge_weight, tmpg));
138 assert(read_graphviz(in, tmpg, tmpdp,
"node_id"));
141 graph_type g (num_vertices(tmpg));
142 graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
143 for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
145 size_t name =
get(vertex_name, tmpg, *vit);
146 int color =
get(vertex_color, tmpg, *vit);
147 put(vertex_color, g, (vertex_descriptor)name, color);
150 graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
151 for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
153 size_t s_name =
get(vertex_name, tmpg, source(*eit, tmpg));
154 size_t t_name =
get(vertex_name, tmpg, target(*eit, tmpg));
155 pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
157 int weight =
get(edge_weight, g, *eit);
158 put(edge_weight, g, pe.first, weight);
161 #ifdef DEBUG_SDPCOLORING
162 dynamic_properties dp;
163 dp.property(
"id",
get(vertex_index, g));
164 dp.property(
"node_id",
get(vertex_index, g));
165 dp.property(
"label",
get(vertex_index, g));
166 dp.property(
"weight",
get(edge_weight, g));
167 dp.property(
"label",
get(edge_weight, g));
168 ofstream out (
"graph_init.gv");
169 write_graphviz_dp(out, g, dp,
string(
"id"));
171 system(
"dot -Tpdf graph_init.gv -o graph_init.pdf");
189 int main(
int argc,
char** argv)
199 cout <<
"cost = " << cost << endl;
int main(int argc, char **argv)
double real_graph(string const &filename)
double simple_graph()
test 1: a simple graph
virtual double stitch_weight() const
graph coloring algorithm based on semidefinite programming (SDP)
virtual void color_num(ColorNumType cn)
double random_graph()
test 2: a random graph