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
//! `[OrientedGraph]`s and their flag implementation.
use crate::combinatorics;
use crate::flag::{Flag, SubClass, SubFlag};
use crate::flags::common::*;
use crate::iterators::{Functions, StreamingIterator};
use canonical_form::Canonize;
use std::fmt;
use std::ops::Neg;
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy, Serialize, Deserialize)]
/// The arc between two nodes of a directed graphs.
#[derive(Default)]
pub enum Arc {
/// No arc.
#[default]
None,
/// Arc from the first to the second vertex considered.
Edge,
/// Arc from the second to the first vertex considered.
BackEdge,
/// Arcs in both directions
Reciprocal,
}
impl Neg for Arc {
type Output = Self;
fn neg(self) -> Self {
match self {
Edge => BackEdge,
BackEdge => Edge,
e => e,
}
}
}
use Arc::*;
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Serialize, Deserialize)]
/// Loopless directed graphs.
///
/// The possible relations between two vertices is described by the type [`Arc`].
pub struct DirectedGraph {
/// Number of vertices.
size: usize,
/// Flat matrix of arcs.
edge: AntiSym<Arc>,
}
impl DirectedGraph {
/// Create a directed graph with `n` vertices and arcs in `arcs`.
///
/// # Panics
/// * If `arcs` contains vertices not in `{0, ..., n-1}`
/// * If a loop `(u, u)` is provided
/// * If some arc is provided twice
/// ```
/// use flag_algebra::flags::OrientedGraph;
///
/// // Oriented path with 3 vertices `0 -> 1 -> 2`
/// let p3 = OrientedGraph::new(3, [(0, 1), (1, 2)]);
/// ```
pub fn new<I>(n: usize, arcs: I) -> Self
where
I: IntoIterator<Item = (usize, usize)>,
{
let mut new_edge = AntiSym::new(None, n);
for (u, v) in arcs {
check_arc((u, v), n);
let new_arc = match new_edge.get(u, v) {
None => Edge,
BackEdge => Reciprocal,
_ => panic!("Arc ({u}, {v}) specified twice"),
};
new_edge.set((u, v), new_arc);
}
Self {
size: n,
edge: new_edge,
}
}
/// Number of vertices
pub fn size(&self) -> usize {
self.size
}
/// Out-neigborhood of `v`.
/// ```
/// use flag_algebra::flags::OrientedGraph;
/// let p3 = OrientedGraph::new(3, [(0,1), (1,2)]);
/// assert_eq!(p3.out_nbrs(1), vec![2]);
/// ```
pub fn out_nbrs(&self, v: usize) -> Vec<usize> {
let mut res = Vec::new();
for u in 0..self.size {
if u != v && matches!(self.edge.get(u, v), BackEdge | Reciprocal) {
res.push(u);
}
}
res
}
/// In-neigborhood of `v`.
/// ```
/// use flag_algebra::flags::OrientedGraph;
/// let p3 = OrientedGraph::new(3, [(0, 1), (1, 2)]);
/// assert_eq!(p3.in_nbrs(1), vec![0]);
/// ```
pub fn in_nbrs(&self, v: usize) -> Vec<usize> {
let mut res = Vec::new();
for u in 0..self.size {
if u != v && matches!(self.edge.get(u, v), Edge | Reciprocal) {
res.push(u);
}
}
res
}
/// Oriented relation between `u` to `v`.
/// ```
/// use flag_algebra::flags::{DirectedGraph, Arc};
/// let g = DirectedGraph::new(3, [(0, 1), (1, 2), (2, 1)]);
/// assert_eq!(g.arc(0, 1), Arc::Edge);
/// assert_eq!(g.arc(1, 0), Arc::BackEdge);
/// assert_eq!(g.arc(0, 2), Arc::None);
/// assert_eq!(g.arc(1, 2), Arc::Reciprocal);
/// ```
pub fn arc(&self, u: usize, v: usize) -> Arc {
check_arc((u, v), self.size);
self.edge.get(u, v)
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Serialize, Deserialize)]
/// Loopless oriented graphs.
///
/// Every pair of vertices has at most one edge.
pub struct OrientedGraph(DirectedGraph);
impl OrientedGraph {
pub fn as_directed(&self) -> &DirectedGraph {
&self.0
}
/// Create an oriented graph with `n` vertices and arcs in `arcs`.
///
/// # Panics
/// * If `arcs` contains vertices not in `{0, ..., n-1}`
/// * If a loop `(u, u)` is provided
/// * If some arc is provided twice
/// * If both arcs `(u, v)` and `(v, u)` are provided
/// ```
/// use flag_algebra::flags::OrientedGraph;
///
/// // Oriented path with 3 vertices `0 -> 1 -> 2`
/// let p3 = OrientedGraph::new(3, [(0, 1), (1, 2)]);
/// ```
pub fn new<I>(n: usize, arcs: I) -> Self
where
I: IntoIterator<Item = (usize, usize)>,
{
let mut new_edge = AntiSym::new(None, n);
for (u, v) in arcs {
check_arc((u, v), n);
assert!(
new_edge.get(u, v) == None,
"Pair {{{u}, {v}}} specified twice"
);
new_edge.set((u, v), Edge);
}
Self(DirectedGraph {
size: n,
edge: new_edge,
})
}
/// Oriented graph with `n` vertices and no edge.
/// ```
/// use flag_algebra::flags::OrientedGraph;
/// assert_eq!(OrientedGraph::empty(4), OrientedGraph::new(4, []));
/// ```
pub fn empty(n: usize) -> Self {
Self::new(n, [])
}
/// Number of vertices
pub fn size(&self) -> usize {
self.0.size()
}
/// Out-neigborhood of `v`.
/// ```
/// use flag_algebra::flags::OrientedGraph;
/// let p3 = OrientedGraph::new(3, [(0,1), (1,2)]);
/// assert_eq!(p3.out_nbrs(1), vec![2]);
/// ```
pub fn out_nbrs(&self, v: usize) -> Vec<usize> {
self.0.out_nbrs(v)
}
/// In-neigborhood of `v`.
/// ```
/// use flag_algebra::flags::OrientedGraph;
/// let p3 = OrientedGraph::new(3, [(0,1), (1,2)]);
/// assert_eq!(p3.in_nbrs(1), vec![0]);
/// ```
pub fn in_nbrs(&self, v: usize) -> Vec<usize> {
self.0.in_nbrs(v)
}
/// Oriented relation between `u` to `v`.
/// ```
/// use flag_algebra::flags::{OrientedGraph, Arc};
/// let p3 = OrientedGraph::new(3, [(0,1), (1,2)]);
/// assert_eq!(p3.arc(0, 1), Arc::Edge);
/// assert_eq!(p3.arc(1, 0), Arc::BackEdge);
/// assert_eq!(p3.arc(0, 2), Arc::None);
/// ```
pub fn arc(&self, u: usize, v: usize) -> Arc {
self.0.arc(u, v)
}
fn is_triangle_free(&self) -> bool {
for u in 0..self.size() {
// Assume u is the largest vertex
for v in 0..u {
if self.arc(v, u) == Edge {
for w in 0..u {
if self.arc(u, w) == Edge && self.arc(w, v) == Edge {
return false;
}
}
}
}
}
true
}
/// Oriented graph obtained from `self` by adding a vertex and every edge
/// from the rest of the graph to that vertex.
pub fn add_sink(&self) -> Self {
let n = self.size();
let mut edge = self.0.edge.clone();
edge.resize(n + 1, None);
for v in 0..n {
edge.set((v, n), Edge);
}
Self(DirectedGraph { edge, size: n + 1 })
}
}
impl fmt::Display for DirectedGraph {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(V=[{}], E={{", self.size)?;
for u in 0..self.size {
for v in 0..self.size {
if v != u {
match self.edge.get(u, v) {
Edge => write!(f, " {u}->{v}")?,
Reciprocal if u < v => write!(f, " {u}<->{v}")?,
_ => (),
}
}
}
}
write!(f, " }})")
}
}
impl fmt::Display for OrientedGraph {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
fn check_vertex(u: usize, graph_size: usize) {
assert!(
u < graph_size,
"Invalid vertex {u}: the vertex set is {{0, ..., {}}}",
graph_size - 1
);
}
fn check_arc((u, v): (usize, usize), graph_size: usize) {
check_vertex(u, graph_size);
check_vertex(v, graph_size);
assert!(
u != v,
"Invalid arc ({u}, {v}): OrientedGraphs have no loop"
);
}
impl Canonize for DirectedGraph {
fn size(&self) -> usize {
self.size
}
fn invariant_neighborhood(&self, v: usize) -> Vec<Vec<usize>> {
assert!(v < self.size);
vec![self.out_nbrs(v), self.in_nbrs(v)]
}
fn apply_morphism(&self, p: &[usize]) -> Self {
self.induce(&combinatorics::invert(p))
}
}
impl Canonize for OrientedGraph {
fn size(&self) -> usize {
self.size()
}
fn invariant_neighborhood(&self, v: usize) -> Vec<Vec<usize>> {
self.0.invariant_neighborhood(v)
}
fn apply_morphism(&self, p: &[usize]) -> Self {
Self(self.0.apply_morphism(p))
}
}
impl Flag for DirectedGraph {
fn induce(&self, p: &[usize]) -> Self {
let k = p.len();
let mut res = Self::new(k, []);
for u1 in 0..k {
for u2 in 0..u1 {
res.edge.set((u1, u2), self.edge.get(p[u1], p[u2]));
}
}
res
}
const NAME: &'static str = "DirectedGraph";
fn size_zero_flags() -> Vec<Self> {
vec![Self::new(0, [])]
}
fn superflags(&self) -> Vec<Self> {
let mut res = Vec::new();
let mut iter = Functions::new(self.size, 4);
let arcs = [Edge, BackEdge, None, Reciprocal];
while let Some(f) = iter.next() {
res.push(extend(self, |v| arcs[f[v]]));
}
res
}
}
fn extend<F: Fn(usize) -> Arc>(g: &DirectedGraph, f: F) -> DirectedGraph {
let mut edge = g.edge.clone();
let n = g.size;
edge.resize(n + 1, None);
for v in 0..n {
edge.set((v, n), f(v));
}
DirectedGraph { size: n + 1, edge }
}
impl Flag for OrientedGraph {
fn induce(&self, p: &[usize]) -> Self {
Self(self.0.induce(p))
}
const NAME: &'static str = "OrientedGraph";
fn size_zero_flags() -> Vec<Self> {
vec![Self::empty(0)]
}
fn superflags(&self) -> Vec<Self> {
let mut res = Vec::new();
let mut iter = Functions::new(self.size(), 3);
let arcs = [Edge, BackEdge, None];
while let Some(f) = iter.next() {
res.push(Self(extend(&self.0, |v| arcs[f[v]])));
}
res
}
}
/// Indicator for oriented graph without directed triangle.
///
/// Makes `SubClass<OrientedGraph, TriangleFree>` usable as flags.
#[derive(Debug, Clone, Copy)]
pub enum TriangleFree {}
impl SubFlag<OrientedGraph> for TriangleFree {
const SUBCLASS_NAME: &'static str = "Triangle-free oriented graph";
fn is_in_subclass(flag: &OrientedGraph) -> bool {
flag.is_triangle_free()
}
}
impl<A> SubClass<OrientedGraph, A> {
pub fn size(&self) -> usize {
self.content.size()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() {
let g = DirectedGraph::new(3, [(0, 1), (1, 2), (2, 1)]);
assert_eq!(format!("{g}"), "(V=[3], E={ 0->1 1<->2 })");
}
#[test]
fn test_display_oriented() {
let g = OrientedGraph::new(2, [(0, 1)]);
assert_eq!(format!("{g}"), "(V=[2], E={ 0->1 })");
}
}