use crate::Error;
use crate::graph::{Graph, GraphBuilder};
pub fn complement(g: &Graph) -> Graph {
let n = g.num_vertices();
let mut b = GraphBuilder::new(n);
for i in 0..n {
for j in i + 1..n {
if !g.has_edge(i, j) {
b.set(i, j, true);
}
}
}
b.build()
}
fn check_set(g: &Graph, vertices: &[usize]) -> Result<Vec<bool>, Error> {
let n = g.num_vertices();
let mut member = vec![false; n];
for &v in vertices {
if v >= n {
return Err(Error::VertexOutOfRange {
vertex: v as i64,
n,
});
}
if std::mem::replace(&mut member[v], true) {
return Err(Error::DuplicateVertex(v));
}
}
Ok(member)
}
pub fn induced(g: &Graph, vertices: &[usize]) -> Result<Graph, Error> {
check_set(g, vertices)?;
let mut b = GraphBuilder::new(vertices.len());
for (i, &u) in vertices.iter().enumerate() {
for (j, &v) in vertices.iter().enumerate().skip(i + 1) {
if g.has_edge(u, v) {
b.set(i, j, true);
}
}
}
Ok(b.build())
}
pub fn rest(g: &Graph, set: &[usize]) -> Result<Vec<usize>, Error> {
let member = check_set(g, set)?;
Ok((0..g.num_vertices()).filter(|&v| !member[v]).collect())
}
pub fn common_neighborhood(g: &Graph, set: &[usize]) -> Result<Vec<usize>, Error> {
common(g, set, true)
}
pub fn common_non_neighborhood(g: &Graph, set: &[usize]) -> Result<Vec<usize>, Error> {
common(g, set, false)
}
fn common(g: &Graph, set: &[usize], adjacent: bool) -> Result<Vec<usize>, Error> {
let member = check_set(g, set)?;
if set.is_empty() {
return Err(Error::Invalid("the vertex set must not be empty".into()));
}
Ok((0..g.num_vertices())
.filter(|&v| !member[v] && set.iter().all(|&f| g.has_edge(f, v) == adjacent))
.collect())
}
pub fn switch(g: &Graph, set: &[usize]) -> Result<Graph, Error> {
let member = check_set(g, set)?;
let n = g.num_vertices();
let mut b = g.clone().into_builder();
for &f in set {
for v in (0..n).filter(|&v| !member[v]) {
let present = b.get(f, v);
b.set(f, v, !present);
}
}
Ok(b.build())
}
pub fn parse_blocks(text: &str) -> Result<Vec<Vec<usize>>, Error> {
let mut blocks = Vec::new();
for (i, line) in text.lines().enumerate() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let block = line
.split(|c: char| c.is_whitespace() || c == ',')
.filter(|t| !t.is_empty())
.map(|t| {
t.parse::<usize>().map_err(|_| Error::Parse {
line: Some(i + 1),
msg: format!("{t:?} is not a point number"),
})
})
.collect::<Result<Vec<_>, _>>()?;
blocks.push(block);
}
Ok(blocks)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Incidence {
symbols: usize,
sets: Vec<Vec<bool>>,
}
impl Incidence {
pub fn new(symbols: usize, sets: &[Vec<usize>]) -> Result<Self, Error> {
let mut rows = Vec::with_capacity(sets.len());
for set in sets {
let mut row = vec![false; symbols];
for &s in set {
if s >= symbols {
return Err(Error::VertexOutOfRange {
vertex: s as i64,
n: symbols,
});
}
row[s] = true;
}
rows.push(row);
}
Ok(Incidence {
symbols,
sets: rows,
})
}
pub fn from_blocks(points: Option<usize>, blocks: &[Vec<usize>]) -> Result<Self, Error> {
let points = points.unwrap_or_else(|| blocks.iter().flatten().max().map_or(0, |&p| p + 1));
Incidence::new(points, blocks)
}
pub fn num_symbols(&self) -> usize {
self.symbols
}
pub fn num_sets(&self) -> usize {
self.sets.len()
}
pub fn symbol_graph(&self, k: usize) -> Graph {
let mut b = GraphBuilder::new(self.symbols);
for i in 0..self.symbols {
let containing: Vec<&Vec<bool>> = self.sets.iter().filter(|s| s[i]).collect();
for j in i + 1..self.symbols {
if containing.iter().filter(|s| s[j]).count() == k {
b.set(i, j, true);
}
}
}
b.build()
}
pub fn set_graph(&self, k: usize) -> Graph {
let n = self.sets.len();
let mut b = GraphBuilder::new(n);
for i in 0..n {
let members: Vec<usize> = (0..self.symbols).filter(|&s| self.sets[i][s]).collect();
for (j, other) in self.sets.iter().enumerate().skip(i + 1) {
if members.iter().filter(|&&s| other[s]).count() == k {
b.set(i, j, true);
}
}
}
b.build()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Product {
Cartesian,
Tensor,
Strong,
Lexicographic,
}
impl std::str::FromStr for Product {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"cartesian" => Ok(Product::Cartesian),
"tensor" => Ok(Product::Tensor),
"strong" => Ok(Product::Strong),
"lexicographic" => Ok(Product::Lexicographic),
_ => Err(Error::Invalid(format!(
"unknown product {s:?}: choose cartesian, tensor, strong or lexicographic"
))),
}
}
}
pub fn product(g: &Graph, h: &Graph, kind: Product) -> Graph {
let (ng, nh) = (g.num_vertices(), h.num_vertices());
let mut b = GraphBuilder::new(ng * nh);
let at = |u: usize, v: usize| u * nh + v;
for u in 0..ng {
for v in 0..nh {
let same_u = matches!(
kind,
Product::Cartesian | Product::Strong | Product::Lexicographic
);
if same_u {
for &w in h.neighbors(v) {
b.set(at(u, v), at(u, w as usize), true);
}
}
for &x in g.neighbors(u) {
let x = x as usize;
match kind {
Product::Cartesian => b.set(at(u, v), at(x, v), true),
Product::Tensor => {
for &w in h.neighbors(v) {
b.set(at(u, v), at(x, w as usize), true);
}
}
Product::Strong => {
b.set(at(u, v), at(x, v), true);
for &w in h.neighbors(v) {
b.set(at(u, v), at(x, w as usize), true);
}
}
Product::Lexicographic => {
for w in 0..nh {
b.set(at(u, v), at(x, w), true);
}
}
}
}
}
}
b.build()
}
fn regular_degree(g: &Graph, what: &str) -> Result<usize, Error> {
let degrees = g.degrees();
match degrees.first() {
Some(&d) if degrees.iter().all(|&x| x == d) => Ok(d),
Some(_) => Err(Error::Invalid(format!("{what} must be regular"))),
None => Err(Error::Invalid(format!("{what} has no vertices"))),
}
}
fn back(g: &Graph, v: usize, w: usize) -> usize {
g.neighbors(w)
.binary_search(&(v as u32))
.expect("an edge has two ends")
}
fn check_cloud(g: &Graph, h: &Graph) -> Result<usize, Error> {
let d = regular_degree(g, "the first graph")?;
if h.num_vertices() != d {
return Err(Error::Invalid(format!(
"the second graph must have as many vertices as the first graph's degree, {d}; it has {}",
h.num_vertices()
)));
}
Ok(d)
}
pub fn replacement(g: &Graph, h: &Graph) -> Result<Graph, Error> {
let d = check_cloud(g, h)?;
let mut b = GraphBuilder::new(g.num_vertices() * d);
for u in 0..g.num_vertices() {
for i in 0..d {
for &j in h.neighbors(i) {
b.set(u * d + i, u * d + j as usize, true);
}
let w = g.neighbors(u)[i] as usize;
b.set(u * d + i, w * d + back(g, u, w), true);
}
}
Ok(b.build())
}
pub fn zigzag(g: &Graph, h: &Graph) -> Result<Graph, Error> {
let d = check_cloud(g, h)?;
regular_degree(h, "the second graph")?;
let mut b = GraphBuilder::new(g.num_vertices() * d);
for u in 0..g.num_vertices() {
for i in 0..d {
for &zig in h.neighbors(i) {
let w = g.neighbors(u)[zig as usize] as usize;
let arrive = back(g, u, w);
for &zag in h.neighbors(arrive) {
let (from, to) = (u * d + i, w * d + zag as usize);
if from != to {
b.set(from, to, true);
}
}
}
}
}
Ok(b.build())
}
#[cfg(test)]
mod tests {
use super::*;
fn complete(n: usize) -> Graph {
complement(&Graph::empty(n))
}
#[test]
fn products_of_small_graphs() {
let rook = product(&complete(4), &complete(4), Product::Cartesian);
let by_hand = Graph::from_edges(
16,
(0..16usize)
.flat_map(|a| (a + 1..16).map(move |b| (a, b)))
.filter(|&(a, b)| a / 4 == b / 4 || a % 4 == b % 4),
)
.unwrap();
assert_eq!(rook, by_hand);
let k2 = complete(2);
let cube = product(
&product(&k2, &k2, Product::Cartesian),
&k2,
Product::Cartesian,
);
assert_eq!(
cube,
Graph::from_edges(
8,
(0..8usize)
.flat_map(|u| (0..3).map(move |b| (u, u ^ (1 << b))))
.filter(|&(u, v)| u < v)
)
.unwrap()
);
assert_eq!(product(&k2, &k2, Product::Tensor).num_edges(), 2);
assert_eq!(product(&k2, &k2, Product::Strong), complete(4));
assert_eq!(product(&path(3), &path(3), Product::Strong).num_edges(), 20);
let lex = product(&path(3), &k2, Product::Lexicographic);
assert_eq!(lex.num_edges(), 2 * 4 + 3);
assert!("cube".parse::<Product>().is_err());
}
#[test]
fn replacement_and_zigzag_products() {
let truncated = replacement(&complete(4), &complete(3)).unwrap();
assert_eq!((truncated.num_vertices(), truncated.num_edges()), (12, 18));
assert!(truncated.degrees().iter().all(|&d| d == 3));
assert_eq!(truncated.components().1, 1);
let cycle4 = Graph::from_edges(4, [(0, 1), (1, 2), (2, 3), (3, 0)]).unwrap();
let z = zigzag(&complete(5), &cycle4).unwrap();
assert_eq!(z.num_vertices(), 20);
assert!(z.degrees().iter().all(|&d| d == 4), "{:?}", z.degrees());
assert!(replacement(&complete(4), &complete(4)).is_err());
assert!(zigzag(&path(3), &complete(2)).is_err());
}
fn path(n: usize) -> Graph {
Graph::from_edges(n, (1..n).map(|i| (i - 1, i))).unwrap()
}
#[test]
fn complement_and_switch() {
let g = path(4);
assert_eq!(complement(&g).num_edges(), 3);
assert_eq!(complement(&complement(&g)), g);
let s = switch(&g, &[0]).unwrap();
assert!(!s.has_edge(0, 1) && s.has_edge(0, 2) && s.has_edge(0, 3) && s.has_edge(1, 2));
}
#[test]
fn neighborhoods_and_induced() {
let g = path(5);
assert_eq!(common_neighborhood(&g, &[1, 3]).unwrap(), vec![2]);
assert_eq!(common_non_neighborhood(&g, &[0]).unwrap(), vec![2, 3, 4]);
let h = induced(&g, &[3, 2, 0]).unwrap();
assert_eq!(h.edges().collect::<Vec<_>>(), vec![(0, 1)]);
assert!(induced(&g, &[1, 1]).is_err());
}
#[test]
fn incidence_graphs() {
let lines = [
[0, 1, 2],
[0, 3, 4],
[0, 5, 6],
[1, 3, 5],
[1, 4, 6],
[2, 3, 6],
[2, 4, 5],
];
let inc = Incidence::new(7, &lines.map(|l| l.to_vec())).unwrap();
assert_eq!(inc.set_graph(1).num_edges(), 21);
assert_eq!(inc.symbol_graph(1).num_edges(), 21);
assert_eq!(inc.symbol_graph(2).num_edges(), 0);
}
#[test]
fn triangular_graph_from_pairs() {
let text: String = (0..8)
.flat_map(|i| (i + 1..8).map(move |j| format!("{i} {j}\n")))
.collect();
let blocks = parse_blocks(&format!("# pairs\n{text}\n")).unwrap();
let t8 = Incidence::from_blocks(None, &blocks).unwrap().set_graph(1);
assert_eq!((t8.num_vertices(), t8.num_edges()), (28, 28 * 12 / 2));
assert!((0..28).all(|v| t8.degree(v) == 12));
assert!(parse_blocks("0 1\n2 x\n").is_err());
assert_eq!(parse_blocks("0,1, 2\n").unwrap(), vec![vec![0, 1, 2]]);
}
}