Skip to content

Commit

Permalink
Issue #6: mark weakest link in cycle as cycle breaker
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed May 28, 2015
1 parent fa4e64f commit caf5386
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ d3.sankey = function() {
});
});
if (nextNodes.length == remainingNodes.length) {
console.warn('Detected cycles in the graph.');
// There must be a cycle here. Let's search for a link that breaks it.
findAndMarkCycleBreaker(nextNodes);
}
else {
Expand Down Expand Up @@ -189,16 +189,26 @@ d3.sankey = function() {
// Skip already known cycle breakers.
continue;
}
// Check if target makes a cycle with current path.

// Check if target of link makes a cycle in current path.
target = link.target;
if (path.indexOf(target) > -1) {
// Mark this link as a known cycle breaker.
link.cycleBreaker = true;
// Stop further search if we found a cycle breaker.
return link;
for (var l = 0; l < path.length; l++) {
if (path[l].source == target) {
// We found a cycle. Search for weakest link in cycle
var weakest = link;
for (; l < path.length; l++) {
if (path[l].value < weakest.value) {
weakest = path[l];
}
}
// Mark weakest link as (known) cycle breaker and abort search.
weakest.cycleBreaker = true;
return weakest;
}
}

// Recurse deeper.
path.push(cursorNode);
path.push(link);
link = depthFirstCycleSearch(target, path);
path.pop();
// Stop further search if we found a cycle breaker.
Expand Down

0 comments on commit caf5386

Please sign in to comment.