From f937eb545571f40e1e470ea530c760c8883bb18a Mon Sep 17 00:00:00 2001 From: Kris Date: Tue, 6 Sep 2022 21:38:24 +0300 Subject: [PATCH] implement feedback --- .../src/connectivity/conn_components.rs | 48 +++++++------------ retworkx-core/src/connectivity/mod.rs | 3 +- src/connectivity/mod.rs | 5 +- 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/retworkx-core/src/connectivity/conn_components.rs b/retworkx-core/src/connectivity/conn_components.rs index 61b512471a..2b2d09a0b6 100644 --- a/retworkx-core/src/connectivity/conn_components.rs +++ b/retworkx-core/src/connectivity/conn_components.rs @@ -9,14 +9,15 @@ // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. -use petgraph::graph::NodeIndex; -// use super::NullGraph; use hashbrown::HashSet; use petgraph::algo; use std::collections::VecDeque; use std::hash::Hash; // use super::digraph; -use petgraph::visit::{GraphProp, IntoNeighborsDirected, IntoNodeIdentifiers, VisitMap, Visitable}; +use petgraph::visit::{ + GraphProp, IntoNeighborsDirected, IntoNodeIdentifiers, NodeCount, NodeIndexable, VisitMap, + Visitable, +}; use petgraph::{Incoming, Outgoing}; /// Given an graph, a node in the graph, and a visit_map, /// return the set of nodes connected to the given node @@ -166,48 +167,31 @@ where num_components } -pub fn strongly_connected_components(graph: G) -> Vec> +pub fn is_connected(graph: G) -> bool where - G: GraphProp + IntoNeighborsDirected + Visitable + IntoNodeIdentifiers, - G::NodeId: Eq + Hash, -{ - algo::kosaraju_scc(&graph) - .iter() - .map(|x| x.iter().map(|id| id.index()).collect()) - .collect() -} - -pub fn is_connected(graph: G) -> Result -where - G: GraphProp + IntoNeighborsDirected + Visitable + IntoNodeIdentifiers, + G: GraphProp + + IntoNeighborsDirected + + Visitable + + IntoNodeIdentifiers + + NodeIndexable + + NodeCount, G::NodeId: Eq + Hash, { match graph.node_identifiers().next() { Some(node) => { - let component = node_connected_component(graph, node.index()); - Ok(component.len() == graph.node_count()) + let component = node_connected_component(graph, node.to_index()); + component.len() == graph.node_count() } - None => Err(NullGraph::new_err("Invalid operation on a NullGraph")), + None => false, } } -pub fn node_connected_component(graph: G, node: usize) -> Result, InvalidNode > +pub fn node_connected_component(graph: G, node: G::NodeId) -> HashSet where G: GraphProp + IntoNeighborsDirected + Visitable + IntoNodeIdentifiers, G::NodeId: Eq + Hash, { - let node = NodeIndex::new(node); - - if !graph.contains_node(node) { - return Err(InvalidNode::new_err( - "The input index for 'node' is not a valid node index", - )); - } - - Ok(bfs_undirected(&graph, node, &mut graph.visit_map()) - .into_iter() - .map(|x| x.index()) - .collect()) + bfs_undirected(&graph, node, &mut graph.visit_map()) } #[cfg(test)] diff --git a/retworkx-core/src/connectivity/mod.rs b/retworkx-core/src/connectivity/mod.rs index 49f502e02c..7d1d1e999e 100644 --- a/retworkx-core/src/connectivity/mod.rs +++ b/retworkx-core/src/connectivity/mod.rs @@ -23,6 +23,5 @@ pub use chain::chain_decomposition; pub use conn_components::bfs_undirected; pub use conn_components::connected_components; pub use conn_components::is_connected; -pub use conn_components::node_connected_components; +// pub use conn_components::node_connected_components; pub use conn_components::number_connected_components; -pub use conn_components::strongly_connected_components; diff --git a/src/connectivity/mod.rs b/src/connectivity/mod.rs index bbafd88fae..3114e36ad5 100644 --- a/src/connectivity/mod.rs +++ b/src/connectivity/mod.rs @@ -138,7 +138,10 @@ pub fn cycle_basis(graph: &graph::PyGraph, root: Option) -> Vec Vec> { - connectivity::strongly_connected_components(&graph.graph) + algo::kosaraju_scc(&graph.graph) + .iter() + .map(|x| x.iter().map(|id| id.index()).collect()) + .collect() } /// Return the first cycle encountered during DFS of a given PyDiGraph,