@@ -13,9 +13,23 @@ var attributes = require('./attributes');
1313var colors = require ( '../../components/color/attributes' ) . defaults ;
1414var Color = require ( '../../components/color' ) ;
1515var tinycolor = require ( 'tinycolor2' ) ;
16+ var tarjan = require ( 'strongly-connected-components' ) ;
1617
17- function circularityPresent ( ) {
18- return false ;
18+ function circularityPresent ( nodeList , sources , targets ) {
19+
20+ var nodes = nodeList . map ( function ( ) { return [ ] ; } ) ;
21+
22+ for ( var i = 0 ; i < Math . min ( sources . length , targets . length ) ; i ++ ) {
23+ nodes [ sources [ i ] ] . push ( targets [ i ] ) ;
24+ }
25+
26+ var scc = tarjan ( nodes ) ;
27+
28+ // Tarján's strongly connected components algorithm coded by Mikola Lysenko
29+ // returns at least one non-singular component if there's circularity in the graph
30+ return scc . components . some ( function ( c ) {
31+ return c . length > 1 ;
32+ } ) ;
1933}
2034
2135module . exports = function supplyDefaults ( traceIn , traceOut , defaultColor , layout ) {
@@ -64,7 +78,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
6478 Lib . log ( 'Some of the nodes are neither sources nor targets, please remove them.' ) ;
6579 }
6680
67- if ( circularityPresent ( traceIn . link . source , traceIn . link . target ) ) {
81+ if ( circularityPresent ( traceOut . node . label , traceOut . link . source , traceOut . link . target ) ) {
6882 Lib . log ( 'Circularity is present in the Sankey data. Removing all nodes and links.' ) ;
6983 traceOut . link . label = [ ] ;
7084 traceOut . link . source = [ ] ;
0 commit comments