Skip to content
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

Move more connected components functions to retworkx-core #638

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions rustworkx-core/src/connectivity/conn_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
// 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 hashbrown::HashSet;
use petgraph::algo;
kris524 marked this conversation as resolved.
Show resolved Hide resolved
use std::collections::VecDeque;
use std::hash::Hash;

use petgraph::visit::{GraphProp, IntoNeighborsDirected, IntoNodeIdentifiers, VisitMap, Visitable};
// use super::digraph;
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
/// using breadth first search and treating all edges
Expand Down Expand Up @@ -165,6 +167,33 @@ where
num_components
}

pub fn is_connected<G>(graph: G) -> bool
where
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.to_index());
kris524 marked this conversation as resolved.
Show resolved Hide resolved
component.len() == graph.node_count()
}
None => false,
}
}

pub fn node_connected_component<G>(graph: G, node: G::NodeId) -> HashSet<G::NodeId>
where
G: GraphProp + IntoNeighborsDirected + Visitable + IntoNodeIdentifiers,
G::NodeId: Eq + Hash,
{
bfs_undirected(&graph, node, &mut graph.visit_map())
}

#[cfg(test)]
mod test_conn_components {
use hashbrown::HashSet;
Expand Down
2 changes: 2 additions & 0 deletions rustworkx-core/src/connectivity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ pub use biconnected::articulation_points;
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;
kris524 marked this conversation as resolved.
Show resolved Hide resolved
pub use conn_components::number_connected_components;
23 changes: 2 additions & 21 deletions src/connectivity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,7 @@ pub fn connected_components(graph: &graph::PyGraph) -> Vec<HashSet<usize>> {
#[pyfunction]
#[pyo3(text_signature = "(graph, node, /)")]
pub fn node_connected_component(graph: &graph::PyGraph, node: usize) -> PyResult<HashSet<usize>> {
let node = NodeIndex::new(node);

if !graph.graph.contains_node(node) {
return Err(InvalidNode::new_err(
"The input index for 'node' is not a valid node index",
));
}

Ok(
connectivity::bfs_undirected(&graph.graph, node, &mut graph.graph.visit_map())
.into_iter()
.map(|x| x.index())
.collect(),
)
connectivity::node_connected_component(&graph.graph, node)
}

/// Check if the graph is connected.
Expand All @@ -288,13 +275,7 @@ pub fn node_connected_component(graph: &graph::PyGraph, node: usize) -> PyResult
#[pyfunction]
#[pyo3(text_signature = "(graph, /)")]
pub fn is_connected(graph: &graph::PyGraph) -> PyResult<bool> {
match graph.graph.node_indices().next() {
Some(node) => {
let component = node_connected_component(graph, node.index())?;
Ok(component.len() == graph.graph.node_count())
}
None => Err(NullGraph::new_err("Invalid operation on a NullGraph")),
}
connectivity::is_connected(&graph.graph)
}

/// Find the number of weakly connected components in a directed graph
Expand Down