|
| 1 | +// Copyright 2019 Gary Pennington |
| 2 | + |
| 3 | +use crate::graph::Graph; |
| 4 | +use crate::vertex_id::VertexId; |
| 5 | + |
| 6 | +use hashbrown::HashMap; |
| 7 | + |
| 8 | +#[cfg(feature = "no_std")] |
| 9 | +extern crate alloc; |
| 10 | +#[cfg(feature = "no_std")] |
| 11 | +use alloc::vec::Vec; |
| 12 | + |
| 13 | +const PANIC_MSG: &str = "graph contains cycle(s)"; |
| 14 | + |
| 15 | +#[derive(Debug)] |
| 16 | +/// Topological Iterator |
| 17 | +pub struct Topo<'a, T> { |
| 18 | + /// The Graph being iterated. |
| 19 | + iterable: &'a Graph<T>, |
| 20 | + /// Processed vertices |
| 21 | + vertices: Vec<&'a VertexId>, |
| 22 | + /// Working set of vertices |
| 23 | + roots: Vec<&'a VertexId>, |
| 24 | + /// Working set of vertex edges |
| 25 | + vertex_edges: HashMap<&'a VertexId, usize>, |
| 26 | +} |
| 27 | + |
| 28 | +impl<'a, T> Topo<'a, T> { |
| 29 | + pub fn new(graph: &'a Graph<T>) -> Topo<'_, T> { |
| 30 | + let mut roots = vec![]; |
| 31 | + for node in graph.roots() { |
| 32 | + roots.push(node); |
| 33 | + } |
| 34 | + |
| 35 | + Topo { |
| 36 | + iterable: graph, |
| 37 | + vertices: vec![], |
| 38 | + roots, |
| 39 | + vertex_edges: HashMap::new(), |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Processes the next vertex. |
| 44 | + /// |
| 45 | + /// Will return None if: |
| 46 | + /// |
| 47 | + /// * No vertices are left. |
| 48 | + fn process_vertex(&mut self, check_cyclic: bool) -> Option<&'a VertexId> { |
| 49 | + match self.roots.pop() { |
| 50 | + Some(node) => { |
| 51 | + self.vertices.push(node); |
| 52 | + for out in self.iterable.out_neighbors(node) { |
| 53 | + let count = match self.vertex_edges.get_mut(out) { |
| 54 | + Some(count) => count, |
| 55 | + None => { |
| 56 | + self.vertex_edges |
| 57 | + .insert(out, self.iterable.in_neighbors_count(out)); |
| 58 | + self.vertex_edges.get_mut(out).unwrap() |
| 59 | + } |
| 60 | + }; |
| 61 | + if *count == 1 { |
| 62 | + self.roots.push(out); |
| 63 | + } else { |
| 64 | + *count -= 1; |
| 65 | + } |
| 66 | + } |
| 67 | + Some(node) |
| 68 | + }, |
| 69 | + None => { |
| 70 | + if check_cyclic && self.vertices.len() != self.iterable.vertex_count() { |
| 71 | + panic!(PANIC_MSG); |
| 72 | + } |
| 73 | + None |
| 74 | + }, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// |
| 79 | + /// Returns true if the iterated graph has a cycle. |
| 80 | + /// |
| 81 | + /// # Warning |
| 82 | + /// |
| 83 | + /// It is a logic error to use this iterator after calling this function. |
| 84 | + pub fn is_cyclic(&mut self) -> bool { |
| 85 | + //Search until an answer is found. |
| 86 | + while self.process_vertex(false).is_some() {} |
| 87 | + |
| 88 | + self.vertices.len() != self.iterable.vertex_count() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<'a, T> Iterator for Topo<'a, T> { |
| 93 | + type Item = &'a VertexId; |
| 94 | + |
| 95 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 96 | + let remaining = self.iterable.vertex_count() - self.vertices.len(); |
| 97 | + |
| 98 | + (0, Some(remaining)) |
| 99 | + } |
| 100 | + fn next(&mut self) -> Option<Self::Item> { |
| 101 | + (0..self.size_hint().1.unwrap()) |
| 102 | + .filter_map(move |_| self.process_vertex(true)) |
| 103 | + .next() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +#[cfg(test)] |
| 108 | +mod tests { |
| 109 | + use super::*; |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn is_not_cyclic() { |
| 113 | + let mut graph: Graph<usize> = Graph::new(); |
| 114 | + |
| 115 | + let v1 = graph.add_vertex(1); |
| 116 | + let v2 = graph.add_vertex(2); |
| 117 | + let v3 = graph.add_vertex(3); |
| 118 | + let v4 = graph.add_vertex(4); |
| 119 | + let v5 = graph.add_vertex(5); |
| 120 | + let v6 = graph.add_vertex(6); |
| 121 | + let v7 = graph.add_vertex(7); |
| 122 | + |
| 123 | + graph.add_edge(&v1, &v4).unwrap(); |
| 124 | + graph.add_edge(&v2, &v4).unwrap(); |
| 125 | + graph.add_edge(&v2, &v5).unwrap(); |
| 126 | + graph.add_edge(&v3, &v5).unwrap(); |
| 127 | + graph.add_edge(&v4, &v6).unwrap(); |
| 128 | + graph.add_edge(&v4, &v7).unwrap(); |
| 129 | + graph.add_edge(&v5, &v6).unwrap(); |
| 130 | + graph.add_edge(&v6, &v7).unwrap(); |
| 131 | + |
| 132 | + let mut topo = graph.topo(); |
| 133 | + |
| 134 | + assert!(!topo.is_cyclic()); |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn is_cyclic() { |
| 139 | + let mut graph: Graph<usize> = Graph::new(); |
| 140 | + |
| 141 | + let v1 = graph.add_vertex(1); |
| 142 | + let v2 = graph.add_vertex(2); |
| 143 | + let v3 = graph.add_vertex(3); |
| 144 | + let v4 = graph.add_vertex(4); |
| 145 | + let v5 = graph.add_vertex(5); |
| 146 | + let v6 = graph.add_vertex(6); |
| 147 | + let v7 = graph.add_vertex(7); |
| 148 | + |
| 149 | + graph.add_edge(&v1, &v4).unwrap(); |
| 150 | + graph.add_edge(&v2, &v4).unwrap(); |
| 151 | + graph.add_edge(&v2, &v5).unwrap(); |
| 152 | + graph.add_edge(&v3, &v5).unwrap(); |
| 153 | + graph.add_edge(&v4, &v6).unwrap(); |
| 154 | + graph.add_edge(&v4, &v7).unwrap(); |
| 155 | + graph.add_edge(&v5, &v6).unwrap(); |
| 156 | + graph.add_edge(&v6, &v7).unwrap(); |
| 157 | + graph.add_edge(&v7, &v2).unwrap(); |
| 158 | + |
| 159 | + let mut topo = graph.topo(); |
| 160 | + |
| 161 | + assert!(topo.is_cyclic()); |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + #[should_panic(expected = "graph contains cycle(s)")] |
| 166 | + fn is_cyclic_and_panic() { |
| 167 | + let mut graph: Graph<usize> = Graph::new(); |
| 168 | + |
| 169 | + let v1 = graph.add_vertex(1); |
| 170 | + let v2 = graph.add_vertex(2); |
| 171 | + let v3 = graph.add_vertex(3); |
| 172 | + let v4 = graph.add_vertex(4); |
| 173 | + let v5 = graph.add_vertex(5); |
| 174 | + let v6 = graph.add_vertex(6); |
| 175 | + let v7 = graph.add_vertex(7); |
| 176 | + |
| 177 | + graph.add_edge(&v1, &v4).unwrap(); |
| 178 | + graph.add_edge(&v2, &v4).unwrap(); |
| 179 | + graph.add_edge(&v2, &v5).unwrap(); |
| 180 | + graph.add_edge(&v3, &v5).unwrap(); |
| 181 | + graph.add_edge(&v4, &v6).unwrap(); |
| 182 | + graph.add_edge(&v4, &v7).unwrap(); |
| 183 | + graph.add_edge(&v5, &v6).unwrap(); |
| 184 | + graph.add_edge(&v6, &v7).unwrap(); |
| 185 | + graph.add_edge(&v7, &v2).unwrap(); |
| 186 | + |
| 187 | + let mut topo = graph.topo(); |
| 188 | + |
| 189 | + topo.next(); |
| 190 | + topo.next(); |
| 191 | + topo.next(); |
| 192 | + topo.next(); |
| 193 | + topo.next(); |
| 194 | + topo.next(); |
| 195 | + topo.next(); |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments