Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/kleene

crates/kleene-plan/tests/procedure_far_ids.rs

//! A graph procedure over vertex ids too far apart to index directly: the ids are numbered
//! by rank for the run (as a graph stratum numbers them) and the answer comes back in the
//! caller's ids. Each procedure over ids spread far apart (an order-keeping map of the
//! dense ids) gives the answer over the dense ids, mapped the same way.

use kleene_plan::{Engine, Facts, HarmonicMode, Inputs, Plan, Procedure, RelId, Value};
use kleene_sparse::{CpuBackend, CpuDevice};

/// Two triangles joined by a path, a two-cycle, and a vertex with only an edge in.
const E: [(u64, u64); 11] = [
    (0, 1),
    (1, 2),
    (2, 0),
    (2, 3),
    (3, 4),
    (4, 3),
    (4, 5),
    (5, 6),
    (6, 7),
    (7, 5),
    (7, 8),
];

/// The dense id `v` spread far apart (order kept): past `u32::MAX` for most.
fn far(v: u64) -> u64 {
    v * 1_000_000_007 + 13
}

fn w(a: u64, b: u64) -> u64 {
    (a * 3 + b) % 4 + 1
}

/// The procedure with its ids mapped by `id`, its weights or capacities relation.
fn mapped(procedure: Procedure, id: fn(u64) -> u64, weights: RelId) -> Procedure {
    match procedure {
        Procedure::Sssp { source, weights: x } => Procedure::Sssp {
            source: id(source),
            weights: x.map(|_| weights),
        },
        Procedure::MaxFlow {
            source,
            target,
            capacities,
        } => Procedure::MaxFlow {
            source: id(source),
            target: id(target),
            capacities: capacities.map(|_| weights),
        },
        Procedure::SpanningForest { weights: x } => Procedure::SpanningForest {
            weights: x.map(|_| weights),
        },
        x => x,
    }
}

/// The procedure's rows over the edges with ids mapped by `id`, and whether the stratum
/// renumbered its ids.
async fn run(procedure: Procedure, id: fn(u64) -> u64) -> (Vec<Vec<Value>>, bool) {
    let mut p = Plan::new();
    let e = p.input("e", 2);
    let ws = p.input("w", 3);
    let procedure = mapped(procedure, id, ws);
    let r = p.derived("r", procedure.arity());
    p.procedure(r, e, procedure).unwrap();
    let mut inputs = Inputs::new();
    inputs
        .facts(
            e,
            Facts::from_rows(2, E.iter().map(|&(a, b)| [id(a), id(b)])).unwrap(),
        )
        .facts(
            ws,
            Facts::from_rows(3, E.iter().map(|&(a, b)| [id(a), id(b), w(a, b)])).unwrap(),
        );
    let out = Engine::<CpuBackend>::new(CpuDevice::new(2))
        .run(&p, &inputs)
        .await
        .unwrap();
    let ranked = out.report.strata.iter().any(|s| s.renumbered);
    (out.get(r).unwrap().sorted_rows(), ranked)
}

#[tokio::test]
async fn procedures_over_far_apart_ids_answer_in_those_ids() {
    // (a weights relation: replaced by the run's own)
    let some = Some(Plan::new().input("w", 3));
    let procedures = [
        Procedure::PageRank,
        Procedure::WeakComponents,
        Procedure::StrongComponents,
        Procedure::Sssp {
            source: 2,
            weights: None,
        },
        Procedure::Sssp {
            source: 2,
            weights: some,
        },
        Procedure::Betweenness,
        Procedure::Triangles,
        Procedure::Harmonic {
            mode: HarmonicMode::Exact,
        },
        Procedure::KTruss { k: 3 },
        Procedure::TrussNumbers,
        Procedure::Mis { seed: 7 },
        Procedure::SpanningForest { weights: None },
        Procedure::SpanningForest { weights: some },
        Procedure::MaxFlow {
            source: 0,
            target: 8,
            capacities: None,
        },
        Procedure::MaxFlow {
            source: 0,
            target: 8,
            capacities: some,
        },
    ];
    for procedure in procedures {
        let (dense, ranked) = run(procedure, |v| v).await;
        assert!(!ranked, "{procedure:?}: dense ids index directly");
        assert!(!dense.is_empty(), "{procedure:?}");
        let (spread, ranked) = run(procedure, far).await;
        assert!(ranked, "{procedure:?}: far-apart ids are numbered by rank");
        let cols = procedure.vertex_columns();
        let mut want: Vec<Vec<Value>> = dense
            .iter()
            .map(|r| {
                let mut r = r.clone();
                for &c in cols {
                    r[c] = far(r[c]);
                }
                r
            })
            .collect();
        want.sort();
        assert_eq!(spread, want, "{procedure:?}");
    }
}

#[tokio::test]
async fn a_source_that_is_no_vertex_is_refused_by_name() {
    let mut p = Plan::new();
    let e = p.input("e", 2);
    let r = p.derived("r", 2);
    p.procedure(
        r,
        e,
        Procedure::Sssp {
            source: far(99),
            weights: None,
        },
    )
    .unwrap();
    let mut inputs = Inputs::new();
    inputs.facts(
        e,
        Facts::from_rows(2, E.iter().map(|&(a, b)| [far(a), far(b)])).unwrap(),
    );
    // the source is in the numbering (the ids it reads), with no edge
    let out = Engine::<CpuBackend>::new(CpuDevice::new(2))
        .run(&p, &inputs)
        .await
        .unwrap();
    let rows = out.get(r).unwrap().sorted_rows();
    assert_eq!(rows, vec![vec![far(99), 0]]);
}