Debian Codegraph @

third_party/rust/smawk/src/recursive.rs

raw
1 //! Recursive algorithm for finding column minima.
2 //!
3 //! The functions here are mostly meant to be used for testing
4 //! correctness of the SMAWK implementation.
5 //!
6 //! **Note: this module is only available if you enable the `ndarray`
7 //! Cargo feature.**
8
9 use ndarray::{s, Array2, ArrayView2, Axis};
10
11 /// Compute row minima in O(*m* + *n* log *m*) time.
12 ///
13 /// This function computes row minima in a totally monotone matrix
14 /// using a recursive algorithm.
15 ///
16 /// Running time on an *m* ✕ *n* matrix: O(*m* + *n* log *m*).
17 ///
18 /// # Examples
19 ///
20 /// ```
21 /// let matrix = ndarray::arr2(&[[4, 2, 4, 3],
22 /// [5, 3, 5, 3],
23 /// [5, 3, 3, 1]]);
24 /// assert_eq!(smawk::recursive::row_minima(&matrix),
25 /// vec![1, 1, 3]);
26 /// ```
27 ///
28 /// # Panics
29 ///
30 /// It is an error to call this on a matrix with zero columns.
31 pub fn row_minima<T: Ord>(matrix: &Array2<T>) -> Vec<usize> {
32 let mut minima = vec![0; matrix.nrows()];
33 recursive_inner(matrix.view(), &|| Direction::Row, 0, &mut minima);
34 minima
35 }
36
37 /// Compute column minima in O(*n* + *m* log *n*) time.
38 ///
39 /// This function computes column minima in a totally monotone matrix
40 /// using a recursive algorithm.
41 ///
42 /// Running time on an *m* ✕ *n* matrix: O(*n* + *m* log *n*).
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// let matrix = ndarray::arr2(&[[4, 2, 4, 3],
48 /// [5, 3, 5, 3],
49 /// [5, 3, 3, 1]]);
50 /// assert_eq!(smawk::recursive::column_minima(&matrix),
51 /// vec![0, 0, 2, 2]);
52 /// ```
53 ///
54 /// # Panics
55 ///
56 /// It is an error to call this on a matrix with zero rows.
57 pub fn column_minima<T: Ord>(matrix: &Array2<T>) -> Vec<usize> {
58 let mut minima = vec![0; matrix.ncols()];
59 recursive_inner(matrix.view(), &|| Direction::Column, 0, &mut minima);
60 minima
61 }
62
63 /// The type of minima (row or column) we compute.
64 enum Direction {
65 Row,
66 Column,
67 }
68
69 /// Compute the minima along the given direction (`Direction::Row` for
70 /// row minima and `Direction::Column` for column minima).
71 ///
72 /// The direction is given as a generic function argument to allow
73 /// monomorphization to kick in. The function calls will be inlined
74 /// and optimized away and the result is that the compiler generates
75 /// differnet code for finding row and column minima.
76 fn recursive_inner<T: Ord, F: Fn() -> Direction>(
77 matrix: ArrayView2<'_, T>,
78 dir: &F,
79 offset: usize,
80 minima: &mut [usize],
81 ) {
82 if matrix.is_empty() {
83 return;
84 }
85
86 let axis = match dir() {
87 Direction::Row => Axis(0),
88 Direction::Column => Axis(1),
89 };
90 let mid = matrix.len_of(axis) / 2;
91 let min_idx = crate::brute_force::lane_minimum(matrix.index_axis(axis, mid));
92 minima[mid] = offset + min_idx;
93
94 if mid == 0 {
95 return; // Matrix has a single row or column, so we're done.
96 }
97
98 let top_left = match dir() {
99 Direction::Row => matrix.slice(s![..mid, ..(min_idx + 1)]),
100 Direction::Column => matrix.slice(s![..(min_idx + 1), ..mid]),
101 };
102 let bot_right = match dir() {
103 Direction::Row => matrix.slice(s![(mid + 1).., min_idx..]),
104 Direction::Column => matrix.slice(s![min_idx.., (mid + 1)..]),
105 };
106 recursive_inner(top_left, dir, offset, &mut minima[..mid]);
107 recursive_inner(bot_right, dir, offset + min_idx, &mut minima[mid + 1..]);
108 }
109
110 #[cfg(test)]
111 mod tests {
112 use super::*;
113 use ndarray::arr2;
114
115 #[test]
116 fn recursive_1x1() {
117 let matrix = arr2(&[[2]]);
118 let minima = vec![0];
119 assert_eq!(row_minima(&matrix), minima);
120 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
121 }
122
123 #[test]
124 fn recursive_2x1() {
125 let matrix = arr2(&[
126 [3], //
127 [2],
128 ]);
129 let minima = vec![0, 0];
130 assert_eq!(row_minima(&matrix), minima);
131 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
132 }
133
134 #[test]
135 fn recursive_1x2() {
136 let matrix = arr2(&[[2, 1]]);
137 let minima = vec![1];
138 assert_eq!(row_minima(&matrix), minima);
139 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
140 }
141
142 #[test]
143 fn recursive_2x2() {
144 let matrix = arr2(&[
145 [3, 2], //
146 [2, 1],
147 ]);
148 let minima = vec![1, 1];
149 assert_eq!(row_minima(&matrix), minima);
150 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
151 }
152
153 #[test]
154 fn recursive_3x3() {
155 let matrix = arr2(&[
156 [3, 4, 4], //
157 [3, 4, 4],
158 [2, 3, 3],
159 ]);
160 let minima = vec![0, 0, 0];
161 assert_eq!(row_minima(&matrix), minima);
162 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
163 }
164
165 #[test]
166 fn recursive_4x4() {
167 let matrix = arr2(&[
168 [4, 5, 5, 5], //
169 [2, 3, 3, 3],
170 [2, 3, 3, 3],
171 [2, 2, 2, 2],
172 ]);
173 let minima = vec![0, 0, 0, 0];
174 assert_eq!(row_minima(&matrix), minima);
175 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
176 }
177
178 #[test]
179 fn recursive_5x5() {
180 let matrix = arr2(&[
181 [3, 2, 4, 5, 6],
182 [2, 1, 3, 3, 4],
183 [2, 1, 3, 3, 4],
184 [3, 2, 4, 3, 4],
185 [4, 3, 2, 1, 1],
186 ]);
187 let minima = vec![1, 1, 1, 1, 3];
188 assert_eq!(row_minima(&matrix), minima);
189 assert_eq!(column_minima(&matrix.reversed_axes()), minima);
190 }
191 }