ArticleslgStudy

computer science

All nearest smaller values

All nearest smaller values is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand All nearest smaller values rather than just read about it. In short: In computer science, the all nearest smaller values (ANSV) problem is: given an array A[1..n], compute for each position i the index P[i]=max⁡{j<i∣A[j]<A[i]}, or determine that no such j exists. The corresponding nearest-smaller value is A[P[i]].

Key takeaways

  • All nearest smaller values belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect All nearest smaller values to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of All nearest smaller values from memory before moving on to harder problems.

Reference excerpt

In computer science, the all nearest smaller values (ANSV) problem is: given an array A[1..n], compute for each position i the index P[i]=max⁡{j<i∣A[j]<A[i]}, or determine that no such j exists. The corresponding nearest-smaller value is A[P[i]]. Some presentations report A[P[i]], but many applications require the indices P[i]. This problem can be solved efficiently both by parallel and non-parallel algorithms: Berkman, Schieber & Vishkin (1993), who first identified the procedure as a useful subroutine for other parallel programs, developed efficient algorithms to solve it in the Parallel Random Access Machine model; it may also be solved in linear time on a non-parallel computer using a stack-based algorithm. Later researchers have studied algorithms to solve it in other models of parallel computation.

Example Suppose that the input is the following:

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 Berkman, Schieber & Vishkin (1993) 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. 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 introduced by Vuillemin (1980) and further studied by Gabow, Bentley & Tarjan (1984) for range searching applications. Cartesian trees also arise in the definition of the treap and randomized binary search tree data structures for binary searching. 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. 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, convex hull construction (parallelizing the sequential Graham scan 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: 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, 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 does not even need a stack; it assumes that the input sequence is given as an array A[1,n] of size n, and stores the index j of the preceding smaller value of the ith value A[i] in P[i]. We assume an artificial overall minimum at A[0]:

for i from 1 to n: j = i-1 while A[j] >= A[i]: j = P[j] P[i] = j

Parallel algorithms Berkman, Schieber & Vishkin (1993) showed how to solve the all nearest smaller values problem efficiently on a concurrent-read concurrent-write Parallel Random Access Machine. For a sequence of n values, stored as an array, they use a doubly logarithmic tree to 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 [1,s], Berkman, Matias & Ragde (1998) 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-structured communications network, and the bulk synchronous parallel model.

Notes

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with All nearest smaller values

Start with the simplest possible case. Write down what All nearest smaller values claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to All nearest smaller values before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about All nearest smaller values ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of All nearest smaller values

In research
All nearest smaller values appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses All nearest smaller values in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
All nearest smaller values is common in secondary-school and first-year university syllabi. It links to neighbouring topics Parallel computing, Search algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for All nearest smaller values outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “All nearest smaller values” →

How to study All nearest smaller values in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what All nearest smaller values means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain All nearest smaller values out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is All nearest smaller values in simple terms?

In computer science, the all nearest smaller values (ANSV) problem is: given an array A[1..n], compute for each position i the index P[i]=max⁡{j<i∣A[j]<A[i]}, or determine that no such j exists. The corresponding nearest-smaller value is A[P[i]].

Why does All nearest smaller values matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study All nearest smaller values?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on All nearest smaller values.

Tags

  • Parallel computing
  • Search algorithms

Keep exploring