Skip to content

Commit caf5386

Browse files
committed
Issue #6: mark weakest link in cycle as cycle breaker
1 parent fa4e64f commit caf5386

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

sankey.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ d3.sankey = function() {
152152
});
153153
});
154154
if (nextNodes.length == remainingNodes.length) {
155-
console.warn('Detected cycles in the graph.');
155+
// There must be a cycle here. Let's search for a link that breaks it.
156156
findAndMarkCycleBreaker(nextNodes);
157157
}
158158
else {
@@ -189,16 +189,26 @@ d3.sankey = function() {
189189
// Skip already known cycle breakers.
190190
continue;
191191
}
192-
// Check if target makes a cycle with current path.
192+
193+
// Check if target of link makes a cycle in current path.
193194
target = link.target;
194-
if (path.indexOf(target) > -1) {
195-
// Mark this link as a known cycle breaker.
196-
link.cycleBreaker = true;
197-
// Stop further search if we found a cycle breaker.
198-
return link;
195+
for (var l = 0; l < path.length; l++) {
196+
if (path[l].source == target) {
197+
// We found a cycle. Search for weakest link in cycle
198+
var weakest = link;
199+
for (; l < path.length; l++) {
200+
if (path[l].value < weakest.value) {
201+
weakest = path[l];
202+
}
203+
}
204+
// Mark weakest link as (known) cycle breaker and abort search.
205+
weakest.cycleBreaker = true;
206+
return weakest;
207+
}
199208
}
209+
200210
// Recurse deeper.
201-
path.push(cursorNode);
211+
path.push(link);
202212
link = depthFirstCycleSearch(target, path);
203213
path.pop();
204214
// Stop further search if we found a cycle breaker.

0 commit comments

Comments
 (0)