-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[Merged by Bors] - Stageless: prettier cycle reporting #7463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| use std::fmt::Debug; | ||
|
|
||
| use bevy_utils::{ | ||
| petgraph::{graphmap::NodeTrait, prelude::*}, | ||
| petgraph::{algo::TarjanScc, graphmap::NodeTrait, prelude::*}, | ||
| HashMap, HashSet, | ||
| }; | ||
| use fixedbitset::FixedBitSet; | ||
|
|
@@ -274,3 +274,119 @@ where | |
| transitive_closure, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the simple cycles in a strongly-connected component of a directed graph. | ||
| /// | ||
| /// The algorithm implemented comes from | ||
| /// ["Finding all the elementary circuits of a directed graph"][1] by D. B. Johnson. | ||
| /// | ||
| /// [1]: https://doi.org/10.1137/0204007 | ||
| pub fn simple_cycles_in_component<N>(graph: &DiGraphMap<N, ()>, scc: &[N]) -> Vec<Vec<N>> | ||
| where | ||
| N: NodeTrait + Debug, | ||
| { | ||
| let mut cycles = vec![]; | ||
| let mut sccs = vec![scc.to_vec()]; | ||
|
|
||
| while let Some(mut scc) = sccs.pop() { | ||
| // only look at nodes and edges in this strongly-connected component | ||
| let mut subgraph = DiGraphMap::new(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also set a capacity here? |
||
| for &node in &scc { | ||
| subgraph.add_node(node); | ||
| } | ||
|
|
||
| for &node in &scc { | ||
| for successor in graph.neighbors(node) { | ||
| if subgraph.contains_node(successor) { | ||
| subgraph.add_edge(node, successor, ()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // path of nodes that may form a cycle | ||
| let mut path = Vec::with_capacity(subgraph.node_count()); | ||
| // we mark nodes as "blocked" to avoid finding permutations of the same cycles | ||
| let mut blocked = HashSet::with_capacity(subgraph.node_count()); | ||
| // connects nodes along path segments that can't be part of a cycle (given current root) | ||
| // those nodes can be unblocked at the same time | ||
| let mut unblock_together: HashMap<N, HashSet<N>> = | ||
| HashMap::with_capacity(subgraph.node_count()); | ||
| // stack for unblocking nodes | ||
| let mut unblock_stack = Vec::with_capacity(subgraph.node_count()); | ||
| // nodes can be involved in multiple cycles | ||
| let mut maybe_in_more_cycles: HashSet<N> = HashSet::with_capacity(subgraph.node_count()); | ||
| // stack for DFS | ||
| let mut stack = Vec::with_capacity(subgraph.node_count()); | ||
|
|
||
| // we're going to look for all cycles that begin and end at this node | ||
| let root = scc.pop().unwrap(); | ||
| // start a path at the root | ||
| path.clear(); | ||
| path.push(root); | ||
| // mark this node as blocked | ||
| blocked.insert(root); | ||
|
|
||
| // DFS | ||
| stack.clear(); | ||
| stack.push((root, subgraph.neighbors(root))); | ||
| while !stack.is_empty() { | ||
| let (ref node, successors) = stack.last_mut().unwrap(); | ||
| if let Some(next) = successors.next() { | ||
| if next == root { | ||
| // found a cycle | ||
| maybe_in_more_cycles.extend(path.iter()); | ||
| cycles.push(path.clone()); | ||
| } else if !blocked.contains(&next) { | ||
| // first time seeing `next` on this path | ||
| maybe_in_more_cycles.remove(&next); | ||
| path.push(next); | ||
| blocked.insert(next); | ||
| stack.push((next, subgraph.neighbors(next))); | ||
| continue; | ||
| } else { | ||
| // not first time seeing `next` on this path | ||
| } | ||
| } | ||
|
|
||
| if successors.peekable().peek().is_none() { | ||
| if maybe_in_more_cycles.contains(node) { | ||
| unblock_stack.push(*node); | ||
| // unblock this node's ancestors | ||
| while let Some(n) = unblock_stack.pop() { | ||
| if blocked.remove(&n) { | ||
| let unblock_predecessors = | ||
| unblock_together.entry(n).or_insert_with(HashSet::new); | ||
| unblock_stack.extend(unblock_predecessors.iter()); | ||
| unblock_predecessors.clear(); | ||
| } | ||
| } | ||
| } else { | ||
| // if its descendants can be unblocked later, this node will be too | ||
| for successor in subgraph.neighbors(*node) { | ||
| unblock_together | ||
| .entry(successor) | ||
| .or_insert_with(HashSet::new) | ||
| .insert(*node); | ||
| } | ||
| } | ||
|
|
||
| // remove node from path and DFS stack | ||
| path.pop(); | ||
| stack.pop(); | ||
| } | ||
| } | ||
|
|
||
| // remove node from subgraph | ||
| subgraph.remove_node(root); | ||
|
|
||
| // divide remainder into smaller SCCs | ||
| let mut tarjan_scc = TarjanScc::new(); | ||
| tarjan_scc.run(&subgraph, |scc| { | ||
| if scc.len() > 1 { | ||
| sccs.push(scc.to_vec()); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| cycles | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.