CMPSC 465 Assignment 2: Name: Shaurya Aryan PSU Username: Sza5604 Format Requirement
CMPSC 465 Assignment 2: Name: Shaurya Aryan PSU Username: Sza5604 Format Requirement
Format Requirement
Algorithms in pseudo code MUST be placed in code blocks/fences (5%), and
use either cpp or java as the syntax highlight.
Algorithms should follow the pseudo code standard described in handout 1.
(2%)
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
Two ways to enter math mode:
Insert a pair of dollar signs: $ your equations go here $. This is the inline math
mode.
Insert a pair of double-dollar signs: $$ your equations go here $$, which
produces a standalone equation/formula set.
Problem Set
Problem 1
https://marxi.co/ Page 1 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
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:
Simply adding a counter to count the number of occurrences when a particular key is
found in the sequential search, starting from the first element to the last element.
Computational: simply finding the first occurrence of key and return its presence
(classical sequential search) has
- the worst case time complexity of ‘n’ comparisons with key present at the last or not
present at all.
- And the best case will occur if the key is present at the first location.
- while for finding out the occurrences of a key element, all the elements must be
scanned to ensure its presence upto last element. Hence, there is no best and worst
case here.
Problem 2
The range of a finite nonempty set of real numbers is defined as the difference
between the largest and smallest elements of . For each representation of given below,
describe an algorithm in pseudo code to compute the range. Indicate the time
efficiency classes of these algorithms using Big-.
1. An unsorted array
2. A sorted array
3. A sorted singly linked list
4. A binary search tree
Answer:
1. An unsorted array-Take the first number in list to initialize two variables “high” and
https://marxi.co/ Page 2 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
“low”.
Then we iterate through a set of operations. These operations include to check if
the next number in the list is greater than that of variable “high”, if so, update the
variable “high” otherwise we have to check if the next number in the list is smaller
than that of variable “low”, if so, update the variable “low”.This would be repeated
for all the numbers in the list. at the end of list we could get the least value in the
list and also the highest value in the list. Then, we can find the range easily by
calculating their difference. Time complexity for doing this would be \Theta(n),
where n is the length of the list or simply number of elements in the list. Therefore
Space complexity would be O(n)
2. A sorted array - In this, if we assume that the list is sorted in asscending order, we
can find that the first element in the list is the least value and the last element in
the list is the highest value. Calculating the difference between them would give
the range. Time complexity for accessing those values would be \Theta(2) , since
we are accessing two values in the list. Therefore Space complexity would be O(n)
3. A sorted singly linked list- Just like sorted array, we consider that the list is sorted
in asscending order. Then we could access the first element from the first node
and last element from the last node. Difference between these two node values
would be our range.Time complexity for accessing the first node value is
\Theta(1).Time complexity for accessing the last node value is \Theta \left ( n \right
). Therefore space complexity for the singly list consisting of n values is O(n)
4. A binary search tree - Here we got to search for two values, which is highest and
one which is the least. Then we can calculate the range by simply finding the
difference between them.Time complexity for searching each value in the binary
search tree is \Theta \left ( n \left log \left ( n \right ) \right )
Problem 3
Compute the following sums (you need to provide detailed steps) :
1.
2.
https://marxi.co/ Page 3 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
Answer:
1-
2-
Problem 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
https://marxi.co/ Page 4 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
Answer:
Ans 2- It is to multiplication.
Ans 5- Nothing further can be be done, since we have to compute the sum of square
of all numbers from i=1 to i=n we need to go through all numbers from i=1 to 1=n to
compute their square and get the total. It implies that atleast it takes O(n) time. Since
given algorithm takes O(n), we can not optimise it further.
Problem 5
Consider the following algorithm:
https://marxi.co/ Page 5 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
for i = 1 to n - 1 do {
if A[i]< minval {
minval = A[i]
}
if A[i]> maxval {
maxval = A[i]
}
}
return maxval − minval;
}
Answer:
1. It computes the difference between the largest and the smallest number present
in the array A.
2. The basic operation performed is to check if a certain number is greater than or
less than another number.
3. The for loop runs for n-1 iterations and for each iteration the basic operation runs
twice hence total times the operations are performed will be 2x(n-1) = 2n-2.
4. As there is just single loop running the efficiency of the algorithm is O(n), i.e. it
runs in linear time.
5. Nothing can be improved further as we have to traverse through all the elements
at least once to find the maximum and minimum value which is already done here
.
Problem 6
https://marxi.co/ Page 6 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
Consider the following recursive algorithm for computing the sum of the first cubes:
.
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:
$……….
https://marxi.co/ Page 7 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
sum = 0;
return sum;
In this case the sum variable will store the sum of first i term in ith iteration.
While in recursive method, n *n*n is added at the end, (n-1)^3 is added second
last.Because it is recursive first the function S(n-1) is computed and then n^3 is
added to the result of S(n-1). It will take more stack memory.
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
https://marxi.co/ Page 8 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
Answer:
Ans 1-
……
Ans -2 Recurrence relation for the number of multiplications made by this algorithm is
M(n) = M(n − 1) + 1 for n > 1, M(1) = 0.
Solving it by backward substitutions or by applying the formula for the nth term of an
arithmetical progression would give M(n) = n − 1.
Problem 8
Consider the following recursive algorithm.
https://marxi.co/ Page 9 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
else {
temp = Riddle(A[0..n − 2]);
if temp ≤ A[n − 1] return temp;
else return A[n − 1];
}
}
Answer:
1 - It computes the smallest number in the array A[0…n-1]; At each recusive step, the
function returns the smallest number yet found and in this way it return the smallest
number in the array.
Reccurrence Relation :
https://marxi.co/ Page 10 of 11
CMPSC 465 Assignment 2 2/7/20, 5:10 PM
Problem 9
Consider the following algorithm to check whether a graph defined by its adjacency
matrix is complete.
Answer:
Let the time required be T(N) therefore to check T(n) you first have a const time
condition then a function call which requires T(n-1) time then finally a linear O(n) time
to check the rows added newly.
So total would be T(n) = T(n-1) + O(n) [worst case], hence T(n) = O(n^2)
https://marxi.co/ Page 11 of 11