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
//! `[Digraph]`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,
}

impl Neg for Arc {
    type Output = Self;

    fn neg(self) -> Self {
        match self {
            Edge => BackEdge,
            BackEdge => Edge,
            None => None,
        }
    }
}

use Arc::*;

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Serialize, Deserialize)]
/// Loopless oriented graphs.
///
/// Every pair of vertices has at most one edge.
/// The possible relations between two vertices is described by the type [`Arc`].
pub struct Digraph {
    /// Number of vertices.
    size: usize,
    /// Flat matrix of arcs.
    edge: AntiSym<Arc>,
}

impl Digraph {
    /// 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::Digraph;
    ///
    /// // Oriented path with 3 vertices `0 -> 1 -> 2`
    /// let p3 = Digraph::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 {
            size: n,
            edge: new_edge,
        }
    }
    /// Oriented graph with `n` vertices and no edge.
    /// ```
    /// use flag_algebra::flags::Digraph;
    /// assert_eq!(Digraph::empty(4), Digraph::new(4, []));
    /// ```
    pub fn empty(n: usize) -> Self {
        Self {
            size: n,
            edge: AntiSym::new(None, n),
        }
    }
    /// Number of vertices
    pub fn size(&self) -> usize {
        self.size
    }
    /// Out-neigborhood of `v`.
    /// ```
    /// use flag_algebra::flags::Digraph;
    /// let p3 = Digraph::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 && self.edge.get(u, v) == BackEdge {
                res.push(u);
            }
        }
        res
    }
    /// In-neigborhood of `v`.
    /// ```
    /// use flag_algebra::flags::Digraph;
    /// let p3 = Digraph::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 && self.edge.get(u, v) == Edge {
                res.push(u);
            }
        }
        res
    }
    /// Oriented relation between `u` to `v`.
    /// ```
    /// use flag_algebra::flags::{Digraph, Arc};
    /// let p3 = Digraph::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 {
        check_arc((u, v), self.size);
        self.edge.get(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.edge.get(v, u) == Edge {
                    for w in 0..u {
                        if self.edge.get(u, w) == Edge && self.edge.get(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.edge.clone();
        edge.resize(n + 1, None);
        for v in 0..n {
            edge.set((v, n), Edge);
        }
        Self { edge, size: n + 1 }
    }
}

impl fmt::Display for Digraph {
    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 && self.edge.get(u, v) == Edge {
                    if self.size < 10 {
                        write!(f, " {}{}", v, u)?;
                    } else {
                        write!(f, " {}-{}", v, u)?;
                    }
                }
            }
        }
        write!(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}): Digraph have no loop");
}

impl Canonize for Digraph {
    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 Flag for Digraph {
    fn induce(&self, p: &[usize]) -> Self {
        let k = p.len();
        let mut res = Self::empty(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 = "Digraph";

    fn size_zero_flags() -> Vec<Self> {
        vec![Self::empty(0)]
    }

    fn superflags(&self) -> Vec<Self> {
        let n = self.size;
        let mut res = Vec::new();
        let mut iter = Functions::new(n, 3);
        let arcs = [Edge, BackEdge, None];
        while let Some(f) = iter.next() {
            let mut edge = self.edge.clone();
            edge.resize(n + 1, None);
            for v in 0..n {
                edge.set((v, n), arcs[f[v]]);
            }
            res.push(Self { edge, size: n + 1 });
        }
        res
    }
}

/// Indicator for digraph without directed triangle.
///
/// Makes `SubClass<Digraph, TriangleFree>` usable as flags.
#[derive(Debug, Clone, Copy)]
pub enum TriangleFree {}

impl SubFlag<Digraph> for TriangleFree {
    const SUBCLASS_NAME: &'static str = "Triangle-free digraph";

    fn is_in_subclass(flag: &Digraph) -> bool {
        flag.is_triangle_free()
    }
}

impl<A> SubClass<Digraph, A> {
    pub fn size(&self) -> usize {
        self.content.size()
    }
}