File tree Expand file tree Collapse file tree 2 files changed +9
-6
lines changed Expand file tree Collapse file tree 2 files changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -19,8 +19,4 @@ export default function* kahn1962(queue, graph) {
1919 }
2020 }
2121 }
22-
23- if ( graph . size > 0 ) {
24- throw new Error ( 'kahn: graph has at least one cycle' ) ;
25- }
2622}
Original file line number Diff line number Diff line change @@ -9,8 +9,9 @@ import kahn1962 from './kahn1962.js';
99 * @param {Iterable<any> } edges - The input graph as a list of edges.
1010 * @param {Function } breakTies - The function to break ties.
1111 * @returns {Iterable<any> } The vertices sorted in topological order.
12+ * @throws {Error } If the input graph contains a cycle.
1213 */
13- export default function sorted ( edges , breakTies = undefined ) {
14+ export default function * sorted ( edges , breakTies = undefined ) {
1415 const graph = Pairs . from ( edges ) ;
1516
1617 const queue = breakTies ? new Heap ( breakTies ) : [ ] ;
@@ -19,5 +20,11 @@ export default function sorted(edges, breakTies = undefined) {
1920 for ( const [ , v ] of graph ) freeVertices . delete ( v ) ;
2021 for ( const u of freeVertices ) queue . push ( u ) ;
2122
22- return kahn1962 ( queue , graph ) ;
23+ yield * kahn1962 ( queue , graph ) ;
24+
25+ if ( graph . size > 0 ) {
26+ throw new Error (
27+ '@aureooms/js-topological-sorting#sorted: input graph contains a cycle' ,
28+ ) ;
29+ }
2330}
You can’t perform that action at this time.
0 commit comments