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
//! Everything related to graphs: edges, nodes and, of course, graphs.
//!
//! The most important part of this module is [`Graph`](Graph).
//! However, if you want to manually build graphs,
//! you will need to use [`Edge`](Edge).

use crate::Sampler;
use indexmap::IndexSet;
use serde::{Deserialize, Serialize};

/// A (variable, constraint) pair.
///
/// Since variables and constraints are different sets of nodes,
/// it is possible to have an edge with the same value for variable and constraint.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)]
pub struct Edge {
    pub variable: usize,
    pub constraint: usize,
}

impl Edge {
    /// Creates a new edge for the given variable and constraint.
    pub fn new(variable: usize, constraint: usize) -> Self {
        Self {
            variable,
            constraint,
        }
    }
}

/// A bipartite regular graph.
///
/// A graph is a set of variables and constraints together with
/// a set of edges.
/// An edge is a (variable, constraint) pair.
///
/// A graph can be build manually or from a [`Sampler`](Sampler).
///
/// # Example
///
/// ```
/// use bigs::graph::{Edge, Graph};
///
/// let mut graph = Graph::new();
///
/// graph.insert_edge(Edge::new(0, 0)); // Edge between variable 0 and constraint 0.
/// graph.insert_edge(Edge::new(0, 1)); // Edge between variable 0 and constraint 1.
/// graph.insert_edge(Edge::new(1, 2)); // Edge between variable 1 and constraint 2.
/// graph.insert_edge(Edge::new(1, 3)); // Edge between variable 1 and constraint 3.
///
/// assert_eq!(graph.number_of_variables(), 2);
/// assert_eq!(graph.number_of_constraints(), 4);
/// assert_eq!(graph.number_of_edges(), 4);
///
/// for variable in graph.variables() {
///     assert_eq!(variable.degree(), 2);
/// }
///
/// for constraint in graph.constraints() {
///     assert_eq!(constraint.degree(), 1);
/// }
/// ```
///
/// # Performance tips
///
/// Don't use unnecessary large labels for variable and constraint.
/// The construction assume that you will use labels 0 to n - 1 if you
/// want n variables and the same for constraints.
///
/// If for whatever reason you want to assign a node with a large label,
/// notes that this will allocate the needed memory for all nodes up to that label.
///
/// ```
/// use bigs::graph::{Edge, Graph};
///
/// let mut graph = Graph::new();
/// graph.insert_edge(Edge::new(0, 42));
///
/// assert_eq!(graph.number_of_variables(), 1);
/// assert_eq!(graph.number_of_constraints(), 43);
/// ```
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Graph {
    variable_neighbors: Vec<IndexSet<usize>>,
    constraint_neighbors: Vec<IndexSet<usize>>,
    edges: IndexSet<Edge>,
}

impl Graph {
    /// Creates a new empty graph.
    pub fn new() -> Self {
        Self {
            variable_neighbors: Vec::new(),
            constraint_neighbors: Vec::new(),
            edges: IndexSet::new(),
        }
    }

    /// Checks if the given edge is in the graph.
    pub fn contains_edge(&self, edge: Edge) -> bool {
        self.edges.contains(&edge)
    }

    /// Inserts the given edge in the graph and returns true if the
    /// edge was not already in the graph.
    ///
    /// If the edge variable is greater or equal to the number of variables in the graph,
    /// the number of variables will be incremented by the difference.
    /// Same holds for constraints.
    ///
    /// # Example
    ///
    /// ```
    /// use bigs::graph::{Edge, Graph};
    ///
    /// let mut graph = Graph::new();
    /// assert_eq!(graph.number_of_variables(), 0);
    /// assert_eq!(graph.number_of_constraints(), 0);
    /// assert_eq!(graph.number_of_edges(), 0);
    ///
    /// graph.insert_edge(Edge::new(0, 0));
    /// assert_eq!(graph.number_of_variables(), 1);
    /// assert_eq!(graph.number_of_constraints(), 1);
    /// assert_eq!(graph.number_of_edges(), 1);
    ///
    /// graph.insert_edge(Edge::new(5, 6));
    /// assert_eq!(graph.number_of_variables(), 6);
    /// assert_eq!(graph.number_of_constraints(), 7);
    /// assert_eq!(graph.number_of_edges(), 2);
    ///
    /// assert_eq!(graph.insert_edge(Edge::new(0, 0)), false);
    /// ```
    pub fn insert_edge(&mut self, edge: Edge) -> bool {
        if self.edges.insert(edge) {
            self.insert_variable(edge);
            self.insert_constraint(edge);
            true
        } else {
            false
        }
    }

    fn insert_variable(&mut self, edge: Edge) {
        if edge.variable >= self.number_of_variables() {
            self.variable_neighbors.extend(vec![
                IndexSet::new();
                edge.variable - self.number_of_variables() + 1
            ]);
        }
        self.variable_neighbors[edge.variable].insert(edge.constraint);
    }

    fn insert_constraint(&mut self, edge: Edge) {
        if edge.constraint >= self.number_of_constraints() {
            self.constraint_neighbors.extend(vec![
                IndexSet::new();
                edge.constraint - self.number_of_constraints()
                    + 1
            ]);
        }
        self.constraint_neighbors[edge.constraint].insert(edge.variable);
    }

    /// Removes the given edge from the graph if it exists and returns true.
    /// Else, returns false.
    ///
    /// However, this do not update the number of variables and constraints in the graph.
    ///
    /// # Example
    ///
    /// ```
    /// use bigs::graph::{Edge, Graph};
    ///
    /// let mut graph = Graph::new();
    /// assert_eq!(graph.number_of_variables(), 0);
    /// assert_eq!(graph.number_of_constraints(), 0);
    /// assert_eq!(graph.number_of_edges(), 0);
    ///
    /// graph.insert_edge(Edge::new(0, 0));
    /// assert_eq!(graph.number_of_variables(), 1);
    /// assert_eq!(graph.number_of_constraints(), 1);
    /// assert_eq!(graph.number_of_edges(), 1);
    ///
    /// graph.remove_edge(Edge::new(0, 0));
    /// assert_eq!(graph.number_of_variables(), 1);
    /// assert_eq!(graph.number_of_constraints(), 1);
    /// assert_eq!(graph.number_of_edges(), 0);
    /// ```
    pub fn remove_edge(&mut self, edge: Edge) -> bool {
        if self.edges.remove(&edge) {
            self.variable_neighbors[edge.variable].remove(&edge.constraint);
            self.constraint_neighbors[edge.constraint].remove(&edge.variable);
            true
        } else {
            false
        }
    }

    /// Returns an iterator over all edges in the graph in some possibly random order.
    pub fn edges(&self) -> impl Iterator<Item = Edge> + '_ {
        self.edges.iter().cloned()
    }

    /// Returns the number of variables in the graph.
    ///
    /// That is, the one more than the highest variable label inserted in the graph.
    pub fn number_of_variables(&self) -> usize {
        self.variable_neighbors.len()
    }

    /// Returns the number of constraints in the graph.
    ///
    /// That is, the one more than the highest constraint label inserted in the graph.
    pub fn number_of_constraints(&self) -> usize {
        self.constraint_neighbors.len()
    }

    /// Returns the number of edges in the graph.
    pub fn number_of_edges(&self) -> usize {
        self.edges.len()
    }

    /// Returns an iterator over all variables in the graph in increasing label order.
    ///
    /// # Example
    ///
    /// ```
    /// use bigs::graph::{Edge, Graph};
    /// use indexmap::indexset;
    ///
    /// let mut graph = Graph::new();
    ///
    /// graph.insert_edge(Edge::new(0, 0)); // Edge between variable 0 and constraint 0.
    /// graph.insert_edge(Edge::new(0, 1)); // Edge between variable 0 and constraint 1.
    /// graph.insert_edge(Edge::new(1, 2)); // Edge between variable 1 and constraint 2.
    /// graph.insert_edge(Edge::new(1, 3)); // Edge between variable 1 and constraint 3.
    ///
    /// let mut iter = graph.variables();
    ///
    /// let first_variable = iter.next().unwrap();
    /// assert_eq!(first_variable.label(), 0);
    /// assert_eq!(first_variable.degree(), 2);
    /// assert_eq!(first_variable.neighbors(), &indexset! { 0, 1 });
    ///
    /// let second_variable = iter.next().unwrap();
    /// assert_eq!(second_variable.label(), 1);
    /// assert_eq!(second_variable.degree(), 2);
    /// assert_eq!(second_variable.neighbors(), &indexset! { 2, 3 });
    ///
    /// assert!(iter.next().is_none());
    /// ```
    pub fn variables(&self) -> Nodes {
        Nodes {
            iter: self.variable_neighbors.iter().enumerate(),
            kind: NodeKind::Variable,
        }
    }

    /// Returns an iterator over all constraints in the graph in increasing label order.
    ///
    /// # Example
    ///
    /// ```
    /// use bigs::graph::{Edge, Graph};
    /// use indexmap::indexset;
    ///
    /// let mut graph = Graph::new();
    ///
    /// graph.insert_edge(Edge::new(0, 0)); // Edge between variable 0 and constraint 0.
    /// graph.insert_edge(Edge::new(0, 1)); // Edge between variable 0 and constraint 1.
    /// graph.insert_edge(Edge::new(1, 2)); // Edge between variable 1 and constraint 2.
    /// graph.insert_edge(Edge::new(1, 3)); // Edge between variable 1 and constraint 3.
    ///
    /// let mut iter = graph.constraints();
    ///
    /// let first_constraint = iter.next().unwrap();
    /// assert_eq!(first_constraint.label(), 0);
    /// assert_eq!(first_constraint.degree(), 1);
    /// assert_eq!(first_constraint.neighbors(), &indexset! { 0 });
    ///
    /// let second_constraint = iter.next().unwrap();
    /// assert_eq!(second_constraint.label(), 1);
    /// assert_eq!(second_constraint.degree(), 1);
    /// assert_eq!(second_constraint.neighbors(), &indexset! { 0 });
    ///
    /// assert!(iter.next().is_some());
    /// assert!(iter.next().is_some());
    /// assert!(iter.next().is_none());
    /// ```
    pub fn constraints(&self) -> Nodes {
        Nodes {
            iter: self.constraint_neighbors.iter().enumerate(),
            kind: NodeKind::Constraint,
        }
    }

    pub(crate) fn from_sampler(sampler: &Sampler) -> Self {
        Self {
            variable_neighbors: vec![
                IndexSet::with_capacity(sampler.variable_degree());
                sampler.number_of_variables()
            ],
            constraint_neighbors: vec![
                IndexSet::with_capacity(sampler.constraint_degree());
                sampler.number_of_constraints()
            ],
            edges: IndexSet::with_capacity(sampler.number_of_edges()),
        }
    }
}

/// An iterator for a set of nodes in a graph.
///
/// This is created via the [`Graph::variables`](Graph::variables)
/// or the [`Graph::constraints`](Graph::constraints) methods.
pub struct Nodes<'g> {
    iter: std::iter::Enumerate<std::slice::Iter<'g, IndexSet<usize>>>,
    kind: NodeKind,
}

impl<'g> Iterator for Nodes<'g> {
    type Item = Node<'g>;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|(label, neighbors)| Node {
            neighbors,
            label,
            kind: self.kind,
        })
    }
}

/// A node in the graph.
///
/// This is used to iterates throught the nodes of a graph.
pub struct Node<'g> {
    neighbors: &'g IndexSet<usize>,
    label: usize,
    kind: NodeKind,
}

impl<'g> Node<'g> {
    /// Returns the set of labels of the neighbors of the node.
    pub fn neighbors(&self) -> &IndexSet<usize> {
        self.neighbors
    }

    /// Returns the label of the node.
    pub fn label(&self) -> usize {
        self.label
    }

    /// Returns the degree of the node.
    /// That is, the number of neighbors.
    pub fn degree(&self) -> usize {
        self.neighbors.len()
    }

    /// Checks if a node a neighbor with the given label
    pub fn has_neighbor(&self, label: usize) -> bool {
        self.neighbors.contains(&label)
    }

    /// Checks if a node is a variable.
    pub fn is_variable(&self) -> bool {
        self.kind == NodeKind::Variable
    }

    /// Checks if a node is a constraint.
    pub fn is_constraint(&self) -> bool {
        self.kind == NodeKind::Constraint
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum NodeKind {
    Variable,
    Constraint,
}