Debian Codegraph @

debian/rust-vendor/smawk/README.md

raw
1 # SMAWK Algorithm in Rust
2
3 [![](https://github.com/mgeisler/smawk/workflows/build/badge.svg)][build-status]
4 [![](https://codecov.io/gh/mgeisler/smawk/branch/master/graph/badge.svg)][codecov]
5 [![](https://img.shields.io/crates/v/smawk.svg)][crates-io]
6 [![](https://docs.rs/smawk/badge.svg)][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 For dynamic programming problems where matrix elements depend on previously
51 computed values (online dynamic programming), you can use the online column
52 minima algorithm via `smawk::online_column_minima`.
53
54 ### Cargo Features
55
56 This crate has an optional dependency on the
57 [`ndarray` crate](https://docs.rs/ndarray/), which provides an efficient matrix
58 implementation. Enable the `ndarray` Cargo feature to use it.
59
60 ### `no_std` Support
61
62 SMAWK is a `no_std` crate by default. It requires the `alloc` crate for dynamic
63 memory allocation.
64
65 ## Documentation
66
67 **[API documentation][api-docs]**
68
69 ## Changelog
70
71 ### Version 0.3.3 (2026-06-16)
72
73 This release contains optimizations that make our benchmarks about 28% faster by
74 eliminating heap allocations and vector bounds checks.
75
76 - [#89](https://github.com/mgeisler/smawk/pull/89): Add support for `no_std`.
77 - [#90](https://github.com/mgeisler/smawk/pull/90): Add GitHub Sponsors
78 configuration.
79 - [#114](https://github.com/mgeisler/smawk/pull/114): Remove heap allocations by
80 pre-allocating vectors up front. This improves benchmarks by 10%.
81 - [#115](https://github.com/mgeisler/smawk/pull/115): Eliminate bounds checks in
82 `smawk_inner`. This improves benchmarks by another 20%.
83
84 ### Version 0.3.2 (2023-09-17)
85
86 This release adds more documentation and renames the top-level SMAWK functions.
87 The old names have been kept for now to ensure backwards compatibility, but they
88 will be removed in a future release.
89
90 - [#65](https://github.com/mgeisler/smawk/pull/65): Forbid the use of unsafe
91 code.
92 - [#69](https://github.com/mgeisler/smawk/pull/69): Migrate to the Rust 2021
93 edition.
94 - [#73](https://github.com/mgeisler/smawk/pull/73): Add examples to all
95 functions.
96 - [#74](https://github.com/mgeisler/smawk/pull/74): Add “mathematics” as a crate
97 category.
98 - [#75](https://github.com/mgeisler/smawk/pull/75): Remove `smawk_` prefix from
99 optimized functions.
100
101 ### Version 0.3.1 (2021-01-30)
102
103 This release relaxes the bounds on the `smawk_row_minima`,
104 `smawk_column_minima`, and `online_column_minima` functions so that they work on
105 matrices containing floating point numbers.
106
107 - [#55](https://github.com/mgeisler/smawk/pull/55): Relax bounds to `PartialOrd`
108 instead of `Ord`.
109 - [#56](https://github.com/mgeisler/smawk/pull/56): Update dependencies to their
110 latest versions.
111 - [#59](https://github.com/mgeisler/smawk/pull/59): Give an example of what
112 SMAWK does in the README.
113
114 ### Version 0.3.0 (2020-09-02)
115
116 This release slims down the crate significantly by making `ndarray` an optional
117 dependency.
118
119 - [#45](https://github.com/mgeisler/smawk/pull/45): Move non-SMAWK code and unit
120 tests out of lib and into separate modules.
121 - [#46](https://github.com/mgeisler/smawk/pull/46): Switch `smawk_row_minima`
122 and `smawk_column_minima` functions to a new `Matrix` trait.
123 - [#47](https://github.com/mgeisler/smawk/pull/47): Make the dependency on the
124 `ndarray` crate optional.
125 - [#48](https://github.com/mgeisler/smawk/pull/48): Let `is_monge` take a
126 `Matrix` argument instead of `ndarray::Array2`.
127 - [#50](https://github.com/mgeisler/smawk/pull/50): Remove mandatory
128 dependencies on `rand` and `num-traits` crates.
129
130 ### Version 0.2.0 (2020-07-29)
131
132 This release updates the code to Rust 2018.
133
134 - [#18](https://github.com/mgeisler/smawk/pull/18): Make `online_column_minima`
135 generic in matrix type.
136 - [#23](https://github.com/mgeisler/smawk/pull/23): Switch to the
137 [Rust 2018][rust-2018] edition. We test against the latest stable and nightly
138 version of Rust.
139 - [#29](https://github.com/mgeisler/smawk/pull/29): Drop strict Rust 2018
140 compatibility by not testing with Rust 1.31.0.
141 - [#32](https://github.com/mgeisler/smawk/pull/32): Fix crash on overflow in
142 `is_monge`.
143 - [#33](https://github.com/mgeisler/smawk/pull/33): Update `rand` dependency to
144 latest version and get rid of `rand_derive`.
145 - [#34](https://github.com/mgeisler/smawk/pull/34): Bump `num-traits` and
146 `version-sync` dependencies to latest versions.
147 - [#35](https://github.com/mgeisler/smawk/pull/35): Drop unnecessary Windows
148 tests. The assumption is that the numeric computations we do are
149 cross-platform.
150 - [#36](https://github.com/mgeisler/smawk/pull/36): Update `ndarray` dependency
151 to the latest version.
152 - [#37](https://github.com/mgeisler/smawk/pull/37): Automate publishing new
153 releases to crates.io.
154
155 ### Version 0.1.0 — August 7th, 2018
156
157 First release with the classical offline SMAWK algorithm as well as a newer
158 online version where the matrix entries can depend on previously computed column
159 minima.
160
161 ## License
162
163 SMAWK can be distributed according to the [MIT license][mit]. Contributions will
164 be accepted under the same license.
165
166 [build-status]: https://github.com/mgeisler/smawk/actions?query=branch%3Amaster+workflow%3Abuild
167 [crates-io]: https://crates.io/crates/smawk
168 [codecov]: https://codecov.io/gh/mgeisler/smawk
169 [textwrap]: https://crates.io/crates/textwrap
170 [smawk]: https://en.wikipedia.org/wiki/SMAWK_algorithm
171 [api-docs]: https://docs.rs/smawk/
172 [rust-2018]: https://doc.rust-lang.org/edition-guide/rust-2018/
173 [mit]: LICENSE