In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, the all nearest smaller values problem is the following task: for each position in a sequence of numbers, search among the previous positions for the last position that contains a smaller value. This problem can be solved efficiently both by parallel and non-parallel algorithms: , who first identified the procedure as a useful subroutine for other parallel programs, developed efficient
algorithms
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
to solve it in the
Parallel Random Access Machine
In computer science, a parallel random-access machine (parallel RAM or PRAM) is a shared-memory abstract machine. As its name indicates, the PRAM is intended as the parallel-computing analogy to the random-access machine (RAM) (not to be confus ...
model; it may also be solved in
linear time
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by t ...
on a non-parallel computer using a
stack
Stack may refer to:
Places
* Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group
* Blue Stack Mountains, in Co. Donegal, Ireland
People
* Stack (surname) (including a list of people ...
-based algorithm. Later researchers have studied algorithms to solve it in other models of parallel computation.
Example
Suppose that the input is the binary
van der Corput sequence
:0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15.
The first element of the sequence (0) has no previous value.
The nearest (only) smaller value previous to 8 and to 4 is 0. All three values previous to 12 are smaller, but the nearest one is 4. Continuing in the same way, the nearest previous smaller values for this sequence (indicating the nonexistence of a previous smaller value by a dash) are
:—, 0, 0, 4, 0, 2, 2, 6, 0, 1, 1, 5, 1, 3, 3, 7.
In most applications, the positions of the nearest smaller values, and not the values themselves, should be computed, and in many applications the same computation should be computed for the reversal of the sequence in order to find the following smaller value that is closest in the sequence.
Applications
mention many other problems that may be solved efficiently in parallel using a nearest smaller values computation. Among them, they include the following:
*
Merge algorithms, computing the merge step of a
merge sort
In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same ...
. The input to these algorithms consists of two sorted arrays of numbers; the desired output is the same set of numbers in a single sorted array. If one concatenates the two sorted arrays, the first in ascending order and the second in descending order, then the predecessor of each value in the output is either its closest previous smaller value or its closest following smaller value (whichever of the two is larger), and the position of each value in the sorted output array may easily be calculated from the positions of these two nearest smaller values.
* Construction of
Cartesian trees. A Cartesian tree is a
data structure
In computer science, a data structure is a data organization, management, and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the rel ...
introduced by and further studied by for
range searching applications. Cartesian trees also arise in the definition of the
treap and
randomized binary search tree
In computer science, the treap and the randomized binary search tree are two closely related forms of binary search tree data structures that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of i ...
data structures for
binary search
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...
ing. The Cartesian tree of a sequence of values has a node for each value. The root of the tree is the minimum value of the sequence; for every other node, the parent of the node is either its closest previous smaller value or its closest following smaller value (whichever of the two exists and is larger). Thus, Cartesian trees may be constructed in linear time based on an all nearest smaller values algorithm.
* Matching
parentheses
A bracket is either of two tall fore- or back-facing punctuation marks commonly used to isolate a segment of text or data from its surroundings. Typically deployed in symmetric pairs, an individual bracket may be identified as a 'left' or 'r ...
. If a sequence of open and close parenthesis characters is given as input, together with the nesting depth of each parenthesis, then the match to each open parenthesis is the next close parenthesis with no larger nesting depth, so it can be found by an all nearest smaller values computation that breaks ties in favor of close parentheses. If the nesting depths are not given, they can be calculated using a
prefix sum computation.
Similar techniques may also be applied to problems of
polygon triangulation
In computational geometry, polygon triangulation is the partition of a polygonal area ( simple polygon) into a set of triangles, i.e., finding a set of triangles with pairwise non-intersecting interiors whose union is .
Triangulations may ...
,
convex hull construction (parallelizing the sequential
Graham scan
Graham's scan is a method of finding the convex hull of a finite set of points in the plane with time complexity O(''n'' log ''n''). It is named after Ronald Graham, who published the original algorithm in 1972. The algorithm finds all vertices ...
convex hull algorithm), reconstruction of trees from two of the trees' traversal orderings, and quadtree construction.
Sequential algorithm
On a sequential computer, all nearest smaller values may be found by using a
stack data structure
In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations:
* Push, which adds an element to the collection, and
* Pop, which removes the most recently added element that was not y ...
: one processes the values in sequence order, using the stack to maintain a subsequence of the values that have been processed so far and are smaller than any later value that has already been processed. In
pseudocode
In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
, the algorithm is as follows.
S = new empty stack data structure
for x in the input sequence do
while S is nonempty and the top element of S is greater than or equal to x do
pop S
if S is empty then
x has no preceding smaller value
else
the nearest smaller value to x is the top element of S
push x onto S
Despite having a nested loop structure, the running time of this algorithm is linear, because every iteration of the inner loop removes an item that had been added in some previous iteration of the outer loop. It is closely related to an algorithm of
Knuth for
sorting with a stack (for inputs that can be sorted in this way).
An even simpler linear-time sequential algorithm (, Lemma 1) does not even need a stack; it assumes that the input sequence is given as an array
A ,n/code> of size n
, and stores the index j
of the preceding smaller value of the i
th value A /code> in P /code>. We assume an artificial overall minimum at A /code>:
for i from 1 to n:
j = i-1
while A >= A
j = P P = j
Parallel algorithms
showed how to solve the all nearest smaller values problem efficiently on a concurrent-read concurrent-write Parallel Random Access Machine
In computer science, a parallel random-access machine (parallel RAM or PRAM) is a shared-memory abstract machine. As its name indicates, the PRAM is intended as the parallel-computing analogy to the random-access machine (RAM) (not to be confus ...
. For a sequence of ''n'' values, stored as an array
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
, they show that the problem may be solved in time O(log log ''n'') using a linear amount of total work. For sequences where all values are integers in the interval ,''s'' improved this bound to O(log log log ''s''); they also showed that, for sufficiently large values of ''s'', the previous doubly logarithmic time bound is the best that can be achieved for the problem. Since this work, parallel algorithms for the all nearest smaller values problem have also been developed on other models of parallel computation, including parallel computers with a hypercube
In geometry, a hypercube is an ''n''-dimensional analogue of a square () and a cube (). It is a closed, compact, convex figure whose 1-skeleton consists of groups of opposite parallel line segments aligned in each of the space's dimensions ...
-structured communications network, and the bulk synchronous parallel model.[.]
Notes
References
*.
*.
*.
*.
*.
*.
*.
*{{citation
, last = Vuillemin , first = Jean , authorlink = Jean Vuillemin
, doi = 10.1145/358841.358852
, issue = 4
, journal = Commun. ACM
, location = New York, NY, USA
, pages = 229–239
, publisher = ACM
, title = A unifying look at data structures
, volume = 23
, year = 1980, doi-access = free
.
Articles with example pseudocode
Parallel computing
Search algorithms