Skip to main content

GraphEngine

Struct GraphEngine 

Source
pub struct GraphEngine { /* private fields */ }
Expand description

In-memory graph backed by petgraph, synced to SQLite via codemem-storage.

Implementations§

Source§

impl GraphEngine

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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

Source

pub fn new() -> Self

Create a new empty graph.

Source

pub fn from_storage(storage: &dyn StorageBackend) -> Result<Self, CodememError>

Load graph from storage.

Source

pub fn node_count(&self) -> usize

Get the number of nodes.

Source

pub fn edge_count(&self) -> usize

Get the number of edges.

Source

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.

Source

pub fn neighbors(&self, node_id: &str) -> Result<Vec<GraphNode>, CodememError>

Get neighbors of a node (1-hop).

Source

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.

Source

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.

Source

pub fn get_all_nodes(&self) -> Vec<GraphNode>

Return all nodes currently in the graph.

Source

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.

Source

pub fn get_pagerank(&self, node_id: &str) -> f64

Get the cached PageRank score for a node. Returns 0.0 if not found.

Source

pub fn get_betweenness(&self, node_id: &str) -> f64

Get the cached betweenness centrality score for a node. Returns 0.0 if not found.

Source

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.

Trait Implementations§

Source§

impl Default for GraphEngine

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl GraphBackend for GraphEngine

Source§

fn add_node(&mut self, node: GraphNode) -> Result<(), CodememError>

Add a node to the graph.
Source§

fn get_node(&self, id: &str) -> Result<Option<GraphNode>, CodememError>

Get a node by ID.
Source§

fn remove_node(&mut self, id: &str) -> Result<bool, CodememError>

Remove a node by ID.
Source§

fn add_edge(&mut self, edge: Edge) -> Result<(), CodememError>

Add an edge between two nodes.
Source§

fn get_edges(&self, node_id: &str) -> Result<Vec<Edge>, CodememError>

Get edges from a node.
Source§

fn remove_edge(&mut self, id: &str) -> Result<bool, CodememError>

Remove an edge by ID.
Source§

fn bfs( &self, start_id: &str, max_depth: usize, ) -> Result<Vec<GraphNode>, CodememError>

BFS traversal from a start node up to max_depth.
Source§

fn dfs( &self, start_id: &str, max_depth: usize, ) -> Result<Vec<GraphNode>, CodememError>

DFS traversal from a start node up to max_depth.
Source§

fn shortest_path( &self, from: &str, to: &str, ) -> Result<Vec<String>, CodememError>

Shortest path between two nodes.
Source§

fn stats(&self) -> GraphStats

Get graph statistics.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.