rustc_data_structures/graph/iterate/mod.rs
1use std::ops::ControlFlow;
2
3use rustc_index::bit_set::DenseBitSet;
4use rustc_index::{IndexSlice, IndexVec};
5
6use super::{DirectedGraph, StartNode, Successors};
7
8#[cfg(test)]
9mod tests;
10
11pub fn post_order_from<G: DirectedGraph + Successors>(
12 graph: &G,
13 start_node: G::Node,
14) -> Vec<G::Node> {
15 post_order_from_to(graph, start_node, None)
16}
17
18pub fn post_order_from_to<G: DirectedGraph + Successors>(
19 graph: &G,
20 start_node: G::Node,
21 end_node: Option<G::Node>,
22) -> Vec<G::Node> {
23 let mut visited: IndexVec<G::Node, bool> = IndexVec::from_elem_n(false, graph.num_nodes());
24 let mut result: Vec<G::Node> = Vec::with_capacity(graph.num_nodes());
25 if let Some(end_node) = end_node {
26 visited[end_node] = true;
27 }
28 post_order_walk(graph, start_node, &mut result, &mut visited);
29 result
30}
31
32fn post_order_walk<G: DirectedGraph + Successors>(
33 graph: &G,
34 node: G::Node,
35 result: &mut Vec<G::Node>,
36 visited: &mut IndexSlice<G::Node, bool>,
37) {
38 struct PostOrderFrame<Node, Iter> {
39 node: Node,
40 iter: Iter,
41 }
42
43 if visited[node] {
44 return;
45 }
46
47 let mut stack = vec![PostOrderFrame { node, iter: graph.successors(node) }];
48
49 'recurse: while let Some(frame) = stack.last_mut() {
50 let node = frame.node;
51 visited[node] = true;
52
53 for successor in frame.iter.by_ref() {
54 if !visited[successor] {
55 stack.push(PostOrderFrame { node: successor, iter: graph.successors(successor) });
56 continue 'recurse;
57 }
58 }
59
60 let _ = stack.pop();
61 result.push(node);
62 }
63}
64
65pub fn reverse_post_order<G: DirectedGraph + Successors>(
66 graph: &G,
67 start_node: G::Node,
68) -> Vec<G::Node> {
69 let mut vec = post_order_from(graph, start_node);
70 vec.reverse();
71 vec
72}
73
74/// A "depth-first search" iterator for a directed graph.
75pub struct DepthFirstSearch<G>
76where
77 G: DirectedGraph + Successors,
78{
79 graph: G,
80 stack: Vec<G::Node>,
81 visited: DenseBitSet<G::Node>,
82}
83
84impl<G> DepthFirstSearch<G>
85where
86 G: DirectedGraph + Successors,
87{
88 pub fn new(graph: G) -> Self {
89 Self { stack: vec![], visited: DenseBitSet::new_empty(graph.num_nodes()), graph }
90 }
91
92 /// Version of `push_start_node` that is convenient for chained
93 /// use.
94 pub fn with_start_node(mut self, start_node: G::Node) -> Self {
95 self.push_start_node(start_node);
96 self
97 }
98
99 /// Pushes another start node onto the stack. If the node
100 /// has not already been visited, then you will be able to
101 /// walk its successors (and so forth) after the current
102 /// contents of the stack are drained. If multiple start nodes
103 /// are added into the walk, then their mutual successors
104 /// will all be walked. You can use this method once the
105 /// iterator has been completely drained to add additional
106 /// start nodes.
107 pub fn push_start_node(&mut self, start_node: G::Node) {
108 if self.visited.insert(start_node) {
109 self.stack.push(start_node);
110 }
111 }
112
113 /// Searches all nodes reachable from the current start nodes.
114 /// This is equivalent to just invoke `next` repeatedly until
115 /// you get a `None` result.
116 pub fn complete_search(&mut self) {
117 for _ in self.by_ref() {}
118 }
119
120 /// Returns true if node has been visited thus far.
121 /// A node is considered "visited" once it is pushed
122 /// onto the internal stack; it may not yet have been yielded
123 /// from the iterator. This method is best used after
124 /// the iterator is completely drained.
125 pub fn visited(&self, node: G::Node) -> bool {
126 self.visited.contains(node)
127 }
128
129 /// Returns a reference to the set of nodes that have been visited, with
130 /// the same caveats as [`Self::visited`].
131 ///
132 /// When incorporating the visited nodes into another bitset, using bulk
133 /// operations like `union` or `intersect` can be more efficient than
134 /// processing each node individually.
135 pub fn visited_set(&self) -> &DenseBitSet<G::Node> {
136 &self.visited
137 }
138}
139
140impl<G> std::fmt::Debug for DepthFirstSearch<G>
141where
142 G: DirectedGraph + Successors,
143{
144 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145 let mut f = fmt.debug_set();
146 for n in self.visited.iter() {
147 f.entry(&n);
148 }
149 f.finish()
150 }
151}
152
153impl<G> Iterator for DepthFirstSearch<G>
154where
155 G: DirectedGraph + Successors,
156{
157 type Item = G::Node;
158
159 fn next(&mut self) -> Option<G::Node> {
160 let DepthFirstSearch { stack, visited, graph } = self;
161 let n = stack.pop()?;
162 stack.extend(graph.successors(n).filter(|&m| visited.insert(m)));
163 Some(n)
164 }
165}
166
167/// The status of a node in the depth-first search.
168///
169/// See the documentation of `TriColorDepthFirstSearch` to see how a node's status is updated
170/// during DFS.
171#[derive(Clone, Copy, Debug, PartialEq, Eq)]
172pub enum NodeStatus {
173 /// This node has been examined by the depth-first search but is not yet `Settled`.
174 ///
175 /// Also referred to as "gray" or "discovered" nodes in [CLR].
176 ///
177 /// [CLR]: https://en.wikipedia.org/wiki/Introduction_to_Algorithms
178 Visited,
179
180 /// This node and all nodes reachable from it have been examined by the depth-first search.
181 ///
182 /// Also referred to as "black" or "finished" nodes in [CLR].
183 ///
184 /// [CLR]: https://en.wikipedia.org/wiki/Introduction_to_Algorithms
185 Settled,
186}
187
188struct Event<N> {
189 node: N,
190 becomes: NodeStatus,
191}
192
193/// A depth-first search that also tracks when all successors of a node have been examined.
194///
195/// This is based on the DFS described in [Introduction to Algorithms (1st ed.)][CLR], hereby
196/// referred to as **CLR**. However, we use the terminology in [`NodeStatus`] above instead of
197/// "discovered"/"finished" or "white"/"grey"/"black". Each node begins the search with no status,
198/// becomes `Visited` when it is first examined by the DFS and is `Settled` when all nodes
199/// reachable from it have been examined. This allows us to differentiate between "tree", "back"
200/// and "forward" edges (see [`TriColorVisitor::node_examined`]).
201///
202/// Unlike the pseudocode in [CLR], this implementation is iterative and does not use timestamps.
203/// We accomplish this by storing `Event`s on the stack that result in a (possible) state change
204/// for each node. A `Visited` event signifies that we should examine this node if it has not yet
205/// been `Visited` or `Settled`. When a node is examined for the first time, we mark it as
206/// `Visited` and push a `Settled` event for it on stack followed by `Visited` events for all of
207/// its predecessors, scheduling them for examination. Multiple `Visited` events for a single node
208/// may exist on the stack simultaneously if a node has multiple predecessors, but only one
209/// `Settled` event will ever be created for each node. After all `Visited` events for a node's
210/// successors have been popped off the stack (as well as any new events triggered by visiting
211/// those successors), we will pop off that node's `Settled` event.
212///
213/// [CLR]: https://en.wikipedia.org/wiki/Introduction_to_Algorithms
214pub struct TriColorDepthFirstSearch<'graph, G>
215where
216 G: ?Sized + DirectedGraph + Successors,
217{
218 graph: &'graph G,
219 stack: Vec<Event<G::Node>>,
220 visited: DenseBitSet<G::Node>,
221 settled: DenseBitSet<G::Node>,
222}
223
224impl<'graph, G> TriColorDepthFirstSearch<'graph, G>
225where
226 G: ?Sized + DirectedGraph + Successors,
227{
228 pub fn new(graph: &'graph G) -> Self {
229 TriColorDepthFirstSearch {
230 graph,
231 stack: vec![],
232 visited: DenseBitSet::new_empty(graph.num_nodes()),
233 settled: DenseBitSet::new_empty(graph.num_nodes()),
234 }
235 }
236
237 /// Performs a depth-first search, starting from the given `root`.
238 ///
239 /// This won't visit nodes that are not reachable from `root`.
240 pub fn run_from<V>(mut self, root: G::Node, visitor: &mut V) -> Option<V::BreakVal>
241 where
242 V: TriColorVisitor<G>,
243 {
244 use NodeStatus::{Settled, Visited};
245
246 self.stack.push(Event { node: root, becomes: Visited });
247
248 loop {
249 match self.stack.pop()? {
250 Event { node, becomes: Settled } => {
251 let not_previously_settled = self.settled.insert(node);
252 assert!(not_previously_settled, "A node should be settled exactly once");
253 if let ControlFlow::Break(val) = visitor.node_settled(node) {
254 return Some(val);
255 }
256 }
257
258 Event { node, becomes: Visited } => {
259 let not_previously_visited = self.visited.insert(node);
260 let prior_status = if not_previously_visited {
261 None
262 } else if self.settled.contains(node) {
263 Some(Settled)
264 } else {
265 Some(Visited)
266 };
267
268 if let ControlFlow::Break(val) = visitor.node_examined(node, prior_status) {
269 return Some(val);
270 }
271
272 // If this node has already been examined, we are done.
273 if prior_status.is_some() {
274 continue;
275 }
276
277 // Otherwise, push a `Settled` event for this node onto the stack, then
278 // schedule its successors for examination.
279 self.stack.push(Event { node, becomes: Settled });
280 for succ in self.graph.successors(node) {
281 if !visitor.ignore_edge(node, succ) {
282 self.stack.push(Event { node: succ, becomes: Visited });
283 }
284 }
285 }
286 }
287 }
288 }
289}
290
291impl<G> TriColorDepthFirstSearch<'_, G>
292where
293 G: ?Sized + DirectedGraph + Successors + StartNode,
294{
295 /// Performs a depth-first search, starting from `G::start_node()`.
296 ///
297 /// This won't visit nodes that are not reachable from the start node.
298 pub fn run_from_start<V>(self, visitor: &mut V) -> Option<V::BreakVal>
299 where
300 V: TriColorVisitor<G>,
301 {
302 let root = self.graph.start_node();
303 self.run_from(root, visitor)
304 }
305}
306
307/// What to do when a node is examined or becomes `Settled` during DFS.
308pub trait TriColorVisitor<G>
309where
310 G: ?Sized + DirectedGraph,
311{
312 /// The value returned by this search.
313 type BreakVal;
314
315 /// Called when a node is examined by the depth-first search.
316 ///
317 /// By checking the value of `prior_status`, this visitor can determine whether the edge
318 /// leading to this node was a tree edge (`None`), forward edge (`Some(Settled)`) or back edge
319 /// (`Some(Visited)`). For a full explanation of each edge type, see the "Depth-first Search"
320 /// chapter in [CLR] or [wikipedia].
321 ///
322 /// If you want to know *both* nodes linked by each edge, you'll need to modify
323 /// `TriColorDepthFirstSearch` to store a `source` node for each `Visited` event.
324 ///
325 /// [wikipedia]: https://en.wikipedia.org/wiki/Depth-first_search#Output_of_a_depth-first_search
326 /// [CLR]: https://en.wikipedia.org/wiki/Introduction_to_Algorithms
327 fn node_examined(
328 &mut self,
329 _node: G::Node,
330 _prior_status: Option<NodeStatus>,
331 ) -> ControlFlow<Self::BreakVal> {
332 ControlFlow::Continue(())
333 }
334
335 /// Called after all nodes reachable from this one have been examined.
336 fn node_settled(&mut self, _node: G::Node) -> ControlFlow<Self::BreakVal> {
337 ControlFlow::Continue(())
338 }
339
340 /// Behave as if no edges exist from `source` to `target`.
341 fn ignore_edge(&mut self, _source: G::Node, _target: G::Node) -> bool {
342 false
343 }
344}
345
346/// This `TriColorVisitor` looks for back edges in a graph, which indicate that a cycle exists.
347pub struct CycleDetector;
348
349impl<G> TriColorVisitor<G> for CycleDetector
350where
351 G: ?Sized + DirectedGraph,
352{
353 type BreakVal = ();
354
355 fn node_examined(
356 &mut self,
357 _node: G::Node,
358 prior_status: Option<NodeStatus>,
359 ) -> ControlFlow<Self::BreakVal> {
360 match prior_status {
361 Some(NodeStatus::Visited) => ControlFlow::Break(()),
362 _ => ControlFlow::Continue(()),
363 }
364 }
365}