# How to find min or max in constant time using infinite # processors?

Given $n$ numbers and an infinite number of processors can the min or max be found in constant time? Assume a shared memory architecture (all processors can write to a common shared memory) and the ability to perform concurrent writes

I have tried dividing up the numbers into groups of 2 each and then using $n/2$ processors at the first step and then $n/4$ processors on the output of the first step etc. I can't see how to make use of more than $n/2$ processors given that I have $\infty$ of them

• Welcome to Computer Science! What have you tried? Where did you get stuck? We do not want to just do your (home-)work for you; we want you to gain understanding. However, as it is we do not know what your underlying problem is, so we can not begin to help. See here for a relevant discussion. If you are uncertain how to improve your question, why not ask around in Computer Science Chat? You may also want to check out our reference questions. – Raphael Jul 2 '16 at 11:33
• Added what I've tried/come up till now. Would appreciate any hint from the community to get me beyond this. – wabbit Jul 2 '16 at 13:51
• Have you tried proving a lower bound? You may want to look at circuit lower bounds. One word to google for is depth (and work) of problems. – Raphael Jul 2 '16 at 13:58

I managed to solve this so hoping it's alright to answer your own question. Initialize to all elements off, an $n$ bit vector $A$ having a 1:1 mapping with the $n$ numbers. Compare each number against every other number on $n \choose 2$ processors. Each processor switches the bit corresponding to the smaller of its two elements to 1. Assumption is that the processors can do concurrent writes to $A$. The bit in $A$ remaining off denotes the maximum element.

This is $\Theta(1)$ (constant time) as the depth of the computational graph is independent of the number of elements. The work required is however $\mathcal {O(n^{2})}$.

• Nice idea! However: 1) How do you initialize $n$ bits in $O(1)$ time? 2) How do you deal with conflicting writes? Many processors try to write the same bits at the same time! (That is to say, this will only work on some very specific machine models, and not at all on others.) 3) How do you find the 0-bit(s) in constant time? – Raphael Jul 3 '16 at 16:42
• Can you not use $n$ processors each writing to a different location in the array to initialize it? For finding the 0 bit also can't we again use $n$ processors each reading a different bit of $A$ and writing the index concurrently to a single location only if the corresponding bit is 0? Assumption is that there is no tie for the max element. – wabbit Jul 4 '16 at 15:55
• Yup, that might work. – Raphael Jul 4 '16 at 16:44

You can get constant time if you use n^2 processors. Basically, every processor makes 1 comparison and writes a 0 to a shared memory array of size n if their comparison is smaller. The index in the array with a 1 left in it at the end is the max.

If you find max and min using recursion, then the best case complexity is O(log n). So, I think you can get constant time, by dividing the work into n parts and assigning each of that partition to a processor. Infinite processors can be used only when n is too large.

• Did you mean "can get constont time" or "can't get constant time"? – gnasher729 Jul 3 '16 at 15:03
• How do you divide this work into $n$ parts that can be executed simultaneously? – Raphael Jul 3 '16 at 15:13
• Use divide and conquer to divide work just like merge sort! And I meant that the problem would require O(1) time! – kiner_shah Jul 4 '16 at 12:39
• I very much doubt O (1), that's why I asked. We want one answer, so the work done by all the processors needs to be combined into one answer. If you use m processors, that takes O (log m) . – gnasher729 Jul 5 '16 at 9:12