use crate::datatypes::values::Value;
use crate::graph::schema::{DirGraph, InternedKey};
use crate::graph::storage::GraphRead;
use petgraph::algo::kosaraju_scc;
use petgraph::graph::NodeIndex;
use std::collections::{HashMap, HashSet};
use std::time::Instant;
pub use super::centrality::*;
pub fn algorithm_timeout_err() -> String {
"CALL procedure timed out. Pass timeout_ms=N to cypher() to extend, \
or timeout_ms=0 to disable the deadline. Subgraph scoping is not \
yet supported — large graphs may not converge within the default 20s."
.to_string()
}
pub(crate) fn intern_connection_types(
connection_types: Option<&[String]>,
) -> Option<Vec<InternedKey>> {
connection_types.map(|types| types.iter().map(|t| InternedKey::from_str(t)).collect())
}
fn filtered_neighbors_undirected(
graph: &DirGraph,
node: NodeIndex,
connection_types: Option<&[InternedKey]>,
) -> Vec<NodeIndex> {
use petgraph::Direction;
let g = &graph.graph;
let mut neighbors: Vec<NodeIndex> = match connection_types {
None => g.neighbors_undirected(node).collect(),
Some(types) => {
let mut n = Vec::new();
for edge in g.edges_directed(node, Direction::Outgoing) {
if types.iter().any(|t| *t == edge.connection_type()) {
n.push(edge.target());
}
}
for edge in g.edges_directed(node, Direction::Incoming) {
if types.iter().any(|t| *t == edge.connection_type()) {
n.push(edge.source());
}
}
n
}
};
if neighbors.len() > 1 {
neighbors.sort_unstable();
neighbors.dedup();
}
neighbors
}
fn filtered_neighbors_outgoing(
graph: &DirGraph,
node: NodeIndex,
connection_types: Option<&[InternedKey]>,
) -> Vec<NodeIndex> {
use petgraph::Direction;
let g = &graph.graph;
match connection_types {
None => g.neighbors_directed(node, Direction::Outgoing).collect(),
Some(types) => g
.edges_directed(node, Direction::Outgoing)
.filter(|e| types.iter().any(|t| *t == e.connection_type()))
.map(|e| e.target())
.collect(),
}
}
fn node_passes_via_filter(
graph: &DirGraph,
node: NodeIndex,
via_types: &Option<HashSet<&str>>,
) -> bool {
match via_types {
None => true,
Some(types) => {
if let Some(node_data) = graph.graph.node_weight(node) {
types.contains(node_data.node_type_str(&graph.interner))
} else {
false
}
}
}
}
#[derive(Debug, Clone)]
pub struct PathResult {
pub path: Vec<NodeIndex>,
pub cost: usize,
}
#[derive(Debug, Clone)]
pub struct PathNodeInfo {
pub node_type: String,
pub title: String,
pub id: Value,
}
pub fn shortest_path(
graph: &DirGraph,
source: NodeIndex,
target: NodeIndex,
connection_types: Option<&[String]>,
via_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Option<PathResult> {
let via_set: Option<HashSet<&str>> =
via_types.map(|vt| vt.iter().map(|s| s.as_str()).collect());
let interned = intern_connection_types(connection_types);
let path = reconstruct_path_bfs(
graph,
source,
target,
interned.as_deref(),
&via_set,
deadline,
)?;
let cost = path.len().saturating_sub(1);
Some(PathResult { path, cost })
}
pub fn shortest_path_cost(graph: &DirGraph, source: NodeIndex, target: NodeIndex) -> Option<usize> {
if source == target {
return Some(0);
}
let node_bound = graph.graph.node_bound();
let mut visited: Vec<bool> = vec![false; node_bound];
let target_idx = target.index();
let mut current_level: Vec<usize> = vec![source.index()];
let mut next_level: Vec<usize> = Vec::new();
visited[source.index()] = true;
let mut depth: usize = 0;
while !current_level.is_empty() {
depth += 1;
next_level.clear();
for ¤t_idx in ¤t_level {
let current = NodeIndex::new(current_idx);
for neighbor in {
let g = &graph.graph;
g.neighbors_undirected(current)
} {
let neighbor_idx = neighbor.index();
if !visited[neighbor_idx] {
if neighbor_idx == target_idx {
return Some(depth);
}
visited[neighbor_idx] = true;
next_level.push(neighbor_idx);
}
}
}
std::mem::swap(&mut current_level, &mut next_level);
}
None
}
pub fn shortest_path_cost_batch(
graph: &DirGraph,
pairs: &[(NodeIndex, NodeIndex)],
) -> Vec<Option<usize>> {
let node_bound = graph.graph.node_bound();
let nodes: Vec<NodeIndex> = {
let g = &graph.graph;
g.node_indices().collect()
};
let n = nodes.len();
let mut node_to_idx = vec![usize::MAX; node_bound];
for (i, &node) in nodes.iter().enumerate() {
node_to_idx[node.index()] = i;
}
let mut adj: Vec<Vec<usize>> = vec![Vec::new(); n];
for edge in {
let g = &graph.graph;
g.edge_references()
} {
let src_i = node_to_idx[edge.source().index()];
let tgt_i = node_to_idx[edge.target().index()];
if src_i != usize::MAX && tgt_i != usize::MAX {
adj[src_i].push(tgt_i);
adj[tgt_i].push(src_i);
}
}
for neighbors in &mut adj {
neighbors.sort_unstable();
neighbors.dedup();
}
let mut visited: Vec<bool> = vec![false; n];
let mut current_level: Vec<usize> = Vec::new();
let mut next_level: Vec<usize> = Vec::new();
let mut results = Vec::with_capacity(pairs.len());
for &(source, target) in pairs {
if source == target {
results.push(Some(0));
continue;
}
let src_i = node_to_idx[source.index()];
let tgt_i = node_to_idx[target.index()];
if src_i == usize::MAX || tgt_i == usize::MAX {
results.push(None);
continue;
}
let mut touched: Vec<usize> = Vec::new();
current_level.clear();
current_level.push(src_i);
visited[src_i] = true;
touched.push(src_i);
let mut depth: usize = 0;
let mut found = false;
'bfs: while !current_level.is_empty() {
depth += 1;
next_level.clear();
for ¤t_idx in ¤t_level {
for &neighbor_idx in &adj[current_idx] {
if !visited[neighbor_idx] {
if neighbor_idx == tgt_i {
found = true;
break 'bfs;
}
visited[neighbor_idx] = true;
touched.push(neighbor_idx);
next_level.push(neighbor_idx);
}
}
}
std::mem::swap(&mut current_level, &mut next_level);
}
results.push(if found { Some(depth) } else { None });
for &idx in &touched {
visited[idx] = false;
}
}
results
}
fn reconstruct_path_bfs(
graph: &DirGraph,
source: NodeIndex,
target: NodeIndex,
connection_types: Option<&[InternedKey]>,
via_types: &Option<HashSet<&str>>,
deadline: Option<Instant>,
) -> Option<Vec<NodeIndex>> {
use std::collections::{HashMap, VecDeque};
if source == target {
return Some(vec![source]);
}
let mut parent: HashMap<usize, u32> = HashMap::with_capacity(64);
let mut queue: VecDeque<usize> = VecDeque::with_capacity(64);
let source_idx = source.index();
let target_idx = target.index();
parent.insert(source_idx, source_idx as u32);
queue.push_back(source_idx);
let mut visit_count = 0u32;
while let Some(current_idx) = queue.pop_front() {
visit_count += 1;
if visit_count.is_multiple_of(1000) {
if let Some(dl) = deadline {
if Instant::now() > dl {
return None;
}
}
}
let current = NodeIndex::new(current_idx);
let neighbors = filtered_neighbors_undirected(graph, current, connection_types);
for neighbor in neighbors {
let neighbor_idx = neighbor.index();
if parent.contains_key(&neighbor_idx) {
continue;
}
if neighbor_idx != target_idx && !node_passes_via_filter(graph, neighbor, via_types) {
continue;
}
parent.insert(neighbor_idx, current_idx as u32);
if neighbor_idx == target_idx {
let mut path = Vec::with_capacity(16);
let mut node_idx = target_idx;
while node_idx != source_idx {
path.push(NodeIndex::new(node_idx));
node_idx = parent[&node_idx] as usize;
}
path.push(source);
path.reverse();
return Some(path);
}
queue.push_back(neighbor_idx);
}
}
None }
pub fn shortest_path_directed(
graph: &DirGraph,
source: NodeIndex,
target: NodeIndex,
connection_types: Option<&[String]>,
via_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Option<PathResult> {
use std::collections::VecDeque;
if source == target {
return Some(PathResult {
path: vec![source],
cost: 0,
});
}
let via_set: Option<HashSet<&str>> =
via_types.map(|vt| vt.iter().map(|s| s.as_str()).collect());
let interned = intern_connection_types(connection_types);
let node_bound = graph.graph.node_bound();
let mut visited: Vec<bool> = vec![false; node_bound];
let mut parent: Vec<u32> = vec![u32::MAX; node_bound];
let mut queue = VecDeque::with_capacity(node_bound / 4);
let source_idx = source.index();
let target_idx = target.index();
queue.push_back(source_idx);
visited[source_idx] = true;
let mut visit_count = 0u32;
while let Some(current_idx) = queue.pop_front() {
visit_count += 1;
if visit_count.is_multiple_of(1000) {
if let Some(dl) = deadline {
if Instant::now() > dl {
return None;
}
}
}
let current = NodeIndex::new(current_idx);
let neighbors = filtered_neighbors_outgoing(graph, current, interned.as_deref());
for neighbor in neighbors {
let neighbor_idx = neighbor.index();
if !visited[neighbor_idx] {
if neighbor_idx != target_idx && !node_passes_via_filter(graph, neighbor, &via_set)
{
continue;
}
visited[neighbor_idx] = true;
parent[neighbor_idx] = current_idx as u32;
queue.push_back(neighbor_idx);
if neighbor_idx == target_idx {
let mut path = Vec::with_capacity(16);
let mut node_idx = target_idx;
while node_idx != source_idx {
path.push(NodeIndex::new(node_idx));
node_idx = parent[node_idx] as usize;
}
path.push(source);
path.reverse();
let cost = path.len().saturating_sub(1);
return Some(PathResult { path, cost });
}
}
}
}
None
}
#[allow(clippy::too_many_arguments)]
pub fn all_paths(
graph: &DirGraph,
source: NodeIndex,
target: NodeIndex,
max_hops: usize,
max_results: Option<usize>,
connection_types: Option<&[String]>,
via_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Vec<Vec<NodeIndex>> {
let via_set: Option<HashSet<&str>> =
via_types.map(|vt| vt.iter().map(|s| s.as_str()).collect());
let interned = intern_connection_types(connection_types);
let mut results = Vec::new();
let mut current_path = vec![source];
let mut visited = HashSet::new();
visited.insert(source);
find_all_paths_recursive(
graph,
source,
target,
max_hops,
&mut current_path,
&mut visited,
&mut results,
max_results,
interned.as_deref(),
&via_set,
deadline,
);
results
}
#[allow(clippy::only_used_in_recursion, clippy::too_many_arguments)]
fn find_all_paths_recursive(
graph: &DirGraph,
current: NodeIndex,
target: NodeIndex,
remaining_hops: usize,
current_path: &mut Vec<NodeIndex>,
visited: &mut HashSet<NodeIndex>,
results: &mut Vec<Vec<NodeIndex>>,
max_results: Option<usize>,
connection_types: Option<&[InternedKey]>,
via_types: &Option<HashSet<&str>>,
deadline: Option<Instant>,
) {
if let Some(max) = max_results {
if results.len() >= max {
return;
}
}
if let Some(dl) = deadline {
if Instant::now() > dl {
return;
}
}
if current == target {
results.push(current_path.clone());
return;
}
if remaining_hops == 0 {
return;
}
let neighbors = filtered_neighbors_undirected(graph, current, connection_types);
for neighbor in neighbors {
if let Some(max) = max_results {
if results.len() >= max {
return;
}
}
if !visited.contains(&neighbor) {
if neighbor != target && !node_passes_via_filter(graph, neighbor, via_types) {
continue;
}
visited.insert(neighbor);
current_path.push(neighbor);
find_all_paths_recursive(
graph,
neighbor,
target,
remaining_hops - 1,
current_path,
visited,
results,
max_results,
connection_types,
via_types,
deadline,
);
current_path.pop();
visited.remove(&neighbor);
}
}
}
pub fn connected_components(graph: &DirGraph) -> Vec<Vec<NodeIndex>> {
if GraphRead::is_disk(&graph.graph) {
return weakly_connected_components(graph, None)
.expect("weakly_connected_components with deadline=None cannot time out");
}
kosaraju_scc(graph.graph.as_stable_digraph())
}
pub fn weakly_connected_components(
graph: &DirGraph,
deadline: Option<Instant>,
) -> Result<Vec<Vec<NodeIndex>>, String> {
weakly_connected_components_scoped(graph, None, None, deadline)
}
pub fn weakly_connected_components_scoped(
graph: &DirGraph,
node_types: Option<&[String]>,
rel_types: Option<&[InternedKey]>,
deadline: Option<Instant>,
) -> Result<Vec<Vec<NodeIndex>>, String> {
let edge_matches = |key: InternedKey| -> bool {
match rel_types {
Some(keys) => keys.contains(&key),
None => true,
}
};
let nodes: Vec<NodeIndex> = if let Some(types) = node_types {
let mut v = Vec::new();
for t in types {
if let Some(type_nodes) = graph.type_indices.get(t.as_str()) {
v.extend(type_nodes.iter());
}
}
v
} else if rel_types.is_some() {
let mut seen: HashSet<NodeIndex> = HashSet::new();
for edge in {
let g = &graph.graph;
g.edge_references()
} {
if edge_matches(edge.connection_type()) {
seen.insert(edge.source());
seen.insert(edge.target());
}
}
seen.into_iter().collect()
} else {
let g = &graph.graph;
g.node_indices().collect()
};
let n = nodes.len();
if n == 0 {
return Ok(Vec::new());
}
let bound = graph.graph.node_bound();
let mut node_to_idx = vec![usize::MAX; bound];
for (i, &node) in nodes.iter().enumerate() {
node_to_idx[node.index()] = i;
}
let mut parent: Vec<usize> = (0..n).collect();
let mut rank: Vec<u8> = vec![0; n];
#[inline]
fn find(parent: &mut [usize], mut x: usize) -> usize {
while parent[x] != x {
parent[x] = parent[parent[x]]; x = parent[x];
}
x
}
#[inline]
fn union(parent: &mut [usize], rank: &mut [u8], a: usize, b: usize) {
let ra = find(parent, a);
let rb = find(parent, b);
if ra == rb {
return;
}
if rank[ra] < rank[rb] {
parent[ra] = rb;
} else if rank[ra] > rank[rb] {
parent[rb] = ra;
} else {
parent[rb] = ra;
rank[ra] += 1;
}
}
let mut edge_counter: usize = 0;
for edge in {
let g = &graph.graph;
g.edge_references()
} {
edge_counter += 1;
if edge_counter & 0xFFFFF == 0 {
if let Some(dl) = deadline {
if Instant::now() > dl {
return Err(algorithm_timeout_err());
}
}
}
if !edge_matches(edge.connection_type()) {
continue;
}
let src_i = node_to_idx[edge.source().index()];
let tgt_i = node_to_idx[edge.target().index()];
if src_i == usize::MAX || tgt_i == usize::MAX {
continue;
}
union(&mut parent, &mut rank, src_i, tgt_i);
}
let mut component_map: HashMap<usize, Vec<NodeIndex>> = HashMap::new();
for (i, &node) in nodes.iter().enumerate() {
let root = find(&mut parent, i);
component_map.entry(root).or_default().push(node);
}
let mut components: Vec<Vec<NodeIndex>> = component_map.into_values().collect();
components.sort_by_key(|b| std::cmp::Reverse(b.len()));
Ok(components)
}
fn build_scoped_undirected_adjacency(
graph: &DirGraph,
node_types: Option<&[String]>,
rel_types: Option<&[InternedKey]>,
deadline: Option<Instant>,
) -> Result<(Vec<NodeIndex>, Vec<Vec<u32>>), String> {
let edge_matches = |key: InternedKey| -> bool {
match rel_types {
Some(keys) => keys.contains(&key),
None => true,
}
};
let nodes: Vec<NodeIndex> = scoped_universe(graph, node_types, rel_types);
let n = nodes.len();
let bound = graph.graph.node_bound();
let mut node_to_idx = vec![u32::MAX; bound];
for (i, &node) in nodes.iter().enumerate() {
node_to_idx[node.index()] = i as u32;
}
let mut adj: Vec<Vec<u32>> = vec![Vec::new(); n];
let mut counter = 0usize;
for edge in {
let g = &graph.graph;
g.edge_references()
} {
counter += 1;
if counter & 0xFFFFF == 0 {
if let Some(dl) = deadline {
if Instant::now() > dl {
return Err(algorithm_timeout_err());
}
}
}
if !edge_matches(edge.connection_type()) {
continue;
}
let s = node_to_idx[edge.source().index()];
let t = node_to_idx[edge.target().index()];
if s == u32::MAX || t == u32::MAX || s == t {
continue;
}
adj[s as usize].push(t);
adj[t as usize].push(s);
}
for list in adj.iter_mut() {
list.sort_unstable();
list.dedup();
}
Ok((nodes, adj))
}
pub fn coreness_scoped(
graph: &DirGraph,
node_types: Option<&[String]>,
rel_types: Option<&[InternedKey]>,
deadline: Option<Instant>,
) -> Result<Vec<(NodeIndex, i64)>, String> {
if graph.graph.is_disk() || graph.graph.is_mapped() {
return coreness_scoped_streaming(graph, node_types, rel_types, deadline);
}
let (nodes, adj) = build_scoped_undirected_adjacency(graph, node_types, rel_types, deadline)?;
let n = nodes.len();
if n == 0 {
return Ok(Vec::new());
}
let mut deg: Vec<u32> = adj.iter().map(|a| a.len() as u32).collect();
let max_deg = deg.iter().copied().max().unwrap_or(0) as usize;
let mut bin = vec![0usize; max_deg + 2];
for &d in ° {
bin[d as usize] += 1;
}
let mut start = 0usize;
for slot in bin.iter_mut().take(max_deg + 1) {
let count = *slot;
*slot = start;
start += count;
}
let mut vert = vec![0usize; n];
let mut pos = vec![0usize; n];
{
let mut binc = bin.clone();
for v in 0..n {
let d = deg[v] as usize;
pos[v] = binc[d];
vert[pos[v]] = v;
binc[d] += 1;
}
}
let mut core = vec![0i64; n];
for i in 0..n {
let v = vert[i];
core[v] = deg[v] as i64;
let dv = deg[v];
for &nbr in &adj[v] {
let u = nbr as usize;
if deg[u] > dv {
let du = deg[u] as usize;
let pu = pos[u];
let pw = bin[du];
let w = vert[pw];
if u != w {
vert[pu] = w;
vert[pw] = u;
pos[u] = pw;
pos[w] = pu;
}
bin[du] += 1;
deg[u] -= 1;
}
}
}
Ok(nodes.into_iter().zip(core).collect())
}
fn coreness_scoped_streaming(
graph: &DirGraph,
node_types: Option<&[String]>,
rel_types: Option<&[InternedKey]>,
deadline: Option<Instant>,
) -> Result<Vec<(NodeIndex, i64)>, String> {
let nodes = scoped_universe(graph, node_types, rel_types);
let src = DedupNeighborSource::new(graph, nodes, rel_types.map(|k| k.to_vec()));
let n = src.len();
if n == 0 {
return Ok(Vec::new());
}
let mut buf: Vec<u32> = Vec::new();
let mut deg: Vec<u32> = vec![0; n];
for (v, d) in deg.iter_mut().enumerate() {
if v & 0xFFFFF == 0 {
if let Some(dl) = deadline {
if Instant::now() > dl {
return Err(algorithm_timeout_err());
}
}
}
src.neighbors_deduped(v, &mut buf);
*d = buf.len() as u32;
}
let max_deg = deg.iter().copied().max().unwrap_or(0) as usize;
let mut bin = vec![0usize; max_deg + 2];
for &d in ° {
bin[d as usize] += 1;
}
let mut start = 0usize;
for slot in bin.iter_mut().take(max_deg + 1) {
let count = *slot;
*slot = start;
start += count;
}
let mut vert = vec![0usize; n];
let mut pos = vec![0usize; n];
{
let mut binc = bin.clone();
for v in 0..n {
let d = deg[v] as usize;
pos[v] = binc[d];
vert[pos[v]] = v;
binc[d] += 1;
}
}
let mut core = vec![0i64; n];
for i in 0..n {
if i & 0xFFFFF == 0 {
if let Some(dl) = deadline {
if Instant::now() > dl {
return Err(algorithm_timeout_err());
}
}
}
let v = vert[i];
core[v] = deg[v] as i64;
let dv = deg[v];
src.neighbors_deduped(v, &mut buf);
for &nbr in &buf {
let u = nbr as usize;
if deg[u] > dv {
let du = deg[u] as usize;
let pu = pos[u];
let pw = bin[du];
let w = vert[pw];
if u != w {
vert[pu] = w;
vert[pw] = u;
pos[u] = pw;
pos[w] = pu;
}
bin[du] += 1;
deg[u] -= 1;
}
}
}
Ok(src.nodes.into_iter().zip(core).collect())
}
pub fn clustering_coefficient_scoped(
graph: &DirGraph,
node_types: Option<&[String]>,
rel_types: Option<&[InternedKey]>,
deadline: Option<Instant>,
) -> Result<Vec<(NodeIndex, f64)>, String> {
let (nodes, adj) = build_scoped_undirected_adjacency(graph, node_types, rel_types, deadline)?;
let n = nodes.len();
let mut out = Vec::with_capacity(n);
for v in 0..n {
if v & 0xFFFF == 0 {
if let Some(dl) = deadline {
if Instant::now() > dl {
return Err(algorithm_timeout_err());
}
}
}
let nbrs = &adj[v];
let k = nbrs.len();
if k < 2 {
out.push((nodes[v], 0.0));
continue;
}
let mut links: u64 = 0;
for &a in nbrs {
links += intersection_count_gt(&adj[a as usize], nbrs, a);
}
let kf = k as f64;
out.push((nodes[v], (2.0 * links as f64) / (kf * (kf - 1.0))));
}
Ok(out)
}
fn intersection_count_gt(a: &[u32], b: &[u32], gt: u32) -> u64 {
let (mut i, mut j) = (0usize, 0usize);
let mut count = 0u64;
while i < a.len() && j < b.len() {
match a[i].cmp(&b[j]) {
std::cmp::Ordering::Less => i += 1,
std::cmp::Ordering::Greater => j += 1,
std::cmp::Ordering::Equal => {
if a[i] > gt {
count += 1;
}
i += 1;
j += 1;
}
}
}
count
}
pub fn get_node_info(graph: &DirGraph, node_idx: NodeIndex) -> Option<PathNodeInfo> {
let node = graph.get_node(node_idx)?;
let node_title = node.title();
let title_str = match &*node_title {
Value::String(s) => s.clone(),
_ => format!("{:?}", &*node_title),
};
Some(PathNodeInfo {
node_type: node.node_type_str(&graph.interner).to_string(),
title: title_str,
id: node.id().into_owned(),
})
}
pub fn get_path_connections(graph: &DirGraph, path: &[NodeIndex]) -> Vec<Option<String>> {
let mut connections = Vec::with_capacity(path.len().saturating_sub(1));
for window in path.windows(2) {
let from = window[0];
let to = window[1];
let conn_type = graph
.graph
.edges(from)
.find(|e| e.target() == to)
.map(|e| e.weight().connection_type_str(&graph.interner).to_string())
.or_else(|| {
graph
.graph
.edges(to)
.find(|e| e.target() == from)
.map(|e| e.weight().connection_type_str(&graph.interner).to_string())
});
connections.push(conn_type);
}
connections
}
pub fn are_connected(graph: &DirGraph, source: NodeIndex, target: NodeIndex) -> bool {
shortest_path(graph, source, target, None, None, None).is_some()
}
pub fn node_degree(graph: &DirGraph, node: NodeIndex) -> usize {
let g = &graph.graph;
g.edges(node).count()
+ g.neighbors_directed(node, petgraph::Direction::Incoming)
.count()
}
#[derive(Debug, Clone)]
pub struct CommunityAssignment {
pub node_idx: NodeIndex,
pub community_id: usize,
}
#[derive(Debug)]
pub struct CommunityResult {
pub assignments: Vec<CommunityAssignment>,
pub num_communities: usize,
pub modularity: f64,
pub levels: Vec<Vec<CommunityAssignment>>,
}
type Adjacency = Vec<Vec<(usize, f64)>>;
type RefineStep = Option<(Vec<usize>, usize, Vec<usize>)>;
fn build_weighted_adjacency(
graph: &DirGraph,
weight_property: Option<&str>,
connection_types: Option<&[String]>,
) -> (Vec<NodeIndex>, Adjacency, f64) {
let nodes: Vec<NodeIndex> = {
let g = &graph.graph;
g.node_indices().collect()
};
let n = nodes.len();
let bound = graph.graph.node_bound();
let mut node_to_idx = vec![0usize; bound];
for (i, &node) in nodes.iter().enumerate() {
node_to_idx[node.index()] = i;
}
let interned_ct = intern_connection_types(connection_types);
let mut adj: Vec<Vec<(usize, f64)>> = vec![Vec::new(); n];
let mut total_weight = 0.0f64;
for edge in {
let g = &graph.graph;
g.edge_references()
} {
if let Some(ref types) = interned_ct {
if !types.iter().any(|t| *t == edge.connection_type()) {
continue;
}
}
let w = edge_weight(graph, edge.id(), weight_property);
let src_i = node_to_idx[edge.source().index()];
let tgt_i = node_to_idx[edge.target().index()];
adj[src_i].push((tgt_i, w));
adj[tgt_i].push((src_i, w));
total_weight += w;
}
for neighbors in &mut adj {
neighbors.sort_unstable_by_key(|&(idx, _)| idx);
neighbors.dedup_by(|a, b| {
if a.0 == b.0 {
b.1 += a.1;
true
} else {
false
}
});
}
(nodes, adj, total_weight)
}
trait NeighborSource {
fn len(&self) -> usize;
fn total_weight(&self) -> f64;
fn for_each_neighbor(&self, i: usize, f: impl FnMut(usize, f64));
}
struct MaterializedAdj<'a> {
adj: &'a [Vec<(usize, f64)>],
m: f64,
}
impl NeighborSource for MaterializedAdj<'_> {
#[inline]
fn len(&self) -> usize {
self.adj.len()
}
#[inline]
fn total_weight(&self) -> f64 {
self.m
}
#[inline]
fn for_each_neighbor(&self, i: usize, mut f: impl FnMut(usize, f64)) {
for &(j, w) in &self.adj[i] {
f(j, w);
}
}
}
struct CsrSource<'a> {
graph: &'a DirGraph,
nodes: Vec<NodeIndex>,
node_to_idx: Vec<usize>,
interned_ct: Option<Vec<InternedKey>>,
weight_property: Option<&'a str>,
use_fast: bool,
fast_conn: Option<u64>,
m: f64,
}
impl<'a> CsrSource<'a> {
fn new(
graph: &'a DirGraph,
weight_property: Option<&'a str>,
connection_types: Option<&[String]>,
) -> Self {
let nodes: Vec<NodeIndex> = graph.graph.node_indices().collect();
let bound = graph.graph.node_bound();
let mut node_to_idx = vec![0usize; bound];
for (i, &node) in nodes.iter().enumerate() {
node_to_idx[node.index()] = i;
}
let interned_ct = intern_connection_types(connection_types);
let multi_type = connection_types.is_some_and(|c| c.len() > 1);
let use_fast = weight_property.is_none() && !multi_type;
let fast_conn = if use_fast {
interned_ct
.as_ref()
.and_then(|v| v.first())
.map(|k| k.as_u64())
} else {
None
};
let mut m = 0.0f64;
for edge in graph.graph.edge_references() {
if let Some(ref types) = interned_ct {
if !types.iter().any(|t| *t == edge.connection_type()) {
continue;
}
}
m += edge_weight(graph, edge.id(), weight_property);
}
Self {
graph,
nodes,
node_to_idx,
interned_ct,
weight_property,
use_fast,
fast_conn,
m,
}
}
}
impl NeighborSource for CsrSource<'_> {
fn len(&self) -> usize {
self.nodes.len()
}
fn total_weight(&self) -> f64 {
self.m
}
fn for_each_neighbor(&self, i: usize, mut f: impl FnMut(usize, f64)) {
use petgraph::Direction;
let node = self.nodes[i];
if self.use_fast {
for dir in [Direction::Outgoing, Direction::Incoming] {
for (peer, _eid) in self
.graph
.graph
.iter_peers_filtered(node, dir, self.fast_conn)
{
f(self.node_to_idx[peer.index()], 1.0);
}
}
return;
}
for dir in [Direction::Outgoing, Direction::Incoming] {
for edge in self.graph.graph.edges_directed(node, dir) {
if let Some(ref types) = self.interned_ct {
if !types.iter().any(|t| *t == edge.connection_type()) {
continue;
}
}
let peer = match dir {
Direction::Outgoing => edge.target(),
Direction::Incoming => edge.source(),
};
let w = edge_weight(self.graph, edge.id(), self.weight_property);
f(self.node_to_idx[peer.index()], w);
}
}
}
}
fn scoped_universe(
graph: &DirGraph,
node_types: Option<&[String]>,
edge_types: Option<&[InternedKey]>,
) -> Vec<NodeIndex> {
if let Some(types) = node_types {
let mut v = Vec::new();
for t in types {
if let Some(tn) = graph.type_indices.get(t.as_str()) {
v.extend(tn.iter());
}
}
v
} else if let Some(keys) = edge_types {
let mut seen: HashSet<NodeIndex> = HashSet::new();
for edge in graph.graph.edge_references() {
if keys.contains(&edge.connection_type()) {
seen.insert(edge.source());
seen.insert(edge.target());
}
}
seen.into_iter().collect()
} else {
graph.graph.node_indices().collect()
}
}
struct DedupNeighborSource<'a> {
graph: &'a DirGraph,
nodes: Vec<NodeIndex>,
node_to_idx: Vec<u32>,
edge_types: Option<Vec<InternedKey>>,
}
impl<'a> DedupNeighborSource<'a> {
fn new(
graph: &'a DirGraph,
nodes: Vec<NodeIndex>,
edge_types: Option<Vec<InternedKey>>,
) -> Self {
let bound = graph.graph.node_bound();
let mut node_to_idx = vec![u32::MAX; bound];
for (i, &node) in nodes.iter().enumerate() {
node_to_idx[node.index()] = i as u32;
}
Self {
graph,
nodes,
node_to_idx,
edge_types,
}
}
fn len(&self) -> usize {
self.nodes.len()
}
fn neighbors_deduped(&self, v: usize, buf: &mut Vec<u32>) {
use petgraph::Direction;
buf.clear();
let node = self.nodes[v];
match &self.edge_types {
None => {
for dir in [Direction::Outgoing, Direction::Incoming] {
for (peer, _e) in self.graph.graph.iter_peers_filtered(node, dir, None) {
let p = self.node_to_idx[peer.index()];
if p != u32::MAX && p as usize != v {
buf.push(p);
}
}
}
}
Some(types) => {
for dir in [Direction::Outgoing, Direction::Incoming] {
for edge in self.graph.graph.edges_directed(node, dir) {
if !types.contains(&edge.connection_type()) {
continue;
}
let peer = match dir {
Direction::Outgoing => edge.target(),
Direction::Incoming => edge.source(),
};
let p = self.node_to_idx[peer.index()];
if p != u32::MAX && p as usize != v {
buf.push(p);
}
}
}
}
}
buf.sort_unstable();
buf.dedup();
}
}
fn local_move<S: NeighborSource>(
src: &S,
init: Option<&[usize]>,
resolution: f64,
deadline: Option<Instant>,
) -> Result<Vec<usize>, String> {
let n = src.len();
let total_weight = src.total_weight();
let mut community: Vec<usize> = match init {
Some(p) => p.to_vec(),
None => (0..n).collect(),
};
if n == 0 || total_weight == 0.0 {
return Ok(community);
}
let mut degree: Vec<f64> = vec![0.0; n];
for (i, d) in degree.iter_mut().enumerate() {
src.for_each_neighbor(i, |_, w| *d += w);
}
let mut sigma_tot: Vec<f64> = vec![0.0; n];
for i in 0..n {
sigma_tot[community[i]] += degree[i];
}
let m = total_weight;
let two_m = 2.0 * m;
let inv_m = 1.0 / m;
let resolution_over_two_m_sq = resolution / (two_m * two_m);
let mut comm_weight: Vec<f64> = vec![0.0; n];
let mut touched_comms: Vec<usize> = Vec::with_capacity(64);
let max_iterations = 100;
for _ in 0..max_iterations {
if let Some(dl) = deadline {
if Instant::now() > dl {
return Err(algorithm_timeout_err());
}
}
let mut improved = false;
for i in 0..n {
let current_community = community[i];
let k_i = degree[i];
let k_i_res = k_i * resolution_over_two_m_sq;
touched_comms.clear();
src.for_each_neighbor(i, |neighbor, w| {
if neighbor == i {
return; }
let c = community[neighbor];
if comm_weight[c] == 0.0 {
touched_comms.push(c);
}
comm_weight[c] += w;
});
let k_i_in_current = comm_weight[current_community];
let mut best_community = current_community;
let mut best_delta = 0.0f64;
for &cand_community in &touched_comms {
if cand_community == current_community {
continue;
}
let k_i_in_cand = comm_weight[cand_community];
let sigma_cand = sigma_tot[cand_community];
let sigma_curr = sigma_tot[current_community] - k_i;
let gain_add = k_i_in_cand * inv_m - sigma_cand * k_i_res;
let loss_remove = k_i_in_current * inv_m - sigma_curr * k_i_res;
let delta = gain_add - loss_remove;
if delta > best_delta {
best_delta = delta;
best_community = cand_community;
}
}
for &c in &touched_comms {
comm_weight[c] = 0.0;
}
if best_community != current_community {
sigma_tot[current_community] -= k_i;
sigma_tot[best_community] += k_i;
community[i] = best_community;
improved = true;
}
}
if !improved {
break;
}
}
Ok(community)
}
fn renumber_communities(community: &[usize]) -> (Vec<usize>, usize) {
let mut id_map: HashMap<usize, usize> = HashMap::new();
let renumbered: Vec<usize> = community
.iter()
.map(|&c| {
let next = id_map.len();
*id_map.entry(c).or_insert(next)
})
.collect();
let k = id_map.len();
(renumbered, k)
}
fn aggregate_graph<S: NeighborSource>(src: &S, community: &[usize], k: usize) -> Adjacency {
let mut acc: Vec<HashMap<usize, f64>> = vec![HashMap::new(); k];
for i in 0..src.len() {
let ci = community[i];
src.for_each_neighbor(i, |j, w| {
let cj = community[j];
*acc[ci].entry(cj).or_insert(0.0) += w;
});
}
let mut new_adj: Vec<Vec<(usize, f64)>> = vec![Vec::new(); k];
for (ci, entry) in acc.into_iter().enumerate() {
let mut row: Vec<(usize, f64)> = entry.into_iter().collect();
row.sort_unstable_by_key(|&(idx, _)| idx); new_adj[ci] = row;
}
new_adj
}
fn louvain_levels<S: NeighborSource>(
level0: &S,
resolution: f64,
deadline: Option<Instant>,
) -> Result<Vec<Vec<usize>>, String> {
let n0 = level0.len();
let m = level0.total_weight();
let mut levels: Vec<Vec<usize>> = Vec::new();
let mut node_to_super: Vec<usize> = (0..n0).collect();
let moved = local_move(level0, None, resolution, deadline)?;
let (partition, k) = renumber_communities(&moved);
for s in node_to_super.iter_mut() {
*s = partition[*s];
}
levels.push(node_to_super.clone());
if k == n0 {
return Ok(levels);
}
let mut adj = aggregate_graph(level0, &partition, k);
loop {
let src = MaterializedAdj { adj: &adj, m };
let moved = local_move(&src, None, resolution, deadline)?;
let (partition, k) = renumber_communities(&moved);
if k == adj.len() {
break;
}
for s in node_to_super.iter_mut() {
*s = partition[*s];
}
levels.push(node_to_super.clone());
adj = aggregate_graph(&src, &partition, k);
}
Ok(levels)
}
fn empty_community_result() -> CommunityResult {
CommunityResult {
assignments: Vec::new(),
num_communities: 0,
modularity: 0.0,
levels: Vec::new(),
}
}
fn build_community_result(
graph: &DirGraph,
nodes: &[NodeIndex],
levels: &[Vec<usize>],
total_weight: f64,
weight_property: Option<&str>,
) -> CommunityResult {
let best = levels.last().expect("at least one level");
let bound = graph.graph.node_bound();
let mut community_bound: Vec<usize> = vec![0; bound];
let mut node_exists: Vec<bool> = vec![false; bound];
for (i, &node) in nodes.iter().enumerate() {
community_bound[node.index()] = best[i];
node_exists[node.index()] = true;
}
let num_communities = best.iter().copied().max().map(|m| m + 1).unwrap_or(0);
let modularity = if total_weight == 0.0 {
0.0
} else {
compute_modularity(
graph,
&community_bound,
&node_exists,
total_weight,
weight_property,
)
};
let levels_out: Vec<Vec<CommunityAssignment>> = levels
.iter()
.map(|lc| {
lc.iter()
.enumerate()
.map(|(i, &c)| CommunityAssignment {
node_idx: nodes[i],
community_id: c,
})
.collect()
})
.collect();
let assignments = levels_out.last().cloned().unwrap_or_default();
CommunityResult {
assignments,
num_communities,
modularity,
levels: levels_out,
}
}
pub fn louvain_communities(
graph: &DirGraph,
weight_property: Option<&str>,
resolution: f64,
connection_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Result<CommunityResult, String> {
let nodes: Vec<NodeIndex> = graph.graph.node_indices().collect();
if nodes.is_empty() {
return Ok(empty_community_result());
}
let (levels, m) = if graph.graph.is_disk() || graph.graph.is_mapped() {
let src = CsrSource::new(graph, weight_property, connection_types);
let m = src.total_weight();
(louvain_levels(&src, resolution, deadline)?, m)
} else {
let (_n, adj, m) = build_weighted_adjacency(graph, weight_property, connection_types);
let src = MaterializedAdj { adj: &adj, m };
(louvain_levels(&src, resolution, deadline)?, m)
};
Ok(build_community_result(
graph,
&nodes,
&levels,
m,
weight_property,
))
}
fn refine_connected<S: NeighborSource>(src: &S, partition: &[usize]) -> (Vec<usize>, usize) {
let n = src.len();
let mut refined = vec![usize::MAX; n];
let mut next_id = 0usize;
let mut stack: Vec<usize> = Vec::new();
for start in 0..n {
if refined[start] != usize::MAX {
continue;
}
let comm = partition[start];
let id = next_id;
next_id += 1;
refined[start] = id;
stack.push(start);
while let Some(u) = stack.pop() {
src.for_each_neighbor(u, |v, _w| {
if v != u && partition[v] == comm && refined[v] == usize::MAX {
refined[v] = id;
stack.push(v);
}
});
}
}
(refined, next_id)
}
fn leiden_levels<S: NeighborSource>(
level0: &S,
resolution: f64,
deadline: Option<Instant>,
) -> Result<Vec<Vec<usize>>, String> {
let n0 = level0.len();
let m = level0.total_weight();
let mut levels: Vec<Vec<usize>> = Vec::new();
let mut node_to_super: Vec<usize> = (0..n0).collect();
fn step<S: NeighborSource>(
src: &S,
n0: usize,
node_to_super: &[usize],
init: Option<&[usize]>,
levels: &mut Vec<Vec<usize>>,
resolution: f64,
deadline: Option<Instant>,
) -> Result<RefineStep, String> {
let moved = local_move(src, init, resolution, deadline)?;
let (p, _kp) = renumber_communities(&moved);
let raw_level: Vec<usize> = (0..n0).map(|o| p[node_to_super[o]]).collect();
let (level_norm, _) = renumber_communities(&raw_level);
if levels.last() == Some(&level_norm) {
return Ok(None); }
levels.push(level_norm);
let (refined, k_ref) = refine_connected(src, &p);
if k_ref == src.len() {
return Ok(None); }
let mut next_init = vec![0usize; k_ref];
for s in 0..src.len() {
next_init[refined[s]] = p[s];
}
Ok(Some((refined, k_ref, next_init)))
}
let Some((refined, k_ref, next_init)) = step(
level0,
n0,
&node_to_super,
None,
&mut levels,
resolution,
deadline,
)?
else {
if levels.is_empty() {
levels.push((0..n0).collect());
}
return Ok(levels);
};
let mut adj = aggregate_graph(level0, &refined, k_ref);
for s in node_to_super.iter_mut() {
*s = refined[*s];
}
let mut init = next_init;
loop {
let src = MaterializedAdj { adj: &adj, m };
match step(
&src,
n0,
&node_to_super,
Some(&init),
&mut levels,
resolution,
deadline,
)? {
None => break,
Some((refined, k_ref, next_init)) => {
adj = aggregate_graph(&src, &refined, k_ref);
for s in node_to_super.iter_mut() {
*s = refined[*s];
}
init = next_init;
}
}
}
Ok(levels)
}
pub fn leiden_communities(
graph: &DirGraph,
weight_property: Option<&str>,
resolution: f64,
connection_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Result<CommunityResult, String> {
let nodes: Vec<NodeIndex> = graph.graph.node_indices().collect();
if nodes.is_empty() {
return Ok(empty_community_result());
}
let (levels, m) = if graph.graph.is_disk() || graph.graph.is_mapped() {
let src = CsrSource::new(graph, weight_property, connection_types);
let m = src.total_weight();
(leiden_levels(&src, resolution, deadline)?, m)
} else {
let (_n, adj, m) = build_weighted_adjacency(graph, weight_property, connection_types);
let src = MaterializedAdj { adj: &adj, m };
(leiden_levels(&src, resolution, deadline)?, m)
};
Ok(build_community_result(
graph,
&nodes,
&levels,
m,
weight_property,
))
}
pub fn label_propagation(
graph: &DirGraph,
max_iterations: usize,
connection_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Result<CommunityResult, String> {
if graph.graph.is_disk() || graph.graph.is_mapped() {
return label_propagation_streaming(graph, max_iterations, connection_types, deadline);
}
let nodes: Vec<NodeIndex> = {
let g = &graph.graph;
g.node_indices().collect()
};
let n = nodes.len();
if n == 0 {
return Ok(empty_community_result());
}
let bound = graph.graph.node_bound();
let mut node_to_idx = vec![0usize; bound];
for (i, &node) in nodes.iter().enumerate() {
node_to_idx[node.index()] = i;
}
let interned_ct = intern_connection_types(connection_types);
let mut adj: Vec<Vec<usize>> = vec![Vec::new(); n];
for edge in {
let g = &graph.graph;
g.edge_references()
} {
if let Some(ref types) = interned_ct {
if !types.iter().any(|t| *t == edge.connection_type()) {
continue;
}
}
let src_i = node_to_idx[edge.source().index()];
let tgt_i = node_to_idx[edge.target().index()];
adj[src_i].push(tgt_i);
adj[tgt_i].push(src_i);
}
for neighbors in &mut adj {
neighbors.sort_unstable();
neighbors.dedup();
}
let mut labels: Vec<usize> = (0..n).collect();
let mut label_count: Vec<usize> = vec![0; n];
let mut touched_labels: Vec<usize> = Vec::with_capacity(64);
for _ in 0..max_iterations {
if let Some(dl) = deadline {
if Instant::now() > dl {
return Err(algorithm_timeout_err());
}
}
let mut changed = false;
for i in 0..n {
let neighbors = &adj[i];
if neighbors.is_empty() {
continue; }
touched_labels.clear();
for &neighbor in neighbors {
let lbl = labels[neighbor];
if label_count[lbl] == 0 {
touched_labels.push(lbl);
}
label_count[lbl] += 1;
}
let mut best_label = labels[i];
let mut best_count = 0;
for &lbl in &touched_labels {
if label_count[lbl] > best_count {
best_count = label_count[lbl];
best_label = lbl;
}
}
for &lbl in &touched_labels {
label_count[lbl] = 0;
}
if best_label != labels[i] {
labels[i] = best_label;
changed = true;
}
}
if !changed {
break;
}
}
Ok(label_prop_result(graph, &nodes, &labels))
}
fn label_prop_result(graph: &DirGraph, nodes: &[NodeIndex], labels: &[usize]) -> CommunityResult {
let bound = graph.graph.node_bound();
let mut labels_bound: Vec<usize> = vec![0; bound];
let mut node_exists: Vec<bool> = vec![false; bound];
for (i, &node) in nodes.iter().enumerate() {
labels_bound[node.index()] = labels[i];
node_exists[node.index()] = true;
}
let mut id_map: HashMap<usize, usize> = HashMap::new();
for &lbl in labels {
let next_id = id_map.len();
id_map.entry(lbl).or_insert(next_id);
}
let assignments: Vec<CommunityAssignment> = nodes
.iter()
.enumerate()
.map(|(i, &idx)| CommunityAssignment {
node_idx: idx,
community_id: *id_map.get(&labels[i]).unwrap(),
})
.collect();
let total_weight = graph.graph.edge_count() as f64;
let num_communities = id_map.len();
let modularity = compute_modularity(graph, &labels_bound, &node_exists, total_weight, None);
CommunityResult {
assignments,
num_communities,
modularity,
levels: Vec::new(),
}
}
fn label_propagation_streaming(
graph: &DirGraph,
max_iterations: usize,
connection_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Result<CommunityResult, String> {
let nodes: Vec<NodeIndex> = graph.graph.node_indices().collect();
if nodes.is_empty() {
return Ok(empty_community_result());
}
let interned_ct = intern_connection_types(connection_types);
let src = DedupNeighborSource::new(graph, nodes, interned_ct);
let n = src.len();
let mut labels: Vec<usize> = (0..n).collect();
let mut label_count: Vec<usize> = vec![0; n];
let mut touched_labels: Vec<usize> = Vec::with_capacity(64);
let mut buf: Vec<u32> = Vec::new();
for _ in 0..max_iterations {
if let Some(dl) = deadline {
if Instant::now() > dl {
return Err(algorithm_timeout_err());
}
}
let mut changed = false;
for i in 0..n {
src.neighbors_deduped(i, &mut buf);
if buf.is_empty() {
continue; }
touched_labels.clear();
for &neighbor in &buf {
let lbl = labels[neighbor as usize];
if label_count[lbl] == 0 {
touched_labels.push(lbl);
}
label_count[lbl] += 1;
}
let mut best_label = labels[i];
let mut best_count = 0;
for &lbl in &touched_labels {
if label_count[lbl] > best_count {
best_count = label_count[lbl];
best_label = lbl;
}
}
for &lbl in &touched_labels {
label_count[lbl] = 0;
}
if best_label != labels[i] {
labels[i] = best_label;
changed = true;
}
}
if !changed {
break;
}
}
Ok(label_prop_result(graph, &src.nodes, &labels))
}
pub(crate) fn edge_weight(
graph: &DirGraph,
edge_id: petgraph::graph::EdgeIndex,
weight_property: Option<&str>,
) -> f64 {
if let Some(prop) = weight_property {
let g = &graph.graph;
if let Some(edge_data) = g.edge_weight(edge_id) {
if let Some(val) = edge_data.get_property(prop) {
return crate::graph::core::value_operations::value_to_f64(val).unwrap_or(1.0);
}
}
}
1.0
}
#[derive(Debug, Clone)]
pub struct WeightedPathResult {
pub path: Vec<NodeIndex>,
pub weight: f64,
}
pub fn shortest_path_weighted(
graph: &DirGraph,
source: NodeIndex,
target: NodeIndex,
weight_property: &str,
connection_types: Option<&[String]>,
via_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Option<WeightedPathResult> {
use std::cmp::Ordering;
use std::collections::BinaryHeap;
if source == target {
return Some(WeightedPathResult {
path: vec![source],
weight: 0.0,
});
}
let via_set: Option<HashSet<&str>> =
via_types.map(|vt| vt.iter().map(|s| s.as_str()).collect());
let interned = intern_connection_types(connection_types);
let conn_filter = interned.as_deref();
let node_bound = graph.graph.node_bound();
let mut dist: Vec<f64> = vec![f64::INFINITY; node_bound];
let mut parent: Vec<u32> = vec![u32::MAX; node_bound];
dist[source.index()] = 0.0;
#[derive(PartialEq)]
struct State(f64, usize);
impl Eq for State {}
impl Ord for State {
fn cmp(&self, other: &Self) -> Ordering {
other
.0
.partial_cmp(&self.0)
.unwrap_or(Ordering::Equal)
.then_with(|| self.1.cmp(&other.1))
}
}
impl PartialOrd for State {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
let mut heap: BinaryHeap<State> = BinaryHeap::new();
heap.push(State(0.0, source.index()));
let mut visit_count = 0u32;
while let Some(State(d, current_idx)) = heap.pop() {
if d > dist[current_idx] {
continue;
}
if current_idx == target.index() {
let mut path = Vec::with_capacity(16);
let mut idx = current_idx;
while idx != source.index() {
path.push(NodeIndex::new(idx));
idx = parent[idx] as usize;
}
path.push(source);
path.reverse();
return Some(WeightedPathResult { path, weight: d });
}
visit_count += 1;
if visit_count.is_multiple_of(1000) {
if let Some(dl) = deadline {
if Instant::now() > dl {
return None;
}
}
}
let current = NodeIndex::new(current_idx);
for edge in graph
.graph
.edges_directed(current, petgraph::Direction::Outgoing)
.chain(
graph
.graph
.edges_directed(current, petgraph::Direction::Incoming),
)
{
if let Some(types) = conn_filter {
if !types.iter().any(|t| *t == edge.connection_type()) {
continue;
}
}
let neighbor = if edge.source() == current {
edge.target()
} else {
edge.source()
};
let n_idx = neighbor.index();
if n_idx != target.index() && !node_passes_via_filter(graph, neighbor, &via_set) {
continue;
}
let w = edge_weight(graph, edge.id(), Some(weight_property));
if w < 0.0 {
return None;
}
let next = d + w;
if next < dist[n_idx] {
dist[n_idx] = next;
parent[n_idx] = current_idx as u32;
heap.push(State(next, n_idx));
}
}
}
None
}
pub fn shortest_path_cost_weighted(
graph: &DirGraph,
source: NodeIndex,
target: NodeIndex,
weight_property: &str,
connection_types: Option<&[String]>,
via_types: Option<&[String]>,
deadline: Option<Instant>,
) -> Option<f64> {
shortest_path_weighted(
graph,
source,
target,
weight_property,
connection_types,
via_types,
deadline,
)
.map(|r| r.weight)
}
fn compute_modularity(
graph: &DirGraph,
community: &[usize],
node_exists: &[bool],
total_weight: f64,
weight_property: Option<&str>,
) -> f64 {
if total_weight == 0.0 {
return 0.0;
}
let two_m = 2.0 * total_weight;
let mut q = 0.0f64;
let g = &graph.graph;
let bound = g.node_bound();
let mut degrees: Vec<f64> = vec![0.0; bound];
for node_idx in g.node_indices() {
let i = node_idx.index();
if !node_exists[i] {
continue;
}
for edge in g.edges(node_idx) {
degrees[i] += edge_weight(graph, edge.id(), weight_property);
}
for edge in g.edges_directed(node_idx, petgraph::Direction::Incoming) {
degrees[i] += edge_weight(graph, edge.id(), weight_property);
}
}
for edge in {
let g = &graph.graph;
g.edge_references()
} {
let u = edge.source().index();
let v = edge.target().index();
let w = edge_weight(graph, edge.id(), weight_property);
if community[u] == community[v] {
q += w - degrees[u] * degrees[v] / two_m;
}
}
q / two_m
}
#[cfg(test)]
#[path = "graph_algorithms_tests.rs"]
mod tests;