pub struct GraphEngine { /* private fields */ }Expand description
In-memory graph backed by petgraph, synced to SQLite via codemem-storage.
Implementations§
Source§impl GraphEngine
impl GraphEngine
Sourcepub fn pagerank(
&self,
damping: f64,
iterations: usize,
tolerance: f64,
) -> HashMap<String, f64>
pub fn pagerank( &self, damping: f64, iterations: usize, tolerance: f64, ) -> HashMap<String, f64>
Compute PageRank scores for all nodes using power iteration.
damping: probability of following an edge (default 0.85)iterations: max number of power iterations (default 100)tolerance: convergence threshold (default 1e-6)
Returns a map from node ID to PageRank score.
Sourcepub fn personalized_pagerank(
&self,
seed_weights: &HashMap<String, f64>,
damping: f64,
iterations: usize,
tolerance: f64,
) -> HashMap<String, f64>
pub fn personalized_pagerank( &self, seed_weights: &HashMap<String, f64>, damping: f64, iterations: usize, tolerance: f64, ) -> HashMap<String, f64>
Compute Personalized PageRank with custom teleport weights.
seed_weights maps node IDs to teleport probabilities (will be normalized).
Nodes not in seed_weights get zero teleport probability.
Used for blast-radius analysis and HippoRAG-2-style retrieval.
Sourcepub fn louvain_communities(&self, resolution: f64) -> Vec<Vec<String>>
pub fn louvain_communities(&self, resolution: f64) -> Vec<Vec<String>>
Detect communities using the Louvain algorithm.
Treats the directed graph as undirected for modularity computation.
resolution controls community granularity (1.0 = standard modularity).
Returns groups of node IDs, one group per community.
Sourcepub fn betweenness_centrality(&self) -> HashMap<String, f64>
pub fn betweenness_centrality(&self) -> HashMap<String, f64>
Compute betweenness centrality for all nodes using Brandes’ algorithm.
For graphs with more than 1000 nodes, samples sqrt(n) source nodes for approximate computation.
Returns a map from node ID to betweenness centrality score (normalized by 1/((n-1)(n-2)) for directed graphs).
Sourcepub fn strongly_connected_components(&self) -> Vec<Vec<String>>
pub fn strongly_connected_components(&self) -> Vec<Vec<String>>
Find all strongly connected components using Tarjan’s algorithm.
Returns groups of node IDs. Each group is a strongly connected component where every node can reach every other node via directed edges.
Sourcepub fn topological_layers(&self) -> Vec<Vec<String>>
pub fn topological_layers(&self) -> Vec<Vec<String>>
Compute topological layers using Kahn’s algorithm.
Returns layers where all nodes in layer i have no dependencies on nodes in layer i or later. For cyclic graphs, SCCs are condensed into single super-nodes first, then the resulting DAG is topologically sorted.
Each inner Vec contains the node IDs at that layer.
Source§impl GraphEngine
impl GraphEngine
Sourcepub fn from_storage(storage: &dyn StorageBackend) -> Result<Self, CodememError>
pub fn from_storage(storage: &dyn StorageBackend) -> Result<Self, CodememError>
Load graph from storage.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Get the number of nodes.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Get the number of edges.
Sourcepub fn expand(
&self,
start_ids: &[String],
max_hops: usize,
) -> Result<Vec<GraphNode>, CodememError>
pub fn expand( &self, start_ids: &[String], max_hops: usize, ) -> Result<Vec<GraphNode>, CodememError>
Multi-hop expansion: given a set of node IDs, expand N hops to find related nodes.
Sourcepub fn neighbors(&self, node_id: &str) -> Result<Vec<GraphNode>, CodememError>
pub fn neighbors(&self, node_id: &str) -> Result<Vec<GraphNode>, CodememError>
Get neighbors of a node (1-hop).
Sourcepub fn connected_components(&self) -> Vec<Vec<String>>
pub fn connected_components(&self) -> Vec<Vec<String>>
Return groups of connected node IDs.
Treats the directed graph as undirected: two nodes are in the same
component if there is a path between them in either direction.
Each inner Vec<String> is one connected component.
Sourcepub fn compute_centrality(&mut self)
pub fn compute_centrality(&mut self)
Compute degree centrality for every node and update their centrality field.
Degree centrality for node v is defined as:
(in_degree(v) + out_degree(v)) / (N - 1)
where N is the total number of nodes. When N <= 1, centrality is 0.
Sourcepub fn get_all_nodes(&self) -> Vec<GraphNode>
pub fn get_all_nodes(&self) -> Vec<GraphNode>
Return all nodes currently in the graph.
Sourcepub fn recompute_centrality(&mut self)
pub fn recompute_centrality(&mut self)
Recompute and cache PageRank and betweenness centrality scores.
This should be called after loading the graph (e.g., on server start) and periodically when the graph changes significantly.
Sourcepub fn get_pagerank(&self, node_id: &str) -> f64
pub fn get_pagerank(&self, node_id: &str) -> f64
Get the cached PageRank score for a node. Returns 0.0 if not found.
Sourcepub fn get_betweenness(&self, node_id: &str) -> f64
pub fn get_betweenness(&self, node_id: &str) -> f64
Get the cached betweenness centrality score for a node. Returns 0.0 if not found.
Sourcepub fn graph_strength_for_memory(&self, memory_id: &str) -> f64
pub fn graph_strength_for_memory(&self, memory_id: &str) -> f64
Compute graph strength for a memory node by bridging to code-graph centrality.
Memory nodes (UUIDs) and code nodes (sym:, file:) exist in disconnected
ID spaces. This method looks up a memory node’s neighbors and collects
centrality data from any connected code-graph nodes to produce a meaningful
graph_strength score.
Sourcepub fn max_degree(&self) -> f64
pub fn max_degree(&self) -> f64
Get the maximum degree (in + out) across all nodes in the graph. Returns 1.0 if the graph has fewer than 2 nodes to avoid division by zero.