-
Notifications
You must be signed in to change notification settings - Fork 1
/
RandESU.cpp
253 lines (182 loc) · 7.69 KB
/
RandESU.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: RandESU.cpp
* Author: Wooyoung
*
* Created on October 25, 2017, 10:28 AM
*/
#include "RandESU.h"
/** Determines whether or not to extend based on a given probability, given
as an integer.
precondition: 0.0 <= prob <= 1.0
**/
// return an integer between min and max inclusively
int nextInt(int min, int max) {
return rand() % (max - min + 1) + min;
}
vertex nextVertex(vertex min, vertex max) {
return rand() % (max - min + 1) + min;
}
bool RandESU::shouldExtend(const double prob) {
if (prob == 1.0) {
return true;
}
if (prob == 0.0) {
return false;
}
if (prob > 1.0 || prob < 0.0) {
throw invalid_argument("RAND-ESU probability outside acceptable range (0.0 to 1.0)");
}
// a random number between 0 and 100
int randNum = nextInt(0, 100);
return randNum <= (prob * 100.0);
}
/** Returns true if the node index is exclusive to the given subgraph
(that is, is not already in the subgraph, and is not adjacent to any of
the nodes in the subgraph)
**/
bool RandESU::isExclusive(Graph& graph, vertex node, Subgraph& subgraph) {
for (int i = 0; i < subgraph.getSize(); i++) {
vertex subgraphNode = subgraph.get(i);
if (subgraphNode == node) {
return false;
}
}
for (int i = 0; i < subgraph.getSize(); i++) {
vertex subgraphNode = subgraph.get(i);
if (graph.getAdjacencyList(subgraphNode).count(node) > 0) {
return false;
}
}
return true;
}
/**
* Enumerates all subgraphSize Subgraphs in the input Graph using the
* RAND-ESU algorithm.
*
* @param graph the graph on which to execute RAND-ESU
* @param subgraphSize the size of the target Subgraphs
*/
void RandESU::enumerate(Graph& graph, SubgraphEnumerationResult& subgraphs, int subgraphsize, const vector<double> probs) {
// maintain list of nodes selected so far
std::vector<vertex> selectedVertices;
if (probs[0] == 1.0) // select all nodes
for (int i = 0; i < graph.getSize(); i++)
selectedVertices.push_back(i);
else {
//determine how many nodes should be sampled initially
int numVerticesToSelect = (int) round(probs[0] * graph.getSize());
for (int nVSelected = 0; nVSelected < numVerticesToSelect; ++nVSelected) {
vertex nodeSelected = nextVertex(0, graph.getSize() - 1); // get the node id
// ensure no duplicates
//std::find(vector.begin(), vector.end(), item) != vector.end()
//while (selectedVertices.count(nodeSelected)>0)
while (std::find(selectedVertices.begin(), selectedVertices.end(), nodeSelected) != selectedVertices.end())
nodeSelected = nextVertex(0, graph.getSize() - 1);
selectedVertices.push_back(nodeSelected);
}
}
NautyLink nautylink(subgraphsize, graph.getEdges(), graph.isDirected());
for (vertex v : selectedVertices) {
enumerate(graph, subgraphs, subgraphsize, probs, v, nautylink);
}
}
/**
* Enumerates all subgraphSize Subgraphs for the specified vertice's branch
* of an ESU execution tree using the RAND-ESU algorithm. Allows for more
* control over execution order compared to the enumerate method that does
* not include a vertex parameter.
* @param graph the graph on which to execute RAND-ESU
* @param subgraphs
* @param subgraphSize
* @param probs
* @param vertex
*/
void RandESU::enumerate(Graph& graph, SubgraphEnumerationResult& subgraphs, int subgraphsize, const vector<double> probs, vertex vertexV, NautyLink& nautylink) {
// create a subgraph with given subgraphsize
Subgraph subgraph(subgraphsize);
// create an extends
unordered_set<vertex> adjacencyList = graph.getAdjacencyList(vertexV);
vector<vertex> extends;
unordered_set<vertex>::const_iterator iter = adjacencyList.begin();
while (iter != adjacencyList.end()) {
vertex next = *iter;
//add the vertex to the adjacencyList only if the index is bigger than the vertex
if (next > vertexV)
extends.push_back(next);
iter++;
}
subgraph.add(vertexV); // add to the subgraph, the vertex and its corresponding adjacencylist
//randomly decide whether to extend
if (shouldExtend(probs.at(1))) {
extend(graph, subgraph, extends, probs, subgraphs, nautylink);
}
}
void RandESU::extend(Graph& graph, Subgraph& subgraph, vector<vertex> extension, const vector<double> probs, SubgraphEnumerationResult& subgraphs, NautyLink& nautylink) {
vertex v = subgraph.root();
vector<vertex>::const_iterator wIter = extension.begin();
// optimize by not creating next extension if subgraph is
// 1 node away from completion
if (subgraph.getSize() == subgraph.getOrder() - 1) {
while (wIter != extension.end()) {
//cout << "1. subgraph = " << subgraph << endl;
// cout << "1. Extension=[";
// for (vertex v : extension) cout << v << " ";
// cout << "]" << endl;
vertex w = *wIter++;
// cout << "1. w = " << w << endl;
// check the last value in prob list
if (shouldExtend(probs.at(probs.size() - 1))) {
Subgraph subgraphUnion = subgraph.copy();
subgraphUnion.add(w);
subgraphs.add(subgraphUnion, nautylink);
}
}
}
// otherwise create the extention
while (wIter != extension.end()) {
// cout << "2. subgraph=" << subgraph << endl;
// cout << "2. Extension=[";
// for (vertex v : extension) cout << v << " ";
// cout << "]" << endl;
vertex w = *wIter;
// cout << "2. w = " << w << endl;
extension.erase(wIter);
// cout << "3. After erase Extension=[";
// for (vertex v : extension) cout << v << " ";
// cout << "]" << endl;
// next extension contains at least the current extension
vector<vertex> nextExtension = extension;
// examine each node 'u' from the set of nodes adjacent to 'w'
// and add it to the next extension if it is exclusive
unordered_set<vertex> adjW = graph.getAdjacencyList(w);
//unordered_set<vertex>::const_iterator uIter = adjW.begin();
auto uIter = adjW.begin();
while (uIter != adjW.end()) {
vertex u = *uIter;
if (u > v)
if (isExclusive(graph, u, subgraph)) {
nextExtension.push_back(u);
}
uIter++;
}
// cout << "2. nextExtension=[";
// for (vertex v : nextExtension) cout << v << " ";
// cout << "]" << endl;
// construct a union of w and the existing subgraph
Subgraph subgraphUnion = subgraph.copy();
subgraphUnion.add(w);
// randomly choose whether or not to extend to the next level
// based on the probability vector provided.
// wIter++;
if (shouldExtend(probs.at(subgraphUnion.getSize() - 1))) {
extend(graph, subgraphUnion, nextExtension, probs, subgraphs, nautylink);
}
}
// }
// cout << "Returning from source " << subgraph << endl;
}