-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharc_core_digraph_algorithm_dft.js
323 lines (275 loc) · 13.4 KB
/
arc_core_digraph_algorithm_dft.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
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.
*/
var algorithmName = "DFT"; // 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
var response = { error: null, result: null };
var errors = [];
var continueSearch = true;
var inBreakScope = false;
while (!inBreakScope) {
inBreakScope = true;
var index, vertexId;
var finishedEdges = {};
var innerRequest = null;
var hash = null;
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;
}
} // end for
} // if searchStatus 'pending'
nrequest.options.traverseContext.searchStatus = 'active';
if (errors.length || !continueSearch) {
break;
}
// Outer depth-first search loop iterates over the start vertex set.
for (index in nrequest.options.startVector) {
vertexId = nrequest.options.startVector[index];
// Ensure the starting vertex is actually in the graph.
if (!nrequest.digraph.isVertex(vertexId)) {
errors.unshift("DFT request failed. Vertex '" + vertexId + "' not found in specified directed graph container.");
break;
}
// Ensure the starting vertex is undicovered (white in the color map).
if (nrequest.options.traverseContext.colorMap[vertexId] !== colors.white) {
errors.unshift("DFT request failed. Vertex '" + vertexId + "' 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: vertexId, g: nrequest.digraph }});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
}
if (!continueSearch) {
break;
}
// searchStack is a FILO of FIFO's (or stack of queues if you prefer)
// initialized with starting vertex set member under-evaluation's ID.
var searchStack = [ [ vertexId ] ];
// Iterate until search stack is empty, a client visitor method returns false, or an error occurs.
while (searchStack.length && continueSearch && !errors.length) {
// Peek at the identifier of the vertex at the front of the queue atop the search stack.
var currentVertexId = (searchStack[searchStack.length - 1])[0];
switch (nrequest.options.traverseContext.colorMap[currentVertexId]) {
case colors.white:
// Remove the vertex from the undiscovered map.
delete nrequest.options.traverseContext.undiscoveredMap[currentVertexId];
// Change the vertex's state to GRAY to record its discovery.
nrequest.options.traverseContext.colorMap[currentVertexId] = colors.gray;
// discoverVertex visitor callback.
innerResponse = visitorCallback({
algorithm: algorithmName,
visitor: nrequest.visitor,
method: 'discoverVertex',
request: { u: currentVertexId, g: nrequest.digraph }
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
// treeEdge visitor callback.
if (searchStack.length > 1) {
innerResponse = visitorCallback({
algorithm: algorithmName,
visitor: nrequest.visitor,
method: 'treeEdge',
request: { e: { u: searchStack[searchStack.length - 2][0], v: currentVertexId }, g: nrequest.digraph }
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
} else {
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
}
}
// Examine adjacent vertices
var vertexOutEdges = nrequest.digraph.outEdges(currentVertexId);
var adjacentVertices = [];
while (vertexOutEdges.length && !errors.length && continueSearch) {
var adjacentVertexId = vertexOutEdges.shift().v;
// examineEdge visitor callback.
innerResponse = visitorCallback({
algorithm: algorithmName,
visitor: nrequest.visitor,
method: 'examineEdge',
request: { e: { u: currentVertexId, v: adjacentVertexId }, g: nrequest.digraph }
});
if (innerResponse.error) {
errors.unshift(innerRepsonse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
switch (nrequest.options.traverseContext.colorMap[adjacentVertexId]) {
case colors.white:
adjacentVertices.push(adjacentVertexId);
break;
case colors.gray:
// backEdge visitor callback.
innerResponse = visitorCallback({
algorithm: algorithmName,
visitor: nrequest.visitor,
method: 'backEdge',
request: { e: { u: currentVertexId, v: adjacentVertexId }, g: nrequest.digraph }
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
} else {
continueSearch = innerResponse.result;
}
break;
case colors.black:
// forwardOrCrossEdge visitor callback.
innerResponse = visitorCallback({
algorithm: algorithmName,
visitor: nrequest.visitor,
method: 'forwardOrCrossEdge',
request: { e: { u: currentVertexId, v: adjacentVertexId }, g: nrequest.digraph }
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
} else {
continueSearch = innerResponse.result;
}
break;
}
}
if (adjacentVertices.length) {
searchStack.push(adjacentVertices);
}
break;
case colors.gray:
// change the vertex's state to black to indicate search completion.
nrequest.options.traverseContext.colorMap[currentVertexId] = colors.black;
// finishVertex visitor callback.
innerResponse = visitorCallback({
algorithm: algorithmName,
visitor: nrequest.visitor,
method: 'finishVertex',
request: { u: currentVertexId, g: nrequest.digraph }
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
var inEdgeSet = nrequest.digraph.inEdges(currentVertexId);
while (inEdgeSet.length) {
var inEdge = inEdgeSet.pop();
hash = inEdge.u + inEdge.v;
finishedEdges[hash] = inEdge;
}
searchStack[searchStack.length - 1].shift();
if (!(searchStack[searchStack.length - 1].length)) {
searchStack.pop();
}
break;
case colors.black:
// The black sheep. The only way for a vertex to end up in this state
// is for it to be queued after another adjacent vertex that reaches
// it first in the depth-first search tree. By definition it's already
// been 'finished'.
if (searchStack.length > 1) {
innerRequest = { e: { u: (searchStack[searchStack.length - 2])[0], v: currentVertexId }, g: nrequest.digraph };
innerResponse = visitorCallback({
algorithm: algorithmName,
visitor: nrequest.visitor,
method: 'forwardOrCrossEdge',
request: innerRequest
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
}
searchStack[searchStack.length - 1].shift();
if (!searchStack[searchStack.length - 1].length) {
searchStack.pop();
}
break;
default:
errors.unshift("DFT failure: An invalid color value was found in the color map for vertex '" + currentVertexId + "'.");
break;
}
} // while search stack is not empty
if (errors.length || !continueSearch) {
break;
}
} // end while outer depth-first search loop
if (errors.length || !continueSearch) {
break;
}
for (hash in finishedEdges) {
innerRequest = { e: finishedEdges[hash], g: nrequest.digraph };
innerResponse = visitorCallback({
algorithm: algorithmName,
visitor: nrequest.visitor,
method: 'finishEdge',
request: innerRequest
});
if (innerResponse.error) {
errors.unshift(innerResponse.error);
break;
}
continueSearch = innerResponse.result;
if (!continueSearch) {
break;
}
} // end for
} // while !inBreakScope
if (errors.length) {
if (nrequest) {
nrequest.options.traverseContext.searchStatus = 'error';
}
errors.unshift("jsgraph.directed.depthFirstTraverse algorithm failure:");
response.error = errors.join(' ');
} else {
nrequest.options.traverseContext.searchStatus = continueSearch?'completed':'terminated';
response.result = nrequest.options.traverseContext;
}
return response;
};