graphops 0.2.0

Graph operators: PageRank/PPR/walks/reachability/node2vec/betweenness.
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Random walk generation.

use crate::graph::{Graph, GraphRef};
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;

/// Random walk hyperparameters. `p` and `q` are the node2vec return / in-out bias.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WalkConfig {
    /// Steps per walk.
    pub length: usize,
    /// Walks initiated per node.
    pub walks_per_node: usize,
    /// node2vec return parameter.
    pub p: f32,
    /// node2vec in-out parameter.
    pub q: f32,
    /// RNG seed for reproducibility.
    pub seed: u64,
}

impl Default for WalkConfig {
    fn default() -> Self {
        Self {
            length: 80,
            walks_per_node: 10,
            p: 1.0,
            q: 1.0,
            seed: 42,
        }
    }
}

/// Deterministically sample up to `k` start nodes from `0..node_count`.
///
/// When `node_count` is huge, materializing all nodes just to choose a subset can be wasteful.
///
/// We use reservoir sampling (uniform sample without replacement) to keep memory bounded, while
/// staying deterministic for a fixed seed.
///
/// ```
/// use graphops::sample_start_nodes_reservoir;
///
/// let nodes = sample_start_nodes_reservoir(1000, 5, 42);
/// assert_eq!(nodes.len(), 5);
/// assert!(nodes.iter().all(|&n| n < 1000));
///
/// // Deterministic: same seed -> same result
/// assert_eq!(nodes, sample_start_nodes_reservoir(1000, 5, 42));
///
/// // k >= n returns all nodes
/// assert_eq!(sample_start_nodes_reservoir(3, 10, 0).len(), 3);
/// ```
pub fn sample_start_nodes_reservoir(node_count: usize, k: usize, seed: u64) -> Vec<usize> {
    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    if k == 0 || node_count == 0 {
        return Vec::new();
    }
    if k >= node_count {
        return (0..node_count).collect();
    }

    // Algorithm R (Vitter): uniform sample without replacement.
    let mut reservoir: Vec<usize> = (0..k).collect();
    for i in k..node_count {
        // Draw j uniformly in [0, i].
        let j = rng.random_range(0..=i);
        if j < k {
            reservoir[j] = i;
        }
    }
    reservoir
}

/// Generate unbiased (first-order) random walks starting from every node.
pub fn generate_walks<G: Graph>(graph: &G, config: WalkConfig) -> Vec<Vec<usize>> {
    let start_nodes: Vec<usize> = (0..graph.node_count()).collect();
    generate_walks_from_nodes(graph, &start_nodes, config)
}

/// Random walk generation (unbiased), but restricted to an explicit set of start nodes.
///
/// Determinism contract:
/// - For a fixed `config` and identical `start_nodes` content, output is deterministic.
/// - We still shuffle start nodes per epoch to avoid systematic ordering bias.
pub fn generate_walks_from_nodes<G: Graph>(
    graph: &G,
    start_nodes: &[usize],
    config: WalkConfig,
) -> Vec<Vec<usize>> {
    let mut walks = Vec::with_capacity(start_nodes.len() * config.walks_per_node);
    let mut rng = ChaCha8Rng::seed_from_u64(config.seed);
    let mut epoch_nodes: Vec<usize> = start_nodes.to_vec();

    for _ in 0..config.walks_per_node {
        // Shuffle start nodes to avoid systematic ordering bias.
        epoch_nodes.shuffle(&mut rng);
        for &node in &epoch_nodes {
            walks.push(unbiased_walk(graph, node, config.length, &mut rng));
        }
    }
    walks
}

fn unbiased_walk<G: Graph, R: Rng>(
    graph: &G,
    start: usize,
    length: usize,
    rng: &mut R,
) -> Vec<usize> {
    let mut walk = Vec::with_capacity(length);
    walk.push(start);
    let mut curr = start;
    for _ in 1..length {
        let neighbors = graph.neighbors(curr);
        if neighbors.is_empty() {
            break;
        }
        curr = *neighbors.choose(rng).unwrap();
        walk.push(curr);
    }
    walk
}

/// Random walk generation for graphs that can return borrowed neighbor slices.
///
/// This avoids the per-step `Vec` allocation implicit in [`Graph::neighbors`].
pub fn generate_walks_ref<G: GraphRef>(graph: &G, config: WalkConfig) -> Vec<Vec<usize>> {
    let start_nodes: Vec<usize> = (0..graph.node_count()).collect();
    generate_walks_ref_from_nodes(graph, &start_nodes, config)
}

/// Random walk generation (unbiased), but restricted to an explicit set of start nodes.
///
/// This is useful for “delta walk” updates in dynamic embeddings (dynnode2vec-style),
/// and for sharding work across machines (partition `start_nodes`).
///
/// Determinism contract:
/// - For a fixed `config` and identical `start_nodes` content, output is deterministic.
/// - We still shuffle start nodes per epoch to avoid systematic ordering bias.
pub fn generate_walks_ref_from_nodes<G: GraphRef>(
    graph: &G,
    start_nodes: &[usize],
    config: WalkConfig,
) -> Vec<Vec<usize>> {
    let mut walks = Vec::with_capacity(start_nodes.len() * config.walks_per_node);
    let mut rng = ChaCha8Rng::seed_from_u64(config.seed);
    let mut epoch_nodes: Vec<usize> = start_nodes.to_vec();

    for _ in 0..config.walks_per_node {
        epoch_nodes.shuffle(&mut rng);
        for &node in &epoch_nodes {
            walks.push(unbiased_walk_ref(graph, node, config.length, &mut rng));
        }
    }
    walks
}

fn unbiased_walk_ref<G: GraphRef, R: Rng>(
    graph: &G,
    start: usize,
    length: usize,
    rng: &mut R,
) -> Vec<usize> {
    let mut walk = Vec::with_capacity(length);
    walk.push(start);
    let mut curr = start;
    for _ in 1..length {
        let neighbors = graph.neighbors_ref(curr);
        if neighbors.is_empty() {
            break;
        }
        curr = *neighbors.choose(rng).unwrap();
        walk.push(curr);
    }
    walk
}

/// Streaming unbiased random walk generation (borrowed neighbor slices).
///
/// Motivation (practical): for large graphs, materializing **all walks** in memory can dominate
/// runtime and memory. This API emits each walk to a caller-provided callback.
///
/// Determinism contract:
/// - For fixed `config` and identical `start_nodes` content, the emitted walk sequence is stable.
pub fn generate_walks_ref_streaming_from_nodes<G, F>(
    graph: &G,
    start_nodes: &[usize],
    config: WalkConfig,
    mut on_walk: F,
) where
    G: GraphRef,
    F: FnMut(&[usize]),
{
    let mut rng = ChaCha8Rng::seed_from_u64(config.seed);
    let mut epoch_nodes: Vec<usize> = start_nodes.to_vec();
    let mut buf: Vec<usize> = Vec::with_capacity(config.length);

    for _ in 0..config.walks_per_node {
        epoch_nodes.shuffle(&mut rng);
        for &node in &epoch_nodes {
            buf.clear();
            unbiased_walk_ref_into(graph, node, config.length, &mut rng, &mut buf);
            on_walk(&buf);
        }
    }
}

fn unbiased_walk_ref_into<G: GraphRef, R: Rng>(
    graph: &G,
    start: usize,
    length: usize,
    rng: &mut R,
    out: &mut Vec<usize>,
) {
    out.reserve(length.saturating_sub(out.capacity()));
    out.push(start);
    let mut curr = start;
    for _ in 1..length {
        let neighbors = graph.neighbors_ref(curr);
        if neighbors.is_empty() {
            break;
        }
        curr = *neighbors.choose(rng).unwrap();
        out.push(curr);
    }
}

/// Generate node2vec biased (second-order) random walks starting from every node.
pub fn generate_biased_walks<G: Graph>(graph: &G, config: WalkConfig) -> Vec<Vec<usize>> {
    let start_nodes: Vec<usize> = (0..graph.node_count()).collect();
    generate_biased_walks_from_nodes(graph, &start_nodes, config)
}

/// Node2Vec-style biased walk generation, restricted to an explicit set of start nodes.
pub fn generate_biased_walks_from_nodes<G: Graph>(
    graph: &G,
    start_nodes: &[usize],
    config: WalkConfig,
) -> Vec<Vec<usize>> {
    let mut walks = Vec::with_capacity(start_nodes.len() * config.walks_per_node);
    let mut rng = ChaCha8Rng::seed_from_u64(config.seed);
    let mut epoch_nodes: Vec<usize> = start_nodes.to_vec();

    for _ in 0..config.walks_per_node {
        epoch_nodes.shuffle(&mut rng);
        for &node in &epoch_nodes {
            walks.push(biased_walk(graph, node, config, &mut rng));
        }
    }
    walks
}

fn biased_walk<G: Graph, R: Rng>(
    graph: &G,
    start: usize,
    config: WalkConfig,
    rng: &mut R,
) -> Vec<usize> {
    let mut walk = Vec::with_capacity(config.length);
    walk.push(start);
    let mut curr = start;
    let mut prev: Option<usize> = None;
    let mut prev_neighbors: Vec<usize> = Vec::new();

    for _ in 1..config.length {
        let neighbors = graph.neighbors(curr);
        if neighbors.is_empty() {
            break;
        }
        let next = if let Some(p_node) = prev {
            sample_biased_rejection(rng, p_node, &prev_neighbors, &neighbors, config.p, config.q)
        } else {
            *neighbors.choose(rng).unwrap()
        };
        walk.push(next);
        prev = Some(curr);
        prev_neighbors = neighbors;
        curr = next;
    }
    walk
}

/// Node2Vec-style biased walk generation for graphs that can return borrowed neighbor slices.
pub fn generate_biased_walks_ref<G: GraphRef>(graph: &G, config: WalkConfig) -> Vec<Vec<usize>> {
    let start_nodes: Vec<usize> = (0..graph.node_count()).collect();
    generate_biased_walks_ref_from_nodes(graph, &start_nodes, config)
}

/// Node2Vec-style biased walk generation, restricted to an explicit set of start nodes.
pub fn generate_biased_walks_ref_from_nodes<G: GraphRef>(
    graph: &G,
    start_nodes: &[usize],
    config: WalkConfig,
) -> Vec<Vec<usize>> {
    let mut walks = Vec::with_capacity(start_nodes.len() * config.walks_per_node);
    let mut rng = ChaCha8Rng::seed_from_u64(config.seed);
    let mut epoch_nodes: Vec<usize> = start_nodes.to_vec();

    for _ in 0..config.walks_per_node {
        epoch_nodes.shuffle(&mut rng);
        for &node in &epoch_nodes {
            walks.push(biased_walk_ref(graph, node, config, &mut rng));
        }
    }
    walks
}

fn biased_walk_ref<G: GraphRef, R: Rng>(
    graph: &G,
    start: usize,
    config: WalkConfig,
    rng: &mut R,
) -> Vec<usize> {
    let mut walk = Vec::with_capacity(config.length);
    walk.push(start);

    let mut curr = start;
    let mut prev: Option<usize> = None;
    let mut prev_neighbors: &[usize] = &[];

    for _ in 1..config.length {
        let neighbors = graph.neighbors_ref(curr);
        if neighbors.is_empty() {
            break;
        }

        let next = if let Some(p_node) = prev {
            sample_biased_rejection(rng, p_node, prev_neighbors, neighbors, config.p, config.q)
        } else {
            *neighbors.choose(rng).unwrap()
        };

        walk.push(next);

        // Cache neighbors(curr) for the next step as "prev_neighbors".
        prev = Some(curr);
        prev_neighbors = neighbors;

        curr = next;
    }
    walk
}

/// Streaming node2vec-style biased walk generation (borrowed neighbor slices).
///
/// Same motivation as `generate_walks_ref_streaming_from_nodes`: avoid materializing all walks.
pub fn generate_biased_walks_ref_streaming_from_nodes<G, F>(
    graph: &G,
    start_nodes: &[usize],
    config: WalkConfig,
    mut on_walk: F,
) where
    G: GraphRef,
    F: FnMut(&[usize]),
{
    let mut rng = ChaCha8Rng::seed_from_u64(config.seed);
    let mut epoch_nodes: Vec<usize> = start_nodes.to_vec();
    let mut buf: Vec<usize> = Vec::with_capacity(config.length);

    for _ in 0..config.walks_per_node {
        epoch_nodes.shuffle(&mut rng);
        for &node in &epoch_nodes {
            buf.clear();
            biased_walk_ref_into(graph, node, config, &mut rng, &mut buf);
            on_walk(&buf);
        }
    }
}

fn biased_walk_ref_into<G: GraphRef, R: Rng>(
    graph: &G,
    start: usize,
    config: WalkConfig,
    rng: &mut R,
    out: &mut Vec<usize>,
) {
    out.reserve(config.length.saturating_sub(out.capacity()));
    out.push(start);

    let mut curr = start;
    let mut prev: Option<usize> = None;
    let mut prev_neighbors: &[usize] = &[];

    for _ in 1..config.length {
        let neighbors = graph.neighbors_ref(curr);
        if neighbors.is_empty() {
            break;
        }

        let next = if let Some(p_node) = prev {
            sample_biased_rejection(rng, p_node, prev_neighbors, neighbors, config.p, config.q)
        } else {
            *neighbors.choose(rng).unwrap()
        };

        out.push(next);

        prev = Some(curr);
        prev_neighbors = neighbors;
        curr = next;
    }
}

fn sample_biased_rejection<R: Rng>(
    rng: &mut R,
    prev_node: usize,
    prev_neighbors: &[usize],
    neighbors: &[usize],
    p: f32,
    q: f32,
) -> usize {
    let max_prob = (1.0 / p).max(1.0).max(1.0 / q);
    loop {
        let candidate = *neighbors.choose(rng).unwrap();
        let r: f32 = rng.random();
        let is_in_edge = prev_neighbors.contains(&candidate);
        let unnorm_prob = if candidate == prev_node {
            1.0 / p
        } else if is_in_edge {
            1.0
        } else {
            1.0 / q
        };
        if r < unnorm_prob / max_prob {
            return candidate;
        }
    }
}

#[cfg(feature = "parallel")]
fn mix64(mut x: u64) -> u64 {
    // SplitMix64 finalizer (stable, good diffusion).
    x ^= x >> 30;
    x = x.wrapping_mul(0xbf58476d1ce4e5b9);
    x ^= x >> 27;
    x = x.wrapping_mul(0x94d049bb133111eb);
    x ^= x >> 31;
    x
}

/// Deterministic parallel unbiased walk generation.
///
/// Invariant: output is stable for a fixed `seed`, independent of Rayon thread count.
#[cfg(feature = "parallel")]
pub fn generate_walks_ref_parallel<G: GraphRef + Sync>(
    graph: &G,
    config: WalkConfig,
) -> Vec<Vec<usize>> {
    let n = graph.node_count();
    let start_nodes: Vec<usize> = (0..n).collect();
    generate_walks_ref_parallel_from_nodes(graph, &start_nodes, config)
}

/// Deterministic parallel unbiased walk generation, restricted to an explicit set of start nodes.
///
/// Invariant: output is stable for a fixed `seed`, independent of Rayon thread count.
#[cfg(feature = "parallel")]
pub fn generate_walks_ref_parallel_from_nodes<G: GraphRef + Sync>(
    graph: &G,
    start_nodes: &[usize],
    config: WalkConfig,
) -> Vec<Vec<usize>> {
    use rayon::prelude::*;

    let mut epoch_nodes: Vec<usize> = start_nodes.to_vec();
    let mut jobs: Vec<(u32, usize)> = Vec::with_capacity(start_nodes.len() * config.walks_per_node);

    for epoch in 0..(config.walks_per_node as u32) {
        let mut rng = ChaCha8Rng::seed_from_u64(mix64(config.seed ^ (epoch as u64)));
        epoch_nodes.shuffle(&mut rng);
        for &node in &epoch_nodes {
            jobs.push((epoch, node));
        }
    }

    jobs.par_iter()
        .enumerate()
        .map(|(i, (epoch, node))| {
            let seed = mix64(config.seed ^ ((*epoch as u64) << 32) ^ (*node as u64) ^ (i as u64));
            let mut rng = ChaCha8Rng::seed_from_u64(seed);
            unbiased_walk_ref(graph, *node, config.length, &mut rng)
        })
        .collect()
}

/// Deterministic parallel node2vec-style biased walk generation.
///
/// Invariant: output is stable for a fixed `seed`, independent of Rayon thread count.
#[cfg(feature = "parallel")]
pub fn generate_biased_walks_ref_parallel<G: GraphRef + Sync>(
    graph: &G,
    config: WalkConfig,
) -> Vec<Vec<usize>> {
    let n = graph.node_count();
    let start_nodes: Vec<usize> = (0..n).collect();
    generate_biased_walks_ref_parallel_from_nodes(graph, &start_nodes, config)
}

/// Deterministic parallel biased walk generation, restricted to an explicit set of start nodes.
///
/// Invariant: output is stable for a fixed `seed`, independent of Rayon thread count.
#[cfg(feature = "parallel")]
pub fn generate_biased_walks_ref_parallel_from_nodes<G: GraphRef + Sync>(
    graph: &G,
    start_nodes: &[usize],
    config: WalkConfig,
) -> Vec<Vec<usize>> {
    use rayon::prelude::*;

    let mut epoch_nodes: Vec<usize> = start_nodes.to_vec();
    let mut jobs: Vec<(u32, usize)> = Vec::with_capacity(start_nodes.len() * config.walks_per_node);

    for epoch in 0..(config.walks_per_node as u32) {
        let mut rng = ChaCha8Rng::seed_from_u64(mix64(config.seed ^ (epoch as u64)));
        epoch_nodes.shuffle(&mut rng);
        for &node in &epoch_nodes {
            jobs.push((epoch, node));
        }
    }

    jobs.par_iter()
        .enumerate()
        .map(|(i, (epoch, node))| {
            let seed = mix64(config.seed ^ ((*epoch as u64) << 32) ^ (*node as u64) ^ (i as u64));
            let mut rng = ChaCha8Rng::seed_from_u64(seed);
            biased_walk_ref(graph, *node, config, &mut rng)
        })
        .collect()
}