Skip to content

Commit

Permalink
cargo fmt and cargo test
Browse files Browse the repository at this point in the history
  • Loading branch information
gluonhiggs committed Jun 5, 2024
1 parent 6564591 commit f2238fd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
77 changes: 40 additions & 37 deletions rustworkx-core/src/connectivity/minimum_cycle_basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use crate::dictmap::*;
use crate::shortest_path::dijkstra;
use crate::Result;
use hashbrown::{HashMap, HashSet};
use indexmap::IndexMap;
use petgraph::algo:: {astar, min_spanning_tree, Measure};
use petgraph::algo::{astar, min_spanning_tree, Measure};
use petgraph::csr::{DefaultIx, IndexType};
use petgraph::data::{DataMap, Element};
use petgraph::graph::Graph;
use petgraph::graph::{NodeIndex, EdgeIndex};
use petgraph::graph::{EdgeIndex, NodeIndex};
use petgraph::visit::{
EdgeCount, EdgeIndexable, EdgeRef, GraphProp, IntoEdgeReferences, IntoEdges, IntoNeighborsDirected, IntoNodeIdentifiers, IntoNodeReferences, NodeIndexable, Visitable
EdgeCount, EdgeIndexable, EdgeRef, GraphProp, IntoEdgeReferences, IntoEdges,
IntoNeighborsDirected, IntoNodeIdentifiers, IntoNodeReferences, NodeIndexable, Visitable,
};
use petgraph::Undirected;
use std::convert::Infallible;
Expand All @@ -20,15 +20,22 @@ fn create_subgraphs_from_components<G, F, K, E>(
graph: G,
components: Vec<HashSet<G::NodeId>>,
mut weight_fn: F,
) ->Result<Vec<(Graph<usize, K, Undirected>, HashMap<usize, NodeIndex>, HashMap<usize, EdgeIndex>)>,E>
) -> Result<
Vec<(
Graph<usize, K, Undirected>,
HashMap<usize, NodeIndex>,
HashMap<usize, EdgeIndex>,
)>,
E,
>
where
G: IntoEdgeReferences
+ NodeIndexable
+ IntoNodeIdentifiers
+ EdgeIndexable
+ EdgeCount
+ Visitable
+ IntoEdges,
G: IntoEdgeReferences
+ NodeIndexable
+ IntoNodeIdentifiers
+ EdgeIndexable
+ EdgeCount
+ Visitable
+ IntoEdges,
G::NodeId: Eq + Hash,
G::EdgeWeight: Clone,
F: FnMut(G::EdgeRef) -> Result<K, E>,
Expand Down Expand Up @@ -70,10 +77,7 @@ where
})
.collect()
}
pub fn minimum_cycle_basis<G, F, K, E>(
graph: G,
mut weight_fn: F,
) -> Result<Vec<Vec<NodeIndex>>, E>
pub fn minimum_cycle_basis<G, F, K, E>(graph: G, mut weight_fn: F) -> Result<Vec<Vec<NodeIndex>>, E>
where
G: EdgeCount
+ IntoNodeIdentifiers
Expand All @@ -91,7 +95,8 @@ where
{
let conn_components = connected_components(&graph);
let mut min_cycle_basis = Vec::new();
let subgraphs_with_maps = create_subgraphs_from_components(&graph, conn_components, &mut weight_fn)?;
let subgraphs_with_maps =
create_subgraphs_from_components(&graph, conn_components, &mut weight_fn)?;
// Convert weight_fn to a closure that takes a subgraph edge and returns the weight of the original graph edge
for (subgraph, node_subnode_map, edge_subedge_map) in subgraphs_with_maps {
// Find the key of edge_subedge_map that corresponds to the value e.id()
Expand Down Expand Up @@ -121,7 +126,12 @@ fn _min_cycle_basis<H, F, K, E>(
node_subnode_map: &HashMap<usize, NodeIndex>,
) -> Result<Vec<Vec<NodeIndex>>, E>
where
H: EdgeCount + IntoNodeReferences + IntoEdgeReferences + NodeIndexable + DataMap + EdgeIndexable,
H: EdgeCount
+ IntoNodeReferences
+ IntoEdgeReferences
+ NodeIndexable
+ DataMap
+ EdgeIndexable,
H::NodeWeight: Clone,
H::EdgeWeight: Clone + PartialOrd,
H::NodeId: Eq + Hash,
Expand Down Expand Up @@ -171,7 +181,7 @@ where
}
while let Some(chord_pop) = set_orth.pop() {
let base = chord_pop;
let cycle_edges = _min_cycle(&subgraph, base.clone(),&mut weight_fn)?;
let cycle_edges = _min_cycle(&subgraph, base.clone(), &mut weight_fn)?;
let mut cb_temp: Vec<usize> = Vec::new();
for edge in cycle_edges.iter() {
cb_temp.push(edge.1);
Expand Down Expand Up @@ -291,7 +301,7 @@ where
&gi,
min_start_node,
|finish| finish == min_start_lifted_node,
|e | *e.weight(),
|e| *e.weight(),
|_| K::default(),
);
let mut min_path: Vec<usize> = Vec::new();
Expand Down Expand Up @@ -346,8 +356,7 @@ mod test_minimum_cycle_basis {
let weight_fn = |edge: petgraph::graph::EdgeReference<i32>| -> Result<i32, Infallible> {
Ok(*edge.weight())
};
let output =
minimum_cycle_basis(&graph, weight_fn).unwrap();
let output = minimum_cycle_basis(&graph, weight_fn).unwrap();
assert_eq!(output.len(), 0);
}

Expand All @@ -363,8 +372,7 @@ mod test_minimum_cycle_basis {
let weight_fn = |edge: petgraph::graph::EdgeReference<i32>| -> Result<i32, Infallible> {
Ok(*edge.weight())
};
let cycles =
minimum_cycle_basis(&graph, weight_fn);
let cycles = minimum_cycle_basis(&graph, weight_fn);
println!("Cycles {:?}", cycles.as_ref().unwrap());
assert_eq!(cycles.unwrap().len(), 1);
}
Expand All @@ -385,8 +393,7 @@ mod test_minimum_cycle_basis {
let weight_fn = |edge: petgraph::graph::EdgeReference<i32>| -> Result<i32, Infallible> {
Ok(*edge.weight())
};
let cycles =
minimum_cycle_basis(&graph, weight_fn);
let cycles = minimum_cycle_basis(&graph, weight_fn);
assert_eq!(cycles.unwrap().len(), 2);
}

Expand All @@ -405,9 +412,8 @@ mod test_minimum_cycle_basis {
let weight_fn = |edge: petgraph::graph::EdgeReference<i32>| -> Result<i32, Infallible> {
Ok(*edge.weight())
};
let output =
minimum_cycle_basis(&weighted_diamond, weight_fn);
let expected_output: Vec<Vec<usize>> = vec![vec![0,1,3], vec![0, 1, 2, 3]];
let output = minimum_cycle_basis(&weighted_diamond, weight_fn);
let expected_output: Vec<Vec<usize>> = vec![vec![0, 1, 3], vec![0, 1, 2, 3]];
for cycle in output.unwrap().iter() {
println!("{:?}", cycle);
let mut node_indices: Vec<usize> = Vec::new();
Expand Down Expand Up @@ -435,12 +441,10 @@ mod test_minimum_cycle_basis {
unweighted_diamond.add_edge(ud_node2, ud_node3, ());
unweighted_diamond.add_edge(ud_node3, ud_node0, ());
unweighted_diamond.add_edge(ud_node1, ud_node3, ());
let weight_fn = |_edge: petgraph::graph::EdgeReference<()>| -> Result<i32, Infallible> {
Ok(1)
};

let output =
minimum_cycle_basis(&unweighted_diamond, weight_fn);
let weight_fn =
|_edge: petgraph::graph::EdgeReference<()>| -> Result<i32, Infallible> { Ok(1) };

let output = minimum_cycle_basis(&unweighted_diamond, weight_fn);
let expected_output: Vec<Vec<usize>> = vec![vec![0, 1, 3], vec![1, 2, 3]];
for cycle in output.unwrap().iter() {
let mut node_indices: Vec<usize> = Vec::new();
Expand Down Expand Up @@ -472,8 +476,7 @@ mod test_minimum_cycle_basis {
let weight_fn = |edge: petgraph::graph::EdgeReference<i32>| -> Result<i32, Infallible> {
Ok(*edge.weight())
};
let output =
minimum_cycle_basis(&complete_graph, weight_fn);
let output = minimum_cycle_basis(&complete_graph, weight_fn);
for cycle in output.unwrap().iter() {
assert_eq!(cycle.len(), 3);
}
Expand Down
3 changes: 1 addition & 2 deletions src/connectivity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use hashbrown::{HashMap, HashSet};
use petgraph::stable_graph::NodeIndex;
use petgraph::unionfind::UnionFind;
use petgraph::visit::{EdgeRef, IntoEdgeReferences, NodeCount, NodeIndexable, Visitable};
use petgraph::{algo, Graph};
use petgraph::algo;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyDict;
Expand Down Expand Up @@ -918,7 +918,6 @@ pub fn stoer_wagner_min_cut(
}))
}


/// Return the articulation points of an undirected graph.
///
/// An articulation point or cut vertex is any node whose removal (along with
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ fn rustworkx(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
))?;
m.add_wrapped(wrap_pyfunction!(metric_closure))?;
m.add_wrapped(wrap_pyfunction!(stoer_wagner_min_cut))?;
m.add_wrapped(wrap_pyfunction!(minimum_cycle_basis))?;
m.add_wrapped(wrap_pyfunction!(steiner_tree::steiner_tree))?;
m.add_wrapped(wrap_pyfunction!(digraph_dfs_search))?;
m.add_wrapped(wrap_pyfunction!(graph_dfs_search))?;
Expand Down

0 comments on commit f2238fd

Please sign in to comment.