@@ -3,14 +3,14 @@ use crate::dictmap::*;
33use crate :: shortest_path:: dijkstra;
44use crate :: Result ;
55use hashbrown:: { HashMap , HashSet } ;
6- use indexmap:: IndexMap ;
7- use petgraph:: algo:: { astar, min_spanning_tree, Measure } ;
6+ use petgraph:: algo:: { astar, min_spanning_tree, Measure } ;
87use petgraph:: csr:: { DefaultIx , IndexType } ;
98use petgraph:: data:: { DataMap , Element } ;
109use petgraph:: graph:: Graph ;
11- use petgraph:: graph:: { NodeIndex , EdgeIndex } ;
10+ use petgraph:: graph:: { EdgeIndex , NodeIndex } ;
1211use petgraph:: visit:: {
13- EdgeCount , EdgeIndexable , EdgeRef , GraphProp , IntoEdgeReferences , IntoEdges , IntoNeighborsDirected , IntoNodeIdentifiers , IntoNodeReferences , NodeIndexable , Visitable
12+ EdgeCount , EdgeIndexable , EdgeRef , GraphProp , IntoEdgeReferences , IntoEdges ,
13+ IntoNeighborsDirected , IntoNodeIdentifiers , IntoNodeReferences , NodeIndexable , Visitable ,
1414} ;
1515use petgraph:: Undirected ;
1616use std:: convert:: Infallible ;
@@ -20,15 +20,22 @@ fn create_subgraphs_from_components<G, F, K, E>(
2020 graph : G ,
2121 components : Vec < HashSet < G :: NodeId > > ,
2222 mut weight_fn : F ,
23- ) ->Result < Vec < ( Graph < usize , K , Undirected > , HashMap < usize , NodeIndex > , HashMap < usize , EdgeIndex > ) > , E >
23+ ) -> Result <
24+ Vec < (
25+ Graph < usize , K , Undirected > ,
26+ HashMap < usize , NodeIndex > ,
27+ HashMap < usize , EdgeIndex > ,
28+ ) > ,
29+ E ,
30+ >
2431where
25- G : IntoEdgeReferences
26- + NodeIndexable
27- + IntoNodeIdentifiers
28- + EdgeIndexable
29- + EdgeCount
30- + Visitable
31- + IntoEdges ,
32+ G : IntoEdgeReferences
33+ + NodeIndexable
34+ + IntoNodeIdentifiers
35+ + EdgeIndexable
36+ + EdgeCount
37+ + Visitable
38+ + IntoEdges ,
3239 G :: NodeId : Eq + Hash ,
3340 G :: EdgeWeight : Clone ,
3441 F : FnMut ( G :: EdgeRef ) -> Result < K , E > ,
7077 } )
7178 . collect ( )
7279}
73- pub fn minimum_cycle_basis < G , F , K , E > (
74- graph : G ,
75- mut weight_fn : F ,
76- ) -> Result < Vec < Vec < NodeIndex > > , E >
80+ pub fn minimum_cycle_basis < G , F , K , E > ( graph : G , mut weight_fn : F ) -> Result < Vec < Vec < NodeIndex > > , E >
7781where
7882 G : EdgeCount
7983 + IntoNodeIdentifiers
9195{
9296 let conn_components = connected_components ( & graph) ;
9397 let mut min_cycle_basis = Vec :: new ( ) ;
94- let subgraphs_with_maps = create_subgraphs_from_components ( & graph, conn_components, & mut weight_fn) ?;
98+ let subgraphs_with_maps =
99+ create_subgraphs_from_components ( & graph, conn_components, & mut weight_fn) ?;
95100 // Convert weight_fn to a closure that takes a subgraph edge and returns the weight of the original graph edge
96101 for ( subgraph, node_subnode_map, edge_subedge_map) in subgraphs_with_maps {
97102 // Find the key of edge_subedge_map that corresponds to the value e.id()
@@ -121,7 +126,12 @@ fn _min_cycle_basis<H, F, K, E>(
121126 node_subnode_map : & HashMap < usize , NodeIndex > ,
122127) -> Result < Vec < Vec < NodeIndex > > , E >
123128where
124- H : EdgeCount + IntoNodeReferences + IntoEdgeReferences + NodeIndexable + DataMap + EdgeIndexable ,
129+ H : EdgeCount
130+ + IntoNodeReferences
131+ + IntoEdgeReferences
132+ + NodeIndexable
133+ + DataMap
134+ + EdgeIndexable ,
125135 H :: NodeWeight : Clone ,
126136 H :: EdgeWeight : Clone + PartialOrd ,
127137 H :: NodeId : Eq + Hash ,
@@ -171,7 +181,7 @@ where
171181 }
172182 while let Some ( chord_pop) = set_orth. pop ( ) {
173183 let base = chord_pop;
174- let cycle_edges = _min_cycle ( & subgraph, base. clone ( ) , & mut weight_fn) ?;
184+ let cycle_edges = _min_cycle ( & subgraph, base. clone ( ) , & mut weight_fn) ?;
175185 let mut cb_temp: Vec < usize > = Vec :: new ( ) ;
176186 for edge in cycle_edges. iter ( ) {
177187 cb_temp. push ( edge. 1 ) ;
@@ -291,7 +301,7 @@ where
291301 & gi,
292302 min_start_node,
293303 |finish| finish == min_start_lifted_node,
294- |e | * e. weight ( ) ,
304+ |e| * e. weight ( ) ,
295305 |_| K :: default ( ) ,
296306 ) ;
297307 let mut min_path: Vec < usize > = Vec :: new ( ) ;
@@ -346,8 +356,7 @@ mod test_minimum_cycle_basis {
346356 let weight_fn = |edge : petgraph:: graph:: EdgeReference < i32 > | -> Result < i32 , Infallible > {
347357 Ok ( * edge. weight ( ) )
348358 } ;
349- let output =
350- minimum_cycle_basis ( & graph, weight_fn) . unwrap ( ) ;
359+ let output = minimum_cycle_basis ( & graph, weight_fn) . unwrap ( ) ;
351360 assert_eq ! ( output. len( ) , 0 ) ;
352361 }
353362
@@ -363,8 +372,7 @@ mod test_minimum_cycle_basis {
363372 let weight_fn = |edge : petgraph:: graph:: EdgeReference < i32 > | -> Result < i32 , Infallible > {
364373 Ok ( * edge. weight ( ) )
365374 } ;
366- let cycles =
367- minimum_cycle_basis ( & graph, weight_fn) ;
375+ let cycles = minimum_cycle_basis ( & graph, weight_fn) ;
368376 println ! ( "Cycles {:?}" , cycles. as_ref( ) . unwrap( ) ) ;
369377 assert_eq ! ( cycles. unwrap( ) . len( ) , 1 ) ;
370378 }
@@ -385,8 +393,7 @@ mod test_minimum_cycle_basis {
385393 let weight_fn = |edge : petgraph:: graph:: EdgeReference < i32 > | -> Result < i32 , Infallible > {
386394 Ok ( * edge. weight ( ) )
387395 } ;
388- let cycles =
389- minimum_cycle_basis ( & graph, weight_fn) ;
396+ let cycles = minimum_cycle_basis ( & graph, weight_fn) ;
390397 assert_eq ! ( cycles. unwrap( ) . len( ) , 2 ) ;
391398 }
392399
@@ -405,9 +412,8 @@ mod test_minimum_cycle_basis {
405412 let weight_fn = |edge : petgraph:: graph:: EdgeReference < i32 > | -> Result < i32 , Infallible > {
406413 Ok ( * edge. weight ( ) )
407414 } ;
408- let output =
409- minimum_cycle_basis ( & weighted_diamond, weight_fn) ;
410- let expected_output: Vec < Vec < usize > > = vec ! [ vec![ 0 , 1 , 3 ] , vec![ 0 , 1 , 2 , 3 ] ] ;
415+ let output = minimum_cycle_basis ( & weighted_diamond, weight_fn) ;
416+ let expected_output: Vec < Vec < usize > > = vec ! [ vec![ 0 , 1 , 3 ] , vec![ 0 , 1 , 2 , 3 ] ] ;
411417 for cycle in output. unwrap ( ) . iter ( ) {
412418 println ! ( "{:?}" , cycle) ;
413419 let mut node_indices: Vec < usize > = Vec :: new ( ) ;
@@ -435,12 +441,10 @@ mod test_minimum_cycle_basis {
435441 unweighted_diamond. add_edge ( ud_node2, ud_node3, ( ) ) ;
436442 unweighted_diamond. add_edge ( ud_node3, ud_node0, ( ) ) ;
437443 unweighted_diamond. add_edge ( ud_node1, ud_node3, ( ) ) ;
438- let weight_fn = |_edge : petgraph:: graph:: EdgeReference < ( ) > | -> Result < i32 , Infallible > {
439- Ok ( 1 )
440- } ;
441-
442- let output =
443- minimum_cycle_basis ( & unweighted_diamond, weight_fn) ;
444+ let weight_fn =
445+ |_edge : petgraph:: graph:: EdgeReference < ( ) > | -> Result < i32 , Infallible > { Ok ( 1 ) } ;
446+
447+ let output = minimum_cycle_basis ( & unweighted_diamond, weight_fn) ;
444448 let expected_output: Vec < Vec < usize > > = vec ! [ vec![ 0 , 1 , 3 ] , vec![ 1 , 2 , 3 ] ] ;
445449 for cycle in output. unwrap ( ) . iter ( ) {
446450 let mut node_indices: Vec < usize > = Vec :: new ( ) ;
@@ -472,8 +476,7 @@ mod test_minimum_cycle_basis {
472476 let weight_fn = |edge : petgraph:: graph:: EdgeReference < i32 > | -> Result < i32 , Infallible > {
473477 Ok ( * edge. weight ( ) )
474478 } ;
475- let output =
476- minimum_cycle_basis ( & complete_graph, weight_fn) ;
479+ let output = minimum_cycle_basis ( & complete_graph, weight_fn) ;
477480 for cycle in output. unwrap ( ) . iter ( ) {
478481 assert_eq ! ( cycle. len( ) , 3 ) ;
479482 }
0 commit comments