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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

use hashbrown::{HashMap, HashSet};
use std::hash::Hash;

use petgraph::{
    visit::{
        EdgeCount, GraphBase, GraphProp, IntoEdges, IntoNodeIdentifiers, NodeIndexable, Time,
        Visitable,
    },
    Undirected,
};

use crate::traversal::{depth_first_search, DfsEvent};

const NULL: usize = std::usize::MAX;

type Edge<G> = (<G as GraphBase>::NodeId, <G as GraphBase>::NodeId);

#[inline]
fn is_root(parent: &[usize], u: usize) -> bool {
    parent[u] == NULL
}

fn _articulation_points<G>(
    graph: G,
    components: Option<&mut HashMap<Edge<G>, usize>>,
    bridges: Option<&mut HashSet<Edge<G>>>,
) -> HashSet<G::NodeId>
where
    G: GraphProp<EdgeType = Undirected>
        + EdgeCount
        + IntoEdges
        + Visitable
        + NodeIndexable
        + IntoNodeIdentifiers,
    G::NodeId: Eq + Hash,
{
    let num_nodes = graph.node_bound();

    let mut low = vec![NULL; num_nodes];
    let mut disc = vec![NULL; num_nodes];
    let mut parent = vec![NULL; num_nodes];

    let mut root_children: usize = 0;
    let mut points = HashSet::new();

    let mut edge_stack = Vec::new();
    let need_components = components.is_some();
    let mut tmp_components = if need_components {
        HashMap::with_capacity(graph.edge_count())
    } else {
        HashMap::new()
    };
    let need_bridges = bridges.is_some();
    let mut tmp_bridges = if need_bridges {
        HashSet::with_capacity(graph.edge_count())
    } else {
        HashSet::new()
    };
    let mut num_components: usize = 0;

    depth_first_search(graph, graph.node_identifiers(), |event| match event {
        DfsEvent::Discover(u_id, Time(t)) => {
            let u = graph.to_index(u_id);
            low[u] = t;
            disc[u] = t;
        }
        DfsEvent::TreeEdge(u_id, v_id, _) => {
            let u = graph.to_index(u_id);
            let v = graph.to_index(v_id);
            parent[v] = u;
            if is_root(&parent, u) {
                root_children += 1;
            }
            if need_components {
                edge_stack.push((u_id, v_id));
            }
        }
        DfsEvent::BackEdge(u_id, v_id, _) => {
            let u = graph.to_index(u_id);
            let v = graph.to_index(v_id);

            // do *not* consider ``(u, v)`` as a back edge if ``(v, u)`` is a tree edge.
            if v != parent[u] {
                low[u] = low[u].min(disc[v]);
                if need_components {
                    edge_stack.push((u_id, v_id));
                }
            }
        }
        DfsEvent::Finish(u_id, _) => {
            let u = graph.to_index(u_id);
            if is_root(&parent, u) {
                if root_children > 1 {
                    points.insert(u_id);
                }
                // restart ``root_children`` for the remaining connected components
                root_children = 0;
            } else {
                let pu = parent[u];
                let pu_id = graph.from_index(pu);
                low[pu] = low[pu].min(low[u]);

                if !is_root(&parent, pu) && low[u] >= disc[pu] {
                    points.insert(pu_id);
                    // now find a biconnected component that the
                    // current articulation point belongs.
                    if need_components {
                        if let Some(at) = edge_stack.iter().rposition(|&x| x == (pu_id, u_id)) {
                            tmp_components.extend(
                                edge_stack[at..].iter().map(|edge| (*edge, num_components)),
                            );
                            edge_stack.truncate(at);
                            num_components += 1;
                        }
                    }
                    if need_bridges && low[u] != disc[pu] {
                        tmp_bridges.insert((pu_id, u_id));
                    }
                }

                if is_root(&parent, pu) && need_components {
                    if let Some(at) = edge_stack.iter().position(|&x| x == (pu_id, u_id)) {
                        tmp_components
                            .extend(edge_stack[at..].iter().map(|edge| (*edge, num_components)));
                        edge_stack.truncate(at);
                        num_components += 1;
                    }
                }
            }
        }
        _ => {}
    });

    if let Some(x) = components {
        *x = tmp_components;
    }
    if let Some(x) = bridges {
        *x = tmp_bridges;
    }

    points
}

/// Return the articulation points of an undirected graph.
///
/// An articulation point or cut vertex is any node whose removal (along with
/// all its incident edges) increases the number of connected components of
/// a graph. An undirected connected graph without articulation points is
/// biconnected.
///
/// At the same time, you can record the biconnected components in `components`.
///
/// Biconnected components are maximal subgraphs such that the removal
/// of a node (and all edges incident on that node) will not disconnect
/// the subgraph. Note that nodes may be part of more than one biconnected
/// component. Those nodes are articulation points, or cut vertices. The
/// algorithm computes how many biconnected components are in the graph,
/// and assigning each component an integer label.
///
/// # Note
/// The function implicitly assumes that there are no parallel edges
/// or self loops. It may produce incorrect/unexpected results if the
/// input graph has self loops or parallel edges.
///
///
/// # Example:
/// ```rust
/// use std::iter::FromIterator;
/// use hashbrown::{HashMap, HashSet};
///
/// use rustworkx_core::connectivity::articulation_points;
/// use rustworkx_core::petgraph::graph::UnGraph;
/// use rustworkx_core::petgraph::graph::node_index as nx;
///
/// let graph = UnGraph::<(), ()>::from_edges(&[
///    (0, 1), (0, 2), (1, 2), (1, 3),
/// ]);
///
/// let mut bicomp = HashMap::new();
/// let a_points = articulation_points(&graph, Some(&mut bicomp));
///
/// assert_eq!(a_points, HashSet::from_iter([nx(1)]));
/// assert_eq!(bicomp, HashMap::from_iter([
///     ((nx(0), nx(2)), 1), ((nx(2), nx(1)), 1), ((nx(1), nx(0)), 1),
///     ((nx(1), nx(3)), 0)
/// ]));
/// ```
pub fn articulation_points<G>(
    graph: G,
    components: Option<&mut HashMap<Edge<G>, usize>>,
) -> HashSet<G::NodeId>
where
    G: GraphProp<EdgeType = Undirected>
        + EdgeCount
        + IntoEdges
        + Visitable
        + NodeIndexable
        + IntoNodeIdentifiers,
    G::NodeId: Eq + Hash,
{
    _articulation_points(graph, components, None)
}

/// Return the bridges of an undirected graph.
///
/// Bridges are edges that, if removed, would increase the number of
/// connected components of a graph.
///
/// # Note
/// The function implicitly assumes that there are no parallel edges
/// or self loops. It may produce incorrect/unexpected results if the
/// input graph has self loops or parallel edges.
///
///
/// # Example:
/// ```rust
/// use std::iter::FromIterator;
/// use hashbrown::{HashMap, HashSet};
///
/// use rustworkx_core::connectivity::bridges;
/// use rustworkx_core::petgraph::graph::UnGraph;
/// use rustworkx_core::petgraph::graph::node_index as nx;
///
/// let graph = UnGraph::<(), ()>::from_edges(&[
///    (0, 1), (0, 2), (1, 2), (1, 3),
/// ]);
///
/// let bridges = bridges(&graph);
///
/// assert_eq!(bridges, HashSet::from_iter([(nx(1), nx(3))]));
/// ```
pub fn bridges<G>(graph: G) -> HashSet<Edge<G>>
where
    G: GraphProp<EdgeType = Undirected>
        + EdgeCount
        + IntoEdges
        + Visitable
        + NodeIndexable
        + IntoNodeIdentifiers,
    G::NodeId: Eq + Hash,
{
    let mut bridges = HashSet::new();
    _articulation_points(graph, None, Some(&mut bridges));
    bridges
}

#[cfg(test)]
mod tests {
    use crate::connectivity::{articulation_points, bridges};
    use hashbrown::{HashMap, HashSet};
    use petgraph::graph::node_index as nx;
    use petgraph::prelude::*;
    use std::iter::FromIterator;

    #[test]
    fn test_articulation_points_repetitions() {
        let graph = UnGraph::<(), ()>::from_edges([(0, 1), (1, 2), (1, 3)]);

        let a_points = articulation_points(&graph, None);

        assert_eq!(a_points, HashSet::from_iter([nx(1)]));
    }

    #[test]
    fn test_articulation_points_cycle() {
        // create a cycle graph
        let graph = UnGraph::<(), ()>::from_edges([(0, 1), (1, 2), (2, 0), (1, 3), (3, 4), (4, 1)]);

        let a_points = articulation_points(&graph, None);

        assert_eq!(a_points, HashSet::from_iter([nx(1)]));
    }

    #[test]
    fn test_single_bridge() {
        let graph = UnGraph::<(), ()>::from_edges([
            (1, 2),
            (2, 3),
            (3, 4),
            (3, 5),
            (5, 6),
            (6, 7),
            (7, 8),
            (5, 9),
            (9, 10),
            // Nontree edges.
            (1, 3),
            (1, 4),
            (2, 5),
            (5, 10),
            (6, 8),
        ]);

        assert_eq!(bridges(&graph), HashSet::from_iter([(nx(5), nx(6))]));
    }

    #[test]
    // generate test cases for bridges
    fn test_bridges_cycle() {
        let graph = UnGraph::<(), ()>::from_edges([(0, 1), (1, 2), (2, 0), (1, 3), (3, 4), (4, 1)]);
        assert_eq!(bridges(&graph), HashSet::from_iter([]));
    }

    #[test]
    fn test_biconnected_components_cycle() {
        // create a cycle graph
        let graph = UnGraph::<(), ()>::from_edges([(0, 1), (1, 2), (2, 0), (1, 3), (3, 4), (4, 1)]);

        let mut components = HashMap::new();
        let _ = articulation_points(&graph, Some(&mut components));

        assert_eq!(
            components,
            HashMap::from_iter([
                ((nx(1), nx(3)), 0),
                ((nx(3), nx(4)), 0),
                ((nx(4), nx(1)), 0),
                ((nx(1), nx(2)), 1),
                ((nx(2), nx(0)), 1),
                ((nx(0), nx(1)), 1)
            ])
        );
    }

    #[test]
    fn test_biconnected_components1() {
        // exmaple from https://web.archive.org/web/20121229123447/http://www.ibluemojo.com/school/articul_algorithm.html
        let graph = UnGraph::<(), ()>::from_edges([
            (0, 1),
            (0, 5),
            (0, 6),
            (0, 14),
            (1, 5),
            (1, 6),
            (1, 14),
            (2, 4),
            (2, 10),
            (3, 4),
            (3, 15),
            (4, 6),
            (4, 7),
            (4, 10),
            (5, 14),
            (6, 14),
            (7, 9),
            (8, 9),
            (8, 12),
            (8, 13),
            (10, 15),
            (11, 12),
            (11, 13),
            (12, 13),
        ]);

        let mut components = HashMap::new();
        let a_points = articulation_points(&graph, Some(&mut components));

        assert_eq!(
            a_points,
            HashSet::from_iter([nx(4), nx(6), nx(7), nx(8), nx(9)])
        );
        assert_eq!(
            components,
            HashMap::from_iter([
                ((nx(3), nx(4)), 0),
                ((nx(15), nx(3)), 0),
                ((nx(10), nx(15)), 0),
                ((nx(4), nx(10)), 0),
                ((nx(10), nx(2)), 0),
                ((nx(2), nx(4)), 0),
                ((nx(13), nx(12)), 1),
                ((nx(8), nx(13)), 1),
                ((nx(11), nx(13)), 1),
                ((nx(12), nx(11)), 1),
                ((nx(12), nx(8)), 1),
                ((nx(9), nx(8)), 2),
                ((nx(7), nx(9)), 3),
                ((nx(4), nx(7)), 4),
                ((nx(6), nx(4)), 5),
                ((nx(0), nx(14)), 6),
                ((nx(1), nx(5)), 6),
                ((nx(5), nx(0)), 6),
                ((nx(5), nx(14)), 6),
                ((nx(1), nx(14)), 6),
                ((nx(14), nx(6)), 6),
                ((nx(6), nx(0)), 6),
                ((nx(6), nx(1)), 6),
                ((nx(1), nx(0)), 6),
            ])
        )
    }

    #[test]
    fn test_biconnected_components2() {
        let mut graph: Graph<&str, (), Undirected> = Graph::new_undirected();
        let a = graph.add_node("A");
        let b = graph.add_node("B");
        let c = graph.add_node("C");
        let d = graph.add_node("D");
        let e = graph.add_node("E");
        let f = graph.add_node("F");
        let g = graph.add_node("G");
        let h = graph.add_node("H");
        let i = graph.add_node("I");
        let j = graph.add_node("J");

        graph.extend_with_edges([
            (a, b),
            (b, c),
            (c, a),
            (c, d),
            (d, e),
            (e, c),
            (f, i),
            (i, j),
            (j, h),
            (h, g),
            (g, f),
            (g, i),
            (g, j),
            (e, g),
        ]);

        let mut components = HashMap::new();
        let _ = articulation_points(&graph, Some(&mut components));

        assert_eq!(
            components,
            HashMap::from_iter([
                ((f, g), 0),
                ((i, f), 0),
                ((i, g), 0),
                ((j, i), 0),
                ((g, j), 0),
                ((j, h), 0),
                ((h, g), 0),
                ((e, g), 1),
                ((c, d), 2),
                ((d, e), 2),
                ((e, c), 2),
                ((c, a), 3),
                ((a, b), 3),
                ((b, c), 3),
            ])
        )
    }

    #[test]
    fn test_null_graph() {
        let graph: Graph<(), (), Undirected> = Graph::new_undirected();

        let mut components = HashMap::new();
        let a_points = articulation_points(&graph, Some(&mut components));
        let bridges = bridges(&graph);

        assert_eq!(a_points, HashSet::new());
        assert_eq!(bridges, HashSet::new());
        assert_eq!(components, HashMap::new());
    }
}