rustworkx_core/connectivity/
isolates.rs1use petgraph::visit::{IntoNeighborsDirected, IntoNodeIdentifiers, NodeIndexable};
14use petgraph::Direction::{Incoming, Outgoing};
15
16pub fn isolates<G>(graph: G) -> Vec<G::NodeId>
51where
52 G: NodeIndexable + IntoNodeIdentifiers + IntoNeighborsDirected,
53{
54 graph
55 .node_identifiers()
56 .filter(|x| {
57 graph
58 .neighbors_directed(*x, Incoming)
59 .chain(graph.neighbors_directed(*x, Outgoing))
60 .next()
61 .is_none()
62 })
63 .collect()
64}
65
66#[cfg(test)]
67mod tests {
68 use crate::connectivity::isolates;
69 use petgraph::prelude::*;
70
71 #[test]
72 fn test_isolates_directed_empty() {
73 let graph = DiGraph::<i32, i32>::new();
74 let res: Vec<NodeIndex> = isolates(&graph);
75 assert_eq!(res, []);
76 }
77
78 #[test]
79 fn test_isolates_undirected_empty() {
80 let graph = UnGraph::<i32, i32>::default();
81 let res: Vec<NodeIndex> = isolates(&graph);
82 assert_eq!(res, []);
83 }
84
85 #[test]
86 fn test_isolates_directed_no_isolates() {
87 let graph = DiGraph::<i32, i32>::from_edges([(0, 1), (1, 2)]);
88 let res: Vec<NodeIndex> = isolates(&graph);
89 assert_eq!(res, []);
90 }
91
92 #[test]
93 fn test_isolates_undirected_no_isolates() {
94 let graph = UnGraph::<i32, i32>::from_edges([(0, 1), (1, 2)]);
95 let res: Vec<NodeIndex> = isolates(&graph);
96 assert_eq!(res, []);
97 }
98
99 #[test]
100 fn test_isolates_directed() {
101 let edge_list = vec![
102 (0, 1),
103 (3, 0),
104 (0, 5),
105 (8, 0),
106 (1, 2),
107 (1, 6),
108 (2, 3),
109 (3, 4),
110 (4, 5),
111 (6, 7),
112 (7, 8),
113 (8, 9),
114 ];
115 let mut graph = DiGraph::<i32, i32>::from_edges(edge_list);
116 graph.add_node(10);
117 graph.add_node(11);
118 let res: Vec<usize> = isolates(&graph).into_iter().map(|x| x.index()).collect();
119 assert_eq!(res, [10, 11])
120 }
121
122 #[test]
123 fn test_isolates_undirected() {
124 let edge_list = vec![
125 (0, 1),
126 (3, 0),
127 (0, 5),
128 (8, 0),
129 (1, 2),
130 (1, 6),
131 (2, 3),
132 (3, 4),
133 (4, 5),
134 (6, 7),
135 (7, 8),
136 (8, 9),
137 ];
138 let mut graph = UnGraph::<i32, i32>::from_edges(edge_list);
139 graph.add_node(10);
140 graph.add_node(11);
141 let res: Vec<usize> = isolates(&graph).into_iter().map(|x| x.index()).collect();
142 assert_eq!(res, [10, 11])
143 }
144}