1pub fn detect_cycle_undirected(n: usize, edges: &[(usize, usize)]) -> Option<Vec<usize>> {
33 fn dfs(
34 curr: usize,
35 prev_edge: Option<usize>,
36 g: &[Vec<(usize, usize)>],
37 depth: &mut [Option<usize>],
38 path: &mut Vec<usize>,
39 ) -> Option<Vec<usize>> {
40 depth[curr] = Some(path.len());
41 for &(nxt, idx) in &g[curr] {
42 if Some(idx) == prev_edge {
43 continue;
44 }
45 if let Some(nxt_depth) = depth[nxt] {
46 let mut cycle = path[nxt_depth..].to_vec();
47 cycle.push(idx);
48 return Some(cycle);
49 }
50 path.push(idx);
51 if let Some(cycle) = dfs(nxt, Some(idx), g, depth, path) {
52 return Some(cycle);
53 }
54 path.pop();
55 }
56 None
57 }
58
59 let mut g = vec![vec![]; n];
60 for (idx, &(u, v)) in edges.iter().enumerate() {
61 g[u].push((v, idx));
62 g[v].push((u, idx));
63 }
64 let mut depth = vec![None; n];
65 let mut path = Vec::new();
66
67 for v in 0..n {
68 if depth[v].is_some() {
69 continue;
70 }
71 if let Some(cycle) = dfs(v, None, &g, &mut depth, &mut path) {
72 return Some(cycle);
73 }
74 }
75 None
76}
77
78pub fn detect_cycle_directed(n: usize, edges: &[(usize, usize)]) -> Option<Vec<usize>> {
101 fn dfs(
102 curr: usize,
103 g: &[Vec<(usize, usize)>],
104 seen: &mut Vec<bool>,
105 on_path: &mut Vec<bool>,
106 ) -> Option<(usize, Vec<usize>, bool)> {
107 seen[curr] = true;
108 on_path[curr] = true;
109 for &(nxt, idx) in &g[curr] {
110 if on_path[nxt] {
111 assert!(seen[nxt]);
112 return Some((nxt, vec![idx], true));
113 }
114 if seen[nxt] {
115 continue;
116 }
117 if let Some((start_node, mut cycle, in_cycle)) = dfs(nxt, g, seen, on_path) {
118 return if in_cycle {
119 cycle.push(idx);
120 if curr == start_node {
121 cycle.reverse();
122 Some((start_node, cycle, false))
123 } else {
124 Some((start_node, cycle, true))
125 }
126 } else {
127 Some((start_node, cycle, false))
128 };
129 }
130 }
131 on_path[curr] = false;
132 None
133 }
134
135 let mut g = vec![vec![]; n];
136 for (idx, &(u, v)) in edges.iter().enumerate() {
137 g[u].push((v, idx));
138 }
139 let mut seen = vec![false; n];
140 let mut on_path = vec![false; n];
141 for v in 0..n {
142 if seen[v] {
143 continue;
144 }
145 if let Some((_, cycle, in_cycle)) = dfs(v, &g, &mut seen, &mut on_path) {
146 assert!(!in_cycle);
147 return Some(cycle);
148 }
149 }
150 None
151}
152
153#[cfg(test)]
154mod tests {
155 use crate::{detect_cycle_directed, detect_cycle_undirected};
156
157 #[test]
158 fn test_undirected_triangle() {
159 let cycle = detect_cycle_undirected(3, &[(0, 2), (2, 1), (1, 0)]).unwrap();
160 assert!(
161 [
162 vec![0, 1, 2],
163 vec![1, 2, 0],
164 vec![2, 0, 1],
165 vec![0, 2, 1],
166 vec![2, 1, 0],
167 vec![1, 0, 2],
168 ]
169 .contains(&cycle)
170 );
171 }
172
173 #[test]
174 fn test_undirected_v() {
175 let cycle = detect_cycle_undirected(3, &[(0, 2), (0, 1)]);
176 assert_eq!(cycle, None);
177 }
178
179 #[test]
180 fn test_undirected_parallel_edges() {
181 let cycle = detect_cycle_undirected(2, &[(0, 1), (0, 1)]).unwrap();
182 assert!([vec![0, 1], vec![1, 0]].contains(&cycle));
183 }
184
185 #[test]
186 fn test_undirected_self_loop() {
187 let cycle = detect_cycle_undirected(1, &[(0, 0)]);
188 assert_eq!(cycle, Some(vec![0]));
189 }
190
191 #[test]
192 fn test_directed_triangle() {
193 let cycle = detect_cycle_directed(3, &[(0, 2), (2, 1), (1, 0)]);
194 assert_eq!(cycle, Some(vec![0, 1, 2]));
195 }
196
197 #[test]
198 fn test_directed_v() {
199 let cycle = detect_cycle_directed(3, &[(0, 2), (0, 1)]);
200 assert_eq!(cycle, None);
201 }
202}