Skip to main content

memstead_base/graph/
mod.rs

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