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)].
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).
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.
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With