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
#![allow(non_snake_case)]

use std::fmt;

/// A `SparseGraph` represents an unweighted graph with `V` nodes and `E` edges using the
/// [compressed sparse row](https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)) format.
#[derive(Debug)]
pub struct SparseGraph<'a> {
    idxptr: &'a [usize],  // O(V)
    indices: &'a [usize], // O(E)
}

impl SparseGraph<'_> {
    /// Returns a sparse graph.
    ///
    /// # Arguments
    ///
    /// * `idxptr` - Starting index in `indices` for each vertex. These must be strictly non-decreasing
    /// and less than `indices.len`. The length defines the number of nodes in the graph, `V`
    /// * `indices` - Values in the range `0..V-1` indicating which nodes each vertex is linked to.
    /// The length defines the number edges in the graph, `E`.
    ///
    /// # Panics
    ///
    /// # Example
    ///
    /// ```
    /// use sparse_graph::SparseGraph;
    ///
    /// let (idxptr, indices) = (vec![0, 1, 3, 4, 5, 6], vec![1, 2, 3, 0, 4, 5, 3]);
    /// let g = SparseGraph::new(&idxptr, &indices);
    /// ```
    pub fn new<'a>(idxptr: &'a [usize], indices: &'a [usize]) -> SparseGraph<'a> {
        let edge_count = indices.len();
        if idxptr.is_empty() && idxptr[0] != 0 {
            panic!("Bad index ptr!");
        }
        if edge_count > 0 {
            for index in idxptr.iter() {
                if *index >= edge_count {
                    panic!("Bad index!");
                }
            }
        }
        SparseGraph { idxptr, indices }
    }

    /// Computes the strongly connected components using a memory optimised version of Tarjan's algorithm.
    ///
    /// See <http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.102.1707>.
    ///
    /// Returns
    ///
    /// # Example
    ///
    /// ```
    /// use sparse_graph::{SparseGraph, ConnectedComponents};
    ///
    /// // Create a graph with two strongly connect components
    /// let (idxptr, indices) = (vec![0, 1, 3, 4, 5, 6], vec![1, 2, 3, 0, 4, 5, 3]);
    /// let g = SparseGraph::new(&idxptr, &indices);
    /// let ConnectedComponents { n, labels, sparse_graph } = g.scc();
    /// assert_eq!(n, 2);
    /// assert_eq!(labels, [1, 1, 1, 0, 0, 0]);
    /// assert!(std::ptr::eq(sparse_graph, &g))
    /// ```
    pub fn scc(&self) -> ConnectedComponents {
        let N = self.idxptr.len();
        let END = N + 1;
        let VOID = N + 2;

        let mut lowlinks = vec![VOID; N];
        let mut stack_f = vec![VOID; N];
        let mut stack_b = vec![VOID; N];

        let mut label = N;
        let mut index = 0;
        let mut ss_head = END;

        // Iterate over every node in order
        for v1 in 0..N {
            // If not node hasn't been processed yet, it won't have a lowlink or a label
            if lowlinks[v1] == VOID {
                // DFS-stack push;
                // At this point, the DFS stack is empty, so pushing sets both the
                // forward and backwards pointers to end.
                let mut stack_head = v1;
                stack_f[v1] = END;
                stack_b[v1] = END;
                // We'll now proceed wih the inner loop algorithm until the stack is empty
                while stack_head != END {
                    let v = stack_head;
                    if lowlinks[v] == VOID {
                        // If the top node in the stack hasn't been visited yet,
                        // assign it the next index value.
                        lowlinks[v] = index;
                        index += 1;

                        // Visit all of the nodes accessible from v and push then onto the stack
                        // ahead of v. If they're already in the stack, bring them to the top.
                        let range_end = if v == N - 1 {
                            self.indices.len()
                        } else {
                            self.idxptr[v + 1]
                        };

                        for &w in &self.indices[self.idxptr[v]..range_end] {
                            if lowlinks[w] == VOID {
                                if stack_f[w] != VOID {
                                    // w is already inside the stack, so excise it.
                                    let f = stack_f[w];
                                    let b = stack_b[w];
                                    if b != END {
                                        stack_f[b] = f;
                                    }
                                    if f != END {
                                        stack_b[f] = b;
                                    }
                                }

                                // Add w to the top of the stack. end <-> w <-> stack_head <-> ...
                                stack_f[w] = stack_head;
                                stack_b[w] = END;
                                stack_b[stack_head] = w;
                                stack_head = w;
                            }
                        }
                    } else {
                        // DFS-stack pop
                        stack_head = stack_f[v];
                        // If the stack_head isn't the end
                        if stack_head < N {
                            stack_b[stack_head] = END;
                        }
                        stack_f[v] = VOID;
                        stack_b[v] = VOID;

                        // Find out whether this node is a root node
                        // We look at all its linked nodes (which have now all had this
                        // process applied to them!). If none of them have a lower index than this
                        // node then we have a root value. Otherwise we reset the index to the lowest
                        // index.
                        let mut root = true;
                        let mut low_v = lowlinks[v];
                        let range_end = if v == N - 1 {
                            self.indices.len()
                        } else {
                            self.idxptr[v + 1]
                        };
                        for &w in &self.indices[self.idxptr[v]..range_end] {
                            let low_w = lowlinks[w];
                            if low_w < low_v {
                                low_v = low_w;
                                root = false;
                            }
                        }
                        lowlinks[v] = low_v;
                        let ss = &mut stack_f;
                        if root {
                            // Found a root node. This means we've found the root of
                            // a strongly connected component. All the items on the stack
                            // with an index greater or equal to the current nodes index
                            // are part of the SCC and get the same level.
                            // We can reclaim their index values at this point.

                            // while S not empty and rindex[v] <= rindex[top[S]]
                            while ss_head != END && lowlinks[v] <= lowlinks[ss_head] {
                                let w = ss_head; // w = pop(S)
                                ss_head = ss[w];
                                ss[w] = VOID;
                                let labels = &mut lowlinks;
                                labels[w] = label; // rindex[w] = c;
                                index -= 1; // index = index - 1
                            }
                            let labels = &mut lowlinks;
                            if index > 0 {
                                index -= 1;
                            }
                            labels[v] = label; // rindex[v] = c

                            // Move to the next available label value
                            label -= 1;
                        } else {
                            // We haven't got to the root of this group, so add v to the sets stack
                            ss[v] = ss_head; // push(S, v)
                            ss_head = v;
                        }
                    }
                }
            }
        }
        let labels = &mut lowlinks;

        for label in labels.iter_mut() {
            *label = N - *label
        }
        ConnectedComponents {
            n: N - label,
            labels: lowlinks,
            sparse_graph: self,
        }
    }
}

/// A label map of the connected components of a graph.
pub struct ConnectedComponents<'a> {
    /// The sparse graph associated with the connected components.
    pub sparse_graph: &'a SparseGraph<'a>,
    /// The number of connected components.
    pub n: usize,
    /// A vector of labels from `0` to `n-1`. Corresponding nodes with the same label beloing to the same connected component.
    pub labels: Vec<usize>,
}

impl fmt::Display for SparseGraph<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "SparseGraph: {{").expect("");
        for i in 0..self.indices.len() {
            let start = self.indices[i];
            let end;
            if i == self.indices.len() - 1 {
                end = self.idxptr.len()
            } else {
                end = self.indices[i + 1];
            }
            let node = &self.idxptr[start..end];
            writeln!(f, "  Node {}: {:?}", i, node).expect("");
        }
        write!(f, "}}")
    }
}

#[derive(Debug)]
pub struct WeightedSparseGraph<'a, W> {
    /// Starting index in `indices` for each vertex. These are strictly non-decreasing
    /// and less than `indices.len`. Length is equal to the number of nodes in the graph, `V`.
    idxptr: &'a Vec<usize>,
    /// Values in the range `0..V-1` indicating which nodes each vertex is linked to.
    /// Length is equal to the number edges in the graph, `E`.
    indices: &'a Vec<usize>,

    // Edge weights. The weights correspond to the edges defined in `indices`.
    weights: &'a Vec<W>,
}

#[derive(Debug)]
pub struct ValuedSparseGraph<'a, V> {
    /// Starting index in `indices` for each vertex. These are strictly non-decreasing
    /// and less than `indices.len`. Length is equal to the number of nodes in the graph.
    idxptr: &'a Vec<usize>,
    /// Values `0..N-1` indicating which nodes each vertex is linked to.
    /// Length is equal to the number edges in the graph.
    indices: &'a Vec<usize>,

    // Vertex values. The values correspond to the values in `idxptr`.
    values: Vec<V>,
}

impl<'a, V> ValuedSparseGraph<'a, V> {
    pub fn as_sparse_graph(&self) -> SparseGraph {
        SparseGraph {
            indices: &self.indices,
            idxptr: &self.idxptr,
        }
    }
}

#[derive(Debug)]
pub struct WeightedValuedSparseGraph<'a, W, V> {
    /// Starting index in `indices` for each vertex. These are strictly non-decreasing
    /// and less than `indices.len`. Length is equal to the number of nodes in the graph.
    idxptr: &'a Vec<usize>,
    /// Values `0..N-1` indicating which nodes each vertex is linked to.
    /// Length is equal to the number edges in the graph.
    indices: &'a Vec<usize>,

    // Vertex values. The values correspond to the values in `idxptr`.
    values: &'a Vec<V>,

    // Edge weights. The weights correspond to the edges defined in `indices`.
    weights: &'a Vec<W>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sparse_graph_new() {
        let (a, b) = (vec![0, 1, 2], vec![1, 2, 0]);
        let g = SparseGraph::new(&a, &b);
        assert_eq!(*(g.idxptr), [0, 1, 2]);
        assert_eq!(*g.indices, [1, 2, 0]);
    }

    #[test]
    fn scc_1() {
        let (a, b) = (vec![0, 1, 2], vec![1, 2, 0]);
        let g = SparseGraph::new(&a, &b);
        let ConnectedComponents {
            n,
            labels,
            sparse_graph,
        } = g.scc();
        assert_eq!(n, 1);
        assert_eq!(labels, [0, 0, 0]);
        assert!(std::ptr::eq(sparse_graph, &g))
    }

    #[test]
    fn scc_2() {
        let (a, b) = (vec![0; 3], vec![]);
        let g = SparseGraph::new(&a, &b);
        dbg!(&g);
        let ConnectedComponents { n, labels, .. } = g.scc();
        assert_eq!(n, 3);
        assert_eq!(labels, [0, 1, 2]);
    }

    #[test]
    fn scc_3() {
        let (a, b) = (vec![0, 1, 3, 4, 5, 6], vec![1, 2, 3, 0, 4, 5, 3]);
        let g = SparseGraph::new(&a, &b);
        let ConnectedComponents { n, labels, .. } = g.scc();
        assert_eq!(n, 2);
        assert_eq!(labels, [1, 1, 1, 0, 0, 0]);
    }
}