Skip to content

Commit

Permalink
implement feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kris524 committed Sep 6, 2022
1 parent fef6492 commit f937eb5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 35 deletions.
48 changes: 16 additions & 32 deletions retworkx-core/src/connectivity/conn_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -166,48 +167,31 @@ where
num_components
}

pub fn strongly_connected_components<G>(graph: G) -> Vec<Vec<G::NodeId>>
pub fn is_connected<G>(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<G>(graph: G) -> Result<bool, NullGraph>
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<G>(graph: G, node: usize) -> Result<HashSet<usize>, InvalidNode >
pub fn node_connected_component<G>(graph: G, node: G::NodeId) -> HashSet<G::NodeId>
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)]
Expand Down
3 changes: 1 addition & 2 deletions retworkx-core/src/connectivity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 4 additions & 1 deletion src/connectivity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ pub fn cycle_basis(graph: &graph::PyGraph, root: Option<usize>) -> Vec<Vec<usize
#[pyfunction]
#[pyo3(text_signature = "(graph, /)")]
pub fn strongly_connected_components(graph: &digraph::PyDiGraph) -> Vec<Vec<usize>> {
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,
Expand Down

0 comments on commit f937eb5

Please sign in to comment.