netoptim_rs/
neg_cycle.rs

1use petgraph::graph::{EdgeReference, NodeIndex};
2use petgraph::prelude::*;
3use petgraph::visit::EdgeRef;
4use petgraph::visit::IntoNodeIdentifiers;
5
6// use petgraph::visit::IntoNeighborsDirected;
7
8/// The `NegCycleFinder` struct is used to find negative cycles in a directed graph.
9///
10/// Properties:
11///
12/// * `digraph`: The `digraph` property is a reference to a directed graph (`DiGraph`) that the
13///             `NegCycleFinder` is operating on. It is annotated with a lifetime `'a`, indicating that the
14///             reference is valid for a certain scope.
15/// * `pred`: The `pred` property is a `HashMap` that maps a `NodeIndex` to a tuple containing the
16///             previous node index and an `EdgeReference`. This is used to keep track of the predecessor node and
17///             the edge that leads to that node during the process of finding negative cycles in a directed graph
18#[derive(Debug, Clone)]
19pub struct NegCycleFinder<'a, V, D> {
20    pub digraph: &'a DiGraph<V, D>,
21    pub pred: std::collections::HashMap<NodeIndex, (NodeIndex, EdgeReference<'a, D>)>,
22}
23
24impl<'a, V, D> NegCycleFinder<'a, V, D>
25where
26    D: std::ops::Add<Output = D> + std::cmp::PartialOrd + Copy,
27{
28    /// The `new` function creates a new `NegCycleFinder` object with an empty predecessor map.
29    ///
30    /// Arguments:
31    ///
32    /// * `digraph`: A reference to a directed graph (`DiGraph`) that the `NegCycleFinder` will operate on.
33    ///
34    /// Returns:
35    ///
36    /// The `new` function is returning an instance of the `NegCycleFinder<V, D>` struct.
37    /// Creates a new [`NegCycleFinder<V, D>`].
38    pub fn new(digraph: &'a DiGraph<V, D>) -> Self {
39        Self {
40            digraph,
41            pred: std::collections::HashMap::new(),
42        }
43    }
44
45    /// The `find_cycle` function in Rust returns the first node in a cycle found in a directed graph.
46    ///
47    /// Returns:
48    ///
49    /// The function `find_cycle` returns an `Option<NodeIndex>`.
50    pub fn find_cycle(&self) -> Option<NodeIndex> {
51        let mut visited = std::collections::HashMap::new();
52        for vtx in self.digraph.node_identifiers() {
53            if visited.contains_key(&vtx) {
54                continue;
55            }
56            let mut utx = vtx;
57            while !visited.contains_key(&utx) {
58                visited.insert(utx, vtx);
59                if !self.pred.contains_key(&utx) {
60                    break;
61                }
62                let result = *self.pred.get(&utx).unwrap();
63                utx = result.0;
64                if visited.contains_key(&utx) {
65                    if visited[&utx] == vtx {
66                        return Some(utx);
67                    }
68                    break;
69                }
70            }
71        }
72        None
73    }
74
75    /// The `relax` function updates the distances between nodes in a graph based on the weights of the
76    /// edges, and returns a boolean indicating whether any distances were changed.
77    ///
78    /// Arguments:
79    ///
80    /// * `dist`: `dist` is a mutable reference to a slice of type `D`. It represents the distances from
81    ///           a source node to each node in a graph.
82    /// * `get_weight`: The `get_weight` parameter is a closure that takes an `EdgeReference<D>` as
83    ///           input and returns a value of type `D`. This closure is used to calculate the weight of each edge
84    ///           in the graph. The `EdgeReference<D>` represents a reference to an edge in the graph, and
85    ///
86    /// Returns:
87    ///
88    /// a boolean value.
89    pub fn relax<F>(&mut self, dist: &mut [D], get_weight: F) -> bool
90    where
91        F: Fn(EdgeReference<D>) -> D,
92    {
93        let mut changed = false;
94        for utx in self.digraph.node_identifiers() {
95            for edge in self.digraph.edges(utx) {
96                let vtx = edge.target();
97                let weight = get_weight(edge);
98                // for utx in self.digraph.node_indices() {
99                //     for vtx in self
100                //         .digraph
101                //         .neighbors_directed(utx, petgraph::Direction::Outgoing)
102                //     {
103                // let weight = get_weight((utx, vtx));
104                let distance = dist[utx.index()] + weight;
105                if dist[vtx.index()] > distance {
106                    dist[vtx.index()] = distance;
107                    self.pred.insert(vtx, (utx, edge));
108                    changed = true;
109                }
110            }
111        }
112        changed
113    }
114
115    /// The `howard` function implements Howard's algorithm for finding negative cycles in a directed
116    /// graph.
117    ///
118    /// Arguments:
119    ///
120    /// * `dist`: `dist` is a mutable reference to an array of type `D`. This array is used to store the
121    ///             distances from the source vertex to each vertex in the graph. The algorithm will update the
122    ///             distances during the execution.
123    /// * `get_weight`: `get_weight` is a closure that takes an `EdgeReference<D>` and returns the
124    ///             weight of that edge. The `howard` function uses this closure to get the weight of each edge in
125    ///             the graph.
126    ///
127    /// Returns:
128    ///
129    /// The `howard` function returns an `Option<Vec<EdgeReference<'a, D>>>`.
130    /// Howard's algorithm for finding negative cycles
131    ///
132    /// # Examples
133    ///
134    /// ```
135    /// use petgraph::prelude::*;
136    /// use netoptim_rs::neg_cycle::NegCycleFinder;
137    /// let digraph = DiGraph::<(), i32>::from_edges([
138    ///     (0, 1, 1),
139    ///     (0, 2, 1),
140    ///     (0, 3, 1),
141    ///     (1, 3, 1),
142    ///     (2, 1, 1),
143    ///     (3, 2, -3),
144    /// ]);
145    /// let mut ncf = NegCycleFinder::new(&digraph);
146    /// let mut dist = [0, 0, 0, 0];
147    /// let result = ncf.howard(&mut dist, |e| { *e.weight()});
148    /// assert!(result.is_some());
149    /// ```
150    pub fn howard<F>(&mut self, dist: &mut [D], get_weight: F) -> Option<Vec<EdgeReference<'a, D>>>
151    where
152        F: Fn(EdgeReference<D>) -> D,
153    {
154        self.pred.clear();
155        while self.relax(dist, &get_weight) {
156            let v_opt = self.find_cycle();
157            if let Some(vtx) = v_opt {
158                return Some(self.cycle_list(vtx));
159            }
160        }
161        None
162    }
163
164    /// The function `cycle_list` takes a node index as input and returns a vector of edge references
165    /// that form a cycle in a graph.
166    ///
167    /// Arguments:
168    ///
169    /// * `handle`: The `handle` parameter is of type `NodeIndex`. It represents the starting node index
170    ///             from which the cycle traversal will begin.
171    ///
172    /// Returns:
173    ///
174    /// The function `cycle_list` returns a vector of `EdgeReference` objects.
175    fn cycle_list(&self, handle: NodeIndex) -> Vec<EdgeReference<'a, D>> {
176        let mut vtx = handle;
177        let mut cycle = Vec::new();
178        loop {
179            let (utx, edge) = self.pred[&vtx];
180            cycle.push(edge);
181            vtx = utx;
182            if vtx == handle {
183                break;
184            }
185        }
186        cycle
187    }
188}
189
190#[cfg(test)]
191mod tests {
192    use super::*;
193    use num::rational::Ratio;
194
195    #[test]
196    fn it_works() {
197        let result = 2 + 2;
198        assert_eq!(result, 4);
199    }
200
201    #[test]
202    fn test_neg_cycle1() {
203        let digraph = DiGraph::<(), Ratio<i32>>::from_edges([
204            (0, 1, Ratio::new(1, 1)),
205            (0, 2, Ratio::new(1, 1)),
206            (0, 3, Ratio::new(1, 1)),
207            (1, 3, Ratio::new(1, 1)),
208            (2, 1, Ratio::new(1, 1)),
209            (3, 2, Ratio::new(-3, 1)),
210        ]);
211
212        let mut ncf = NegCycleFinder::new(&digraph);
213        let mut dist = [
214            Ratio::new(0, 1),
215            Ratio::new(0, 1),
216            Ratio::new(0, 1),
217            Ratio::new(0, 1),
218        ];
219        let result = ncf.howard(&mut dist, |e| *e.weight());
220        assert!(result.is_some());
221    }
222
223    #[test]
224    fn test_neg_cycle2() {
225        let mut graph = DiGraph::new();
226        let a = graph.add_node("a");
227        let b = graph.add_node("b");
228        let c = graph.add_node("c");
229        let d = graph.add_node("d");
230        let e = graph.add_node("e");
231        let f = graph.add_node("f");
232        let g = graph.add_node("g");
233        let h = graph.add_node("h");
234        let i = graph.add_node("i");
235        graph.add_edge(a, b, Ratio::new(1, 1));
236        graph.add_edge(a, c, Ratio::new(1, 1));
237        graph.add_edge(b, d, Ratio::new(1, 1));
238        graph.add_edge(c, d, Ratio::new(1, 1));
239        graph.add_edge(d, e, Ratio::new(-3, 1));
240        graph.add_edge(d, f, Ratio::new(1, 1));
241        graph.add_edge(e, g, Ratio::new(1, 1));
242        graph.add_edge(f, g, Ratio::new(1, 1));
243        graph.add_edge(g, h, Ratio::new(1, 1));
244        graph.add_edge(h, i, Ratio::new(1, 1));
245        graph.add_edge(i, f, Ratio::new(1, 1));
246
247        let mut ncf = NegCycleFinder::new(&graph);
248        let mut dist = [
249            Ratio::new(0, 1),
250            Ratio::new(0, 1),
251            Ratio::new(0, 1),
252            Ratio::new(0, 1),
253            Ratio::new(0, 1),
254            Ratio::new(0, 1),
255            Ratio::new(0, 1),
256            Ratio::new(0, 1),
257            Ratio::new(0, 1),
258        ];
259        let result = ncf.howard(&mut dist, |e| *e.weight());
260        assert!(result.is_none());
261    }
262}