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
//! Outerplanar graph predicate (ALGO-PR-129).
//!
//! A graph is outerplanar if it can be embedded in the plane with all
//! vertices on the outer face. Equivalently, a graph is outerplanar if
//! and only if it has no `K_4` minor and no `K_{2,3}` minor.
//!
//! We check: series-parallel (no `K_4` minor) + no `K_{2,3}` minor.
//! The `K_{2,3}` check uses a modified SP reduction that tags edges as
//! *original* or *synthetic* (created by series contraction). Three or
//! more synthetic parallel edges between the same pair reveal a
//! `K_{2,3}` minor.
//!
//! Every forest and every cycle is outerplanar. Every outerplanar
//! graph is series-parallel (but not vice versa: `K_{2,3}` is
//! series-parallel but not outerplanar).
//!
//! Directed graphs are treated as undirected.
use crate::algorithms::properties::is_series_parallel::is_series_parallel;
use crate::core::{Graph, IgraphResult};
/// Check whether a graph is outerplanar.
///
/// Uses the characterization: outerplanar iff no `K_4` minor
/// (series-parallel) and no `K_{2,3}` minor.
///
/// Directed graphs are treated as undirected.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_outerplanar};
///
/// // Cycle C_5: outerplanar
/// let mut g = Graph::with_vertices(5);
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(1, 2).unwrap();
/// g.add_edge(2, 3).unwrap();
/// g.add_edge(3, 4).unwrap();
/// g.add_edge(4, 0).unwrap();
/// assert!(is_outerplanar(&g).unwrap());
///
/// // K_4: NOT outerplanar (has K_4 minor)
/// let mut g = Graph::with_vertices(4);
/// for i in 0..4u32 {
/// for j in (i+1)..4 {
/// g.add_edge(i, j).unwrap();
/// }
/// }
/// assert!(!is_outerplanar(&g).unwrap());
/// ```
pub fn is_outerplanar(graph: &Graph) -> IgraphResult<bool> {
let n = graph.vcount();
if n <= 3 {
return is_series_parallel(graph);
}
if !is_series_parallel(graph)? {
return Ok(false);
}
// Quick global edge bound: outerplanar => edges <= 2n - 3.
let e = graph.ecount();
let n_usize = n as usize;
if e > 2 * n_usize - 3 {
return Ok(false);
}
has_no_k23_minor(graph)
}
/// Detect `K_{2,3}` minor via SP reduction with edge tagging.
///
/// Each edge is tagged as *original* (from the input graph) or
/// *synthetic* (created when a degree-2 vertex is series-contracted).
/// A synthetic edge represents a path with at least one internal
/// vertex. If three or more synthetic parallel edges accumulate
/// between any vertex pair, that certifies a `K_{2,3}` minor: the
/// two endpoints are the hubs, and one internal vertex from each of
/// the three paths forms the three branch vertices.
fn has_no_k23_minor(graph: &Graph) -> IgraphResult<bool> {
let n = graph.vcount() as usize;
// K_{2,3} minor needs >= 5 vertices.
if n < 5 {
return Ok(true);
}
let mut adj: Vec<Vec<(usize, bool)>> = vec![vec![]; n];
for v in 0..graph.vcount() {
let nbrs = graph.neighbors(v)?;
for &w in &nbrs {
let vv = v as usize;
let ww = w as usize;
if vv < ww {
adj[vv].push((ww, false));
adj[ww].push((vv, false));
}
}
}
Ok(!k23_reduce_check(&mut adj, n))
}
/// Returns `true` if a `K_{2,3}` minor is detected.
fn k23_reduce_check(adj: &mut [Vec<(usize, bool)>], n: usize) -> bool {
loop {
// Check for >= 3 synthetic parallel edges between any pair.
for edges in adj.iter() {
let mut syn_count = std::collections::HashMap::new();
for &(w, syn) in edges {
if syn {
*syn_count.entry(w).or_insert(0usize) += 1;
}
}
for &count in syn_count.values() {
if count >= 3 {
return true;
}
}
}
let mut changed = false;
// Peel vertices with <= 1 distinct neighbor.
for v in 0..n {
if adj[v].is_empty() {
continue;
}
let distinct = distinct_neighbors(&adj[v]);
if distinct.len() <= 1 {
changed = true;
for &w in &distinct {
adj[w].retain(|&(x, _)| x != v);
}
adj[v].clear();
}
}
// Series reduce: vertices with exactly 2 distinct neighbors.
for v in 0..n {
if adj[v].is_empty() {
continue;
}
let distinct = distinct_neighbors(&adj[v]);
if distinct.len() == 2 {
let a = distinct[0];
let b = distinct[1];
changed = true;
adj[v].clear();
adj[a].retain(|&(x, _)| x != v);
adj[b].retain(|&(x, _)| x != v);
adj[a].push((b, true));
adj[b].push((a, true));
}
}
if !changed {
break;
}
}
false
}
fn distinct_neighbors(edges: &[(usize, bool)]) -> Vec<usize> {
let mut d: Vec<usize> = edges.iter().map(|&(w, _)| w).collect();
d.sort_unstable();
d.dedup();
d
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn single_vertex() {
let g = Graph::with_vertices(1);
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn single_edge() {
let mut g = Graph::with_vertices(2);
g.add_edge(0, 1).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn triangle() {
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn path() {
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn cycle_c5() {
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();
g.add_edge(4, 0).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn k4_not_outerplanar() {
let mut g = Graph::with_vertices(4);
for i in 0..4u32 {
for j in (i + 1)..4 {
g.add_edge(i, j).unwrap();
}
}
assert!(!is_outerplanar(&g).unwrap());
}
#[test]
fn k23_not_outerplanar() {
// K_{2,3} is series-parallel but NOT outerplanar.
let mut g = Graph::with_vertices(5);
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(0, 4).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(1, 4).unwrap();
assert!(!is_outerplanar(&g).unwrap());
}
#[test]
fn diamond_outerplanar() {
// Diamond: K_4 minus one edge. 4 vertices, 5 edges. Outerplanar.
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn star() {
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(0, 4).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn tree() {
let mut g = Graph::with_vertices(6);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(3, 4).unwrap();
g.add_edge(3, 5).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn edgeless() {
let g = Graph::with_vertices(5);
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn maximal_outerplanar_5() {
// Triangulated pentagon: 7 = 2*5-3 edges.
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();
g.add_edge(4, 0).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn petersen_not_outerplanar() {
let mut g = Graph::with_vertices(10);
let edges = [
(0, 1),
(1, 2),
(2, 3),
(3, 4),
(4, 0),
(5, 7),
(7, 9),
(9, 6),
(6, 8),
(8, 5),
(0, 5),
(1, 6),
(2, 7),
(3, 8),
(4, 9),
];
for (u, v) in edges {
g.add_edge(u, v).unwrap();
}
assert!(!is_outerplanar(&g).unwrap());
}
#[test]
fn directed_treated_as_undirected() {
let mut g = Graph::new(5, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();
g.add_edge(4, 0).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn disconnected_outerplanar() {
let mut g = Graph::with_vertices(6);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
g.add_edge(3, 4).unwrap();
g.add_edge(4, 5).unwrap();
g.add_edge(5, 3).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
#[test]
fn subdivided_k23_not_outerplanar() {
// K_{2,3} with one edge subdivided: still not outerplanar.
// Hubs: 0, 1. Branches: 2, 3, 4. Edge 0-2 subdivided with vertex 5.
let mut g = Graph::with_vertices(6);
g.add_edge(0, 5).unwrap();
g.add_edge(5, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(0, 4).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(1, 4).unwrap();
assert!(!is_outerplanar(&g).unwrap());
}
#[test]
fn three_parallel_paths_with_direct_edge() {
// 0-1 direct + 0-2-1 + 0-3-4-1: outerplanar (only 2 K_{2,3} branches).
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(2, 1).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(3, 4).unwrap();
g.add_edge(4, 1).unwrap();
assert!(is_outerplanar(&g).unwrap());
}
}