-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.cc
137 lines (121 loc) · 2.8 KB
/
graph.cc
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
#include "graph.h"
#include <boost/algorithm/string.hpp>
#include <queue>
#include <unordered_set>
using boost::is_any_of;
using boost::split;
int VelvetNumToMyNum(int x) {
if (x > 0) {
return (x-1)*2;
} else {
return (-x-1)*2+1;
}
}
pair<int, int> LoadArc(istream& is) {
string arc_str;
if (!getline(is, arc_str)) {
return make_pair(-1, -1);
}
if (arc_str.substr(0, 3) != "ARC") {
return make_pair(-1, -1);
}
vector<string> arc_tokens;
split(arc_tokens, arc_str, is_any_of("\t"));
return make_pair(VelvetNumToMyNum(stoi(arc_tokens[1])),
VelvetNumToMyNum(stoi(arc_tokens[2])));
}
Graph* LoadGraph(const string &filename) {
ifstream is(filename);
if (!is) return NULL;
return LoadGraph(is);
}
Graph* LoadGraph(istream &is) {
string header;
getline(is, header);
vector<string> header_parts;
split(header_parts, header, is_any_of("\t"));
Graph* g = new Graph();
int n_nodes = stoi(header_parts[0]);
g->k_ = stoi(header_parts[2]);
for (int i = 0; i < n_nodes; i++) {
Node *a, *b;
tie(a, b) = LoadNode(is, i, g);
g->nodes_.push_back(a);
g->nodes_.push_back(b);
}
while (is) {
int a, b;
tie(a, b) = LoadArc(is);
if (a != -1) {
g->nodes_[a]->AddNext(g->nodes_[b]);
g->nodes_[b]->rc_->AddNext(g->nodes_[a]->rc_);
}
}
return g;
}
vector<Node*> Graph::GetBigNodes(int threshold) const {
vector<Node*> ret;
for (size_t i = 0; i < nodes_.size(); i+= 2) {
if (nodes_[i]->IsBig(threshold)) {
ret.push_back(nodes_[i]);
}
}
return ret;
}
vector<Node*> Graph::ReachForwardWithThreshold(Node* start, int threshold) const {
unordered_set<int> visited;
queue<Node*> fr;
fr.push(start);
visited.insert(start->id_);
vector<Node*> ret;
while (!fr.empty()) {
Node *x = fr.front();
fr.pop();
ret.push_back(x);
for (auto &nx: x->next_) {
if (nx->IsBig(threshold)) {
continue;
}
if (visited.count(nx->id_)) {
continue;
}
visited.insert(nx->id_);
fr.push(nx);
}
}
return ret;
}
vector<Node*> Graph::ReachLocalWithThreshold(Node* start, int threshold) const {
unordered_set<int> visited;
queue<Node*> fr;
fr.push(start);
visited.insert(start->id_);
vector<Node*> ret;
while (!fr.empty()) {
Node *x = fr.front();
fr.pop();
ret.push_back(x);
for (auto &nx: x->next_) {
if (nx->IsBig(threshold)) {
continue;
}
if (visited.count(nx->id_)) {
continue;
}
visited.insert(nx->id_);
fr.push(nx);
}
for (auto &nxr: x->rc_->next_) {
auto &nx = nxr->rc_;
if (nx->IsBig(threshold)) {
continue;
}
if (visited.count(nx->id_)) {
continue;
}
visited.insert(nx->id_);
fr.push(nx);
}
}
return ret;
}