@@ -25,10 +25,14 @@ The implementation is loop based. It does not use recursion and does not have st
25
25
Complexity: worst-case `O(|V| + |E|)`.
26
26
27
27
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.
29
29
Returns:
30
30
components (ndslice of arrays of indexes)
31
31
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
+
32
36
See_also:
33
37
$(SUBREF utility, graph)
34
38
+/
@@ -206,22 +210,19 @@ auto tarjan(G, I = Unqual!(ForeachType!(ForeachType!G)))(G graph)
206
210
/+ +
207
211
------
208
212
4 <- 5 <- 6 -------> 7 -> 8 -> 11
209
- \ ^ ^ ^ \
210
- v \ \ \ \
213
+ | ^ ^ ^ |
214
+ v | | | |
211
215
0 -> 1 -> 2 -> 3 -> 10 9 <---
212
216
------
213
217
+/
214
218
pure version (mir_test) unittest
215
219
{
216
220
import mir.graph.utility;
217
- import mir.ndslice.algorithm: each;
218
- import mir.ndslice.sorting: sort;
219
- import std.array : array;
220
221
221
222
GraphSeries! (string , uint ) gs = [
222
223
" 00" : [" 01" ],
223
224
" 01" : [" 02" ],
224
- " 02" : [" 03 " , " 05 " ],
225
+ " 02" : [" 05 " , " 03 " ],
225
226
" 03" : [" 06" , " 10" ],
226
227
" 04" : [" 01" ],
227
228
" 05" : [" 04" ],
@@ -234,15 +235,13 @@ pure version(mir_test) unittest
234
235
].graphSeries;
235
236
236
237
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 ]]);
246
245
}
247
246
248
247
/+ +
@@ -262,28 +261,20 @@ pure version(mir_test) unittest
262
261
263
262
auto scc = gs.data.tarjan;
264
263
265
- assert (scc.length == 5 );
266
-
267
- foreach (uint [] component; scc)
268
- assert (component.length == 1 );
269
-
270
264
assert (scc == [[0 ], [1 ], [2 ], [3 ], [4 ]]);
271
265
}
272
266
273
267
/+ +
274
268
----
275
269
0 <- 2 <-- 5 <--> 6
276
- \ ^ ^ ^ ^
277
- v / \ \ \
278
- 1 <- 3 <-> 4 <-- 7 <--(links to self)
270
+ | ^ ^ ^___
271
+ v / | \ /\
272
+ 1 <- 3 <-> 4 <-- 7_|
279
273
----
280
274
+/
281
275
pure version (mir_test) unittest
282
276
{
283
277
import mir.graph.utility;
284
- import mir.ndslice.algorithm: each;
285
- import mir.ndslice.sorting: sort;
286
- import std.array : array;
287
278
288
279
auto gs = [
289
280
0 : [1 ],
@@ -297,30 +288,26 @@ pure version(mir_test) unittest
297
288
].graphSeries;
298
289
299
290
auto components = gs.data.tarjan;
300
- components.each! sort; // sort indexes in each component
301
291
302
- assert (components.array.sort == [
303
- [0 , 1 , 2 ],
304
- [3 , 4 ],
292
+ assert (components == [
293
+ [7 ],
305
294
[5 , 6 ],
306
- [7 ]
295
+ [3 , 4 ],
296
+ [0 , 1 , 2 ],
307
297
]);
308
298
}
309
299
310
300
/+ +
311
301
-----
312
302
2 <-> 1
313
- \ ^
314
- v /
315
- 0
303
+ \ ^
304
+ v /
305
+ 0
316
306
-----
317
307
+/
318
308
pure version (mir_test) unittest
319
309
{
320
310
import mir.graph.utility;
321
- import mir.ndslice.algorithm: each;
322
- import mir.ndslice.sorting: sort;
323
- import std.array : array;
324
311
325
312
auto gs = [
326
313
0 : [1 ],
@@ -329,7 +316,6 @@ pure version(mir_test) unittest
329
316
].graphSeries;
330
317
331
318
auto components = gs.data.tarjan;
332
- components.each! sort; // sort indexes in each component
333
319
334
320
assert (components == [[0 , 1 , 2 ]]);
335
321
}
@@ -346,9 +332,6 @@ not when they were actually removed from the stack
346
332
pure version (mir_test) unittest
347
333
{
348
334
import mir.graph.utility;
349
- import mir.ndslice.algorithm: each;
350
- import mir.ndslice.sorting: sort;
351
- import std.array : array;
352
335
353
336
auto root = 0 ;
354
337
auto lvl1 = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ];
@@ -364,7 +347,6 @@ pure version(mir_test) unittest
364
347
auto gs = aar.graphSeries;
365
348
366
349
auto components = gs.data.tarjan;
367
- components.each! sort; // sort indexes in each component
368
350
369
- assert (components == [root ~ lvl1 ~ lvl2]);
351
+ assert (components == [[ root] ~ [ lvl1[ 0 ]] ~ lvl2 ~ lvl1[ 1 .. $] ]);
370
352
}
0 commit comments