1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! PageRank-based importance scoring for memory entries.
//!
//! Models memory entries as nodes in a graph where edges represent
//! co-access (entries accessed in the same session are linked).
//! PageRank iteration propagates importance through the graph.
use std::collections::HashMap;
/// A memory link graph for computing PageRank-style importance.
///
/// Nodes are memory entry IDs (mapped to u64 indices internally).
/// Edges represent co-access relationships.
#[derive(Debug, Clone, Default)]
pub struct MemoryGraph {
/// Adjacency list: node -> outgoing edges (neighbors).
edges: HashMap<u64, Vec<u64>>,
/// Reverse mapping: node -> incoming edges.
incoming: HashMap<u64, Vec<u64>>,
/// Number of nodes.
node_count: usize,
}
impl MemoryGraph {
/// Create an empty graph.
pub fn new() -> Self {
Self::default()
}
/// Add a directed edge from `from` to `to`.
///
/// If the edge already exists, this is a no-op.
pub fn add_edge(&mut self, from: u64, to: u64) {
if from == to {
return; // no self-loops
}
self.edges.entry(from).or_default();
self.edges.entry(to).or_default();
self.incoming.entry(from).or_default();
self.incoming.entry(to).or_default();
let neighbors = self
.edges
.get_mut(&from)
.expect("entry(or_default) guarantees existence");
if !neighbors.contains(&to) {
neighbors.push(to);
self.incoming
.get_mut(&to)
.expect("entry(or_default) guarantees existence")
.push(from);
}
self.node_count = self.edges.len();
}
/// Add a bidirectional link between two nodes (co-access).
pub fn link(&mut self, a: u64, b: u64) {
self.add_edge(a, b);
self.add_edge(b, a);
}
/// Get the number of nodes in the graph.
pub fn node_count(&self) -> usize {
self.node_count
}
/// Get outgoing neighbors of a node.
pub fn neighbors(&self, node: u64) -> &[u64] {
self.edges.get(&node).map(|v| v.as_slice()).unwrap_or(&[])
}
/// Compute PageRank scores for all nodes.
///
/// Uses the standard iterative algorithm with damping factor.
///
/// # Arguments
/// * `damping` — Damping factor (typically 0.85).
/// * `iterations` — Number of iterations (typically 20-50).
/// * `initial_scores` — Optional initial scores (e.g., base importance).
/// If provided, the initial PageRank is seeded with these values.
///
/// # Returns
/// A map of node ID -> PageRank score.
pub fn pagerank(
&self,
damping: f64,
iterations: usize,
initial_scores: Option<&HashMap<u64, f64>>,
) -> HashMap<u64, f64> {
if self.node_count == 0 {
return HashMap::new();
}
let n = self.node_count as f64;
let base = 1.0 / n;
// Initialize scores
let mut scores: HashMap<u64, f64> = self
.edges
.keys()
.map(|&k| {
let init = initial_scores
.and_then(|m| m.get(&k))
.copied()
.unwrap_or(base);
(k, init)
})
.collect();
// Compute out-degree for each node
let out_degree: HashMap<u64, usize> =
self.edges.iter().map(|(&k, v)| (k, v.len())).collect();
// Iterative PageRank
for _ in 0..iterations {
let mut new_scores = HashMap::with_capacity(self.node_count);
let sink_sum: f64 = scores
.iter()
.filter(|(&k, _)| out_degree.get(&k).copied().unwrap_or(0) == 0)
.map(|(_, &s)| s)
.sum();
for &node in self.edges.keys() {
// Sum of incoming PageRank contributions
let incoming_sum: f64 = self
.incoming
.get(&node)
.map(|neighbors| {
neighbors
.iter()
.map(|&src| {
let src_out = out_degree.get(&src).copied().unwrap_or(1) as f64;
scores.get(&src).copied().unwrap_or(0.0) / src_out
})
.sum()
})
.unwrap_or(0.0);
let rank = (1.0 - damping) / n + damping * (incoming_sum + sink_sum / n);
new_scores.insert(node, rank);
}
scores = new_scores;
}
scores
}
/// Compute co-access graph from session access patterns.
///
/// Given a list of sessions where each session contains a list of
/// memory IDs that were accessed together, build a graph where
/// co-accessed memories are linked.
pub fn from_co_access(sessions: &[Vec<u64>]) -> Self {
let mut graph = Self::new();
for session in sessions {
// Link all pairs of co-accessed memories
for i in 0..session.len() {
for j in (i + 1)..session.len() {
graph.link(session[i], session[j]);
}
}
}
graph
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_graph() {
let graph = MemoryGraph::new();
let scores = graph.pagerank(0.85, 20, None);
assert!(scores.is_empty());
}
#[test]
fn test_single_node() {
let mut graph = MemoryGraph::new();
graph.add_edge(1, 1); // self-loop ignored
let scores = graph.pagerank(0.85, 20, None);
assert!(scores.is_empty() || scores.values().all(|&v| v > 0.0));
}
#[test]
fn test_two_nodes() {
let mut graph = MemoryGraph::new();
graph.link(1, 2);
let scores = graph.pagerank(0.85, 50, None);
assert_eq!(scores.len(), 2);
// Both nodes should have similar scores (symmetric graph)
let s1 = scores.get(&1).unwrap();
let s2 = scores.get(&2).unwrap();
assert!(
(s1 - s2).abs() < 0.01,
"Symmetric graph should have equal scores"
);
}
#[test]
fn test_hub_authority() {
// Node 1 links to 2, 3, 4
// Node 2, 3, 4 link back to 1
// Node 1 is a hub
let mut graph = MemoryGraph::new();
graph.add_edge(1, 2);
graph.add_edge(1, 3);
graph.add_edge(1, 4);
graph.add_edge(2, 1);
graph.add_edge(3, 1);
graph.add_edge(4, 1);
let scores = graph.pagerank(0.85, 50, None);
let s1 = scores.get(&1).unwrap();
// Node 1 should have higher score than any single leaf
for &node in &[2u64, 3, 4] {
let sn = scores.get(&node).unwrap();
assert!(*s1 >= *sn, "Hub node should have >= score than leaf");
}
}
#[test]
fn test_from_co_access() {
let sessions = vec![
vec![1, 2, 3], // session 1: memories 1, 2, 3 co-accessed
vec![2, 4], // session 2: memories 2, 4 co-accessed
];
let graph = MemoryGraph::from_co_access(&sessions);
assert_eq!(graph.node_count(), 4);
// Node 2 is in both sessions, should have highest PageRank
let scores = graph.pagerank(0.85, 50, None);
let s2 = scores.get(&2).unwrap();
for &node in &[1u64, 3, 4] {
let sn = scores.get(&node).unwrap();
assert!(*s2 >= *sn, "Node 2 should have highest score");
}
}
#[test]
fn test_initial_scores_influence() {
// Asymmetric graph: 1 -> 2 (one-way)
let mut graph = MemoryGraph::new();
graph.add_edge(1, 2);
// Give node 1 a much higher initial score
let initial = HashMap::from([(1u64, 10.0), (2u64, 0.1)]);
let scores = graph.pagerank(0.85, 5, Some(&initial));
// Node 1 should maintain higher score due to being the only hub
let s1 = scores.get(&1).unwrap();
let s2 = scores.get(&2).unwrap();
// PageRank should propagate some to node 2, but 1 started higher
assert!(*s1 > 0.0, "Node 1 should have positive score");
assert!(*s2 > 0.0, "Node 2 should have positive score");
}
}