Skip to content

Commit dab87ae

Browse files
committed
circularity detection
1 parent 426b50d commit dab87ae

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"right-now": "^1.0.0",
9696
"robust-orientation": "^1.1.3",
9797
"sane-topojson": "^2.0.0",
98+
"strongly-connected-components": "^1.0.1",
9899
"superscript-text": "^1.0.0",
99100
"tinycolor2": "^1.3.0",
100101
"topojson-client": "^2.1.0",

src/traces/sankey/defaults.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,23 @@ var attributes = require('./attributes');
1313
var colors = require('../../components/color/attributes').defaults;
1414
var Color = require('../../components/color');
1515
var 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

2135
module.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 = [];

test/image/mocks/energy_dark.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,4 +502,3 @@
502502
]
503503
}
504504
}
505-

0 commit comments

Comments
 (0)