@@ -17,7 +17,7 @@ use std::sync::RwLock;
1717use hashbrown:: HashMap ;
1818use petgraph:: algo:: dijkstra;
1919use petgraph:: visit:: {
20- EdgeCount , EdgeIndexable , EdgeRef , GraphBase , GraphProp , IntoEdgeReferences , IntoEdges ,
20+ Bfs , EdgeCount , EdgeIndexable , EdgeRef , GraphBase , GraphProp , IntoEdgeReferences , IntoEdges ,
2121 IntoEdgesDirected , IntoNeighbors , IntoNeighborsDirected , IntoNodeIdentifiers , NodeCount ,
2222 NodeIndexable , Reversed , ReversedEdgeReference , Visitable ,
2323} ;
@@ -1133,11 +1133,26 @@ where
11331133 let index = graph. to_index ( node) ;
11341134 node_indices[ index] = Some ( node) ;
11351135 } ) ;
1136+
1137+ let unweighted_shortest_path = |g : Reversed < & G > , s : G :: NodeId | -> HashMap < G :: NodeId , usize > {
1138+ let mut distances = HashMap :: new ( ) ;
1139+ let mut bfs = Bfs :: new ( g, s) ;
1140+ distances. insert ( s, 0 ) ;
1141+ while let Some ( node) = bfs. next ( g) {
1142+ let distance = distances[ & node] ;
1143+ for edge in g. edges ( node) {
1144+ let target = edge. target ( ) ;
1145+ distances. entry ( target) . or_insert ( distance + 1 ) ;
1146+ }
1147+ }
1148+ distances
1149+ } ;
1150+
11361151 let closeness: Vec < Option < f64 > > =
11371152 CondIterator :: new ( node_indices, graph. node_count ( ) >= parallel_threshold)
11381153 . map ( |node_s| {
11391154 let node_s = node_s?;
1140- let map = dijkstra ( Reversed ( & graph) , node_s, None , |_| 1 ) ;
1155+ let map = unweighted_shortest_path ( Reversed ( & graph) , node_s) ;
11411156 let reachable_nodes_count = map. len ( ) ;
11421157 let dists_sum: usize = map. into_values ( ) . sum ( ) ;
11431158 if reachable_nodes_count == 1 {
@@ -1265,6 +1280,7 @@ where
12651280 let index = graph. to_index ( node) ;
12661281 node_indices[ index] = Some ( node) ;
12671282 } ) ;
1283+
12681284 let closeness: Vec < Option < f64 > > =
12691285 CondIterator :: new ( node_indices, graph. node_count ( ) >= parallel_threshold)
12701286 . map ( |node_s| {
0 commit comments