Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_ChromaticNumber.cpp
Go to the documentation of this file.
1 
8 #include <iostream>
9 #include <fstream>
10 #include <string>
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>
17 #include <boost/graph/erdos_renyi_generator.hpp>
18 #include <boost/random/mersenne_twister.hpp>
19 #include <boost/graph/random.hpp>
20 #include <boost/graph/iteration_macros.hpp>
21 
22 #include <boost/version.hpp>
23 #if BOOST_VERSION <= 14601
24 #include <boost/graph/detail/is_same.hpp>
25 #else
26 #include <boost/type_traits/is_same.hpp>
27 #endif
28 
29 using std::cout;
30 using std::endl;
31 using std::ofstream;
32 using std::string;
33 using namespace boost;
34 
37 template <typename GraphType>
38 struct MisVisitor
39 {
40  GraphType& g;
41 
44  MisVisitor(GraphType& g_) : g(g_) {}
47  MisVisitor(MisVisitor const& rhs) : g(rhs.g) {}
48 
52  template <typename MisType>
53  void mis(MisType const& is)
54  {
55  typename property_map<GraphType, vertex_index_t>::type vertex_index_map = get(vertex_index, g);
56 
57 #if 1
58  if (num_vertices(g) < 20)
59  {
60  cout << "independent sets" << endl;
61  for (typename MisType::const_iterator it = is.begin();
62  it != is.end(); ++it)
63  {
64  cout << vertex_index_map[*it] << " ";
65  }
66  cout << endl;
67  }
68 #endif
69  }
70 };
71 
77 int main()
78 {
79  // do not use setS, it does not compile for subgraph
80  // do not use custom property tags, it does not compile for most utilities
81  typedef adjacency_list<vecS, vecS, undirectedS,
82  property<vertex_index_t, std::size_t>,
83  property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
84  property<graph_name_t, string> > graph_type;
85  typedef subgraph<graph_type> subgraph_type;
86  typedef property<vertex_index_t, std::size_t> VertexId;
87  typedef property<edge_index_t, std::size_t> EdgeID;
88  typedef typename boost::graph_traits<subgraph_type>::vertex_descriptor vertex_descriptor;
89 
90 #if 1
91  mt19937 gen;
92  subgraph_type g;
93  int N = 40;
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 #else
100  subgraph_type g;
101 
102  {
103  vertex_descriptor v1 = add_vertex(g);
104  vertex_descriptor v2 = add_vertex(g);
105  vertex_descriptor v3 = add_vertex(g);
106  vertex_descriptor v4 = add_vertex(g);
107  vertex_descriptor v5 = add_vertex(g);
108  add_edge(v1, v2, g);
109  add_edge(v1, v3, g);
110  add_edge(v1, v4, g);
111  add_edge(v2, v3, g);
112  add_edge(v2, v4, g);
113  add_edge(v3, v4, g);
114  add_edge(v3, v5, g);
115  }
116 
117 #endif
118 
119  property_map<subgraph_type, vertex_index_t>::type vertex_index_map = get(vertex_index, g);
120  property_map<subgraph_type, edge_index_t>::type edge_index_map = get(edge_index, g);
121 
122  cout << "original graph" << endl;
123  print_edges2(g, vertex_index_map, edge_index_map);
124  print_graph(g, vertex_index_map);
125 
126  dynamic_properties dp;
127  //dp.property("style", edge_style);
128  //dp.property("pos", vertex_pos);
129  //dp.property("len", weight);
130  dp.property("node_id", get(vertex_index, g));
131  ofstream out("graph.gv");
132  write_graphviz_dp(out, g, dp);
133  system("dot -Tpdf graph.gv -o graph.pdf");
134 
135  // test complement_graph
136  {
137  subgraph_type gp;
138  std::map<vertex_descriptor, vertex_descriptor> mCompG2G;
139  limbo::algorithms::complement_graph(g, gp, mCompG2G);
140 
141  cout << "complement graph" << endl;
142  BGL_FORALL_VERTICES_T(v, gp, subgraph_type)
143  {
144  cout << vertex_index_map[mCompG2G[v]] << " <--> ";
145  boost::graph_traits<subgraph_type>::out_edge_iterator e, e_end;
146  for (boost::tie(e, e_end) = out_edges(v, gp); e != e_end; ++e)
147  cout << vertex_index_map[mCompG2G[target(*e, gp)]] << " ";
148  cout << endl;
149  }
150  }
151 
152  // test max_independent_set
153  {
155  }
156 
157  // test LawlerChromaticNumber
158  {
160  cout << "chromatic number = " << lcn(g) << endl;
161  }
162 
163  // test DsatColoring
164  {
166  cout << "dsat coloring number = " << dc() << endl;
167  }
168 
169  return 0;
170 }
Boost.Geometry.
Definition: GdsObjects.h:724
void complement_graph(GraphType const &g, GraphType &gp, std::map< typename boost::graph_traits< GraphType >::vertex_descriptor, typename boost::graph_traits< GraphType >::vertex_descriptor > &mCompG2G)
get the complement graph of original graph
Definition: GraphUtility.h:34
MisVisitor(GraphType &g_)
void mis(MisType const &is)
void max_independent_set(GraphType const &g, VisitorType vis, AlgorithmType const &)
GraphType & g
graph
int main()
coloring a graph with saturation degree based heuristics
MisVisitor(MisVisitor const &rhs)
return chromatic number of a graph