HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

counting arithmetic progressions $a, a+r, a+2r$ in a list

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
progressionslistcountingarithmetic

Problem

I have a large list $L$ of numbers and I need to count instances of elements $a,b,c \in L$ such that $a +c = 2b$. A brute-force approach would be to check all possible triples $(a,b,c)$ and this will run in $L^3$ time.

Can I do any better?

Solution

Given that the OP is rather unprecise as to the kind of numbers he is
considering (though he later said integers or integers modulo $p$ in a comment), I will try to answer nevertheless, by trying to limit the
number of assumptions I can make. And while I am at it anyway, I will
generalize a bit.

This builds on the contributions of previous answer and comments, by
Rick Decker, Hendrik Jan. and HEKTO.

Generalized statement of the problem

We consider four sets of values $\mathcal{U}, \mathcal{V}, \mathcal{W}, \mathcal{Z}$, two functions $f:\mathcal{U}\times
\mathcal{V}\to \mathcal{Z}$ and $g:\mathcal{W}\to\mathcal{Z}$. Given 3
sets or
lists of values $U,V,W$, respectively from $\mathcal{U}, \mathcal{V},
\mathcal{W}$, count the number of triples $(u,v,w)\in U\times V\times W$ such
that $f(u,v)=g(w)$. Each computation of the functions $f$ and $g$ is supposed to
be in constant time.

Let $n=max(|U|,|V|,|W|)$

General solution

This is essentially HEKTO's suggestion.

Build a hash table containing each value $w\in W$ indexed by
$g(w)$. This has a cost $O(n)$ under the Simple Uniform Hashing Assumption(SUHA). Without the SUHA, one can use a balanced tree with complexity $O(n\log n)$.

Then for each $u\in U$ and for each $v\in V$ check whether
$f(u,v)$ is in the hash table in constant time (or in the balanced tree in logarithmic time). If it is in an entry $g(w)$, then add $(u,v,w)$ to the
list of solutions (or increase the solution count by one).

Complexity is $O(n^2)$ with hash table, under SUHA, or $O(n^2\log n)$ with a balanced tree.

Case of ordered sets

This is essentially Hendrik Jan's suggestion.

We assume that one of the two sets $\mathcal{U}$ or $\mathcal{V}$ (say $\mathcal{V}$, w.l.o.g.) and the
set $\mathcal{Z}$ are totally ordered and that the function $f$ is monotonic with respect to
the argument in the ordered set (here the second argument).

Then we can have a modified algorithm that does not need a hash table.

First sort the set $V$.

Build a list $L$ of pairs $(w,g(w))$ for all $w\in W$, sorted with respect
to the second element.

Then for each $u\in U$, compare the ordered values of $f(u,v)$ with
the ordered values $g(w)$ of the pairs in $L$, by moving up the two
list $V$ and $L$ as you would do for set intersection, and add the answer
$(u,v,w)$ whenever $f(u,v)=g(w)$ for some pair $(w,g(w))$.

Complexity is $O(n^2)$.

Application to "numbers" and arithmetic progression

We consider the initial problem of detecting arithmetic progressions
in the elements of a list.

The three lists of the abstract case are the same list. The function $f$
is addition, and the function $g$ is doubling (i.e. product by 2).

The ordered set solution applies well to integers, rationals, or reals (with
some care).

Complex numbers are not an ordered field. However they can be considered as an ordered groups with respect to addition, by prioritizing coordinates, so that $a+ib> a'+ib'$ iff $a>a' \vee (a=a' \wedge b>b')$. So the ordered set construction can be used.

The hash table algorithm seems necessary in the case of
$\mathbb{Z}/p$ (i.e. integers modulo $P$) since non-trivial linearly ordered groups are
necessarily infinite, which is not the case for $\mathbb{Z}/p$.

However the ordered set construction could be adapted to
$\mathbb{Z}/p$ without loss in complexity, by allowing the list $L$
to be scanned twice, with some proper bookkeeping.

Context

StackExchange Computer Science Q#28932, answer score: 3

Revisions (0)

No revisions yet.