CSE 305 - Tutorial 2 - PRAM complexity model
In class problems
Below is a list of theoretical problems to be solved and discussed during the class. The homework assignment consisting of two theoretical problems will appear on the bottom of this page after the class.
In each of the problems below, you have to justify the correctness and complexity of your algorithm.
Consider the following problem:
Input: an array \(A\) of length \(n\) consisting of \(1\)’s and \(0\)’s
Output: the index of the leftmost \(1\) if there is at least one, and \(-1\) otherwise
Design an algorithm in EREW PRAM model with complexity \(\mathrm{O}(\log n)\).
Design an algorithm in CRCW PRAM (consistent mode) model with complexity \(\mathrm{O}(1)\).
Hint: compare with the algorithm for computing maximum from the lecture.
Solution. Let us consider the following algorithm:
- Create a new array \(B\) of length \(n\).
- For \(i = 1, \ldots, n\) in parallel, set \(B[i]\) to be \(i\) if \(A[i] = 1\) and \(n + 1\) otherwise.
- Compute \(c\), the minimal value in \(B\).
- If \(c \neq n + 1\), return \(c\). Otherwise, return \(-1\).
Clearly, the leftmost one, if exists, will yield the minimal value in \(B\) less than \(n + 1\), so the algorithm is correct. Complexity-wise, we see that the first two steps have constant complexity in both EREW and CRCW. The last step, as we know from the lecture, has complexity \(\mathrm{O}(\log n)\) and \(\mathrm{O}(1)\) in EREW and CRCW, respectively. Therefore, we get the required complexity.
Consider the following problem:
Input: a string \(S\) of length \(n\)
Output: True if \(S\) is a palindrome, and False otherwise
Design an algorithm in EREW PRAM model with complexity \(\mathrm{O}(\log n)\).
Design an algorithm in CRCW PRAM (consistent mode) model with complexity \(\mathrm{O}(1)\).
Refine your solution from the previous bullet to use \(\mathrm{O}(n)\) processors.
Solution. For the first two bullets, let us consider the following algorithm:
- Create a new array \(B\) of length \([n / 2]\).
- For \(i = 1, \ldots, [n / 2]\) in parallel, set \(B[i]\) to be \(1\) if \(A[i] = A[n - i]\) and \(0\) otherwise.
- Compute \(c\), the minimal value in \(B\).
- Return True if \(c = 1\) and False otherwise.
Similarly to the previous problem, this algorithm solves the problem within the required complexity for both EREW and CRCW.
Let us give a more direct solution for the CRCW case using linear number of processors.
- Create a variable
resand set it to True - For \(i = 1, \ldots, [n / 2]\) in
parallel: if \(A[i] != A[n - i]\), set
resto be False - return
res
Each step of \(\mathcal{O}(1)\).
Consider the following problem:
Input: an array \(A\) of length \(n\) in which \(A[i]\) is the cost of a stock on \(i\)-th day
Output: the maximal amount of money you can earn if you buy stock on some day and sell it on one of the subsequent days
Design an algorithm in EREW PRAM model with complexity \(\mathrm{O}(\log n)\).
Solution. We will present a recursive algorithm
Auxsolving the following subproblem:Input: Integers \(i < j\)
Output: For the days from \(i\)-th to \(j - 1\)-st, the following numbers: minimal and maximal prices of the stock, and the maximal amount which can be earned.
You are encouraged to justify yourself that the following divide-and-conquer aproach solves this algorithmic problem:
- If \(j - i = 1\), return \(A[i], A[i], 0\).
- Let \(m_1, M_1, p_1\) and \(m_2, M_2, p_2\) be the results of the
algorithm
Auxfor the pairs \(i, (i + j) / 2\) and \((i + j) / 2, j\). - Return \(\min(m_1, m_2), \max(M_1, M_2), \max(p_1, p_2, M_2 - m_1)\).
If we do the
Auxcomputations at the second step in parallel, then the parallel complexity of the whole computation on an array of size \(n\)
denoted by \(T(n)\) will satisfy the recurrence \(T(n) = T(n / 2) + \mathrm{O}(1)\). Unfolding this relation as in the lecture, we obtain \(T(n) = \mathrm{O}(\log n)\).Another solution. Let us form an array filled with numbers \(A[i] - A[j]\) for \(i > j\). This can be done in parallel in \(\mathrm{O}(\log n)\) (we can start with creating \(n^2\) copies of the input array in logarithmic time, and the compute the differences in one step). Then we can compute the maximum in \(\mathrm{O}(\log n^2) = \mathrm{O}(\log n)\).
Yet another solution. Since min is an associative binary operation, we can compute an auxiliary array \(B\) such that \(B[i] = \min\limits_{j=1,\ldots, i}A[j]\) in \(\mathrm{O}(\log n)\). Then we can compute the final result by computing \(\max\limits_{i = 1, \ldots, n} (A[i] - B[i])\) in \(\mathrm{O}(n)\) time.
Consider the following problem:
Input: an array \(A\) of length \(n\)
Output: the number of distinct elements in the array
Design an algorithm in EREW PRAM model with complexity \(\mathrm{O}(\log n)\).
Solution.
- Create \(n^2\) copies of the input array in \(\mathrm{O}(\log n)\) time.
- Use them with \(n^2\) processors to fill a matrix \(B\) such that \(B[i, j] = 1\) if \(i < j\) and \(A[i] = A[j]\) and \(B[i, j] = 0\) otherwise. This takes \(\mathrm{O}(1)\) times.
- Compute array \(C\) such that \(C[i] = 1 - \max B[i, j]\) (takes \(\mathrm{O}(\log n)\) time).
- Return the sum of the elements in \(C\) (takes \(\mathrm{O}(\log n)\) time).
The correctness algorithm follows from the fact that \(C[i] = 1\) if and only if \(A[i]\) is the rightmost among all the elements with the same value. So the sum of the elements in \(C\) is precisely the number of distinct elements in \(A\).
Consider the following problem:
Input: an array \(A\) of length \(n\) consisting of \(1\)’s and \(0\)’s
Output: the length of the longest continuous subarray consisting of \(1\)’s
Design an algorithm in EREW PRAM model with complexity \(\mathrm{O}(\log n)\).
Hint: use the jumping pointer technique from the lecture.
(optional) Design a CREW PRAM algorithm for mutiplying \(n \times n\) matrices in \(\mathcal(O)(\log n)\) times. How small can you make the number of processors?
(optional) Consider the following problem:
Input: an undirected graph defined by adjacency lists (that is, you are given an array of \(n\) arrays where the \(i\)-th array is a list of the neighbours of the \(i\)-th vertex) with \(n\) vertices and \(m\) edges.
Output: the number of connected components.
Design an algorithm with complexity \(\mathrm{O}(n)\) in PRAM CRCW model (consistent mode). Try to get it down to \(\mathrm{O}(\log n )\).
Hint: you may want to have “a thread per edge”, this is doable but requires care (and time!).
Homework assignment
Two problems below is your homework assignment with the deadline being 23:59 on April 1. The solutions shold be uploaded to Moodle. Note that your solution must contain explicit description of the algorithm, and the justification of the correctness and complexity.
Consider the following problem:
Input: an array \(A\) of length \(n\) of integers
Output: an array \(B\) containing the positive elements of \(A\) in the same order as they are in \(A\).
Design an algorithm in CREW PRAM model with complexity \(\mathrm{O}(\log n)\).
Consider the following problem:
Input: an array \(A\) of numbers of length \(n\)
Output:
Trueif the array is sorted in the increasing order, andFalseotherwisedesign an algorithm in EREW PRAM model with complexity \(\mathrm{O}(\log n)\)
design an algorithm in CRCW PRAM (consistent mode) model with complexity \(\mathrm{O}(1)\)