MTE Solution Sketch and Grading Policy

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

CSN-212 Design and Analysis of Algorithms

[A+B] Mid Term Exam (Max Marks 100)


Solution sketch and Grading Policy
SOLUTIONS QUES 1-3 are not provided as they are DIRECTLY from class material.

1. Answer exactly ONE of the following TWO questions: Maximum Marks: 10


(Aa) Euclid’s Algorithm. Describe Euclid’s algorithm to compute the GCD of two numbers, and
analyze its complexity to give an exact bound.
Grading Policy.
i. [5 Marks] Algorithm.
ii. [5 Marks] Complexity Analysis. 3 Marks upper bound, 2 Marks lower bound.
iii. Deduct 20-100% in the corresponding part based on the severity of the mistake.
(Ab) Dynamic Arrays. Describe the construction of dynamic arrays (insertion and deletion at the
end) and analyze it using the potential method to prove constant amortized bound.
Grading Policy.
i. [5 Marks] Algorithm.
ii. [5 Marks] Amortized Analysis.
iii. Deduct 20-100% in the corresponding part based on the severity of the mistake.
(Ba) Wonderful Chocolate. Describe and analyze an algorithm to compute the number of unique
chocolates of size a × b cells of white and black which are wonderful (no sub-rectangle of both
length > 1 and width > 1 has the same colour). Given that 1 ≤ a ≤ 6 and 1 ≤ b ≤ 263 .
Grading Policy.
i. [5 Marks] DP Formulation. Define the variable used [2 Marks]
ii. [3 Marks] Matrix Formulation. Describe the base case and size of the matrix [1 Marks].
iii. [2 Marks] Complexity Analysis. Time complexity in terms of input.
iv. Deduct 90% for ω(23a log b) solution.
v. Deduct 20-100% in the corresponding part based on the severity of the mistake.
(Bb) Shortest Path using at most k steps. Describe and analyze an algorithm using divide and
conquer strategy to compute the shortest path between any pair of vertices using at most k edges.
Grading Policy.
i. [3 Marks] DP Formulation. Define the variable used [1 Marks]
ii. [5 Marks] Matrix Formulation. Define the modified operation [2 Marks].
iii. [2 Marks] Complexity Analysis. Time complexity in terms of input.
iv. Deduct 90% for ω(n3 log k) solution.
v. Deduct 20-100% in the corresponding part based on the severity of the mistake.

1
2. Answer exactly ONE of the following TWO questions: Maximum Marks: 15
(Aa) Multistacks. Consider a multistack, i.e., an infinite series of stacks S0 , S1 , S2 , · · ·, the capacity of
Si is 3i . For any full stack Si , an element is pushed by popping all its elements and pushing them
on Si+1 before the push. Similarly, for any empty stack Si , an element is popped by popping 3i
elements from Si+1 and pushing them onto Si before the pop. Prove that for any sequence of n
push and pop operations on S0 , the amortized time for each operation is O(log n).
Grading Policy. No explicit grading policy. Deduct marks based on mistakes.
(Ab) Palindrome Swaps. Given n strings, each of length m having lowercase english letters. A
swap operation swap(i, j) (where i ∈ [1, n], j ∈ [1, m]) swaps the j th character of Si with the j th
character of Si+1 . Design and analyze an algorithm to compute the minimum number of swaps
to convert every string into a palindrome.
Grading Policy. Marks Distribution:
i. [12 Marks] Algorithm,
ii. [3 Marks] Analysis.
iii. Deduct 80% marks if ω(mn log n) algorithm given.
(Ba) Closest Pair of Points. Given a set of n points in a 2D plane, describe and analyze an algorithm
using divide and conquer strategy to compute the closest pair of points.
Grading Policy. Marks Distribution:
i. [10 Marks] Algorithm,
ii. [5 Marks] Analysis.
iii. Deduct 80% marks if ω(n log2 n) algorithm given and 60% if Θ(n log2 n) given.
(Bb) Median of Medians. Given an array of n numbers, describe and analyze the pick algorithm to
compute the k th ranked number in the sorted array in linear time.
Grading Policy. Marks Distribution:
i. [10 Marks] Algorithm,
ii. [5 Marks] Analysis.
iii. Deduct 80% marks if ω(n) algorithm given.
3. Answer exactly ONE of the following THREE questions: Maximum Marks: 25
(a) Disjoint Set Union. Describe the amortized analysis of the DSU data structure, using the
properties of rank and groups as a black box (without explicit proof).
Grading Policy. Not explicit, marks are deducted based on mistakes or incompleteness.
(b) Fast Fourier Transform. Describe and analyze the divide and conquer algorithm to compute
FFT using properties of roots of unity as a black box (without explicit proof).
Grading Policy. Not explicit, marks are deducted based on mistakes or incompleteness.
(c) String Matching using FFT. Given a text T and a pattern P , describe and analyze an algo-
rithm to compute the number of occurrences of P in T using Fast Fourier Transform. Also, prove
the correctness of the algorithm explicitly.
Grading Policy. Marks Distribution:
i. [10 Marks] Polynomial Definition,
ii. [10 Marks] Proof for matched,
iii. [5 Marks] Analysis.
4. Answer exactly ONE of the following TWO questions: Maximum Marks: 20

2
(Aa) Maximum Difference in an array. Given an array A of n positive integers, the difference
of the ordered pair (i, j) where i ≤ j is A[j] − A[i]. Design and analyze a divide and conquer
algorithm to compute the pair having the maximum difference.
Solution. Following subproblems:
M axD[a, b] = Maximum difference for a ≤ i ≤ j ≤ b.
max[a, b] = Maximum element in A[a], · · · , A[b]
min[a, b] = Minimum element in A[a], · · · , A[b]
Divide step: Compute for first half and second half recursively.
Combine step: Base case 0 added for case of decreasing array, either by going till a = b or 0.
M axD[a, b] = max(M axD[a, a+b a+b a+b a+b
2 ], M axD[ 2 + 1, b], max[ 2 + 1, b] − min[[a, 2 ]], 0)
max[a, b] = max(max[a, a+b a+b
2 ], max[ 2 + 1, b])
a+b a+b
min[a, b] = max(min[a, 2 ], min[ 2 + 1, b])
Grading Policy.
i. Distribution: [15 Marks] Algorithm (define variables 5 Marks), [5 Marks] Analysis.
ii. Deduc t 90% if non-divide and conquer algorithm presented.
iii. Deduct 70% if complexity ω(n).
iv. Deduct 10% if decreasing array not handled.
(Ab) Rank in two sorted arrays. Given two sorted Arrays of size m and n. Design and analyze an
algorithm to compute the k th smallest number in the union of two arrays.
Solution. Clearly if m > k or n > k we can ignore the elements after k, so effectively m, n ≤ k.
Various variants of solutions described in have O(log k) complexity assuming the above fact.
https://www.geeksforgeeks.org/k-th-element-two-sorted-arrays/
Grading Policy.
i. Distribution: [15 Marks] Algorithm, [5 Marks] Analysis.
ii. Deduct 60% if complexity is Θ(log2 k) or Θ(log m log n) etc.
iii. Deduct 80% if complexity is ω(log2 k) or ω(log m log n)/
iv. Deduct 1 Mark if space not analyzed.
(Ba) Maximum Sum Subarray. Given an array A of n integers, the sum of its sub-array is A[i, j] =
A[i] + A[i + 1] + ... + A[j]. Design and analyze a divide and conquer algorithm to compute the
subarray having the maximum sum.
Solution. Following subproblems:
M axSm[a, b] = Maximum sum A[i, j] for a ≤ i ≤ j ≤ b.
maxP x[a, b] = Maximum sum of a prefix A[a, i] for a ≤ i ≤ b
maxSx[a, b] = Maximum sum of a suffix A[i, b] for a ≤ i ≤ b
totalSm[a, b] = Total sum of array A[a, b].
Divide step: Compute for first half and second half recursively.
Combine step: Base case minimum for case of negative array, by going till a = b.
M axSm[a, b] = max(M axSm[a, a+b a+b a+b a+b
2 ], M axSm[ 2 + 1, b], maxP x[ 2 + 1, b] + maxSx[[a, 2 ]])
a+b a+b a+b
maxP x[a, b] = max(maxP x[a, 2 ], totalSm[a, 2 ] + maxP x[ 2 + 1, b])
maxSx[a, b] = max(maxSx[a, a+b a+b a+b
2 ] + totalSm[ 2 + 1, b], maxSx[ 2 + 1, b])
a+b a+b
totalSm[a, b] = totalSm[a, 2 ] + totalSm[ 2 + 1, b]
Grading Policy.
i. Distribution: [15 Marks] Algorithm, [5 Marks] Analysis.
ii. Deduc t 90% if non-divide and conquer algorithm presented.
iii. Deduct 70% if complexity ω(n).
iv. Deduct 10% if negative array not handled.

3
(Bb) k-way merge. Suppose you have k sorted arrays, each with n elements. Design and analyze an
algorithm to combine them into a single sorted array of kn elements.
Solution. Merge k arrays similar to merge sort (assume starting from log n the level such that
each recursively solved subproblem already has n elements). This can be done in O(log k) levels,
each level merging nk elements with each other. Time complexity is O(nk log k), space complexity
is O(nk).
Grading Policy.
i. Distribution: [15 Marks] Algorithm, [5 Marks] Analysis.
ii. Deduct 80% if complexity ω(nk log k).
iii. Deduct 1 Mark if space not analyzed.

5. Answer exactly ONE of the following TWO questions: Maximum Marks: 30


(Aa) Chef ’s scheme. Utopia at IIT Roorkee is planning to serve students during exam season using
four dishes A, B, C, and D, where each day one is the day’s dish which is given for free. Consecutive
days must have a different dish of the day, and that of the first day and the last day of the exam
season must be different. Design and analyze an algorithm to compute the number of such possible
schedules for n days using the four dishes.
Solution. DP solution using Matrix exponentiation.
k
SXY = Number of ways to assign dishes for first k days such that first dish is X and last is Y .
k k−1 k−1 k−1
SAA = SAB + SAC + SAD
k k−1 k−1 k−1
SAB = SAA + SAC + SAD
1 1 1 1
Hence, we get a 16 × 16 matrix, with base case SAA = SBB = SCC = SDD = 1 and rest zero.
Alternatively: Solution can be simplified by ignoring the first dish and restricting the corre-
sponding solution on the base case.
k
SX = Number of ways to assign dishes for first k days such that last dish is X.
k k−1 k−1 k−1
SA = SB + SC + SD
1 1
Hence, we get a 4 × 4 matrix with 4 different base cases (SA = 1 and rest zero, or SB = 1 and
rest zero and so on) whose sum is the answer.
k k k
Alternatively: Solution can be simplified given that all SXX will be equal (Ssame ) and SXY
k
will be equal (Sdif f ). We get
k k−1
Ssame = 3Sdif f
k k−1 k−1
Sdif f = Ssame + 2Sdif f
1 1
Hence, we get a 2 matrix with base case Ssame = 4 and Sdif f = 0.
Complexity is the same for all cases O(log n).
Details. https://discuss.codechef.com/t/newsch-editorial/706
Grading Policy.
i. [15 Marks] DP Formulation. Clearly define the variable used [5 Marks]
ii. [10 Marks] Matrix Formulation. Describe the base case and size of the matrix [5 Marks].
iii. [5 Marks] Complexity Analysis. Time complexity in terms of input.
iv. Deduct 90% for ω(log n) solution.
v. Deduct 20-100% in the corresponding part based on the severity of the mistake.
(Ab) Self Matching. Given a string s of n containing three characters a, b, and c. The self-matching
score of s at index k is the number of indices i such that s[i] = s[i + k]. Design and analyze an
algorithm to compute the value of k having the maximum self matching score.
Solution. https://problemsolvingnotes.wordpress.com/2012/08/28/spoj-maxmatch/
Grading Policy.

4
i. [15 Marks] Describe the polynomial formulation.
ii. [10 Marks] Describe and prove the procedure for computation of results using FFT.
iii. [5 Marks] Complexity Analysis. Time complexity in terms of input.
iv. Deduct 90% for ω(log n) solution.
v. Deduct 20-100% in the corresponding part based on the severity of the mistake.
(Ba) Random Walk. Facing the extreme exam stress, the students of IIT Roorkee are found sleep-
walking at 2 am. They can take steps in any direction but are still aware enough to avoid short
circles and short-term repetitions. We need to compute the number of possible paths taken using
n steps starting from the library. Essentially, compute all possible strings of {L, R, D, U } such
that circles (or U RDL, DLU R, U LDR, RDLU , etc.) are avoided where as U DLR or RLDU are
allowed. Further, immediate and alternate characters should not be the same, or ith character
cannot be the same as i + 1th or i + 2th character. Design and analyze an algorithm to compute
number of such strings of length n.
Solution. https://discuss.codechef.com/t/cr195-editorial/22145
Grading Policy.
i. [15 Marks] DP Formulation. Clearly define the variable used [5 Marks]
ii. [10 Marks] Matrix Formulation. Describe the base case and size of the matrix [5 Marks].
iii. [5 Marks] Complexity Analysis. Time complexity in terms of input.
iv. Deduct 90% for ω(log n) solution.
v. Deduct 20-100% in the corresponding part based on the severity of the mistake.
(Bb) Smallest Triangle. Given a set of n points on a 2D plane, design and analyze an algorithm to
find three points making a triangle of the smallest size (perimeter).
Solution. Application of closest pair of points. Only difference is proof for existence of constant
triangles from a vertex.
Grading Policy.
i. [15 Marks] Algorithm,
ii. [10 Marks] Proof of comparison with Constant vertices.
iii. [5 Marks] Analysis.
iv. Deduct 90% marks if ω(n log2 n) algorithm given and 60% if Θ(n log2 n) given. solution.
v. Deduct 20-100% in the corresponding part based on the severity of the mistake.

You might also like