| 1 |
# SMAWK Algorithm in Rust |
| 2 |
|
| 3 |
[][build-status] |
| 4 |
[][codecov] |
| 5 |
[][crates-io] |
| 6 |
[][api-docs] |
| 7 |
|
| 8 |
This crate contains an implementation of the [SMAWK algorithm][smawk] for |
| 9 |
finding the smallest element per row in a totally monotone matrix. |
| 10 |
|
| 11 |
The SMAWK algorithm allows you to lower the running time of some algorithms from |
| 12 |
O(_n_²) to just O(_n_). In other words, you can turn a quadratic time complexity |
| 13 |
(which is often too expensive) into linear time complexity. |
| 14 |
|
| 15 |
Finding optimal line breaks in a paragraph of text is an example of an algorithm |
| 16 |
which would normally take O(_n_²) time for _n_ words. With this crate, the |
| 17 |
running time becomes linear. Please see the [textwrap crate][textwrap] for an |
| 18 |
example of this. |
| 19 |
|
| 20 |
## Usage |
| 21 |
|
| 22 |
Add this to your `Cargo.toml`: |
| 23 |
|
| 24 |
```toml |
| 25 |
[dependencies] |
| 26 |
smawk = "0.3" |
| 27 |
``` |
| 28 |
|
| 29 |
You can now efficiently find row and column minima. Here is an example where we |
| 30 |
find the column minima: |
| 31 |
|
| 32 |
```rust |
| 33 |
use smawk::Matrix; |
| 34 |
|
| 35 |
let matrix = vec![ |
| 36 |
vec![3, 2, 4, 5, 6], |
| 37 |
vec![2, 1, 3, 3, 4], |
| 38 |
vec![2, 1, 3, 3, 4], |
| 39 |
vec![3, 2, 4, 3, 4], |
| 40 |
vec![4, 3, 2, 1, 1], |
| 41 |
]; |
| 42 |
let minima = vec![1, 1, 4, 4, 4]; |
| 43 |
assert_eq!(smawk::column_minima(&matrix), minima); |
| 44 |
``` |
| 45 |
|
| 46 |
The `minima` vector gives the index of the minimum value per column, so |
| 47 |
`minima[0] == 1` since the minimum value in the first column is 2 (row 1). Note |
| 48 |
that the smallest row index is returned. |
| 49 |
|
| 50 |
### Cargo Features |
| 51 |
|
| 52 |
This crate has an optional dependency on the |
| 53 |
[`ndarray` crate](https://docs.rs/ndarray/), which provides an efficient matrix |
| 54 |
implementation. Enable the `ndarray` Cargo feature to use it. |
| 55 |
|
| 56 |
## Documentation |
| 57 |
|
| 58 |
**[API documentation][api-docs]** |
| 59 |
|
| 60 |
## Changelog |
| 61 |
|
| 62 |
### Version 0.3.2 (2023-09-17) |
| 63 |
|
| 64 |
This release adds more documentation and renames the top-level SMAWK functions. |
| 65 |
The old names have been kept for now to ensure backwards compatibility, but they |
| 66 |
will be removed in a future release. |
| 67 |
|
| 68 |
- [#65](https://github.com/mgeisler/smawk/pull/65): Forbid the use of unsafe |
| 69 |
code. |
| 70 |
- [#69](https://github.com/mgeisler/smawk/pull/69): Migrate to the Rust 2021 |
| 71 |
edition. |
| 72 |
- [#73](https://github.com/mgeisler/smawk/pull/73): Add examples to all |
| 73 |
functions. |
| 74 |
- [#74](https://github.com/mgeisler/smawk/pull/74): Add “mathematics” as a crate |
| 75 |
category. |
| 76 |
- [#75](https://github.com/mgeisler/smawk/pull/75): Remove `smawk_` prefix from |
| 77 |
optimized functions. |
| 78 |
|
| 79 |
### Version 0.3.1 (2021-01-30) |
| 80 |
|
| 81 |
This release relaxes the bounds on the `smawk_row_minima`, |
| 82 |
`smawk_column_minima`, and `online_column_minima` functions so that they work on |
| 83 |
matrices containing floating point numbers. |
| 84 |
|
| 85 |
- [#55](https://github.com/mgeisler/smawk/pull/55): Relax bounds to `PartialOrd` |
| 86 |
instead of `Ord`. |
| 87 |
- [#56](https://github.com/mgeisler/smawk/pull/56): Update dependencies to their |
| 88 |
latest versions. |
| 89 |
- [#59](https://github.com/mgeisler/smawk/pull/59): Give an example of what |
| 90 |
SMAWK does in the README. |
| 91 |
|
| 92 |
### Version 0.3.0 (2020-09-02) |
| 93 |
|
| 94 |
This release slims down the crate significantly by making `ndarray` an optional |
| 95 |
dependency. |
| 96 |
|
| 97 |
- [#45](https://github.com/mgeisler/smawk/pull/45): Move non-SMAWK code and unit |
| 98 |
tests out of lib and into separate modules. |
| 99 |
- [#46](https://github.com/mgeisler/smawk/pull/46): Switch `smawk_row_minima` |
| 100 |
and `smawk_column_minima` functions to a new `Matrix` trait. |
| 101 |
- [#47](https://github.com/mgeisler/smawk/pull/47): Make the dependency on the |
| 102 |
`ndarray` crate optional. |
| 103 |
- [#48](https://github.com/mgeisler/smawk/pull/48): Let `is_monge` take a |
| 104 |
`Matrix` argument instead of `ndarray::Array2`. |
| 105 |
- [#50](https://github.com/mgeisler/smawk/pull/50): Remove mandatory |
| 106 |
dependencies on `rand` and `num-traits` crates. |
| 107 |
|
| 108 |
### Version 0.2.0 (2020-07-29) |
| 109 |
|
| 110 |
This release updates the code to Rust 2018. |
| 111 |
|
| 112 |
- [#18](https://github.com/mgeisler/smawk/pull/18): Make `online_column_minima` |
| 113 |
generic in matrix type. |
| 114 |
- [#23](https://github.com/mgeisler/smawk/pull/23): Switch to the |
| 115 |
[Rust 2018][rust-2018] edition. We test against the latest stable and nightly |
| 116 |
version of Rust. |
| 117 |
- [#29](https://github.com/mgeisler/smawk/pull/29): Drop strict Rust 2018 |
| 118 |
compatibility by not testing with Rust 1.31.0. |
| 119 |
- [#32](https://github.com/mgeisler/smawk/pull/32): Fix crash on overflow in |
| 120 |
`is_monge`. |
| 121 |
- [#33](https://github.com/mgeisler/smawk/pull/33): Update `rand` dependency to |
| 122 |
latest version and get rid of `rand_derive`. |
| 123 |
- [#34](https://github.com/mgeisler/smawk/pull/34): Bump `num-traits` and |
| 124 |
`version-sync` dependencies to latest versions. |
| 125 |
- [#35](https://github.com/mgeisler/smawk/pull/35): Drop unnecessary Windows |
| 126 |
tests. The assumption is that the numeric computations we do are |
| 127 |
cross-platform. |
| 128 |
- [#36](https://github.com/mgeisler/smawk/pull/36): Update `ndarray` dependency |
| 129 |
to the latest version. |
| 130 |
- [#37](https://github.com/mgeisler/smawk/pull/37): Automate publishing new |
| 131 |
releases to crates.io. |
| 132 |
|
| 133 |
### Version 0.1.0 — August 7th, 2018 |
| 134 |
|
| 135 |
First release with the classical offline SMAWK algorithm as well as a newer |
| 136 |
online version where the matrix entries can depend on previously computed column |
| 137 |
minima. |
| 138 |
|
| 139 |
## License |
| 140 |
|
| 141 |
SMAWK can be distributed according to the [MIT license][mit]. Contributions will |
| 142 |
be accepted under the same license. |
| 143 |
|
| 144 |
[build-status]: https://github.com/mgeisler/smawk/actions?query=branch%3Amaster+workflow%3Abuild |
| 145 |
[crates-io]: https://crates.io/crates/smawk |
| 146 |
[codecov]: https://codecov.io/gh/mgeisler/smawk |
| 147 |
[textwrap]: https://crates.io/crates/textwrap |
| 148 |
[smawk]: https://en.wikipedia.org/wiki/SMAWK_algorithm |
| 149 |
[api-docs]: https://docs.rs/smawk/ |
| 150 |
[rust-2018]: https://doc.rust-lang.org/edition-guide/rust-2018/ |
| 151 |
[mit]: LICENSE |