/
arc_core_digraph_algorithm_bft.js
299 lines (253 loc) · 12.8 KB
/
arc_core_digraph_algorithm_bft.js
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright (C) 2014-2016 Christopher D. Russell
This library is published under the MIT License and is part of the
Encapsule Project System in Cloud (SiC) open service architecture.
Please follow https://twitter.com/Encapsule for news and updates
about jsgraph and other time saving libraries that do amazing things
with in-memory data on Node.js and HTML.
*/
/*
Inspired by the design of the Boost Graph Library (BGL)
http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/index.html
All visitor callback functions are optional.
See also BFS Visitor Concept documentation from the BGL:
http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/BFSVisitor.html
var breadthFirstVisitorInterface = {
initializeVertex: function(vertexId_, digraph_),
discoverVertex: function(vertexId_, digraph_),
startVertex: function(vertexId_, digraph_),
examineVertex: function(vertexId_, digraph_),
examineEdge: function(vertexIdU_, vertexIdV_, digraph_),
treeEdge: function(vertexIdU_, vertexIdV_, digraph_),
nonTreeEdge: function(vertexIdU_, vertexIdV_, digraph_),
grayTarget: function(vertexIdU_, vertexIdV_, digraph_),
blackTarget: function(vertexIdU_, vertexIdV_, digraph_),
finishVertex: function(vertexId_, digraph_)
};
request = {
digraph: reference to jsgraph.DirectedGraph container object (required)
visitor: reference to jsgraph BFV visitor object (required)
options: {
startVector: reference to a vertex ID string, or an array of vertex ID strings (optional)
Note: if ommitted, BFT uses the digraph's root vertex set as the start vertex set
signalStart: Boolean flag (optional - default is true if ommitted)
Note: By default, BFT will call startVertex on each search root vertex.
In advanced scenarios you may wish to override this behavior.
traverseContext: reference to BFT search context object (optional)
Note: By default, BFT allocates the traversal context internally and returns it to
the caller. In advanced scenarios you may wish to provide a pre-initialized
(or potentially pre-colored) traversal context object.
}
}
}
response = {
error: null or string explaining why result is null
result: BFS search context object
}
*/
var algorithmName = "BFT"; // constant string used in error messages
var colors = require('./arc_core_digraph_algorithm_colors');
var visitorCallback = require('./arc_core_digraph_algorithm_visit');
var normalizeRequest = require('./arc_core_digraph_algorithm_request');
module.exports = function (request_) {
var nrequest = null; // normalized request object
var response = { error: null, result: null };
var errors = [];
var continueSearch = true;
var inBreakScope = false;
var searchQueue = [];
while (!inBreakScope) {
inBreakScope = true;
var index, vertexId;
var innerResponse = normalizeRequest(request_);
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
nrequest = innerResponse.result;
// initializeVertex visitor callback.
if (nrequest.options.traverseContext.searchStatus === 'pending') {
for (vertexId in nrequest.options.traverseContext.colorMap) {
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'initializeVertex', request: { u: vertexId, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
}
} // if searchStatus 'pending'
nrequest.options.traverseContext.searchStatus = 'active';
if (errors.length || !continueSearch) {
break;
}
// Initialize the BF visit or search.
// Note that all that distinguishes visit from search is the number of starting vertices. One -> visit, N -> search.
for (index in nrequest.options.startVector) {
var startingVertexId = nrequest.options.startVector[index];
// Ensure the starting vertex is in the graph container.
if (!nrequest.digraph.isVertex(startingVertexId)) {
errors.unshift("BFT request failed. Vertex '" + startingVertexId + "' not found in specfied directed graph container.");
break;
}
// Ensure the vertex is white in the color map.
if (nrequest.options.traverseContext.colorMap[startingVertexId] !== colors.white) {
errors.unshift("BFT request failed. Vertex '" + startingVertexId + "' color map not initialized to white.");
break;
}
// startVertex visitor callback.
if (nrequest.options.signalStart) {
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'startVertex', request: { u: startingVertexId, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
}
// Conditionally exit the loop if discoverVertex returned false.
if (errors.length || !continueSearch) {
break;
}
// discoverVertex visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'discoverVertex', request: { u: startingVertexId, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
// Remove the vertex from the undiscovered vertex map.
delete nrequest.options.traverseContext.undiscoveredMap[startingVertexId];
// Add the vertex to the search
searchQueue.push(startingVertexId);
// Color the vertex discovered (gray)
nrequest.options.traverseContext.colorMap[startingVertexId] = colors.gray;
// Conditionally exit the loop if discoverVertex returned false.
if (!continueSearch) {
break;
}
} // for initialize search
// Execute the main breadth-first algorithm using the starting vertex set as the initial contents of the searchQueue.
while (searchQueue.length && continueSearch && !errors.length) {
vertexId = searchQueue.shift();
// By convention
nrequest.options.traverseContext.colorMap[vertexId] = colors.black;
// examineVertex visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'examineVertex', request: { u: vertexId, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
var outEdges = nrequest.digraph.outEdges(vertexId);
for (index in outEdges) {
var outEdge = outEdges[index];
// examineEdge visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'examineEdge', request: { e: outEdge, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
var colorV = nrequest.options.traverseContext.colorMap[outEdge.v];
switch (colorV) {
case colors.white:
// discoverVertex visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'discoverVertex', request: { u: outEdge.v, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
delete nrequest.options.traverseContext.undiscoveredMap[outEdge.v];
if (!continueSearch) {
break;
}
// treeEdge visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'treeEdge', request: { e: outEdge, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
searchQueue.push(outEdge.v);
nrequest.options.traverseContext.colorMap[outEdge.v] = colors.gray;
break;
case colors.gray:
// nonTreeEdge visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'nonTreeEdge', request: { e: outEdge, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (continueSearch) {
// grayTarget visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'grayTarget', request: { e: outEdge, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
}
break;
case colors.black:
// nonTreeEdge visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'nonTreeEdge', request: { e: outEdge, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (continueSearch) {
// blackTarget visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'blackTarget', request: { e: outEdge, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
}
break;
default:
errors.unshift("BFT failure: An invalid color value was found in the color map for vertex '" + outEdge.v + "'. Please file an issue!");
break;
} // switch (colorV)
if (errors.length || !continueSearch) {
break;
}
} // for (outEdge in outEdges)
if (errors.length || !continueSearch) {
break;
}
// finishVertex visitor callback.
innerResponse = visitorCallback({ algorithm: algorithmName, visitor: nrequest.visitor, method: 'finishVertex', request: { u: vertexId, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
} // while (searchQueue.length)
} // end while (!inBreakScope)
if (errors.length) {
if (nrequest) {
nrequest.options.traverseContext.searchStatus = 'error';
}
errors.unshift("jsgraph.directed.breadthFirstTraverse algorithm failure:");
response.error = errors.join(' ');
} else {
nrequest.options.traverseContext.searchStatus = continueSearch?'completed':'terminated';
response.result = nrequest.options.traverseContext;
}
return response;
};