# sum versus maximum in computer science

I would be interested in some References (books or articles) concerning an answer to the following question :

what is more difficult to calculate from the viewpoint of a computer : the "sum" or the "maximum" of a finite set of numbers ?

My guess is that "sum" and "maximum" are of the same level of difficulty, but I would like very much to know References concerning this question.

Thank you in advance.

George

• Not a research-level theoretical computer science question. – Dave Clarke Dec 3 '10 at 11:40
• @Dave: But I guess it could be turned into a research-level question. Can we come up with a model that separates sum and max? For example, could we show that in certain models, computing the sum of $n$ $b$-bit numbers requires strictly more extra space (in addition to the read-only input tape and write-only output tape) than max? Or is the answer always trivial? – Jukka Suomela Dec 3 '10 at 12:09
• What is the measure of difficulty your are interested in? What is the model of computation? – Kaveh Dec 3 '10 at 14:03
• I like the answers more than the question ! I wish we could change the question to the one Jukka proposes and use Kaveh's answer ;) – Suresh Venkat Dec 3 '10 at 17:14

## 3 Answers

The sum of finitely many binary numbers can be computed in $TC^0$ and is in fact complete for $TC^0$ under $AC^0$ reductions.

The maximum of finitely many binary numbers can be computed in $AC^0$.

So from the circuit-complexity perspective, sum is more difficult.

• Really? Could you please tell us how to compute the maximum of $n$ $n$-bit numbers in AC$^0$? – slimton Dec 3 '10 at 16:04
• @slimton: Note first that we can compare two numbers in $AC^0$. Then here is how to compute the maximum of $n$ numbers $x_1,\dots,x_n$, each of $n$ bits. First we compare all pairs of numbers. Next we compute for each $x_i$ the AND of all tests $x_i \geq x_j$. For each bit of $x_i$ we take the AND with this and the bit. Now, all numbers that are not equal to the maximum has been zeroed out. Now take the bitwise OR of all these numbers. – Kristoffer Arnsfelt Hansen Dec 3 '10 at 16:36

From pure algorithmic PoV in both cases there are N-1 addition operations.

For a computer though MAX might be harder to perform as there is one additional branch - determining the index of the currently max number. In sum operation this is not needed as interim result is just reused on the next number.

• I do not have to add even once for finding the maximum. – Raphael Dec 3 '10 at 12:57
• Any comparison operation in effect does a subtraction and checks for 0 result. Subtraction is addition with different sign. Modern processors might do it differently but it all boils down to checking bits one against another and then branching based on the result (which i what I meant). Perhaps tried to say it in too few words. – Георги Кременлиев Dec 3 '10 at 14:25
• The fact stands that for comparison I only have to check from the MSB downward to the first difference while for addition I have to look at all bits. This makes a difference. Comparing by subtracting is not a good implementation (in terms of TM). – Raphael Dec 5 '10 at 13:25

What machine model do you want to use?

On RM, both are equally hard if you assume that $+$ and $<=$ are equally expensive (common assumption).

On 1-TM, sum is slightly more expensive since we have to add all bits compared to comparing only the the most signifikant bits (up to the first difference).