Skip to content

Commit 95b3133

Browse files
committed
update unittests and docs
1 parent 01e10f3 commit 95b3133

File tree

2 files changed

+27
-45
lines changed

2 files changed

+27
-45
lines changed

source/mir/graph/tarjan.d

+26-44
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ The implementation is loop based. It does not use recursion and does not have st
2525
Complexity: worst-case `O(|V| + |E|)`.
2626
2727
Params:
28-
graph = random access range of random accees ranges of nodes indeces
28+
graph = components (ndslice) sorted in the direction of traversal of the graph. Each component is an array of indeces.
2929
Returns:
3030
components (ndslice of arrays of indexes)
3131
32+
Note:
33+
The implementation returns components sorted in the direction of traversal of the graph.
34+
$(NOTE Most of other Tarjan implementations returns reverse order.)
35+
3236
See_also:
3337
$(SUBREF utility, graph)
3438
+/
@@ -206,22 +210,19 @@ auto tarjan(G, I = Unqual!(ForeachType!(ForeachType!G)))(G graph)
206210
/++
207211
------
208212
4 <- 5 <- 6 -------> 7 -> 8 -> 11
209-
\ ^ ^ ^ \
210-
v \ \ \ \
213+
| ^ ^ ^ |
214+
v | | | |
211215
0 -> 1 -> 2 -> 3 -> 10 9 <---
212216
------
213217
+/
214218
pure version(mir_test) unittest
215219
{
216220
import mir.graph.utility;
217-
import mir.ndslice.algorithm: each;
218-
import mir.ndslice.sorting: sort;
219-
import std.array: array;
220221

221222
GraphSeries!(string, uint) gs = [
222223
"00": ["01"],
223224
"01": ["02"],
224-
"02": ["03", "05"],
225+
"02": ["05", "03"],
225226
"03": ["06", "10"],
226227
"04": ["01"],
227228
"05": ["04"],
@@ -234,15 +235,13 @@ pure version(mir_test) unittest
234235
].graphSeries;
235236

236237
auto components = gs.data.tarjan;
237-
components.each!sort; // sort indexes in each component
238-
239-
assert(components.array.sort == [
240-
[0u],
241-
[1u, 2, 3, 4, 5, 6],
242-
[7u, 8, 9],
243-
[10u],
244-
[11u],
245-
]);
238+
239+
assert(components == [
240+
[0],
241+
[1, 2, 5, 4, 3, 6],
242+
[10],
243+
[7, 8, 9],
244+
[11]]);
246245
}
247246

248247
/++
@@ -262,28 +261,20 @@ pure version(mir_test) unittest
262261

263262
auto scc = gs.data.tarjan;
264263

265-
assert(scc.length == 5);
266-
267-
foreach(uint[] component; scc)
268-
assert(component.length == 1);
269-
270264
assert(scc == [[0], [1], [2], [3], [4]]);
271265
}
272266

273267
/++
274268
----
275269
0 <- 2 <-- 5 <--> 6
276-
\ ^ ^ ^ ^
277-
v / \ \ \
278-
1 <- 3 <-> 4 <-- 7 <--(links to self)
270+
| ^ ^ ^___
271+
v / | \ /\
272+
1 <- 3 <-> 4 <-- 7_|
279273
----
280274
+/
281275
pure version(mir_test) unittest
282276
{
283277
import mir.graph.utility;
284-
import mir.ndslice.algorithm: each;
285-
import mir.ndslice.sorting: sort;
286-
import std.array: array;
287278

288279
auto gs = [
289280
0: [1],
@@ -297,30 +288,26 @@ pure version(mir_test) unittest
297288
].graphSeries;
298289

299290
auto components = gs.data.tarjan;
300-
components.each!sort; // sort indexes in each component
301291

302-
assert(components.array.sort == [
303-
[0, 1, 2],
304-
[3, 4],
292+
assert(components == [
293+
[7],
305294
[5, 6],
306-
[7]
295+
[3, 4],
296+
[0, 1, 2],
307297
]);
308298
}
309299

310300
/++
311301
-----
312302
2 <-> 1
313-
\ ^
314-
v /
315-
0
303+
\ ^
304+
v /
305+
0
316306
-----
317307
+/
318308
pure version(mir_test) unittest
319309
{
320310
import mir.graph.utility;
321-
import mir.ndslice.algorithm: each;
322-
import mir.ndslice.sorting: sort;
323-
import std.array: array;
324311

325312
auto gs = [
326313
0: [1],
@@ -329,7 +316,6 @@ pure version(mir_test) unittest
329316
].graphSeries;
330317

331318
auto components = gs.data.tarjan;
332-
components.each!sort; // sort indexes in each component
333319

334320
assert(components == [[0, 1, 2]]);
335321
}
@@ -346,9 +332,6 @@ not when they were actually removed from the stack
346332
pure version(mir_test) unittest
347333
{
348334
import mir.graph.utility;
349-
import mir.ndslice.algorithm: each;
350-
import mir.ndslice.sorting: sort;
351-
import std.array: array;
352335

353336
auto root = 0;
354337
auto lvl1 = [1,2,3,4,5,6,7,8,9,10];
@@ -364,7 +347,6 @@ pure version(mir_test) unittest
364347
auto gs = aar.graphSeries;
365348

366349
auto components = gs.data.tarjan;
367-
components.each!sort; // sort indexes in each component
368350

369-
assert(components == [root ~ lvl1 ~ lvl2]);
351+
assert(components == [[root] ~ [lvl1[0]] ~ lvl2 ~ lvl1[1 .. $]]);
370352
}

source/mir/graph/utility.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ alias GraphSeries(T, I) = Series!(T*, Contiguous, [1], GraphIterator!I);
2727
Param:
2828
aaGraph = graph that is represented as associative array
2929
Returns:
30-
A graph series composed of keys (`.index`) and arrays of indeces (`.d`)
30+
A graph series composed of keys (sorted `.index`) and arrays of indeces (`.data`)
3131
Complexity: `O(log(V) (V + E))`
3232
+/
3333
GraphSeries!(T, uint) graphSeries(T, Range)(in Range[T] aaGraph)

0 commit comments

Comments
 (0)