summaryrefslogtreecommitdiff
path: root/viz/src/decycle.rs
blob: 7bda9584bf1b43aace8461d849c8a846f5872e98 (plain)
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
//! This module implements a preprocessing of graphs that get rid of
//! the loops in the graph.  This step is needed because we want to
//! assign ranks to the nodes.

// REVIEW: Maybe we need no heuristic to break cycles here.  We can
// simply reverse all backward edges.  These are already found through
// Tarjan's algorithm.
//
// We need to experiment with this approach before determining whether
// or not to adapt a heuristic to reverse less edges.

use graph::{error::Error, Graph};
use std::borrow::Borrow;

// REFACTOR: Replace the use of 0 as the initial unused index by an
// Optional usize.  The compiler is almost surely going to compile the
// extra usage away, and the replaced code will be much more readable.

/// This function accepts a graph and returns a list of edges whose
/// direction should be reversed, in order to "break cycles".  That
/// is, this function returns a list of edges, and if we view the
/// edges on the list as reversed, then the graph would contain no
/// cycles.
///
/// This is adapted from Tarjan's algorithm for finding strongly
/// connected components, which is also implemented in the module
/// [strong_component][crate::strong_component].
pub fn decycle<B, G>(g: B) -> Result<Vec<(usize, usize)>, Error>
where
    B: Borrow<G>,
    G: Graph,
{
    let g = g.borrow();

    // List of edges to reverse
    let mut edges: Vec<(usize, usize)> = Vec::new();

    // List of depth levels of nodes
    let mut indices: Vec<usize> = vec![0; g.nodes_len()];

    indices.shrink_to_fit();

    // List of low link numbers of nodes
    let mut lowlinks: Vec<usize> = indices.clone();

    // The stack used in Trajan's algorithm
    let mut tarjan_stack: Vec<usize> = Vec::new();

    // The list of booleans to indicate whether a node is waiting on
    // the stack
    let mut waiting: Vec<bool> = vec![false; g.nodes_len()];

    waiting.shrink_to_fit();

    // a struct to help recursing

    #[derive(Debug)]
    enum StackElement {
        Seen(usize, Vec<usize>),
        Unseen(usize),
    }

    use StackElement::{Seen, Unseen};

    // convenient macros

    macro_rules! get_mut {
        ($arr:ident, $index:ident) => {
            *$arr.get_mut($index).unwrap()
        };
    }

    macro_rules! index {
        ($num: ident) => {
            indices.get($num).copied().unwrap()
        };
    }

    macro_rules! lowlink {
        ($num: ident) => {
            lowlinks.get($num).copied().unwrap()
        };
    }

    // The stack used to replace recursive function calls
    let mut recursive_stack: Vec<StackElement> = Vec::new();

    // The next index to assign
    let mut next_index: usize = 1;

    for node in g.nodes() {
        if indices.get(node).copied() == Some(0) {
            recursive_stack.push(Unseen(node));

            'recursion: while let Some(stack_element) = recursive_stack.pop() {
                let stack_node: usize;

                match stack_element {
                    Seen(node, children) => {
                        stack_node = node;

                        for child in children {
                            get_mut!(lowlinks, node) =
                                std::cmp::min(lowlink!(node), lowlink!(child));
                        }
                    }

                    Unseen(node) => {
                        stack_node = node;

                        tarjan_stack.push(node);

                        // It is safe to unwrap here since the
                        // condition of the if clause already serves
                        // as a guard.
                        get_mut!(indices, node) = next_index;
                        get_mut!(lowlinks, node) = next_index;
                        get_mut!(waiting, node) = true;

                        next_index += 1;

                        let mut node_index: Option<usize> = None;

                        for child in g.children_of(node)? {
                            // Ignore self-loops
                            if node == child {
                                continue;
                            }

                            match indices.get(child).copied() {
                                Some(0) => {
                                    // forward edge

                                    match node_index {
                                        Some(index) => match recursive_stack.get_mut(index) {
                                            Some(Seen(_, children)) => {
                                                children.push(child);
                                            }
                                            Some(_) => {
                                                unreachable!("wrong index: {index}");
                                            }
                                            None => {
                                                unreachable!("index {index} out of bounds");
                                            }
                                        },
                                        None => {
                                            node_index = Some(recursive_stack.len());

                                            let mut children = Vec::with_capacity(g.degree(node)?);
                                            children.push(child);

                                            recursive_stack.push(Seen(node, children));
                                        }
                                    }

                                    recursive_stack.push(Unseen(child));
                                }
                                Some(_) if waiting.get(child).copied().unwrap() => {
                                    // backward edge

                                    edges.push((node, child));

                                    get_mut!(lowlinks, node) =
                                        std::cmp::min(lowlink!(node), index!(child));
                                }
                                None => {
                                    return Err(Error::IndexOutOfBounds(child, g.nodes_len()));
                                }
                                _ => {
                                    // crossing edge
                                }
                            }
                        }

                        if node_index.is_some() {
                            continue 'recursion;
                        }
                    }
                }

                if lowlink!(stack_node) == index!(stack_node) {
                    while let Some(top) = tarjan_stack.pop() {
                        get_mut!(waiting, top) = false;

                        if top == stack_node {
                            break;
                        }
                    }
                }
            }
        }
    }

    Ok(edges)
}

#[cfg(test)]
mod test_decycle {
    use super::*;
    use graph::adlist::{ALGBuilder, ALGraph};
    use graph::builder::Builder;

    fn make_cycle(n: usize) -> Result<ALGraph, graph::error::Error> {
        let mut builder = ALGBuilder::default();

        builder.add_vertices(n);

        for i in 0..(n - 1) {
            builder.add_edge(i, i + 1, ())?;
        }

        builder.add_edge(n - 1, 0, ())?;

        Ok(builder.build())
    }

    fn make_two_cycles(n: usize) -> Result<ALGraph, graph::error::Error> {
        let mut builder = ALGBuilder::default();

        builder.add_vertices(2 * n);

        for i in 0..(2 * n - 1) {
            builder.add_edge(i, i + 1, ())?;
        }

        builder.add_edge(n - 1, 0, ())?;
        builder.add_edge(n - 2, 0, ())?; // random noise
        builder.add_edge(0, n - 1, ())?; // random noise
        builder.add_edge(0, 2 * n - 1, ())?; // random noise
        builder.add_edge(2 * n - 1, n, ())?;

        Ok(builder.build())
    }

    fn make_chain(n: usize) -> Result<ALGraph, graph::error::Error> {
        let mut builder = ALGBuilder::default();

        builder.add_vertices(n);

        for i in 0..(n - 1) {
            builder.add_edge(i, i + 1, ())?;
        }

        Ok(builder.build())
    }

    #[test]
    fn test_dechain() -> Result<(), Box<dyn std::error::Error>> {
        let length = 20;

        let chain = make_chain(length)?;

        // chain.print_viz("chain.gv")?;

        let edges = decycle::<_, ALGraph>(chain)?;

        println!("edges = {edges:?}");

        assert!(edges.is_empty());

        Ok(())
    }

    #[test]
    fn test_decycle() -> Result<(), Box<dyn std::error::Error>> {
        let length = 20;

        let cycle = make_cycle(length)?;

        // cycle.print_viz("cycle.gv")?;

        let edges = decycle::<_, ALGraph>(cycle)?;

        println!("edges = {edges:?}");

        assert_eq!(edges.len(), 1);
        assert_eq!(edges.first().copied(), Some((length - 1, 0)));

        Ok(())
    }

    #[test]
    fn test_de_two_components() -> Result<(), Box<dyn std::error::Error>> {
        let half_length = 10;

        let graph = make_two_cycles(half_length)?;

        // graph.print_viz("two cycles.gv")?;

        let edges = decycle::<_, ALGraph>(graph)?;

        println!("edges = {edges:?}");

        assert_eq!(edges.len(), 4);
        assert_eq!(
            edges.get(0).copied(),
            Some((2 * half_length - 2, 2 * half_length - 1))
        );
        assert_eq!(edges.get(1).copied(), Some((half_length - 1, 0)));
        assert_eq!(
            edges.get(2).copied(),
            Some((half_length - 2, half_length - 1))
        );
        assert_eq!(edges.get(3).copied(), Some((half_length - 2, 0)));

        Ok(())
    }
}