# SMAWK algorithm

The **SMAWK algorithm** is an algorithm for finding the minimum value in each row of an implicitly-defined totally monotone matrix. It is named after the initials of its five inventors, Peter Shor, Shlomo Moran, Alok Aggarwal, Robert Wilber, and Maria Klawe.[1]

## Input

For the purposes of this algorithm, a matrix is defined to be monotone if each row's minimum value occurs in a column which is equal to or greater than the column of the previous row's minimum. It is totally monotone if the same property is true for every submatrix (defined by an arbitrary subset of the rows and columns of the given matrix). Equivalently, a matrix is totally monotone if there does not exist a 2×2 submatrix whose row minima are in the top right and bottom left corners. Every Monge array is totally monotone, but not necessarily vice versa.

For the SMAWK algorithm, the matrix to be searched should be defined as a function, and this function is given as input to the algorithm (together with the dimensions of the matrix). The algorithm then evaluates the function whenever it needs to know the value of a particular matrix cell. If this evaluation takes *O*(*1*), then, for a matrix with *r* rows and *c* columns, the running time and number of function evaluations are both *O*(*c*(1 + log(*r*/*c*))). This is much faster than the *O*(*r* *c*) time of a naive algorithm that evaluates all matrix cells.

## Method

The basic idea of the algorithm is to follow a prune and search strategy in which the problem to be solved is reduced to a single recursive subproblem of the same type whose size is smaller by a constant factor. To do so, the algorithm first preprocesses the matrix to remove some of its columns that cannot contain a row-minimum, using a stack-based algorithm similar to the one in the Graham scan and all nearest smaller values algorithms. After this phase of the algorithm, the number of remaining columns will at most equal the number of rows. Next, the algorithm calls itself recursively to find the row minima of the even-numbered rows of the matrix. Finally, by searching the columns between the positions of consecutive even-row minima, the algorithm fills out the remaining minima in the odd rows.

## Applications

The main applications of this method presented in the original paper by Aggarwal et al. were in computational geometry, in finding the farthest point from each point of a convex polygon, and in finding optimal enclosing polygons. Subsequent research found applications of the same algorithm in breaking paragraphs into lines,[2] RNA secondary structure prediction,[3] DNA and protein sequence alignment,[4][5] the construction of prefix codes,[6] and image thresholding,[7] among others.

## References

- Aggarwal, Alok; Klawe, Maria M.; Moran, Shlomo; Shor, Peter; Wilber, Robert (1987), "Geometric applications of a matrix-searching algorithm",
*Algorithmica*,**2**(2): 195–208, doi:10.1007/BF01840359, MR 0895444. - Wilber, Robert (1988), "The concave least-weight subsequence problem revisited",
*Journal of Algorithms*,**9**(3): 418–425, doi:10.1016/0196-6774(88)90032-6, MR 0955150 - Larmore, Lawrence L.; Schieber, Baruch (1991), "On-line dynamic programming with applications to the prediction of RNA secondary structure",
*Journal of Algorithms*,**12**(3): 490–515, doi:10.1016/0196-6774(91)90016-R, MR 1114923. - Russo, Luís M. S. (2012), "Monge properties of sequence alignment",
*Theoretical Computer Science*,**423**: 30–49, doi:10.1016/j.tcs.2011.12.068, MR 2887979. - Crochemore, Maxime; Landau, Gad M.; Ziv-Ukelson, Michal (2003), "A subquadratic sequence alignment algorithm for unrestricted scoring matrices",
*SIAM Journal on Computing*,**32**(6): 1654–1673 (electronic), CiteSeerX 10.1.1.57.8562, doi:10.1137/S0097539702402007, MR 2034254. - Bradford, Phil; Golin, Mordecai J.; Larmore, Lawrence L.; Rytter, Wojciech (2002), "Optimal prefix-free codes for unequal letter costs: dynamic programming with the Monge property",
*Journal of Algorithms*,**42**(2): 277–303, CiteSeerX 10.1.1.45.5501, doi:10.1006/jagm.2002.1213, MR 1895977. - Luessi, M.; Eichmann, M.; Schuster, G.M.; Katsaggelos, A.K. (2006), "New results on efficient optimal multilevel image thresholding",
*IEEE International Conference on Image Processing*, pp. 773–776, CiteSeerX 10.1.1.461.663, doi:10.1109/ICIP.2006.312426, ISBN 978-1-4244-0480-3.