Crates.io | smawk |
lib.rs | smawk |
version | 0.3.2 |
source | src |
created_at | 2018-08-07 20:23:20.055686 |
updated_at | 2023-09-17 20:46:37.049294 |
description | Functions for finding row-minima in a totally monotone matrix. |
homepage | |
repository | https://github.com/mgeisler/smawk |
max_upload_size | |
id | 78268 |
size | 51,875 |
This crate contains an implementation of the SMAWK algorithm for finding the smallest element per row in a totally monotone matrix.
The SMAWK algorithm allows you to lower the running time of some algorithms from O(_n_²) to just O(n). In other words, you can turn a quadratic time complexity (which is often too expensive) into linear time complexity.
Finding optimal line breaks in a paragraph of text is an example of an algorithm which would normally take O(_n_²) time for n words. With this crate, the running time becomes linear. Please see the textwrap crate for an example of this.
Add this to your Cargo.toml
:
[dependencies]
smawk = "0.3"
You can now efficiently find row and column minima. Here is an example where we find the column minima:
use smawk::Matrix;
let matrix = vec![
vec![3, 2, 4, 5, 6],
vec![2, 1, 3, 3, 4],
vec![2, 1, 3, 3, 4],
vec![3, 2, 4, 3, 4],
vec![4, 3, 2, 1, 1],
];
let minima = vec![1, 1, 4, 4, 4];
assert_eq!(smawk::column_minima(&matrix), minima);
The minima
vector gives the index of the minimum value per column, so
minima[0] == 1
since the minimum value in the first column is 2 (row 1). Note
that the smallest row index is returned.
This crate has an optional dependency on the
ndarray
crate, which provides an efficient matrix
implementation. Enable the ndarray
Cargo feature to use it.
This release adds more documentation and renames the top-level SMAWK functions. The old names have been kept for now to ensure backwards compatibility, but they will be removed in a future release.
smawk_
prefix from
optimized functions.This release relaxes the bounds on the smawk_row_minima
,
smawk_column_minima
, and online_column_minima
functions so that they work on
matrices containing floating point numbers.
PartialOrd
instead of Ord
.This release slims down the crate significantly by making ndarray
an optional
dependency.
smawk_row_minima
and smawk_column_minima
functions to a new Matrix
trait.ndarray
crate optional.is_monge
take a
Matrix
argument instead of ndarray::Array2
.rand
and num-traits
crates.This release updates the code to Rust 2018.
online_column_minima
generic in matrix type.is_monge
.rand
dependency to
latest version and get rid of rand_derive
.num-traits
and
version-sync
dependencies to latest versions.ndarray
dependency
to the latest version.First release with the classical offline SMAWK algorithm as well as a newer online version where the matrix entries can depend on previously computed column minima.
SMAWK can be distributed according to the MIT license. Contributions will be accepted under the same license.