Question

In: Computer Science

Give an O(lg n)-time EREW algorithm to perform the prefix computation on an array x[1. ....

Give an O(lg n)-time EREW algorithm to perform the prefix computation on an array x[1. . n]. Do not use pointers, but perform index computations directly.

Solutions

Expert Solution

EREW:

It is Exclusive Read Exclusive Write (EREW) algorithm in which every memory cell can be read or written to by only one processor at a time.

A parallel random-access machine (PRAM) is a shared-memory abstract machine. As its name indicates, the PRAM was intended as the parallel-computing analogy to the random-access machine .

PRAM Models:

  1. Exclusive read exclusive write (EREW)—every memory cell can be read or written to by only one processor at a time
  2. Concurrent read exclusive write (CREW)—multiple processors can read a memory cell but only one can write at a time
  3. Exclusive read concurrent write (ERCW)—never considered
  4. Concurrent read concurrent write (CRCW)—multiple processors can read and write. A CRCW PRAM is sometimes called a concurrent random-access machine.

EREW is a PRAM model.

We can perform prefix sum by using EREW algorithm in O(logn) complexity.

SIMPLIFIED ALGORITHM:

Definition (Prefix Problem)

Input: an array A of n elements ai .

Output: All terms a1 × a2 × · · · × ak for k = 1, . . . , n.

× may be any associative operation.

Straightforward serial implementation:

P r e f i x ( A: Array [ 1 . . n ] ) {

/ / in−pla ce computation :

for i from 2 to n do {

A[ i ] := A[ i −1]∗A[ i ] ;

}

This takes O(logn) time to perform prefix computation by using EREW algorithm.


Related Solutions

Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of...
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of n integers for i ← 0 to n − 1 do s ← X[0] for j ← 1 to i do s ← s + X[j] A[i] ← s / (i + 1) return A ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Algorithm2 prefixAverages2(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of...
20. Give the values of x for which x > 500 lg x. Note: lg is...
20. Give the values of x for which x > 500 lg x. Note: lg is log base 2 (not ln).
give an algorithm where given as input an array A[1...n] withthe following property. There exists...
give an algorithm where given as input an array A[1...n] with the following property. There exists an index I such that if we append A[1...i−1] after A[i...n], we get an array in sorted increasing order. For simplicity you can assume that n is a power of 2.Give an efficient algorithm that returns the smallest element in A. Analyze the time complexity of your algorithm.Hint: you may want to compare A[1] and A[n/2].
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows: iqsort(A, p, q){ if p ≥ q, return; r=partition(A, p, q); //run quick sort on the low part quicksort(A, p, r − 1); //run insert sort on the high part insertsort(A, r + 1, q); } Compute the best-case, worst-case, and average-case complexities of iqsort.
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a number output result: true if x in a false otherwise Sideeffect NA Plan if (top < bottom) result := false else // get the middle of the array (refining) middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) // example what is midpoint between 6 and 8 // (8-6)/2 = 1 we need to add 6 to...
Algorithm problem 4 [3.1-4] Is (2^(n+1))∈O(2^n)? Is (2^(2n))∈O(2^n)?
Algorithm problem 4 [3.1-4] Is (2^(n+1))∈O(2^n)? Is (2^(2n))∈O(2^n)?
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 2. Give an efficient sorting algorithm for an array C[1, ..., n] whose elements are taken from the set {1, 2, 3, 4, 5}.
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 3. Give an efficient sorting algorithm for an array D[1, ..., n] whose elements are distinct (D[i] ̸= D[j], for every i ̸= j ∈ {1, ..., n}) and are taken from the set {1, 2, ..., 2n}.
Describe an O(n)-time algorithm that checks if brackets in a given arithmetic expression is properly placed,...
Describe an O(n)-time algorithm that checks if brackets in a given arithmetic expression is properly placed, i.e., all left brackets are matched with their right counterparts and there are no unmatched brackets between any pair of matched brackets. Note that there are generally more than one types of brackets and only those of the same type can match against each other. For simplicity, you can assume that all operands are represented as ‘a’ and addition, +, is the only available...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT