CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
24.5.2016
1. Write in pseudocode a CRCW PRAM algorithm that sorts a list L[1:n] using less than n2
processors. The complexity must be (1). You can use any strategy to resolve concurrent
writes (e.g. a combining strate

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
8.1.2011
1. Write an algorithm for a CREW PRAM with n processors that multiplies two nXn matrices in
(n2) time. Analyze its complexity; find the cost and the speedup.
2. The radix sort algorithm for a list L[1:n] is a

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
4.8.2008
1. Consider the problem of finding the k th smallest number in a given list. That is, given a list of
integers x1,x2,xn and given an integer k, if xi1xi2xin, where i1,i2,in are between 1 and
n and they are di

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1. We can represent the problem as a dag D where the vertices of D correspond to the tasks and a
directed edge from u to v exists in D iff task u must precede task v. Suppose that edges are
stored as follows:

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
6.8.2007
1. Topological sorting problem: Suppose that there are n tasks to be performed and that certain
tasks must be performed before others. The problem is to obtain a linear ordering of the tasks in
such a way tha

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
01.06.2013
1. Define briefly the following:
a) -invariant under scaling
b) Stable sorting algorithm
c) Characteristic equation
d) SIMD model of computation
e) Semigroup operation
f) Cost of a parallel algorithm
g) Cos

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1. a) function SumEREW (L[1:n])
Model: EREW PRAM with p=n/log n processors
Input: L[1:n] (a list of size n)
Output: sum of elements in the list
for 1in/log n do in parallel
/ Each processor finds the sum of lo

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
6.1.2003
1. The search algorithm on Binary Search Tree (BST) is as follows: Suppose that the search
element is X. The current node is initialized to the root node and the loop is entered. If X is
equal to the current

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
10.1.2004
1. L[1:n] is a list of distinct numbers. Each element of the list is a k-digit positive integer, for
a fixed k. Write in pseudocode an algorithm with W(n)O(n) that sorts L.
2. The matrix-vector product of an

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1. The idea of the algorithm is as follows: Suppose that we multiply A and B to obtain C. The n
processors operate simultaneously and independent of each other. The i th processor multiplies
the ith row of the

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
7.1.2013
1. (Winograds matrix multiplication) Let A=[aij] and B=[bij], 1i,jn, be two n*n square matrices,
where n is a power of 2. Let C=A*B. To calculate C, first the rows of A and columns of B are
processed as follo

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1. a)
procedure Search1DMesh (L, value, n)
Model: 1-dimensional mesh Mp with p=n processors
Input: L (a list of size n), range: Pi, 1in
value (a search element), front end variable
Output: a boolean result ind

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
4.1.2005
1. Given a list L[1:n] of integers, we want to multiply the elements in the list (i.e. the output will
be the number L[1]*L[2]*L[n]). Write (in pseudocode) a cost optimal parallel algorithm on a
2-dimensional

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1.
a) This problem is completely parallelizable. Because, given sufficient number of processors,
we can compare all pairs of elements in parallel. In this way, for each element L[i] in the
list, we can determi

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1. The number of multiplications during preprocessing:
There are n/2 multiplications in calculating a row/column value
Thus, the total number of multiplications is 2n*(n/2) = n2
The number of additions during

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
13.1.2007
1. Given a list of n numbers a1,a2,.,an, write in pseudocode a parallel algorithm that
calculates a1+a2+.+an, for the following architectures:
a) EREW PRAM
b) 2-dimensional mesh
The algorithms must be cost o

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
3.1.2015
1. Write in pseudocode a comparison-based sorting algorithm for sorting a list of size n on 2dimensional mesh Mq,q, where n=q2. Suppose that initially each element of the list is in a
separate processor. The

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1.
procedure MinK_PT (L,n,k,Index)
Model: Processor tree PTp with p=2n-1 processors
Input: L (a list of elements), range: Pi , 0i<n (i is in log2n-digit binary representation)
k (an integer denoting the kth sm

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1. If X is equal to an element at level i in the tree (level of the root is 0), then 2i+1
comparisons are done. If X is not found in the tree, then 2(d+1) comparisons are done,
where d is the depth of the tree

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
11.1.2008
1. Consider the problem of comparison-based sorting (sorting a list L[1:n]). Suppose that the
elements in the list are distinct.
a) Apply Amdahls Law to this problem. Explain the reasoning you use and the re

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
15.1.2009
1. Consider the problem of searching on a list L[1:n] for a 1-dimensional mesh having n processors.
Suppose that initially the searched value is in the central control and the list was distributed to the
pro

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1.
a) -invariant under scaling: For a function f(n), if f(c*n)(f(n) for any positive
constant c, then f(n) is called -invariant under scaling.
b) Stable sorting algorithm: A sorting algorithm is a stable sorti

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
17.1.2006
1. Explain briefly the three goodness measures used to evaluate the quality of an interconnection
network. (For each measure; give a formula, explain what it indicates in 1 sentence, and explain
its importan

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1.
function SortCRCW (L[1:n])
Model: CRCW PRAM with p=n(n-1)/2 processors, write conflicts are resolved by
summing
Input: L[1:n] (an array of n elements)
Output: L[1:n] (sorted list in descending order)
for 1i

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1. (Radix sort. See the textbook.)
2. procedure matrix_vector_product (A, X, n)
Model: two-dimensional mesh Mn,n with p=n2 processors
Input: A (matrix to be multiplied) , range: Pi,j, 1i,jn
X (vector to be mul

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
21.1.2002
1. Solve the following recurrence relation:
n 1
T ( n) T (i ) 1
, n 2
i 1
T (1) 1
2. Calculate the average complexity A(n) of LinearSearch under the following assumptions about
the input list L[1:n] and sear

1
I. C MP E 300 H OMEWORK I A NSWERS
A. Question I
f (n) is given as:
f (n) = n3 log(n!)
(1)
and Stirlings approximation is as follows:
nn
en
Using Stirlings approximation, we can nd an approximate value for f (n).
n!
2n
(2)
1
1
f (n) n4 log(n) n4 log(e)

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
15.01.2010
1. One way of designing a probabilistic algorithm is randomizing a given deterministic
algorithm (i.e. by inserting some random commands, converting it into a probabilistic
algorithm). Suppose that A is a g

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL
11.8.2009
1. a) Explain the three main approaches of designing parallel algorithms. Explain briefly and
give an example for each.
b) As we know, a parallel algorithm is cost optimal if C(n)=W*(n). What is the reason b

CMPE 300 ANALYSIS OF ALGORITHMS
FINAL ANSWERS
1.
a) The first approach is, given a sequential algorithm, converting it into a parallel algorithm.
We change the code of the sequential algorithm such that those parts that are
parallelizable will be executed