MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / build_personalization

Function build_personalization

nodedb/src/engine/graph/algo/pagerank.rs:126–144  ·  view source on GitHub ↗

Build the normalized per-node seed distribution for Personalized PageRank. Returns `None` (→ standard uniform PageRank) when no personalization vector is supplied, or when none of its seed nodes exist in the graph / all seed weights are non-positive — falling back to uniform rather than emitting an all-zero ranking. Negative weights are clamped to 0.0. The returned vector is indexed by CSR node o

(csr: &CsrIndex, params: &AlgoParams, n: usize)

Source from the content-addressed store, hash-verified

124/// all-zero ranking. Negative weights are clamped to 0.0. The returned vector
125/// is indexed by CSR node ordinal and sums to 1.0.
126fn build_personalization(csr: &CsrIndex, params: &AlgoParams, n: usize) -> Option<Vec<f64>> {
127 let seeds = params.personalization_vector()?;
128 let mut p = vec![0.0f64; n];
129 let mut sum = 0.0;
130 for (i, slot) in p.iter_mut().enumerate() {
131 if let Some(&w) = seeds.get(csr.node_name_raw(i as u32)) {
132 let w = w.max(0.0);
133 *slot = w;
134 sum += w;
135 }
136 }
137 if sum <= 0.0 {
138 return None;
139 }
140 for v in &mut p {
141 *v /= sum;
142 }
143 Some(p)
144}
145
146#[cfg(test)]
147mod tests {

Callers 1

runFunction · 0.85

Calls 4

iter_mutMethod · 0.80
getMethod · 0.45
node_name_rawMethod · 0.45

Tested by

no test coverage detected