You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -660,6 +661,120 @@ setReadOnly( CompactAdjacencyMatrix.prototype, 'toposort', function toposort() {
660
661
returnout;
661
662
});
662
663
664
+
/**
665
+
* Returns a topological ordering of the directed graph.
666
+
*
667
+
* ## Notes
668
+
*
669
+
* - The function returns a two-element array.
670
+
* - If the function is able to compute a topological ordering, the first array element is the topological ordering and the second element is `null`.
671
+
* - If a topological ordering cannot be achieved (e.g., due to the graph not being a directed acyclic graph (DAG)), the first array element is `null` and the second element is the first encountered cycle.
// Initialize an empty list that will contain the sorted vertices:
710
+
out=[];
711
+
712
+
// If the graph is empty, nothing to sort...
713
+
if(this._N===0){
714
+
return[out,null];
715
+
}
716
+
// Initialize an array for keeping track of whether a vertex has been "visited":
717
+
marks=newInt8Array(N);
718
+
719
+
// Initialize a stack for keeping track of cycles:
720
+
s=[];
721
+
722
+
// Process vertices using depth-first-search...
723
+
idx=[0,0];
724
+
for(i=0;i<N;i++){
725
+
if(marks[i]===0){
726
+
err=visit(i);
727
+
if(err!==0){
728
+
// Found a cycle...
729
+
return[null,s];
730
+
}
731
+
}
732
+
}
733
+
grev(out.length,out,1);
734
+
return[out,null];
735
+
736
+
/**
737
+
* Visits a graph vertex and follows edges until finding a leaf vertex (if one exists).
738
+
*
739
+
* ## Notes
740
+
*
741
+
* - If the function is able to successfully perform a depth-first-search, the functions returns `0`; otherwise, the function returns `-1` in the event of a cycle.
0 commit comments