Index: lib/src/graph.dart |
diff --git a/lib/src/graph.dart b/lib/src/graph.dart |
index 4bb7f4b78c3daf17c132088dbe5b041fae918b64..bea74558a103581569894c5317f47786de4fe2d9 100644 |
--- a/lib/src/graph.dart |
+++ b/lib/src/graph.dart |
@@ -3,7 +3,7 @@ |
// BSD-style license that can be found in the LICENSE file. |
/// A library to work with graphs. It contains a couple algorithms, including |
-/// Tarjan's algorithm to compute strongest connected components in a graph and |
+/// Tarjan's algorithm to compute strongly connected components in a graph and |
/// Cooper et al's dominator algorithm. |
/// |
/// Portions of the code in this library was adapted from |
@@ -53,9 +53,21 @@ abstract class Graph<N> { |
return result; |
} |
- /// Return a list of nodes that form a cycle containing the given node. If the |
- /// node is not part of a cycle in this graph, then a list containing only the |
- /// node itself will be returned. |
+ /// Returns an iterable of all nodes reachable from [root]. |
Siggi Cherem (dart-lang)
2015/10/16 23:07:41
maybe indicate that they are in preorder?
Harry Terkelsen
2015/10/19 22:11:12
Done.
|
+ Iterable<N> reachableFrom(N root) sync* { |
Siggi Cherem (dart-lang)
2015/10/16 23:07:41
maybe rename this and/or `postOrder` above so we h
Harry Terkelsen
2015/10/19 22:11:12
Done.
|
+ var seen = new Set<N>(); |
+ var stack = <N>[root]; |
+ while (!stack.isEmpty) { |
+ var next = stack.removeLast(); |
+ seen.add(next); |
+ yield next; |
Siggi Cherem (dart-lang)
2015/10/16 23:07:41
nice use of sync*/yield :)
Harry Terkelsen
2015/10/19 22:11:12
thanks!
|
+ stack.addAll(targetsOf(next).where((x) => !seen.contains(x))); |
+ } |
+ } |
+ |
+ /// Returns a list of nodes that form a cycle containing the given node. If |
+ /// the node is not part of a cycle in this graph, then a list containing only |
+ /// the node itself will be returned. |
List<N> findCycleContaining(N node) { |
assert(node != null); |
_SccFinder<N> finder = new _SccFinder<N>(this); |