|
1 | 1 | use crate::connectivity::conn_components::connected_components;
|
2 | 2 | use crate::dictmap::*;
|
| 3 | +use crate::shortest_path::dijkstra; |
3 | 4 | use crate::Result;
|
4 | 5 | use hashbrown::{HashMap, HashSet};
|
5 | 6 | use petgraph::algo::{astar, min_spanning_tree};
|
6 | 7 | use petgraph::csr::IndexType;
|
7 |
| -use crate::shortest_path::dijkstra; |
8 | 8 | use petgraph::data::{DataMap, Element};
|
9 | 9 | use petgraph::graph::Graph;
|
10 | 10 | use petgraph::graph::NodeIndex;
|
@@ -35,20 +35,18 @@ impl EdgeWeightToNumber for i32 {
|
35 | 35 |
|
36 | 36 | fn create_subgraphs_from_components<G>(
|
37 | 37 | graph: G,
|
38 |
| - components: Vec<HashSet<G::NodeId>> |
| 38 | + components: Vec<HashSet<G::NodeId>>, |
39 | 39 | ) -> Vec<(Graph<G::NodeId, i32, Undirected>, HashMap<usize, NodeIndex>)>
|
40 | 40 | where
|
41 |
| - G: IntoEdgeReferences |
42 |
| - + NodeIndexable |
43 |
| - + IntoNodeIdentifiers, |
| 41 | + G: IntoEdgeReferences + NodeIndexable + IntoNodeIdentifiers, |
44 | 42 | G::NodeId: Eq + Hash,
|
45 | 43 | G::EdgeWeight: EdgeWeightToNumber,
|
46 | 44 | {
|
47 | 45 | components
|
48 | 46 | .into_iter()
|
49 | 47 | .map(|component| {
|
50 |
| - let mut subgraph = Graph::<_ ,i32, Undirected>::default(); |
51 |
| - let mut node_subnode_map:HashMap<usize, NodeIndex> = HashMap::new(); |
| 48 | + let mut subgraph = Graph::<_, i32, Undirected>::default(); |
| 49 | + let mut node_subnode_map: HashMap<usize, NodeIndex> = HashMap::new(); |
52 | 50 | for nodeid in graph.node_identifiers() {
|
53 | 51 | if component.contains(&nodeid) {
|
54 | 52 | let node = graph.to_index(nodeid);
|
|
66 | 64 | }
|
67 | 65 | }
|
68 | 66 | (subgraph, node_subnode_map)
|
69 |
| - }).collect() |
| 67 | + }) |
| 68 | + .collect() |
70 | 69 | }
|
71 | 70 | pub fn minimum_cycle_basis<G, E>(graph: G) -> Result<Vec<Vec<NodeIndex>>, E>
|
72 | 71 | where
|
@@ -114,20 +113,23 @@ where
|
114 | 113 | .map(|(index, node_index)| (node_index, index))
|
115 | 114 | .collect();
|
116 | 115 | for edge in subgraph.edge_references() {
|
117 |
| - sub_edges.push(( |
118 |
| - node_map[&edge.source()], |
119 |
| - node_map[&edge.target()], |
120 |
| - )); |
| 116 | + sub_edges.push((node_map[&edge.source()], node_map[&edge.target()])); |
121 | 117 | }
|
122 | 118 | let mst = min_spanning_tree(&subgraph);
|
123 |
| - let sub_mst_edges: Vec<_> = mst.filter_map(|element| { |
124 |
| - if let Element::Edge { source, target, weight: _ } = element { |
125 |
| - Some((source, target)) |
| 119 | + let sub_mst_edges: Vec<_> = mst |
| 120 | + .filter_map(|element| { |
| 121 | + if let Element::Edge { |
| 122 | + source, |
| 123 | + target, |
| 124 | + weight: _, |
| 125 | + } = element |
| 126 | + { |
| 127 | + Some((source, target)) |
126 | 128 | } else {
|
127 |
| - None |
| 129 | + None |
128 | 130 | }
|
129 |
| - }) |
130 |
| - .collect(); |
| 131 | + }) |
| 132 | + .collect(); |
131 | 133 |
|
132 | 134 | let mut chords: Vec<(usize, usize)> = Vec::new();
|
133 | 135 | for edge in sub_edges.iter() {
|
@@ -201,7 +203,7 @@ where
|
201 | 203 | fn _min_cycle<G, F, E>(
|
202 | 204 | subgraph: G,
|
203 | 205 | orth: HashSet<(usize, usize)>,
|
204 |
| - mut weight_fn: F |
| 206 | + mut weight_fn: F, |
205 | 207 | ) -> Result<Vec<(usize, usize)>, E>
|
206 | 208 | where
|
207 | 209 | G: IntoNodeReferences + IntoEdgeReferences + DataMap + NodeIndexable,
|
@@ -243,11 +245,13 @@ where
|
243 | 245 | let mut shortest_path_map: HashMap<G::NodeId, i32> = HashMap::new();
|
244 | 246 | for nodeid in subgraph.node_identifiers() {
|
245 | 247 | let (node, lifted_node) = subgraph_gi_map[&nodeid];
|
246 |
| - let result: Result<DictMap<NodeIndex, i32>> = dijkstra(&gi, |
247 |
| - node, |
248 |
| - Some(lifted_node), |
| 248 | + let result: Result<DictMap<NodeIndex, i32>> = dijkstra( |
| 249 | + &gi, |
| 250 | + node, |
| 251 | + Some(lifted_node), |
249 | 252 | |e| Ok(*e.weight() as i32),
|
250 |
| - None); |
| 253 | + None, |
| 254 | + ); |
251 | 255 | // Find the shortest distance in the result and store it in the shortest_path_map
|
252 | 256 | let spl = result.unwrap()[&lifted_node];
|
253 | 257 | shortest_path_map.insert(nodeid, spl);
|
@@ -301,16 +305,13 @@ where
|
301 | 305 | Ok(min_edgelist)
|
302 | 306 | }
|
303 | 307 |
|
304 |
| - |
305 |
| - |
306 | 308 | #[cfg(test)]
|
307 | 309 | mod test_minimum_cycle_basis {
|
308 | 310 |
|
309 | 311 | use crate::connectivity::minimum_cycle_basis::minimum_cycle_basis;
|
310 | 312 | use petgraph::graph::{Graph, NodeIndex};
|
311 | 313 | use petgraph::Undirected;
|
312 | 314 |
|
313 |
| - |
314 | 315 | #[test]
|
315 | 316 | fn test_empty_graph() {
|
316 | 317 | let graph: Graph<String, i32> = Graph::new();
|
|
0 commit comments