-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSubgraph.cpp
222 lines (193 loc) · 6.12 KB
/
Subgraph.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
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
/**
Superbubbles
Copyright (C) 2016 Ritu Kundu, Fatima Vayani, Manal Mohamed, Solon P. Pissis
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/** Implements class Subraph
*/
#include "Subgraph.hpp"
namespace supbub{
Subgraph::Subgraph(int64_t n):Graph(n) {
//super();
_reverseMapId = new int64_t[_numVertices];
std::fill_n(_reverseMapId, _numVertices, -1); // set to -1
_offSet = n-2;
_dag = nullptr;
_discovery = nullptr;
_finish = nullptr;
}
Subgraph::~Subgraph() {
delete[] _reverseMapId;
if (_dag != nullptr) {
delete _dag;
delete[] _discovery;
delete[] _finish;
}
}
int64_t
Subgraph::getGlobalId(int64_t v){
if (v < _numVertices && v >=0 ) {
return _reverseMapId[v];
} else {
log("Invalid v : ", v);
return -1;
}
}
void
Subgraph::setGlobalId(int64_t localId, int64_t globalId){
if (localId <_numVertices && localId >=0 ) {
_reverseMapId[localId] = globalId;
} else {
log("Invalid localId : ", localId);
}
}
int64_t
Subgraph::getSourceId(){
return _numVertices-2; // second last vertex
}
int64_t
Subgraph::getTerminalId(){
return _numVertices-1; // last vertex
}
int64_t
Subgraph::getDuplicateId(int64_t v){
if (v < _numVertices && v >=0 ) {
return v + _offSet;
} else {
log("Invalid v : ", v);
return -1;
}
}
int64_t
Subgraph::getOriginalId(int64_t v){
if (v < 2*_offSet && v >= _offSet ) {
return v - _offSet;
} else {
log("Invalid v'' : ", v);
return -1;
}
}
bool
Subgraph::isDuplicateId(int64_t v){
if (v < _numVertices && v >=0 ) {
return !(v < _offSet);
} else {
log("Invalid v : ", v);
return false;
}
}
bool
Subgraph::isAncestor(int64_t anc, int64_t des){
if (anc < _numVertices && anc >=0 && des < _numVertices && des > 0) {
if (_dag != nullptr) {
return (_discovery[des] > _discovery[anc] && _finish[des] < _finish[anc]);
} else {
log("DFS Traversal not made yet. Call getDAG() before making call to this function.", 0);
return false;
}
} else {
log("Invalid ancestor or descendent id : ", anc, des);
return false;
}
}
int64_t Subgraph::getOffset(){
return _offSet;
}
DAG*
Subgraph::getDAG(){
_dag = new DAG(2*_offSet + 2);
_discovery = new int64_t[_numVertices];
_finish = new int64_t[_numVertices];
int64_t_LIST_ITERATOR i;
int64_t newSource = _dag->getSourceId();
int64_t thisSource = getSourceId();
int64_t newTerminal = _dag->getTerminalId();
int64_t thisTerminal = getTerminalId();
/* Add {(r, v' ) | (r, v) ∈ E(G)} */
if (! _adjList[thisSource].empty()) {
for (i = _adjList[thisSource].begin(); i != _adjList[thisSource].end(); ++i) {
if (*i != thisTerminal) {
_dag->addEdge(newSource, *i); // as v and v' have same local-id
}
}
}
/* Add {(v'' , r' ) | (v, r' ) ∈ E(G)} */
if (! _parentList[thisTerminal].empty()) {
for (i = _parentList[thisTerminal].begin(); i != _parentList[thisTerminal].end(); ++i) {
if (*i != thisSource) {
_dag->addEdge(getDuplicateId(*i), newTerminal);
}
}
}
/* Add {(u', v'), (u'', v'') |(u, v) ∈ E(G), (u, v) is not a back edge } and {(u', v'') | (u, v) ∈ E(G), (u, v) is a back edge} */
int64_t source = thisSource;
if (_outDegree[thisSource] == 0) { // no source r, select a random vertex to be source/root
source = 0; // 0 is chosen
}
Subgraph::Color* color = new Subgraph::Color[_numVertices];
std::fill_n( color, _numVertices, WHITE ); // set to false
DFSVisit(source, 0, color);
/* Adjust source and terminal vertices */
int64_t lastDAGID = _dag->numVertices()-2;
if (_outDegree[thisSource] == 0) { // G does not contain r
for (int64_t u=0; u < lastDAGID; ++u) {
if (_dag->getInDegree(u) == 0) { // for every u ∈ V (G ) such that u has no incoming edge in G'
_dag->addEdge(newSource, u);//create an edge (r, u)
}
}
}
if (_inDegree[thisTerminal] == 0) { // G does not contain r'
for (int64_t u=0; u < lastDAGID; ++u) {
if (_dag->getOutDegree(u) == 0) { // for every u ∈ V (G ) such that u has no outgoing edge in G'
_dag->addEdge(u, newTerminal);//create an edge (u, r')
}
}
}
//clean-up
delete[] color;
return _dag;
}
//////////////////////// private ////////////////////////
void
Subgraph::DFSVisit(int64_t u, int64_t& tick, Subgraph::Color* color){
color[u] = GRAY;
_discovery[u] = ++tick;
int64_t_LIST_ITERATOR i;
int64_t thisSource = getSourceId();
int64_t thisTERMINAL = getTerminalId();
if (! _adjList[u].empty()) {
for (i = _adjList[u].begin(); i != _adjList[u].end(); ++i) {
int64_t v = *i;
if (color[v] == WHITE){ // u-v is tree-edge
if (v != thisTERMINAL && u!= thisTERMINAL && v!=thisSource && u!=thisSource) {
_dag->addEdge(u, v); // add u'-v'
_dag->addEdge(getDuplicateId(u), getDuplicateId(v)); // add u''-v''
}
DFSVisit(v, tick, color);
}
else if (color[v] == GRAY) { // u-v is back edge
if (v != thisTERMINAL && u!= thisTERMINAL && v!=thisSource && u!=thisSource) {
_dag->addEdge(u, getDuplicateId(v)); // add u'-v''
}
}
else{// forward or cross edge
if (v != thisTERMINAL && u!= thisTERMINAL && v!=thisSource && u!=thisSource) {
_dag->addEdge(u, v); // add u'-v'
_dag->addEdge(getDuplicateId(u), getDuplicateId(v)); // add u''-v''
}
}
}
}
color[u] = BLACK;
_finish[u] = ++tick;
}
}// end namespace