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
//! The note graph: nodes are notes, edges are resolved links with a relation
//! type. Links whose target does not exist are kept as `dangling` (Obsidian
//! calls them unresolved links) and shown as ghost nodes in exports.
use crate::mem::vault::Vault;
use std::collections::{HashMap, HashSet, VecDeque};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dir {
Out,
In,
}
impl Dir {
pub fn arrow(self) -> &'static str {
match self {
Dir::Out => "->",
Dir::In => "<-",
}
}
}
#[derive(Debug, Clone)]
pub struct Dangling {
pub from: usize,
pub target: String,
pub rel: String,
pub line: usize,
}
pub struct Graph {
/// out[i] = (target, rel)
pub out: Vec<Vec<(usize, String)>>,
/// inc[i] = (source, rel)
pub inc: Vec<Vec<(usize, String)>>,
pub dangling: Vec<Dangling>,
}
impl Graph {
pub fn build(v: &Vault) -> Graph {
let n = v.notes.len();
let mut out: Vec<Vec<(usize, String)>> = vec![Vec::new(); n];
let mut inc: Vec<Vec<(usize, String)>> = vec![Vec::new(); n];
let mut seen: HashSet<(usize, usize, String)> = HashSet::new();
let mut dangling = Vec::new();
for (i, note) in v.notes.iter().enumerate() {
for l in ¬e.links {
match v.resolve(&l.target) {
Some(j) if j == i => {}
Some(j) => {
if seen.insert((i, j, l.rel.clone())) {
out[i].push((j, l.rel.clone()));
inc[j].push((i, l.rel.clone()));
}
}
None => dangling.push(Dangling {
from: i,
target: l.target.clone(),
rel: l.rel.clone(),
line: l.line,
}),
}
}
}
Graph { out, inc, dangling }
}
pub fn edge_count(&self) -> usize {
self.out.iter().map(|e| e.len()).sum()
}
pub fn degree(&self, i: usize) -> usize {
self.out[i].len() + self.inc[i].len()
}
/// Both directions, as (neighbour, rel, direction-from-i).
pub fn neighbours(&self, i: usize) -> impl Iterator<Item = (usize, &str, Dir)> {
self.out[i]
.iter()
.map(|(j, r)| (*j, r.as_str(), Dir::Out))
.chain(self.inc[i].iter().map(|(j, r)| (*j, r.as_str(), Dir::In)))
}
/// Breadth-first neighbourhood up to `depth` hops, ignoring direction.
/// Returns (node, hops, parent step) in visit order, start excluded.
pub fn bfs(&self, start: usize, depth: usize, rel: Option<&str>) -> Vec<(usize, usize, Step)> {
let mut seen = HashSet::from([start]);
let mut q = VecDeque::from([(start, 0usize)]);
let mut out = Vec::new();
while let Some((u, d)) = q.pop_front() {
if d >= depth {
continue;
}
for (w, r, dir) in self.neighbours(u) {
if rel.map(|x| x != r).unwrap_or(false) || !seen.insert(w) {
continue;
}
out.push((
w,
d + 1,
Step {
from: u,
to: w,
rel: r.to_string(),
dir,
},
));
q.push_back((w, d + 1));
}
}
out
}
/// Shortest undirected path; each step names the relation used.
pub fn path(&self, a: usize, b: usize) -> Option<Vec<Step>> {
if a == b {
return Some(Vec::new());
}
let mut prev: HashMap<usize, Step> = HashMap::new();
let mut seen = HashSet::from([a]);
let mut q = VecDeque::from([a]);
while let Some(u) = q.pop_front() {
for (w, r, dir) in self.neighbours(u) {
if !seen.insert(w) {
continue;
}
prev.insert(
w,
Step {
from: u,
to: w,
rel: r.to_string(),
dir,
},
);
if w == b {
let mut steps = Vec::new();
let mut cur = b;
while cur != a {
let s = prev[&cur].clone();
cur = s.from;
steps.push(s);
}
steps.reverse();
return Some(steps);
}
q.push_back(w);
}
}
None
}
/// Spreading activation: every seed passes `decay^hops` of its score to
/// its neighbourhood. A node keeps its best score and the step that gave
/// it, so every recalled note can explain why it was recalled.
pub fn activate(
&self,
seeds: &[(usize, f64)],
depth: usize,
decay: f64,
) -> HashMap<usize, (f64, Option<Step>)> {
let mut best: HashMap<usize, (f64, Option<Step>)> = HashMap::new();
let mut frontier: Vec<(usize, f64)> = Vec::new();
for &(i, s) in seeds {
let e = best.entry(i).or_insert((0.0, None));
if s > e.0 {
*e = (s, None);
}
frontier.push((i, s));
}
for _ in 0..depth {
let mut next: HashMap<usize, (f64, Step)> = HashMap::new();
for &(u, s) in &frontier {
// Damp hubs a little so one index note does not drag in everything.
let fan = 1.0 + (self.degree(u) as f64).ln_1p() * 0.25;
let pass = s * decay / fan;
for (w, r, dir) in self.neighbours(u) {
if pass <= best.get(&w).map(|b| b.0).unwrap_or(0.0) {
continue;
}
let step = Step {
from: u,
to: w,
rel: r.to_string(),
dir,
};
match next.get(&w) {
Some((cur, _)) if *cur >= pass => {}
_ => {
next.insert(w, (pass, step));
}
}
}
}
frontier.clear();
for (w, (s, step)) in next {
best.insert(w, (s, Some(step)));
frontier.push((w, s));
}
frontier.sort_by_key(|x| x.0);
if frontier.is_empty() {
break;
}
}
best
}
}
#[derive(Debug, Clone)]
pub struct Step {
pub from: usize,
pub to: usize,
pub rel: String,
pub dir: Dir,
}