-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert_graph_types.cpp
More file actions
executable file
·239 lines (196 loc) · 6.57 KB
/
convert_graph_types.cpp
File metadata and controls
executable file
·239 lines (196 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <boost/algorithm/string/replace.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
//#include <libs/graph/src/read_graphviz_new.cpp>
#include <boost/graph/copy.hpp>
#ifndef TD_TREEDEC
#define TD_TREEDEC
struct _Vertex{
unsigned int id;
};
struct _bag{
std::set<unsigned int> bag;
};
#endif
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, _Vertex> graph_t;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, _bag> decomp_t;
template <typename T_t>
int get_width(T_t &T){
int max = -1;
typename boost::graph_traits<T_t>::vertex_iterator tIt, tEnd;
for(boost::tie(tIt, tEnd) = boost::vertices(T); tIt != tEnd; tIt++){
max = ((int)T[*tIt].bag.size() > max)? (int)T[*tIt].bag.size() : max;
}
return (max-1);
}
template <typename G_t>
void read_dot_graph(std::string &src, G_t &G){
G.clear();
//try to read as a directed graph
try{
typename boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, _Vertex> dotG;
boost::dynamic_properties dp(boost::ignore_other_properties);
dp.property("node_id",get(&_Vertex::id, dotG));
std::ifstream dot_graph(src.c_str());
read_graphviz(dot_graph, dotG, dp);
boost::copy_graph(dotG, G);
}
//try to read as a undirected graph
catch(...){
typename boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, _Vertex> dotG;
boost::dynamic_properties dp(boost::ignore_other_properties);
dp.property("node_id",get(&_Vertex::id, dotG));
std::ifstream dot_graph(src.c_str());
read_graphviz(dot_graph, dotG, dp);
boost::copy_graph(dotG, G);
}
}
/* GR -> DOT */
unsigned int from_gr_to_dot(std::string &fname_in, std::string &fname_out){
std::ifstream fin;
fin.open(fname_in.c_str());
unsigned int errorcode = 0;
std::ofstream fout;
fout.open(fname_out.c_str());
fout << "graph G {\n";
if(!fin.is_open()){
errorcode = 1;
}
else{
std::string line;
std::string delimiter = " ";
int n = 0;
int e = 0;
unsigned int type = 0;
bool exists_descriptor = false;
unsigned int edges_count = 0;
while(std::getline(fin, line) && errorcode == 0){
if(line == "" || line == "\n"){
break;
}
unsigned int c = 0;
std::vector<std::string> tokens;
unsigned int oldpos = 0;
unsigned int newpos = 0;
while(true){
newpos = line.find(delimiter, oldpos);
if(newpos > line.size()){
tokens.push_back(line.substr(oldpos, newpos-oldpos));
break;
}
else{
tokens.push_back(line.substr(oldpos, newpos-oldpos));
}
oldpos = newpos+1;
}
//comment line
if(tokens[0] == "c"){
type = 0;
}
//problem descriptor
else if(tokens[0] == "p"){
if(exists_descriptor){ errorcode = 4; }
else{
exists_descriptor = true;
if(tokens.size() != 4){ errorcode = 2; }
else if(type != 0){ errorcode = 3; }
else if(tokens[1] != "tw"){ errorcode = 5; }
else{
n = atoi(tokens[2].c_str());
e = atoi(tokens[3].c_str());
if(n < 0 || e < 0){ errorcode = 6; }
else{
//prefix of vertex idenfifiers
for(unsigned int i = 0; i < n; i++){
fout << i << ";\n";
}
}
}
}
}
//an edge
else{
edges_count++;
int s, d;
s = atoi(tokens[0].c_str());
d = atoi(tokens[1].c_str());
if(s < 1 || e < 1){ errorcode = 7; }
else{
fout << (s-1) << " -- " << (d-1) << ";\n";
}
}
}
if(errorcode == 0){
if(!exists_descriptor){ errorcode = 8; }
else if(edges_count != e){ errorcode = 9; }
}
fout << "}\n";
fin.close();
fout.close();
}
return 0;
}
/* DOT -> GR */
void from_dot_to_gr(std::string &fname_in, std::string &fname_out){
graph_t G;
read_dot_graph(fname_in, G);
unsigned n = boost::num_vertices(G);
unsigned e = boost::num_edges(G);
std::ofstream fout(fname_out.c_str());
fout << "p " << "tw " << n << " " << e << std::endl;
boost::graph_traits<graph_t>::edge_iterator eIt, eEnd;
for(boost::tie(eIt, eEnd) = boost::edges(G); eIt != eEnd; eIt++){
unsigned s = boost::source(*eIt, G);
unsigned t = boost::target(*eIt, G);
assert( G[s].id < n );
assert( G[t].id < n );
fout << G[s].id+1 << " " << G[t].id+1 << std::endl;
}
fout.close();
}
int main(int argc, char * const * argv){
std::cout.setf( std::ios_base::unitbuf );
std::vector<std::string> sources;
const char* st = argv[0];
const char* dt = argv[0];
if(argc>3){
st = argv[1];
dt = argv[2];
if(!(
(strcmp(st,"dot") == 0 and strcmp(dt,"gr") == 0)
or (strcmp(st,"gr") == 0 and strcmp(dt,"dot") == 0))){
std::cerr << "error: invalid combination of graph types" << std::endl;
exit(1);
}
int x=3;
const char* token = argv[x];
while(true){
if(x == argc){
break;
}
sources.push_back(token);
token = argv[++x];
continue;
}
}else{
std::cout << "usage:\n"
<< argv[0] << " [source type] [dest type] [graphname1, graphname2, ..]\n";
exit(1);
}
for(unsigned int i = 0; i < sources.size(); i++){
if(strcmp(st,"dot") == 0 && strcmp(dt,"gr") == 0){
std::string dest = sources[i];
boost::replace_all(dest, ".dot", ".gr");
from_dot_to_gr(sources[i], dest);
}
if(strcmp(st,"gr") == 0 && strcmp(dt,"dot") == 0){
std::string dest = sources[i];
boost::replace_all(dest, ".gr", ".dot");
from_gr_to_dot(sources[i], dest);
}
}
}