Limbo 3.5.4
Loading...
Searching...
No Matches
test_GraphSimplification.cpp
Go to the documentation of this file.
1
7
8#include <iostream>
10#include <boost/graph/graphviz.hpp>
11#include <boost/graph/graph_utility.hpp>
12#include <boost/graph/adjacency_list.hpp>
13#include <boost/graph/undirected_graph.hpp>
15#include <boost/version.hpp>
16#if BOOST_VERSION <= 14601
17#include <boost/graph/detail/is_same.hpp>
18#else
19#include <boost/type_traits/is_same.hpp>
20#endif
21
22using std::cout;
23using std::endl;
24using std::ifstream;
25using std::ofstream;
26using std::string;
27using std::pair;
28using namespace boost;
29
31// do not use setS, it does not compile for subgraph
32// do not use custom property tags, it does not compile for most utilities
33typedef adjacency_list<vecS, vecS, undirectedS,
34 property<vertex_index_t, std::size_t, property<vertex_color_t, int> >,
35 property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
36 property<graph_name_t, string> > graph_type;
37typedef subgraph<graph_type> subgraph_type;
38typedef property<vertex_index_t, std::size_t> VertexId;
39typedef property<edge_index_t, std::size_t> EdgeID;
40typedef graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
41typedef graph_traits<graph_type>::edge_descriptor edge_descriptor;
42typedef property_map<graph_type, edge_weight_t>::type edge_weight_map_type;
43typedef property_map<graph_type, vertex_color_t>::type vertex_color_map_type;
45
48void realGraph(string const& filename)
49{
50 ifstream in (filename.c_str());
51
52 // the graphviz reader in boost cannot specify vertex_index_t
53 // I have to create a temporary graph and then construct the real graph
54 typedef adjacency_list<vecS, vecS, undirectedS,
55 property<vertex_index_t, std::size_t, property<vertex_color_t, int, property<vertex_name_t, std::size_t> > >,
56 property<edge_index_t, std::size_t, property<edge_weight_t, int> >,
57 property<graph_name_t, string> > tmp_graph_type;
58 tmp_graph_type tmpg;
59 dynamic_properties tmpdp;
60 tmpdp.property("node_id", get(vertex_name, tmpg));
61 tmpdp.property("label", get(vertex_name, tmpg));
62 tmpdp.property("weight", get(edge_weight, tmpg));
63 tmpdp.property("label", get(edge_weight, tmpg));
64 std::cout << "read_graphviz\n";
65 limboAssert(read_graphviz(in, tmpg, tmpdp, "node_id"));
66
67 std::cout << "num_vertices: " << num_vertices(tmpg) << "\n";
68 std::cout << "num_edges: " << num_edges(tmpg) << "\n";
69
70 // real graph
71 graph_type g (num_vertices(tmpg));
72 graph_traits<tmp_graph_type>::vertex_iterator vit, vit_end;
73 for (tie(vit, vit_end) = vertices(tmpg); vit != vit_end; ++vit)
74 {
75 size_t name = get(vertex_name, tmpg, *vit);
76 int color = get(vertex_color, tmpg, *vit);
77 put(vertex_color, g, (vertex_descriptor)name, color);
78 }
79
80 graph_traits<tmp_graph_type>::edge_iterator eit, eit_end;
81 for (tie(eit, eit_end) = edges(tmpg); eit != eit_end; ++eit)
82 {
83 size_t s_name = get(vertex_name, tmpg, source(*eit, tmpg));
84 size_t t_name = get(vertex_name, tmpg, target(*eit, tmpg));
85 pair<edge_descriptor, bool> pe = add_edge(s_name, t_name, g);
86 limboAssert(pe.second);
87 int weight = get(edge_weight, g, *eit);
88 put(edge_weight, g, pe.first, weight);
89 }
90
91#ifdef DEBUG_GRAPHSIMPLIFICATION
92 dynamic_properties dp;
93 dp.property("id", get(vertex_index, g));
94 dp.property("node_id", get(vertex_index, g));
95 dp.property("label", get(vertex_index, g));
96 dp.property("weight", get(edge_weight, g));
97 dp.property("label", get(edge_weight, g));
98 ofstream out ("graph_init.gv");
99 write_graphviz_dp(out, g, dp, string("id"));
100 out.close();
101 system("dot -Tpdf graph_init.gv -o graph_init.pdf");
102#endif
103
104 //test relaxed LP based coloring
106 simplification_type gs (g, 3);
107 std::vector<int> vPrecolor (num_vertices(g), -1);
108 if (vPrecolor.size() > 0) vPrecolor[0] = 0;
109 if (vPrecolor.size() > 3) vPrecolor[3] = 0;
110 if (vPrecolor.size() > 4) vPrecolor[4] = 0;
111 gs.precolor(vPrecolor.begin(), vPrecolor.end());
112#if 0
113 gs.hide_small_degree();
114 gs.write_graph_dot("graph_simpl1");
115 //gs.merge_subK4();
116 gs.biconnected_component();
117 //gs.connected_component();
118#else
119 gs.simplify(simplification_type::HIDE_SMALL_DEGREE | simplification_type::BICONNECTED_COMPONENT);
120#endif
121 gs.write_graph_dot("graph_simpl3");
122 gs.write_simplified_graph_dot("graph_simpl_merge");
123
124 in.close();
125}
126
132int main(int argc, char** argv)
133{
134 if (argc < 2)
135 {
136 std::cout << "ERROR: at least one input graph required\n";
137 return 1;
138 }
139 realGraph(argv[1]);
140
141 return 0;
142}
assertion with message
#define limboAssert(condition)
custom assertion without message
Definition AssertMsg.h:36
Various graph simplification techniques for graph coloring. Some of them can also be used in other ap...
Boost.Geometry.
Definition GdsObjects.h:740
int main()
void realGraph(string const &filename)