Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How fast can 'finding the max in an array' possibly get?

This question comes from a discussion that was touched off on this other question: Parallelize already linear-time algorithm. It is not homework.

You are given an array of N numbers, and a machine with P processors and a shared CREW memory (Concurrent Read, Exclusive Write memory).

What is the tightest upper bound on the fastest algorithm to find the largest number in the array? [Obviously, also: What is the algorithm itself?]

I am not referring to the total amount of work performed [which can never be less than O(N)].

like image 217
ArjunShankar Avatar asked Dec 23 '11 11:12

ArjunShankar


People also ask

What is the complexity of finding maximum in an array?

We have to find the largest/ maximum element in an array. The time complexity to solve this is linear O(N) and space compexity is O(1).

What is the complexity of finding maximum and minimum value from an array of N values?

Time Complexity is O(n) and Space Complexity is O(1). For each pair, there are a total of three comparisons, first among the elements of the pair and the other two with min and max.

What is the maximum value of an array?

M = max( A ) returns the maximum elements of an array. If A is a vector, then max(A) returns the maximum of A . If A is a matrix, then max(A) is a row vector containing the maximum value of each column of A .

What is the best algorithm to find the maximum and the minimum numbers in an array?

Using divide and conquer: Idea similar to the merge sort Divide step: We divide the array into two equal parts around mid-value i.e divide the problem into two equal-size sub-problems. Conquer step: We recursively find the minimum and maximum of both left and right parts.


2 Answers

I think it's O(N/P') + O(Log2(P')), where P'=min{N,P}. P' processors search for max of N/P' elements each, followed by Log2 pairwise merges done in parallel. The first P'/2 merges are done by even-numbered processors, next 'P'/4' - by processors at locations divisible by 8, then by 16, and so on.

Edit P' is introduced to cover the case when you have significantly more processor nodes than the elements that you need to search.

like image 111
Sergey Kalinichenko Avatar answered Oct 08 '22 22:10

Sergey Kalinichenko


Cook, Dwork, and Reischuk showed that any CREW algorithm for finding the maximum of n elements must run in Omega(lg n) time, even with an unlimited number of processors and unlimited memory. If I remember correctly, an algorithm with a matching upper bound appears in their paper:

Stephen Cook, Cynthia Dwork, and Rüdiger Reischuk. Upper and lower time bounds for parallel random access machines without simultaneous writes. SIAM Journal on Computing, 15(1):87-97, 1986.

like image 24
Massimo Cafaro Avatar answered Oct 08 '22 20:10

Massimo Cafaro