root / custompackages / graph-parser / src / utility.cpp @ 911d0520
History | View | Annotate | Download (6.92 KB)
1 |
//
|
---|---|
2 |
// Created by quynh on 12/15/15.
|
3 |
//
|
4 |
|
5 |
#include "utility.h" |
6 |
using namespace boost; |
7 |
|
8 |
/* outops namespace */
|
9 |
namespace outops {
|
10 |
std::ostream& operator<<(std::ostream& os, const Graph& g) { |
11 |
os << "Graph has: \n"
|
12 |
"---------- " << boost::num_vertices(g) << " vertices\n" |
13 |
"---------- " << boost::num_edges(g) << " edges\n"; |
14 |
|
15 |
std::vector<std::string> verticesVec;
|
16 |
BGL_FORALL_VERTICES(v, g, Graph) verticesVec.push_back(g[v].id); |
17 |
|
18 |
std::vector<std::string> edgesVec;
|
19 |
std::vector<double> costsVec;
|
20 |
BGL_FORALL_EDGES(e, g, Graph) { |
21 |
std::string s = "(" + g[e.m_source].id + ", " + g[e.m_target].id + ") - " + stdhelper::to_string(g[e].cost); |
22 |
edgesVec.push_back(s); |
23 |
costsVec.push_back(g[e].cost); |
24 |
} |
25 |
|
26 |
using namespace boost::spirit::karma; |
27 |
os << "Vertices:\n"
|
28 |
" ";
|
29 |
os << format("(" << auto_ % ", " << ") ", verticesVec); |
30 |
os << "\n";
|
31 |
|
32 |
os << "Edges:\n";
|
33 |
os << format(" " << (auto_ % "\n ") << eol, edgesVec); |
34 |
os << "\n";
|
35 |
|
36 |
return os;
|
37 |
} |
38 |
|
39 |
std::ostream& operator<<(std::ostream& os, std::pair<const Graph&, const VertexIndexPMap&> p) { |
40 |
// ERROR: wrong output.
|
41 |
// I think it's because of copy constructor.
|
42 |
// Check out shell_output/w14
|
43 |
|
44 |
// Calling example:
|
45 |
// outops::operator<<(cout, std::pair<const Graph&, const VertexIndexPMap&>(g_, v_index_pmap_));
|
46 |
Graph g = p.first; |
47 |
VertexIndexPMap v_index_map = p.second; |
48 |
|
49 |
std::list<std::string> outputs;
|
50 |
BGL_FORALL_VERTICES_T(v, g, Graph) { |
51 |
int index = boost::get(v_index_map, v);
|
52 |
cout << g[v].id << ": " << v << endl;
|
53 |
string vertex_id = g[v].id;
|
54 |
outputs.push_back(vertex_id + ": " + stdhelper::to_string(index));
|
55 |
} |
56 |
|
57 |
using namespace boost::spirit::karma; |
58 |
os << "Vertex Index Map:\n";
|
59 |
os << "[\n";
|
60 |
os << format(" " << (auto_ % "\n ") << "]\n", outputs); |
61 |
|
62 |
return os;
|
63 |
} |
64 |
|
65 |
std::ostream& operator<<(std::ostream& os, const vector< vector< int> >& data) { |
66 |
cout << "cout << vector<vector<int> >\n";
|
67 |
int row_size = data.size();
|
68 |
int col_size = 0; |
69 |
if (row_size > 0) { |
70 |
col_size = data[0].size();
|
71 |
} |
72 |
for (int i = 0; i < row_size; ++i) { |
73 |
for (int j = 0; j < col_size; ++j) { |
74 |
os << data[i][j] << " ";
|
75 |
} |
76 |
os << endl; |
77 |
} |
78 |
return os;
|
79 |
} |
80 |
} |
81 |
|
82 |
/* graphext namespace */
|
83 |
namespace graphext {
|
84 |
void id_of_all_vertices(const Graph& g, std::set<std::string>& r) { |
85 |
BGL_FORALL_VERTICES_T(v, g, Graph) { |
86 |
r.insert(g[v].id); |
87 |
} |
88 |
} |
89 |
|
90 |
bool is_connected(const Graph& g, const VertexIndexPMap& v_index_pmap) { |
91 |
Vertex v = *(boost::vertices(g).first); |
92 |
|
93 |
// This one shows a nice way to incoperate the index, discover_time into VertexProperties
|
94 |
// http://www.boost.org/doc/libs/1_58_0/libs/graph/example/bfs-example2.cpp
|
95 |
typedef boost::graph_traits < Graph >::vertices_size_type Size;
|
96 |
typedef boost::iterator_property_map < std::vector< Size >::iterator,
|
97 |
VertexIndexPMap > dtime_pm_t; |
98 |
|
99 |
std::vector < Size > dtime(num_vertices(g)); |
100 |
dtime_pm_t dtime_pm(dtime.begin(), v_index_pmap); |
101 |
|
102 |
Size time = 0;
|
103 |
bfs_time_visitor < dtime_pm_t, Size >vis(dtime_pm, time); |
104 |
boost::breadth_first_search(g, v, boost::vertex_index_map(v_index_pmap).visitor(vis)); |
105 |
|
106 |
if (dtime.size() == boost::num_vertices(g)) {
|
107 |
return true; // BFS discovers all vertices in the graph, so the graph is connected |
108 |
} |
109 |
else {
|
110 |
return false; |
111 |
} |
112 |
} |
113 |
|
114 |
void print_edge(const Graph& g, const Edge& e) { |
115 |
string s = g[boost::source(e, g)].id;
|
116 |
string t = g[boost::target(e, g)].id;
|
117 |
|
118 |
printf("edge (%s, %s)", s.c_str(), t.c_str());
|
119 |
} |
120 |
|
121 |
void print_v_index_std_map(const Graph& g, const VertexIndexStdMap& v_index_std_map) { |
122 |
std::list<std::string> outputs;
|
123 |
|
124 |
VertexIndexStdMap::const_iterator iter; |
125 |
for (iter = v_index_std_map.begin(); iter != v_index_std_map.end(); ++iter) {
|
126 |
outputs.push_back(stdhelper::to_string(iter->second)); |
127 |
// outputs.push_back(stdhelper::to_string(&(iter->first)) + ": " + stdhelper::to_string(iter->second));
|
128 |
} |
129 |
|
130 |
using namespace boost::spirit::karma; |
131 |
cout << "Vertex Index Std Map:\n";
|
132 |
cout << format("[\n " << (auto_ % "\n ") << "\n]\n", outputs); |
133 |
} |
134 |
|
135 |
void print_v_index_pmap(const Graph& g, const VertexIndexPMap& v_index_pmap) { |
136 |
std::list<std::string> outputs;
|
137 |
BGL_FORALL_VERTICES_T(v, g, Graph) { |
138 |
int index = boost::get(v_index_pmap, v);
|
139 |
std::string vertex_id = g[v].id;
|
140 |
// Uncomment to print the address of vertex v
|
141 |
// cout << v << endl;
|
142 |
outputs.push_back(vertex_id + ": " + stdhelper::to_string(index));
|
143 |
} |
144 |
|
145 |
using namespace boost::spirit::karma; |
146 |
cout << "Vertex Index Map:\n";
|
147 |
cout << format("[\n " << (auto_ % "\n ") << "\n]\n", outputs); |
148 |
} |
149 |
|
150 |
void print_e_index_pmap(const Graph& g, const EdgeIndexPMap& e_index_pmap) { |
151 |
std::list<std::string> outputs;
|
152 |
BGL_FORALL_EDGES_T(e, g, Graph) { |
153 |
int index = boost::get(e_index_pmap, e);
|
154 |
std::string source_id = g[boost::source(e, g)].id;
|
155 |
std::string target_id = g[boost::target(e, g)].id;
|
156 |
outputs.push_back("edge (" + source_id + ", " + target_id + ")" + ": " + stdhelper::to_string(index)); |
157 |
} |
158 |
|
159 |
using namespace boost::spirit::karma; |
160 |
cout << "Edge Index Map:\n";
|
161 |
cout << format("[\n " << (auto_ % "\n ") << "\n]\n", outputs); |
162 |
} |
163 |
|
164 |
void write_betweenness_centrality(Graph const& g, std::vector<double> v_centrality_vec, string file_path) { |
165 |
cout << "XXX Writing to File";
|
166 |
// string filepath = "../output/boost_" + fileSuffix + ".csv";
|
167 |
ofstream out_file(file_path.c_str()); |
168 |
|
169 |
Viter vi, ve; |
170 |
size_t i = 0;
|
171 |
if (out_file.is_open()) {
|
172 |
for (boost::tie(vi, ve) = boost::vertices(g); vi != ve; ++vi) {
|
173 |
out_file << g[*vi].id << ", " << v_centrality_vec.at(i) << endl;
|
174 |
++i; |
175 |
} |
176 |
} |
177 |
out_file.close(); |
178 |
|
179 |
cout << "Done Writing BC score to file " << file_path << endl;
|
180 |
} |
181 |
} |
182 |
|
183 |
// GENERAL HELPERS
|
184 |
namespace helper {
|
185 |
void get_file_name_and_extension(string path, string& name, string& ext) { |
186 |
size_t sep = path.find_last_of("\\/");
|
187 |
if (sep != std::string::npos) |
188 |
path = path.substr(sep + 1, path.size() - sep - 1); |
189 |
|
190 |
size_t dot = path.find_last_of(".");
|
191 |
if (dot != std::string::npos) |
192 |
{ |
193 |
name = path.substr(0, dot);
|
194 |
ext = path.substr(dot, path.size() - dot); |
195 |
} |
196 |
else
|
197 |
{ |
198 |
name = path; |
199 |
ext = "";
|
200 |
} |
201 |
} |
202 |
} |