crates/kleene-graph/src/mis.rs
//! A maximal independent set, in parallel rounds on the CPU device's workers.
//!
//! On the undirected graph ([`crate::undirected`]; a self-loop is not counted as a
//! neighbour). Every vertex gets a fixed priority from the seed (splitmix64 of the seed and
//! the vertex, ties by vertex number). Each round every vertex still undecided whose
//! priority is above those of all its undecided neighbours joins the set, and its
//! neighbours leave; the rounds stop when no vertex is undecided. The priorities do not
//! change between rounds (Blelloch, Fineman and Shun's deterministic form of Luby's
//! algorithm), so the set is the one the greedy pass gives that takes the vertices by
//! falling priority, whatever the number of workers, and a run with the same seed is
//! repeated exactly. The rounds are few (about log² of the vertex count at most, with
//! high probability).
use std::sync::atomic::{AtomicU8, Ordering::Relaxed};
use kleene_sparse::{CpuDevice, Csr};
use crate::centrality::{each_of_level, square};
/// A maximal independent set.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mis {
/// Its vertices, rising.
pub members: Vec<u32>,
/// Rounds run.
pub rounds: u32,
}
const UNDECIDED: u8 = 0;
const IN: u8 = 1;
const OUT: u8 = 2;
/// Vertex `v`'s priority under `seed`: splitmix64 of the two, then the vertex (a higher
/// pair goes first).
pub fn mis_priority(seed: u64, v: u32) -> (u64, u32) {
let mut z = seed
.wrapping_add(u64::from(v).wrapping_mul(0x9E37_79B9_7F4A_7C15))
.wrapping_add(0x9E37_79B9_7F4A_7C15);
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
(z ^ (z >> 31), v)
}
/// A maximal independent set of the undirected graph `und` (each edge both ways, rows
/// sorted: [`crate::undirected`]): no two of its vertices are neighbours and every other
/// vertex has a neighbour in it. Vertices without neighbours are in it. The set is fixed by
/// `seed` ([`mis_priority`]): the greedy one by falling priority.
pub fn mis(und: &Csr<u8>, seed: u64, cpu: &CpuDevice) -> crate::Result<Mis> {
let n = square(und)?;
let off = und.pattern().offsets();
let state: Vec<AtomicU8> = (0..n).map(|_| AtomicU8::new(UNDECIDED)).collect();
let prio = |v: u32| mis_priority(seed, v);
let mut active: Vec<u32> = (0..n as u32).collect();
let mut join: Vec<u32> = Vec::new();
let mut rounds = 0;
while !active.is_empty() {
rounds += 1;
// the undecided vertices above all their undecided neighbours
let parts = each_of_level(cpu, &active, off, |part| {
let mut mine = Vec::new();
for &v in part {
let pv = prio(v);
let top = und.row(v as usize).iter().all(|&u| {
u == v || state[u as usize].load(Relaxed) != UNDECIDED || prio(u) < pv
});
if top {
mine.push(v);
}
}
mine
});
join.clear();
for p in parts {
join.extend(p);
}
// No two joining vertices are neighbours (each is above the other), and a
// joining vertex's neighbours do not join: they leave.
each_of_level(cpu, &join, off, |part| {
for &v in part {
state[v as usize].store(IN, Relaxed);
for &u in und.row(v as usize) {
if u != v {
state[u as usize].store(OUT, Relaxed);
}
}
}
});
let parts = each_of_level(cpu, &active, off, |part| {
part.iter()
.copied()
.filter(|&v| state[v as usize].load(Relaxed) == UNDECIDED)
.collect::<Vec<u32>>()
});
active.clear();
for p in parts {
active.extend(p);
}
}
let parts = cpu.split(n, |r| {
r.filter(|&v| state[v].load(Relaxed) == IN)
.map(|v| v as u32)
.collect::<Vec<u32>>()
});
Ok(Mis {
members: parts.concat(),
rounds,
})
}