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