Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

CMPSC 465 Assignment 2

Fill your name below


Name: Sashwat Anagolum
PSU Username: ssa5517

Format Requirement
Algorithms in pseudo code MUST be placed in code blocks/fences (5%), and use
either the cpp or java syntax highlighter.

Algorithms should follow the pseudo code standard described in handout 1. (2%)

Your pseudo code should be placed in a code block, for example

Algorithm alg_name(a, b) {
// A description of the algorithm
// input: a - a positive decimal integer; b - an integer
// ouput: the sum of a and b
c = a + b // or, c <- a + b
return c
}

Do NOT change the template except the answer portion. (5%)

Formulas and equations should be in math mode using Latex math symbols. (5%)

Markdown math tutorial:


http://tug.ctan.org/info/undergradmath/undergradmath.pdf

Ways enter into the math mode in Notion : https://www.notion.so/help/math-


equations

Export your work to a pdf file for submission

click the ... button at the top right of your notion page

in the dropdown menu, scroll down to select export .

in the popup window, select pdf to export the file.

CMPSC 465 Assignment 2 1


Problem Set

Problem 1
Consider a variation of sequential search that scans a list to return the number of
occurrences of a given search key in the list. Does its efficiency differ from the efficiency
of classic sequential search?

Answer:
Asymptotically, no. However, in the average case there is a constant factor difference.
There is no worst / best / average case for sequentially going through a list to find the
frequency of an element, since you always need to go through the entire list.
While performing regular sequential search, the best case is finding the key at the first
element, and the average case is traversing half of the elements in the list before finding
the key, making it twice as fast as the other variant of sequential search on average.

Problem 2
The range of a finite nonempty set of n real numbers S is defined as the difference
between the largest and smallest elements of S . For each representation of S given
below, describe an algorithm in pseudo code to compute the range. Indicate the time
efficiency classes of these algorithms using Big- O .

1. An unsorted array

2. A sorted array

3. A sorted singly linked list

4. A binary search tree

Answer:

1. Time complexity: O(n)

Algorithm rangeUnsortedArray(arr[0...n - 1]) {


// Computes the range of an array
// input: arr - array of real numbers
// ouput: the range of arr
minVal = arr[0]
maxVal = arr[0]

CMPSC 465 Assignment 2 2


for i = 1 to n - 1 do:
minVal = min(minVal, arr[i])
maxVal = max(maxVal, arr[i])

return maxVal - minVal


}

2. Time complexity: O(1)

Algorithm rangeSortedArray(arr[0...n - 1]) {


// Computes the range of an array
// input: arr - array of real numbers
// ouput: the range of arr
return arr[n - 1] - arr[0]
}

3. Time complexity: O(n)

Algorithm rangeSortedLinkedList(head) {
// Computes the range of an linked list
// input: head - head of linked list
// ouput: the range of the linked list
minVal = head->val

while head->next != nullptr do:


head = head->next

maxVal = head->val

return maxVal - minVal


}

4. Time complexity: O(n) worst case, O(log n) average case

Algorithm rangeBST(root) {
// Computes the range of an BST
// input: root - root of BST
// ouput: the range of the BST
leftNode = root
rightNode = root

while leftNode->left do:


leftNode = leftNode->left

CMPSC 465 Assignment 2 3


while rightNode->right do:
rightNode = rightNode->right

return rightNode->val - leftNode->val


}

Problem 3
Compute the following sums (you need to provide detailed steps) :

1. 1 + 3 + 5 + 7 + ⋯ + 999
n−1 i−1
2. ∑i=0 ∑j=0 (i + j)
Answer:

Part 1
500 500
This sum is equal to ∑i=1 2i − 1 = (2 ∑i=1 i) − 500.
500×501
This can be simplified to 2( 2 )− 500 = 500 × 500 = 25000.

Part 2

This sum can be simplified as follows:


n−1 i−1 n−1 i−1
∑i=0 ∑j=0 (i + j) = ∑i=0 i2 + ∑j=0 j

n−1 i−1 n−1 (i−1)i


∑i=0 i2 + ∑j=0 j = ∑i=0 i2 + 2

(i−1)i
∑i=0 i2 + 3
∑i=0 i2 − 12 ∑i=0 i
n−1 n−1 n−1
2 = 2

3 3 (n−1)n(2n−1) (n−1)n
∑i=0 i2 − 12 ∑i=0 i =
n−1 n−1
2 2 6 − 4

3 (n−1)n(2n−1) (n−1)n 2n3 −4n2 +2n n3 −2n2 +n


2 6 − 4 = 4 = 2

Problem 4

CMPSC 465 Assignment 2 4


Consider the following algorithm:

Algorithm Mystery(n) {
// Input:A nonnegative integer n
S = 0;
for i = 1 to n do {
S = S + i * i;
}
return S
}

1. What does this algorithm compute?

2. What is its basic operation?

3. How many times is the basic operation executed?

4. What is the efficiency class of this algorithm?

5. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency


class. If you cannot do it, try to prove that, in fact, it cannot be done.

Answer:
Part 1

It computes the sum of the squares of the numbers 1 to n.

Part 2
The basic operation is multiplication (addition or assignment can also be chosen).

Part 3

Multiplication is performed n times.

Part 4

The algorithm takes O(n) time.

Part 5

CMPSC 465 Assignment 2 5


We can compute this sum in O(1) time using the formula given below:
n
∑i=1 i2 = n(n+1)(2n+1)
6
.

Problem 5
Consider the following algorithm:

ALGORITHM Secret(A[0..n − 1]) {


//Input: An array A[0..n − 1] of n real numbers
minval = A[0];
maxval = A[0];
for i = 1 to n - 1 do {
if A[i]< minval {
minval = A[i]
}
if A[i]> maxval {
maxval = A[i]
}
}
return maxval − minval;
}

1. What does this algorithm compute?

2. What is its basic operation?

3. How many times is the basic operation executed?

4. What is the efficiency class of this algorithm?

5. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency


class. If you cannot do it, try to prove that, in fact, it cannot be done.

Answer:

Part 1

This algorithm computes the range of an array.

Part 2

The basic operation is the comparison.

CMPSC 465 Assignment 2 6


Part 3
There are 2(n − 1) comparisons performed.

Part 4

The algorithm takes O(n) time.

Part 5

A better algorithm does not exist for unsorted arrays. To see this, note that it takes
O(n) time simply to check all the elements in the array; and without looking at all the
elements of the array, we cannot guarantee that we have the minimum and maximum of
an array (and hence have computed the correct value for its range). Hence any
algorithm to compute the range of an array must take at least O(n) time. Of course,
constant factor improvements are possible in practice, using techniques such as loop
unrolling and multiprocessing.

Problem 6
Consider the following recursive algorithm for computing the sum of the first $n$ cubes:
S(n) = 13 + 23 + ⋯ + n3 .

ALGORITHM S(n) {
//Input: A positive integer n
//Output: The sum of the first n cubes
if n == 1 return 1;
else return S(n - 1) + n * n * n;
}

1. Set up and solve a recurrence relation for the number of times the algorithm’s basic
operation is executed.

2. How does this algorithm compare with the straightforward non-recursive algorithm
for computing this sum?

Answer:
Part 1

CMPSC 465 Assignment 2 7


The basic operation, multiplication, is performed twice in every function call. Thus, the
recurrence relation is T (n) = 2 + T (n − 1), and the base case is T (1) = 0. Solving
this yields T (n) = 2(n − 1), making T (n) ∈ O(n).

Part 2

The time complexity of this algorithm versus directly summing each term is the same
(both are O(n)). However, in practice the recursive solution will be slower due to the
overheads of recursion - performing stack operations to manage recursive calls, and
additional memory usage. Additionally, it is possible to compute this sum in O(1) time
using the formula
n2 (n−1)2
∑ni=1 i3 = 4
.

Problem 7
Consider the following recursive algorithm

ALGORITHM Q(n) {
//Input: A positive integer n
if n == 1 return 1;
else return Q(n - 1) + 2 * n - 1;
}

1. Set up a recurrence relation for this function’s values and solve it to determine what
this algorithm computes.

2. Set up a recurrence relation for the number of multiplications made by this algorithm
and solve it.

Answer:
Part 1:

The recurrence relation for the return value is: Q(n) = Q(n − 1) + 2n − 1. The base
case is Q(1) = 1 = 2(1) − 1.
Then, using backward substitution, we get:

Q(n) = Q(n − 2) + 2(n − 1) − 1 + 2n − 1


Q(n) = Q(n − 3) + 2(n − 2) − 1 + 2(n − 1) − 1 + 2n − 1

CMPSC 465 Assignment 2 8


From this we can see that the return value of Q(n) will be:
n
Q(n) = ∑i=1 2i − 1
Simplifying this, we get Q(n) = n(n + 1) − n = n2 .

Part 2:
Let the basic operation being considered be multiplication. Then we have T (n) = 1 +
T (n − 1), and the base case is T (1) = 0. Solving this gives T (n) = n − 1, which
makes T (n) ∈ O(n).

Problem 8
Consider the following recursive algorithm.

ALGORITHM Riddle(A[0..n − 1]) {


//Input: An array A[0..n − 1] of real numbers
if n == 1 return A[0];
else {
temp = Riddle(A[0..n − 2]);
if temp ≤ A[n − 1] return temp;
else return A[n − 1];
}
}

1. What does this algorithm compute?

2. Set up a recurrence relation for the algorithm’s basic operation count and solve it.

Answer:

Part 1:
This algorithm finds the minimum value in an array.

Part 2:
The basic operation here is the indexing operation (assignment, lesser than check can
also be chosen), which is used either once or twice in every function call. In either case,
the work done is constant, so we can simply consider just 1 basic operation in each
function call.

CMPSC 465 Assignment 2 9


Then, the recurrence relation for this algorithm becomes: T (n) = 1 + T (n − 1), with
the base case being T (1) = 1. Solving this yields T (n) = n, indicating that T (n) ∈
O(n).

Problem 9
Consider the following algorithm to check whether a graph defined by its adjacency
matrix is complete.

ALGORITHM GraphComplete(A[0..n − 1, 0..n − 1]) {


//Input: Adjacency matrix A[0..n − 1, 0..n − 1]) of an undirected graph G
//Output: 1 (true) if G is complete and 0 (false) otherwise
if n == 1 return 1; //one-vertex graph is complete by definition
else {
if not GraphComplete(A[0..n − 2, 0..n − 2]) return 0;
else {
for j = 0 to n - 2 do {
if A[n − 1, j] == 0 return 0;
}
return 1;
}
}
}

What is the algorithm’s efficiency class in the worst case?

Answer:
The worst case input for this algorithm is when the graph is complete. In this case, the
algorithm will take O(n2 ) time. This is because the for loop in every recursive call
except the call that is the base case (every call GraphComplete(A[ 0..i, 0..i ] for 1 <i<
n)) will execute the for loop in the nested else block.
The basic operation (considered to be checking for equality) is performed once, plus
once every iteration from 0 to i − 1, for a total of i + 1 times, in the function
call GraphComplete(A[ 0..i, 0..i ]) .
The recurrence relation for the number of basic operations will be T(n) = n + 1 + T(n -
1), with the base case being T(0) = 1.
Thus, the number of basic operations in all the recursive calls will be (n + 1) + n +
(n+1)(n+2)
(n − 1) + (n − 2) + (n − 3) + (n − 4) … . + 3 + 2 + 1 = 2
, which
makes T (n) ∈ O(n2 ).

CMPSC 465 Assignment 2 10


CMPSC 465 Assignment 2 11

You might also like