-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_input_to_gspan.cpp
83 lines (77 loc) · 3.02 KB
/
convert_input_to_gspan.cpp
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
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include<cmath>
#include<string>
#include<sstream>
#include<iostream>
using namespace std;
vector<std::string> split(std::string &strToSplit, char delimeter)
{
std::stringstream ss(strToSplit);
std::string item;
std::vector<std::string> splittedStrings;
while (std::getline(ss, item, delimeter))
{
splittedStrings.push_back(item);
}
return splittedStrings;
}
int main(int argc, char** argv){
ios_base::sync_with_stdio(false);
string in_file = argv[1];
string out_file = argv[2];
ifstream inFile;
inFile.open(in_file);
stringstream strStream;
strStream << inFile.rdbuf();
string str = strStream.str();
vector<string> data = split(str,'\n');
ofstream ofile(out_file);
unordered_map<string, int> label_to_ct;
int ct_label = 1;
int file_ct = 0;
while(file_ct < data.size()){
string line = data[file_ct];
if (line.size() > 0 && line[0] == '#'){
int num_nodes = 0;
int num_edges = 0;
string graph_id = line.substr(1);
ofile << "t # " << graph_id << "\n";
file_ct++;
num_nodes = stoi(data[file_ct]);
for(int i =0;i < num_nodes;i++){
file_ct++;
string label = data[file_ct];
if(label_to_ct.find(label) == label_to_ct.end()){
label_to_ct[label] = ct_label;
ct_label++;
}
ofile << "v " << i << " " << label_to_ct[label] << "\n";
}
file_ct++;
num_edges = stoi(data[file_ct]);
for(int i = 0;i < num_edges;i++){
int start_node;
int end_node;
string edge_label;
file_ct ++;
istringstream line_stream(data[file_ct]);
line_stream >> start_node >> end_node >> edge_label;
if (start_node > end_node){
int tmp = start_node;start_node = end_node; end_node =tmp;
}
if(label_to_ct.find(edge_label) == label_to_ct.end()){
label_to_ct[edge_label] = ct_label;
ct_label += 1;
}
ofile << "e " << start_node <<" " << end_node << " " << label_to_ct[edge_label] << "\n";
}
}
file_ct += 1;
}
}