File tree Expand file tree Collapse file tree 1 file changed +18
-8
lines changed Expand file tree Collapse file tree 1 file changed +18
-8
lines changed Original file line number Diff line number Diff line change @@ -152,7 +152,7 @@ d3.sankey = function() {
152
152
} ) ;
153
153
} ) ;
154
154
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.
156
156
findAndMarkCycleBreaker ( nextNodes ) ;
157
157
}
158
158
else {
@@ -189,16 +189,26 @@ d3.sankey = function() {
189
189
// Skip already known cycle breakers.
190
190
continue ;
191
191
}
192
- // Check if target makes a cycle with current path.
192
+
193
+ // Check if target of link makes a cycle in current path.
193
194
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
+ }
199
208
}
209
+
200
210
// Recurse deeper.
201
- path . push ( cursorNode ) ;
211
+ path . push ( link ) ;
202
212
link = depthFirstCycleSearch ( target , path ) ;
203
213
path . pop ( ) ;
204
214
// Stop further search if we found a cycle breaker.
You can’t perform that action at this time.
0 commit comments