3SUM
   HOME

TheInfoList



OR:

In
computational complexity theory In theoretical computer science and mathematics, computational complexity theory focuses on classifying computational problems according to their resource usage, and relating these classes to each other. A computational problem is a task solved by ...
, the 3SUM problem asks if a given set of n real numbers contains three elements that sum to zero. A generalized version, ''k''-SUM, asks the same question on ''k'' numbers. 3SUM can be easily solved in O(n^2) time, and matching \Omega(n^) lower bounds are known in some specialized
models of computation In computer science, and more specifically in computability theory and computational complexity theory, a model of computation is a model which describes how an output of a mathematical function is computed given an input. A model describes how ...
. It was conjectured that any deterministic algorithm for the 3SUM requires \Omega(n^2) time. In 2014, the original 3SUM conjecture was refuted by Allan Grønlund and Seth Pettie who gave a deterministic algorithm that solves 3SUM in O(n^2 / ( / )^) time. Additionally, Grønlund and Pettie showed that the 4- linear decision tree complexity of 3SUM is O(n^\sqrt) . These bounds were subsequently improved. The current best known algorithm for 3SUM runs in O(n^2 (\log \log n)^ / ) time. Kane, Lovett, and Moran showed that the 6- linear decision tree complexity of 3SUM is O(n). The latter bound is tight (up to a logarithmic factor). It is still conjectured that 3SUM is unsolvable in O(n^) expected time. When the elements are integers in the range N, \dots, N/math>, 3SUM can be solved in O(n + N\log N) time by representing the input set S as a
bit vector A bit array (also known as bitmask, bit map, bit set, bit string, or bit vector) is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level p ...
, computing the set S+S of all pairwise sums as a
discrete convolution In mathematics (in particular, functional analysis), convolution is a mathematical operation on two functions ( and ) that produces a third function (f*g) that expresses how the shape of one is modified by the other. The term ''convolution'' ...
using the fast Fourier transform, and finally comparing this set to S.


Quadratic algorithm

Suppose the input array is S ..n-1/math>. In integer (
word RAM In theoretical computer science, the word RAM (word random-access machine) model is a model of computation in which a random-access machine does bitwise operations on a word of bits. Michael Fredman and Dan Willard created it in 1990 to simulate p ...
) models of computing, 3SUM can be solved in O(n^2) time on average by inserting each number S /math> into a
hash table In computing, a hash table, also known as hash map, is a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an ''index'', als ...
, and then, for each index i and j, checking whether the hash table contains the integer -(S S . It is also possible to solve the problem in the same time in a
comparison Comparison or comparing is the act of evaluating two or more things by determining the relevant, comparable characteristics of each thing, and then determining which characteristics of each are similar to the other, which are different, and t ...
-based model of computing or
real RAM In computing, especially computational geometry, a real RAM (random-access machine) is a mathematical model of a computer that can compute with exact real numbers instead of the binary fixed point or floating point numbers used by most actual com ...
, for which hashing is not allowed. The algorithm below first sorts the input array and then tests all possible pairs in a careful order that avoids the need to
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 ...
for the pairs in the sorted list, achieving worst-case O(n^2) time, as follows. sort(S); for i = 0 to n - 2 do a = S start = i + 1; end = n - 1; while (start < end) do b = S
tart A tart is a baked dish consisting of a filling over a pastry base with an open top not covered with pastry. The pastry is usually shortcrust pastry; the filling may be sweet or savoury, though modern tarts are usually fruit-based, sometimes with ...
c = S nd if (a + b + c

0) then output a, b, c; // Continue search for all triplet combinations summing to zero. // We need to update both end and start together since the array values are distinct. start = start + 1; end = end - 1; else if (a + b + c > 0) then end = end - 1; else start = start + 1; end end The following example shows this algorithm's execution on a small sorted array. Current values of a are shown in red, values of b and c are shown in magenta. -25 -10 -7 -3 2 4 8 10 (a+b+c

-25) -25 -10 -7 -3 2 4 8 10 (a+b+c

-22) . . . -25 -10 -7 -3 2 4 8 10 (a+b+c

-7) -25 -10 -7 -3 2 4 8 10 (a+b+c

-7) -25 -10 -7 -3 2 4 8 10 (a+b+c

-3) -25 -10 -7 -3 2 4 8 10 (a+b+c

2) -25 -10 -7 -3 2 4 8 10 (a+b+c

0) The correctness of the algorithm can be seen as follows. Suppose we have a solution a + b + c = 0. Since the pointers only move in one direction, we can run the algorithm until the leftmost pointer points to a. Run the algorithm until either one of the remaining pointers points to b or c, whichever occurs first. Then the algorithm will run until the last pointer points to the remaining term, giving the affirmative solution.


Variants


Non-zero sum

Instead of looking for numbers whose sum is 0, it is possible to look for numbers whose sum is any constant ''C''. The simplest way would be to modify the original algorithm to search the hash table for the integer . Another method: * Subtract ''C''/3 from all elements of the input array. * In the modified array, find 3 elements whose sum is 0. For example, if A=
,2,3,4 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
and if you are asked to find 3SUM for ''C''=4, then subtract 4/3 from all the elements of A, and solve it in the usual 3sum way, i.e., .


Three different arrays

Instead of searching for the 3 numbers in a single array, we can search for them in 3 different arrays. I.e., given three arrays X, Y and Z, find three numbers , such that . Call the 1-array variant 3SUM×1 and the 3-array variant 3SUM×3. Given a solver for 3SUM×1, the 3SUM×3 problem can be solved in the following way (assuming all elements are integers): * For every element in ''X'', ''Y'' and ''Z'', set: , , . * Let ''S'' be a concatenation of the arrays ''X'', ''Y'' and ''Z''. * Use the 3SUM×1 oracle to find three elements such that . * Return . By the way we transformed the arrays, it is guaranteed that .


Convolution sum

Instead of looking for arbitrary elements of the array such that: :S S S /math> the ''convolution 3sum'' problem (Conv3SUM) looks for elements in specific locations: :S +jS S /math>


Reduction from Conv3SUM to 3SUM

Given a solver for 3SUM, the Conv3SUM problem can be solved in the following way. * Define a new array ''T'', such that for every index ''i'': T 2n S i (where ''n'' is the number of elements in the array, and the indices run from 0 to ''n''-1). * Solve 3SUM on the array ''T''. Correctness proof: * If in the original array there is a triple with S +jS S /math>, then T +j2n S +ji+j = (2n S + i) + (2n S + j)=T T /math>, so this solution will be found by 3SUM on ''T''. * Conversely, if in the new array there is a triple with T T T /math>, then 2n S + k = 2n(S S + (i+j). Because i+j<2n, necessarily S = S S /math> and k=i+j, so this is a valid solution for Conv3SUM on ''S''.


Reduction from 3SUM to Conv3SUM

Given a solver for Conv3SUM, the 3SUM problem can be solved in the following way. The reduction uses a
hash function A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called ''hash values'', ''hash codes'', ''digests'', or simply ''hashes''. The values are usually u ...
. As a first approximation, assume that we have a linear hash function, i.e. a function ''h'' such that: :h(x+y)=h(x)+h(y) Suppose that all elements are integers in the range: 0...''N''-1, and that the function ''h'' maps each element to an element in the smaller range of indices: 0...''n''-1. Create a new array ''T'' and send each element of ''S'' to its hash value in ''T'', i.e., for every ''x'' in ''S''(): :T
(x) An emoticon (, , rarely , ), short for "emotion icon", also known simply as an emote, is a pictorial representation of a facial expression using characters—usually punctuation marks, numbers, and letters—to express a person's feelings, ...
= x Initially, suppose that the mappings are unique (i.e. each cell in ''T'' accepts only a single element from ''S''). Solve Conv3SUM on ''T''. Now: * If there is a solution for 3SUM: z=x+y, then: T (z)T
(x) An emoticon (, , rarely , ), short for "emotion icon", also known simply as an emote, is a pictorial representation of a facial expression using characters—usually punctuation marks, numbers, and letters—to express a person's feelings, ...
T (y)/math> and h(z)=h(x)+h(y), so this solution will be found by the Conv3SUM solver on ''T''. * Conversely, if a Conv3SUM is found on ''T'', then obviously it corresponds to a 3SUM solution on ''S'' since ''T'' is just a permutation of ''S''. This idealized solution doesn't work, because any hash function might map several distinct elements of ''S'' to the same cell of ''T''. The trick is to create an array by selecting a single random element from each cell of ''T'', and run Conv3SUM on . If a solution is found, then it is a correct solution for 3SUM on ''S''. If no solution is found, then create a different random and try again. Suppose there are at most ''R'' elements in each cell of ''T''. Then the probability of finding a solution (if a solution exists) is the probability that the random selection will select the correct element from each cell, which is (1/R)^3. By running Conv3SUM R^3 times, the solution will be found with a high probability. Unfortunately, we do not have linear perfect hashing, so we have to use an almost linear hash function, i.e. a function ''h'' such that: :h(x+y)=h(x)+h(y) or :h(x+y)=h(x)+h(y)+1 This requires to duplicate the elements of ''S'' when copying them into ''T'', i.e., put every element x\in S both in T
(x) An emoticon (, , rarely , ), short for "emotion icon", also known simply as an emote, is a pictorial representation of a facial expression using characters—usually punctuation marks, numbers, and letters—to express a person's feelings, ...
/math> (as before) and in T
(x) An emoticon (, , rarely , ), short for "emotion icon", also known simply as an emote, is a pictorial representation of a facial expression using characters—usually punctuation marks, numbers, and letters—to express a person's feelings, ...
1. So each cell will have 2''R'' elements, and we will have to run Conv3SUM (2R)^3 times.


3SUM-hardness

A problem is called 3SUM-hard if solving it in subquadratic time implies a subquadratic-time
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
for 3SUM. The concept of 3SUM-hardness was introduced by . They proved that a large class of problems in
computational geometry Computational geometry is a branch of computer science devoted to the study of algorithms which can be stated in terms of geometry. Some purely geometrical problems arise out of the study of computational geometric algorithms, and such problems ar ...
are 3SUM-hard, including the following ones. (The authors acknowledge that many of these problems are contributed by other researchers.) * Given a set of lines in the plane, are there three that meet in a point? * Given a set of non-intersecting axis-parallel line segments, is there a line that separates them into two non-empty subsets? * Given a set of infinite strips in the plane, do they fully cover a given rectangle? * Given a set of triangles in the plane, compute their measure. * Given a set of triangles in the plane, does their union have a hole? * A number of visibility and motion planning problems, e.g., ** Given a set of horizontal triangles in space, can a particular triangle be seen from a particular point? ** Given a set of non-intersecting axis-parallel line segment obstacles in the plane, can a given rod be moved by translations and rotations between a start and finish positions without colliding with the obstacles? By now there are a multitude of other problems that fall into this category. An example is the decision version of
X + Y sorting In computer science, \boldsymbol+\boldsymbol sorting is the problem of sorting pairs of numbers by their sums. Applications of the problem include transit fare minimisation, VLSI design, and sparse polynomial multiplication. As with comparis ...
: given sets of numbers and of elements each, are there distinct for ?


See also

*
Subset sum problem The subset sum problem (SSP) is a decision problem in computer science. In its most general formulation, there is a multiset S of integers and a target-sum T, and the question is to decide whether any subset of the integers sum to precisely T''.'' T ...


Notes


References

* * * *. * *. *. *. *. *{{citation , last = King , first = James , title = A survey of 3SUM-hard problems , url = http://www.cs.mcgill.ca/~jking/papers/3sumhard.pdf , year = 2004. Computational geometry Polynomial-time problems Unsolved problems in computer science