CS341 Advanced Topics in Algorithms: Assignment 2

advertisement
CS341 Advanced Topics in Algorithms: Assignment 2
Due Thursday 10 March 2016 at 12noon
The maximum mark for this assignment is 10 points, which represents 10% of the total
module credit.
1. [2 points] Let a be a number array of size
Pn. The maximum subarray problem asks for
a pair of indices i, k, such that the sum i≤j<k aj is maximised. The sum of an empty
subarray (i = k) is taken to be 0. Design an efficient BSP algorithm for solving the
maximum subarray problem. Give the algorithm’s asymptotic BSP costs and state all
necessary assumptions.
2. [2 points] Let a be a number array of size n. A subset of k indices 0 ≤ i0 < i1 < . . . <
ik−1 < n is said to define an alternating subsequence, if ai0 < ai1 , ai1 > ai2 , ai2 < ai3 ,
ai3 > ai4 , . . . Design an efficient BSP algorithm to determine the length k of the longest
alternating subsequence of array a. Give the algorithm’s asymptotic BSP costs and
state all necessary assumptions.
3. [2 points] A group of n children sit in row. Every child is given a numerical rating
of their school performance. The ratings are all different and are represented by a
number array of size n. The teacher wants to give at least one sweet to every child;
if two children sit next to one another, the one with a higher rating must get more
sweets. Design an efficient BSP algorithm to determine the number of sweets given
to every child, so that the total number of sweets is minimised. Give the algorithm’s
asymptotic BSP costs and state all necessary assumptions.
4. [2 points] Let a, b be character arrays of size n, and c a character array of size 2n.
Array c is said to be an interleaving of arrays a and b, if there exists a subset I of
indices from 0 to 2n − 1, such that the characters {c[i] | i ∈ I}, taken in order of
increasing indices, spell out array a, and the characters {c[i] | i 6∈ I}, taken in order of
increasing indices, spell out array b. Design an efficient BSP algorithm to determine if
the array c is an interleaving of arrays a and b. Give the algorithm’s asymptotic BSP
costs and state all necessary assumptions.
5. [2 points] Let a be an integer array of size n. Every integer between 0 and n − 1
inclusive appears exactly once in the array. A value a[i] = j is interpreted as a pointer
from element i to element j (in particular, we may have a self-pointer a[i] = i). By
starting at a given index i and following the pointers a[i], a[a[i]], . . . , we will eventually
come back to index i; such a circular sequence of pointers is called a cycle. The total
number of cycles in array a is an integer between 1 and n inclusive, and the lengths of
all cycles are positive integers adding up to n. Design an efficient BSP algorithm to
determine the total number of cycles and the length of every cycle. Give the algorithm’s
asymptotic BSP costs and state all necessary assumptions.
Download