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