Skip to main content

memstead_base/graph/
mod.rs

1//! Graph algorithms — BFS traversal, community detection, neighborhood queries.
2
3pub mod community;
4pub mod query;
5pub mod relations;
6pub mod topology;
7
8use serde::Serialize;
9use std::collections::HashMap;
10
11/// Output of Louvain community detection: clusters, reverse lookup, and
12/// quality metrics. Computed on demand by `Engine::communities()` and
13/// cached in an in-memory memo that is invalidated whenever the graph
14/// mutates (see `Engine::invalidate_communities`).
15#[derive(Debug, Clone, Serialize)]
16pub struct LouvainOutput {
17    pub modularity: f64,
18    pub count: usize,
19    pub clusters: HashMap<String, ClusterInfo>,
20    pub entity_cluster_map: HashMap<String, String>,
21}
22
23/// Info about a single community cluster.
24#[derive(Debug, Clone, Serialize)]
25pub struct ClusterInfo {
26    pub entities: Vec<String>,
27}
28
29/// Inter-cluster edge aggregation. Pair keys are lexicographically normalised
30/// (`from_cluster <= to_cluster`), so each unordered cluster pair appears at
31/// most once. `sample_edges` carries the directed tuples as they exist in
32/// the store, capped at [`BRIDGE_SAMPLE_CAP`].
33#[derive(Debug, Clone, Serialize)]
34pub struct CommunityBridge {
35    pub from_cluster: String,
36    pub to_cluster: String,
37    pub edge_count: usize,
38    pub edge_types: Vec<String>,
39    pub sample_edges: Vec<SampleEdge>,
40}
41
42/// A directed sample edge for a [`CommunityBridge`].
43#[derive(Debug, Clone, Serialize)]
44pub struct SampleEdge {
45    pub from: String,
46    pub to: String,
47    pub rel_type: String,
48}
49
50/// Maximum number of [`SampleEdge`] tuples attached to a [`CommunityBridge`].
51pub const BRIDGE_SAMPLE_CAP: usize = 3;