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
405
use std::fmt;

use crate::utility::graph::{
    kosaraju_scc_with_filter,
    subgraph::{CFGraph, CFSubGraph},
};

use itertools::Itertools;
use petgraph::{
    algo::all_simple_paths,
    prelude::*,
    visit::{GraphBase, IntoEdgeReferences, IntoNeighborsDirected},
};

#[derive(Clone)]
pub struct BindedScc<'a> {
    pub graph_part: CFSubGraph<'a>,
    pub top_level: bool,
}

impl<'a> PartialEq for BindedScc<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.graph_part.nodes.eq(&other.graph_part.nodes)
    }
}
impl<'a> BindedScc<'a> {
    pub fn new(
        graph: &'a CFGraph,
        nodes: impl IntoIterator<Item = <CFGraph as GraphBase>::NodeId>,
        edges: impl IntoIterator<Item = <CFGraph as GraphBase>::EdgeId>,
        top_level: bool,
    ) -> Self {
        Self {
            graph_part: CFSubGraph::new(graph, nodes, edges),
            top_level,
        }
    }
    pub fn new_top_level_from_graph(graph: &'a CFGraph) -> Self {
        let nodes = graph.node_indices();
        let edges = graph.edge_indices();
        Self::new(graph, nodes, edges, true)
    }
    pub fn is_trivial(&self) -> bool {
        self.graph_part.nodes.len() == 1
    }
    pub fn contains(&self, node: usize) -> bool {
        self.graph_part.nodes.contains(&node.into())
    }
    pub fn edges(&self) -> Vec<(usize, usize)> {
        self.graph_part
            .edge_references()
            .filter(|edge| {
                self.graph_part.nodes.contains(&edge.source())
                    && self.graph_part.nodes.contains(&edge.target())
            })
            .map(|it| (it.source().index(), it.target().index()))
            .collect()
    }
    pub fn entry_edges(&self) -> Vec<(usize, usize)> {
        self.graph_part
            .graph
            .edge_references()
            .filter(|edge| {
                !self.graph_part.nodes.contains(&edge.source())
                    && self.graph_part.nodes.contains(&edge.target())
            })
            .map(|it| (it.source().index(), it.target().index()))
            .collect()
    }

    pub fn entry_nodes(&self) -> Vec<usize> {
        if self.top_level || self.graph_part.nodes.len() == 1 {
            vec![self.graph_part.nodes[0].index()]
        } else {
            self.entry_edges()
                .into_iter()
                .map(|(_, to)| to)
                .sorted()
                .dedup()
                .collect()
        }
    }

    pub fn edges_into_entry_nodes(&self) -> Vec<(usize, usize)> {
        self.graph_part
            .edge_references()
            .filter(|edge| self.entry_nodes().contains(&edge.target().index()))
            .map(|it| (it.source().index(), it.target().index()))
            .collect()
    }

    pub fn extern_edges_into_entry_nodes(&self) -> Vec<(usize, usize)> {
        self.graph_part
            .graph
            .edge_references()
            .filter(|edge| self.entry_nodes().contains(&edge.target().index()))
            .map(|it| (it.source().index(), it.target().index()))
            .collect()
    }

    pub fn reduciable(&self) -> bool {
        self.entry_nodes().len() == 1
    }

    /// Returns all top level sccs for a reducible subgraph.
    /// Return None if the subgraph is not reducible.
    pub fn top_level_sccs(&self) -> Option<Vec<Self>> {
        let entry_nodes = self.entry_nodes();
        if entry_nodes.len() != 1 {
            None
        } else {
            let entry_node = entry_nodes[0];
            let largest_simple_loop = self
                .graph_part
                .neighbors_directed(entry_node.into(), Incoming)
                .flat_map(|pred| {
                    all_simple_paths::<Vec<_>, _>(
                        &self.graph_part,
                        entry_node.into(),
                        pred,
                        0,
                        None,
                    )
                    .max_by(|a, b| a.len().cmp(&b.len()))
                })
                .max_by(|a, b| a.len().cmp(&b.len()));
            let backedge = if let Some(mut largest_simple_loops) = largest_simple_loop {
                let last_node = largest_simple_loops.pop().unwrap();
                self.graph_part.find_edge(last_node, entry_node.into())
            } else {
                None
            };
            let edges_without_backedge = if let Some(backedge) = backedge {
                self.graph_part
                    .edges
                    .iter()
                    .filter(|&&e| e != backedge)
                    .cloned()
                    .collect()
            } else {
                self.graph_part.edges.clone()
            };
            let sccs = kosaraju_scc_with_filter(
                &self.graph_part,
                entry_nodes[0].into(),
                |_| true,
                |e| Some(e) != backedge,
            );
            let result = sccs
                .into_iter()
                .map(|content| {
                    Self::new(
                        self.graph_part.graph,
                        content,
                        edges_without_backedge.clone(),
                        false,
                    )
                })
                .collect();
            Some(result)
        }
    }

    pub fn first_irreducible_sub_scc(&self) -> Option<Self> {
        if self.graph_part.nodes.len() == 1 {
            return None;
        } else if !self.reduciable() {
            return Some(self.clone());
        } else {
            // dbg!(self.top_level_sccs());
            // if self
            //     .top_level_sccs()
            //     .map(|it| it.len() != 2)
            //     .unwrap_or(false)
            // {
            //     return None;
            // }
            let sccs = self.top_level_sccs().unwrap();
            for scc in sccs {
                // if &scc == self {
                //     return Some(scc);
                // }
                if let Some(first_irreducible) = scc.first_irreducible_sub_scc() {
                    return Some(first_irreducible);
                }
            }
        }
        None
    }

    /// Returns the smallest non trivial (ie. not a single node) scc
    /// the node is in.
    pub fn smallest_non_trivial_scc_node_in(&self, node: usize) -> Option<Self> {
        if !self.contains(node) || self.is_trivial() {
            None
        } else if let Some(sub_sccs) = self.top_level_sccs() {
            for sub_scc in sub_sccs {
                if sub_scc.is_trivial() && sub_scc.contains(node) {
                    return Some(self.clone());
                } else if let Some(result) = sub_scc.smallest_non_trivial_scc_node_in(node) {
                    return Some(result);
                }
            }
            unreachable!()
        } else {
            debug_assert!(!self.reduciable());
            Some(self.clone())
        }
    }
}

impl<'a> fmt::Display for BindedScc<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Scc{{{:?}, top_level: {}}}",
            self.graph_part.nodes, self.top_level
        )
    }
}

impl<'a> fmt::Debug for BindedScc<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Scc{{{:?}, top_level: {}}}",
            self.graph_part.nodes, self.top_level
        )
    }
}

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

    #[test]
    fn test_top_level_scc() {
        let mut graph: DiGraph<_, _, usize> = DiGraph::default();
        let node_0 = graph.add_node(());
        let node_1 = graph.add_node(());
        let node_2 = graph.add_node(());
        let node_3 = graph.add_node(());
        let node_4 = graph.add_node(());
        let node_5 = graph.add_node(());
        let node_6 = graph.add_node(());
        let node_7 = graph.add_node(());
        let node_8 = graph.add_node(());
        let node_9 = graph.add_node(());
        let node_10 = graph.add_node(());
        graph.add_edge(node_0, node_1, ());
        graph.add_edge(node_1, node_2, ());
        graph.add_edge(node_2, node_3, ());
        graph.add_edge(node_2, node_7, ());
        graph.add_edge(node_3, node_4, ());
        graph.add_edge(node_3, node_8, ());
        graph.add_edge(node_7, node_5, ());
        graph.add_edge(node_7, node_9, ());
        graph.add_edge(node_9, node_8, ());
        graph.add_edge(node_8, node_9, ());
        graph.add_edge(node_5, node_2, ());
        graph.add_edge(node_4, node_6, ());
        graph.add_edge(node_6, node_2, ());
        graph.add_edge(node_6, node_10, ());
        graph.add_edge(node_10, node_6, ());
        graph.add_edge(node_10, node_4, ());

        let scc = BindedScc::new_top_level_from_graph(&graph);
        println!("{:?}", scc.top_level_sccs());
        println!(
            "first_irreducible_sub_scc={:?}",
            scc.first_irreducible_sub_scc()
        );
    }

    #[test]
    fn test_top_level_scc_recursive() {
        let mut graph: DiGraph<_, _, usize> = DiGraph::default();
        let node_0 = graph.add_node(());
        let node_1 = graph.add_node(());
        let node_2 = graph.add_node(());
        let node_3 = graph.add_node(());
        let node_4 = graph.add_node(());
        let node_5 = graph.add_node(());
        graph.add_edge(node_0, node_1, ());
        graph.add_edge(node_1, node_2, ());
        graph.add_edge(node_2, node_3, ());
        graph.add_edge(node_3, node_4, ());
        graph.add_edge(node_4, node_1, ());
        graph.add_edge(node_3, node_1, ());
        graph.add_edge(node_4, node_5, ());
        let scc = BindedScc::new_top_level_from_graph(&graph);
        let top_level_sccs = scc.top_level_sccs().unwrap();
        println!("{:?}", &top_level_sccs);
        let scc = &top_level_sccs[1];
        println!("{:?}", &scc);
        let top_level_sccs = scc.top_level_sccs().unwrap();
        println!("{:?}", &top_level_sccs);
        let scc = &top_level_sccs[0];
        let top_level_sccs = scc.top_level_sccs().unwrap();
        println!("{:?}", &top_level_sccs);
    }

    #[test]
    fn test_top_level_scc_recursive2() {
        let mut graph: DiGraph<_, _, usize> = DiGraph::default();
        let node_0 = graph.add_node(());
        let node_1 = graph.add_node(());
        let node_2 = graph.add_node(());
        let node_3 = graph.add_node(());
        let node_4 = graph.add_node(());
        let node_5 = graph.add_node(());
        let node_6 = graph.add_node(());
        graph.add_edge(node_0, node_1, ());
        graph.add_edge(node_1, node_2, ());
        graph.add_edge(node_2, node_3, ());
        graph.add_edge(node_3, node_4, ());
        graph.add_edge(node_4, node_5, ());
        graph.add_edge(node_3, node_6, ());
        graph.add_edge(node_6, node_1, ());
        graph.add_edge(node_4, node_6, ());

        let scc = BindedScc::new_top_level_from_graph(&graph);
        let top_level_sccs = scc.top_level_sccs().unwrap();
        println!("{:?}", &top_level_sccs);
        let scc = &top_level_sccs[1];
        let top_level_sccs = scc.top_level_sccs().unwrap();
        println!("{:?}", &top_level_sccs);
        // let scc = &top_level_sccs[0];
        // let top_level_sccs = scc.top_level_sccs().unwrap();
        // println!("{:?}", &top_level_sccs);
    }
    #[test]
    fn test_top_level_scc_recursive3() {
        let mut graph: DiGraph<_, _, usize> = DiGraph::default();
        let node_0 = graph.add_node(());
        let node_1 = graph.add_node(());
        let node_2 = graph.add_node(());
        let node_3 = graph.add_node(());
        let node_4 = graph.add_node(());
        let node_5 = graph.add_node(());
        let node_6 = graph.add_node(());
        graph.add_edge(node_0, node_1, ());
        graph.add_edge(node_1, node_2, ());
        graph.add_edge(node_2, node_3, ());
        graph.add_edge(node_3, node_1, ());
        graph.add_edge(node_1, node_4, ());
        graph.add_edge(node_4, node_5, ());
        graph.add_edge(node_5, node_1, ());
        graph.add_edge(node_3, node_6, ());

        let scc = BindedScc::new_top_level_from_graph(&graph);
        let top_level_sccs = scc.top_level_sccs().unwrap();
        println!("{:?}", &top_level_sccs);
        let scc = &top_level_sccs[1];
        let top_level_sccs = scc.top_level_sccs().unwrap();
        println!("{:?}", &top_level_sccs);
        // let scc = &top_level_sccs[0];
        // let top_level_sccs = scc.top_level_sccs().unwrap();
        // println!("{:?}", &top_level_sccs);
    }

    #[test]
    fn test_top_level_strange() {
        let mut graph: DiGraph<_, _, usize> = DiGraph::default();
        let node_0 = graph.add_node(());
        let node_1 = graph.add_node(());
        let node_2 = graph.add_node(());
        graph.add_edge(node_0, node_1, ());
        graph.add_edge(node_1, node_2, ());
        graph.add_edge(node_2, node_0, ());
        graph.add_edge(node_0, node_2, ());
        graph.add_edge(node_2, node_1, ());
        graph.add_edge(node_1, node_0, ());

        let scc = BindedScc::new_top_level_from_graph(&graph);
        println!("{:?}", scc.top_level_sccs());
        println!(
            "first_irreducible_sub_scc={:?}",
            scc.first_irreducible_sub_scc()
        );
    }

    #[test]
    fn test_top_level_strange_0() {
        let mut graph: DiGraph<_, _, usize> = DiGraph::default();
        let node_0 = graph.add_node(());
        let node_1 = graph.add_node(());
        let node_2 = graph.add_node(());
        let node_3 = graph.add_node(());
        graph.add_edge(node_0, node_1, ());
        graph.add_edge(node_1, node_2, ());
        graph.add_edge(node_1, node_3, ());
        graph.add_edge(node_2, node_3, ());
        graph.add_edge(node_3, node_1, ());
        graph.add_edge(node_2, node_1, ());
        graph.add_edge(node_3, node_2, ());

        let scc = BindedScc::new_top_level_from_graph(&graph);

        println!(
            "first_irreducible_sub_scc={:?}",
            scc.first_irreducible_sub_scc()
        );
    }
}