ruvector-solver 2.0.6

Sublinear-time solver for RuVector: O(log n) to O(√n) algorithms for sparse linear systems, PageRank, and spectral methods
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! Backward Push solver for target-centric Personalized PageRank.
//!
//! The backward (reverse) push algorithm computes approximate PPR
//! contributions **to** a target vertex by propagating residual mass
//! backward along incoming edges (on the transpose of the adjacency
//! matrix). This is the dual of the Andersen-Chung-Lang (2006) Forward
//! Push algorithm.
//!
//! # Algorithm
//!
//! Maintain two vectors over all `n` vertices:
//! - `estimate[v]`: accumulated PPR contribution from `v` to the target.
//! - `residual[v]`: unprocessed mass waiting at `v`.
//!
//! Initially `residual[target] = 1`, everything else is zero.
//!
//! While any vertex `v` has `|residual[v]| / max(1, in_degree(v)) > epsilon`:
//!   1. Dequeue `v` from the active set.
//!   2. `estimate[v] += alpha * residual[v]`.
//!   3. For each in-neighbour `u` of `v` (edge `u -> v` in the original graph):
//!        `residual[u] += (1 - alpha) * residual[v] / out_degree(v)`.
//!   4. `residual[v] = 0`.
//!
//! In-neighbours are obtained from the transposed adjacency matrix.
//!
//! # Complexity
//!
//! O(1 / (alpha * epsilon)) pushes total. Each push visits the in-neighbours
//! of one vertex. The queue-based design avoids scanning all `n` vertices
//! per push, achieving true sublinear time.

use std::collections::VecDeque;
use std::time::Instant;

use tracing::debug;

use crate::error::{SolverError, ValidationError};
use crate::traits::{SolverEngine, SublinearPageRank};
use crate::types::{
    Algorithm, ComplexityClass, ComplexityEstimate, ComputeBudget, CsrMatrix, SolverResult,
    SparsityProfile,
};

/// Maximum number of graph nodes to prevent OOM denial-of-service.
const MAX_GRAPH_NODES: usize = 100_000_000;

// ---------------------------------------------------------------------------
// Solver struct
// ---------------------------------------------------------------------------

/// Backward-push PPR solver.
///
/// Pushes probability mass backward along edges from target nodes.
/// Complementary to [`ForwardPushSolver`](crate::forward_push::ForwardPushSolver)
/// and often combined with it in bidirectional schemes.
///
/// # Example
///
/// ```rust,ignore
/// use ruvector_solver::backward_push::BackwardPushSolver;
/// use ruvector_solver::types::CsrMatrix;
///
/// let graph = CsrMatrix::<f64>::from_coo(3, 3, vec![
///     (0, 1, 1.0), (1, 2, 1.0), (2, 0, 1.0),
/// ]);
/// let solver = BackwardPushSolver::new(0.15, 1e-6);
/// let ppr = solver.ppr_to_target(&graph, 0).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct BackwardPushSolver {
    /// Teleportation probability (alpha). Must be in (0, 1).
    pub alpha: f64,
    /// Approximation tolerance (epsilon). Smaller values yield higher
    /// accuracy at the cost of more push operations.
    pub epsilon: f64,
}

impl BackwardPushSolver {
    /// Create a new backward-push solver.
    ///
    /// # Parameters
    ///
    /// - `alpha`: teleportation probability in (0, 1). Typical: 0.15 or 0.2.
    /// - `epsilon`: push threshold controlling accuracy vs speed.
    pub fn new(alpha: f64, epsilon: f64) -> Self {
        Self { alpha, epsilon }
    }

    /// Validate configuration parameters eagerly.
    fn validate_params(alpha: f64, epsilon: f64) -> Result<(), SolverError> {
        if alpha <= 0.0 || alpha >= 1.0 {
            return Err(SolverError::InvalidInput(
                ValidationError::ParameterOutOfRange {
                    name: "alpha".into(),
                    value: alpha.to_string(),
                    expected: "(0.0, 1.0) exclusive".into(),
                },
            ));
        }
        if epsilon <= 0.0 {
            return Err(SolverError::InvalidInput(
                ValidationError::ParameterOutOfRange {
                    name: "epsilon".into(),
                    value: epsilon.to_string(),
                    expected: "> 0.0".into(),
                },
            ));
        }
        Ok(())
    }

    /// Validate that the graph is square and the node index is in bounds.
    fn validate_graph_node(
        graph: &CsrMatrix<f64>,
        node: usize,
        name: &str,
    ) -> Result<(), SolverError> {
        if graph.rows != graph.cols {
            return Err(SolverError::InvalidInput(
                ValidationError::DimensionMismatch(format!(
                    "graph must be square, got {}x{}",
                    graph.rows, graph.cols,
                )),
            ));
        }
        if node >= graph.rows {
            return Err(SolverError::InvalidInput(
                ValidationError::ParameterOutOfRange {
                    name: name.into(),
                    value: node.to_string(),
                    expected: format!("[0, {})", graph.rows),
                },
            ));
        }
        Ok(())
    }

    /// Compute approximate PPR contributions **to** `target`.
    ///
    /// Returns a sparse vector of `(vertex, ppr_value)` pairs sorted by
    /// descending PPR value. Only vertices whose estimate exceeds 1e-15
    /// are included.
    pub fn ppr_to_target(
        &self,
        graph: &CsrMatrix<f64>,
        target: usize,
    ) -> Result<Vec<(usize, f64)>, SolverError> {
        Self::backward_push_core(
            graph,
            target,
            self.alpha,
            self.epsilon,
            &ComputeBudget::default(),
        )
    }

    /// Same as [`ppr_to_target`](Self::ppr_to_target) with an explicit budget.
    pub fn ppr_to_target_with_budget(
        &self,
        graph: &CsrMatrix<f64>,
        target: usize,
        budget: &ComputeBudget,
    ) -> Result<Vec<(usize, f64)>, SolverError> {
        Self::backward_push_core(graph, target, self.alpha, self.epsilon, budget)
    }

    /// Core backward push implementation.
    ///
    /// Uses a FIFO queue so that each vertex is only re-scanned when its
    /// residual has been increased above the threshold, giving O(1/(alpha*eps))
    /// total pushes rather than O(n) scans per push.
    fn backward_push_core(
        graph: &CsrMatrix<f64>,
        target: usize,
        alpha: f64,
        epsilon: f64,
        budget: &ComputeBudget,
    ) -> Result<Vec<(usize, f64)>, SolverError> {
        Self::validate_params(alpha, epsilon)?;
        Self::validate_graph_node(graph, target, "target")?;

        let start = Instant::now();
        let n = graph.rows;

        if n > MAX_GRAPH_NODES {
            return Err(SolverError::InvalidInput(ValidationError::MatrixTooLarge {
                rows: n,
                cols: n,
                max_dim: MAX_GRAPH_NODES,
            }));
        }

        // Build the transposed adjacency so row_entries(v) in `graph_t`
        // yields the in-neighbours of v in the original graph.
        let graph_t = graph.transpose();

        let mut estimate = vec![0.0f64; n];
        let mut residual = vec![0.0f64; n];

        // Seed: all mass starts at the target vertex.
        residual[target] = 1.0;

        // FIFO queue of vertices whose residual exceeds the push threshold.
        let mut queue: VecDeque<usize> = VecDeque::with_capacity(n.min(1024));
        let mut in_queue = vec![false; n];
        queue.push_back(target);
        in_queue[target] = true;

        let mut pushes = 0usize;
        let max_pushes = budget.max_iterations;

        while let Some(v) = queue.pop_front() {
            in_queue[v] = false;

            let r_v = residual[v];
            if r_v.abs() < 1e-15 {
                continue;
            }

            // Check the push threshold: |r_v| / max(1, in_deg_t(v)) > epsilon.
            let in_deg_t = graph_t.row_degree(v).max(1);
            if r_v.abs() / in_deg_t as f64 <= epsilon {
                continue;
            }

            // Budget enforcement.
            pushes += 1;
            if pushes > max_pushes {
                return Err(SolverError::BudgetExhausted {
                    reason: format!("backward push exceeded {} push budget", max_pushes,),
                    elapsed: start.elapsed(),
                });
            }
            if start.elapsed() > budget.max_time {
                return Err(SolverError::BudgetExhausted {
                    reason: "wall-clock budget exceeded".into(),
                    elapsed: start.elapsed(),
                });
            }

            // Absorb alpha fraction into the PPR estimate.
            estimate[v] += alpha * r_v;

            // Distribute (1 - alpha) * r_v backward along in-edges.
            // The denominator is out_degree(v) in the original graph, which
            // corresponds to row_degree(v) in `graph`.
            let out_deg = graph.row_degree(v);
            if out_deg == 0 {
                // Dangling node: no outgoing edges; residual fully absorbed.
                residual[v] = 0.0;
                continue;
            }

            let push_mass = (1.0 - alpha) * r_v / out_deg as f64;

            for (u, _weight) in graph_t.row_entries(v) {
                residual[u] += push_mass;

                // Enqueue u if it exceeds the push threshold and is not
                // already queued.
                let u_in_deg = graph_t.row_degree(u).max(1);
                if residual[u].abs() / u_in_deg as f64 > epsilon && !in_queue[u] {
                    queue.push_back(u);
                    in_queue[u] = true;
                }
            }

            residual[v] = 0.0;
        }

        debug!(
            target: "ruvector_solver::backward_push",
            pushes,
            target,
            elapsed_us = start.elapsed().as_micros() as u64,
            "backward push converged",
        );

        // Collect non-zero estimates, sorted descending by PPR value.
        let mut result: Vec<(usize, f64)> = estimate
            .into_iter()
            .enumerate()
            .filter(|(_, val)| *val > 1e-15)
            .collect();
        result.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        Ok(result)
    }
}

// ---------------------------------------------------------------------------
// SolverEngine implementation
// ---------------------------------------------------------------------------

impl SolverEngine for BackwardPushSolver {
    fn solve(
        &self,
        matrix: &CsrMatrix<f64>,
        rhs: &[f64],
        budget: &ComputeBudget,
    ) -> Result<SolverResult, SolverError> {
        // For SolverEngine compatibility, interpret rhs as a target indicator
        // vector: pick the node with the largest weight as the target.
        let target = rhs
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(i, _)| i)
            .unwrap_or(0);

        let wall_start = Instant::now();
        let ppr = self.ppr_to_target_with_budget(matrix, target, budget)?;

        let mut solution = vec![0.0f32; matrix.rows];
        for &(node, val) in &ppr {
            solution[node] = val as f32;
        }

        Ok(SolverResult {
            solution,
            iterations: ppr.len(),
            residual_norm: 0.0,
            wall_time: wall_start.elapsed(),
            convergence_history: Vec::new(),
            algorithm: Algorithm::BackwardPush,
        })
    }

    fn estimate_complexity(&self, _profile: &SparsityProfile, n: usize) -> ComplexityEstimate {
        let est_pushes = (1.0 / (self.alpha * self.epsilon)) as usize;
        ComplexityEstimate {
            algorithm: Algorithm::BackwardPush,
            estimated_flops: est_pushes as u64 * 10,
            estimated_iterations: est_pushes,
            estimated_memory_bytes: n * 16, // estimate + residual vectors
            complexity_class: ComplexityClass::SublinearNnz,
        }
    }

    fn algorithm(&self) -> Algorithm {
        Algorithm::BackwardPush
    }
}

// ---------------------------------------------------------------------------
// SublinearPageRank implementation
// ---------------------------------------------------------------------------

impl SublinearPageRank for BackwardPushSolver {
    fn ppr(
        &self,
        matrix: &CsrMatrix<f64>,
        target: usize,
        alpha: f64,
        epsilon: f64,
    ) -> Result<Vec<(usize, f64)>, SolverError> {
        Self::backward_push_core(matrix, target, alpha, epsilon, &ComputeBudget::default())
    }

    fn ppr_multi_seed(
        &self,
        matrix: &CsrMatrix<f64>,
        seeds: &[(usize, f64)],
        alpha: f64,
        epsilon: f64,
    ) -> Result<Vec<(usize, f64)>, SolverError> {
        let n = matrix.rows;
        for &(node, _) in seeds {
            Self::validate_graph_node(matrix, node, "seed")?;
        }

        // Build transposed graph once and reuse across all seeds.
        let graph_t = matrix.transpose();

        let mut combined = vec![0.0f64; n];

        for &(seed, weight) in seeds {
            // Run backward push for each seed target. We inline the core
            // logic with the shared transpose to avoid rebuilding it.
            let ppr = backward_push_with_transpose(
                matrix,
                &graph_t,
                seed,
                alpha,
                epsilon,
                &ComputeBudget::default(),
            )?;
            for &(node, val) in &ppr {
                combined[node] += weight * val;
            }
        }

        let mut result: Vec<(usize, f64)> = combined
            .into_iter()
            .enumerate()
            .filter(|(_, val)| *val > 1e-15)
            .collect();
        result.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        Ok(result)
    }
}

/// Internal helper: backward push using a pre-computed transpose.
///
/// Avoids re-transposing for multi-seed queries.
fn backward_push_with_transpose(
    graph: &CsrMatrix<f64>,
    graph_t: &CsrMatrix<f64>,
    target: usize,
    alpha: f64,
    epsilon: f64,
    budget: &ComputeBudget,
) -> Result<Vec<(usize, f64)>, SolverError> {
    let start = Instant::now();
    let n = graph.rows;

    let mut estimate = vec![0.0f64; n];
    let mut residual = vec![0.0f64; n];
    residual[target] = 1.0;

    let mut queue: VecDeque<usize> = VecDeque::with_capacity(n.min(1024));
    let mut in_queue = vec![false; n];
    queue.push_back(target);
    in_queue[target] = true;

    let mut pushes = 0usize;
    let max_pushes = budget.max_iterations;

    while let Some(v) = queue.pop_front() {
        in_queue[v] = false;

        let r_v = residual[v];
        if r_v.abs() < 1e-15 {
            continue;
        }

        let in_deg_t = graph_t.row_degree(v).max(1);
        if r_v.abs() / in_deg_t as f64 <= epsilon {
            continue;
        }

        pushes += 1;
        if pushes > max_pushes {
            return Err(SolverError::BudgetExhausted {
                reason: format!("backward push exceeded {} push budget", max_pushes,),
                elapsed: start.elapsed(),
            });
        }
        if start.elapsed() > budget.max_time {
            return Err(SolverError::BudgetExhausted {
                reason: "wall-clock budget exceeded".into(),
                elapsed: start.elapsed(),
            });
        }

        estimate[v] += alpha * r_v;

        let out_deg = graph.row_degree(v);
        if out_deg == 0 {
            residual[v] = 0.0;
            continue;
        }

        let push_mass = (1.0 - alpha) * r_v / out_deg as f64;

        for (u, _weight) in graph_t.row_entries(v) {
            residual[u] += push_mass;
            let u_in_deg = graph_t.row_degree(u).max(1);
            if residual[u].abs() / u_in_deg as f64 > epsilon && !in_queue[u] {
                queue.push_back(u);
                in_queue[u] = true;
            }
        }

        residual[v] = 0.0;
    }

    let mut result: Vec<(usize, f64)> = estimate
        .into_iter()
        .enumerate()
        .filter(|(_, val)| *val > 1e-15)
        .collect();
    result.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

    Ok(result)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    /// Build a directed cycle 0->1->2->...->n-1->0.
    fn directed_cycle(n: usize) -> CsrMatrix<f64> {
        let entries: Vec<_> = (0..n).map(|i| (i, (i + 1) % n, 1.0f64)).collect();
        CsrMatrix::<f64>::from_coo(n, n, entries)
    }

    /// Build a star graph with edges i->0 for i in 1..n.
    fn star_to_center(n: usize) -> CsrMatrix<f64> {
        let entries: Vec<_> = (1..n).map(|i| (i, 0, 1.0f64)).collect();
        CsrMatrix::<f64>::from_coo(n, n, entries)
    }

    /// Build a complete graph on n vertices (every pair connected).
    fn complete_graph(n: usize) -> CsrMatrix<f64> {
        let mut entries = Vec::new();
        for i in 0..n {
            for j in 0..n {
                if i != j {
                    entries.push((i, j, 1.0f64));
                }
            }
        }
        CsrMatrix::<f64>::from_coo(n, n, entries)
    }

    #[test]
    fn single_node_no_edges() {
        let graph = CsrMatrix::<f64>::from_coo(1, 1, Vec::<(usize, usize, f64)>::new());
        let solver = BackwardPushSolver::new(0.15, 1e-6);
        let result = solver.ppr_to_target(&graph, 0).unwrap();

        // Dangling node: estimate[0] = alpha * 1.0 = 0.15.
        assert_eq!(result.len(), 1);
        assert!((result[0].1 - 0.15).abs() < 1e-10);
    }

    #[test]
    fn directed_cycle_all_vertices_contribute() {
        let graph = directed_cycle(3);
        let solver = BackwardPushSolver::new(0.2, 1e-8);
        let result = solver.ppr_to_target(&graph, 0).unwrap();

        let total: f64 = result.iter().map(|(_, v)| v).sum();
        assert!(total <= 1.0 + 1e-6, "total PPR = {}", total);
        assert!(total > 0.1, "total too small: {}", total);
        assert!(result.len() >= 2);
    }

    #[test]
    fn star_graph_center_highest_ppr() {
        let graph = star_to_center(5);
        let solver = BackwardPushSolver::new(0.15, 1e-8);
        let result = solver.ppr_to_target(&graph, 0).unwrap();

        let ppr_0 = result
            .iter()
            .find(|&&(v, _)| v == 0)
            .map(|&(_, p)| p)
            .unwrap_or(0.0);
        for &(v, p) in &result {
            if v != 0 {
                assert!(ppr_0 >= p, "expected ppr[0]={} >= ppr[{}]={}", ppr_0, v, p,);
            }
        }
    }

    #[test]
    fn complete_graph_uniform_ppr() {
        // On a complete graph, by symmetry PPR should be approximately
        // uniform for non-target vertices.
        let graph = complete_graph(5);
        let solver = BackwardPushSolver::new(0.15, 1e-8);
        let result = solver.ppr_to_target(&graph, 0).unwrap();

        // All vertices should be represented.
        assert!(result.len() >= 4);

        let total: f64 = result.iter().map(|(_, v)| v).sum();
        assert!(total > 0.5 && total <= 1.0 + 1e-6);
    }

    #[test]
    fn rejects_non_square_graph() {
        let graph = CsrMatrix::<f64>::from_coo(2, 3, vec![(0, 1, 1.0f64)]);
        let solver = BackwardPushSolver::new(0.15, 1e-6);
        assert!(solver.ppr_to_target(&graph, 0).is_err());
    }

    #[test]
    fn rejects_out_of_bounds_target() {
        let graph = CsrMatrix::<f64>::from_coo(3, 3, vec![(0, 1, 1.0f64)]);
        let solver = BackwardPushSolver::new(0.15, 1e-6);
        assert!(solver.ppr_to_target(&graph, 5).is_err());
    }

    #[test]
    fn rejects_bad_alpha() {
        let graph = CsrMatrix::<f64>::from_coo(3, 3, vec![(0, 1, 1.0f64)]);

        let zero_alpha = BackwardPushSolver::new(0.0, 1e-6);
        assert!(zero_alpha.ppr_to_target(&graph, 0).is_err());

        let one_alpha = BackwardPushSolver::new(1.0, 1e-6);
        assert!(one_alpha.ppr_to_target(&graph, 0).is_err());

        let neg_alpha = BackwardPushSolver::new(-0.5, 1e-6);
        assert!(neg_alpha.ppr_to_target(&graph, 0).is_err());
    }

    #[test]
    fn rejects_bad_epsilon() {
        let graph = CsrMatrix::<f64>::from_coo(3, 3, vec![(0, 1, 1.0f64)]);

        let zero_eps = BackwardPushSolver::new(0.15, 0.0);
        assert!(zero_eps.ppr_to_target(&graph, 0).is_err());

        let neg_eps = BackwardPushSolver::new(0.15, -1e-6);
        assert!(neg_eps.ppr_to_target(&graph, 0).is_err());
    }

    #[test]
    fn solver_engine_trait_integration() {
        let graph = directed_cycle(4);
        let solver = BackwardPushSolver::new(0.15, 1e-6);
        let rhs = vec![0.0, 0.0, 1.0, 0.0]; // node 2 is the target
        let result = solver
            .solve(&graph, &rhs, &ComputeBudget::default())
            .unwrap();

        assert_eq!(result.algorithm, Algorithm::BackwardPush);
        assert!(!result.solution.is_empty());
    }

    #[test]
    fn sublinear_pagerank_trait_ppr() {
        let graph = directed_cycle(5);
        let solver = BackwardPushSolver::new(0.15, 1e-6);
        let result = solver.ppr(&graph, 0, 0.15, 1e-6).unwrap();
        assert!(!result.is_empty());

        let total: f64 = result.iter().map(|(_, v)| v).sum();
        assert!(total <= 1.0 + 1e-6);
    }

    #[test]
    fn multi_seed_combines_correctly() {
        let graph = directed_cycle(4);
        let solver = BackwardPushSolver::new(0.15, 1e-6);
        let seeds = vec![(0, 0.5), (2, 0.5)];
        let result = solver.ppr_multi_seed(&graph, &seeds, 0.15, 1e-6).unwrap();
        assert!(!result.is_empty());
    }

    #[test]
    fn converges_on_100_node_cycle() {
        let graph = directed_cycle(100);
        let solver = BackwardPushSolver::new(0.15, 1e-6);
        let result = solver.ppr_to_target(&graph, 50).unwrap();

        let total: f64 = result.iter().map(|(_, v)| v).sum();
        assert!(total > 0.0 && total <= 1.0 + 1e-6);
    }

    #[test]
    fn transpose_correctness() {
        let graph =
            CsrMatrix::<f64>::from_coo(3, 3, vec![(0, 1, 1.0f64), (1, 2, 1.0f64), (2, 0, 1.0f64)]);
        let gt = graph.transpose();

        // Transposed row 1 should contain (0, 1.0) because 0->1 in original.
        let r1: Vec<_> = gt.row_entries(1).collect();
        assert_eq!(r1.len(), 1);
        assert_eq!(*r1[0].1, 1.0f64);
        assert_eq!(r1[0].0, 0);
    }

    #[test]
    fn estimate_complexity_reports_sublinear() {
        let solver = BackwardPushSolver::new(0.15, 1e-4);
        let profile = SparsityProfile {
            rows: 1000,
            cols: 1000,
            nnz: 5000,
            density: 0.005,
            is_diag_dominant: false,
            estimated_spectral_radius: 0.9,
            estimated_condition: 10.0,
            is_symmetric_structure: false,
            avg_nnz_per_row: 5.0,
            max_nnz_per_row: 10,
        };
        let est = solver.estimate_complexity(&profile, 1000);
        assert_eq!(est.algorithm, Algorithm::BackwardPush);
        assert_eq!(est.complexity_class, ComplexityClass::SublinearNnz);
        assert!(est.estimated_iterations > 0);
    }
}