Final Exam Fall 23
Final Exam Fall 23
Final Exam Fall 23
Q1) Solve the following recurrences and find out asymptotic time complexity. Use recursion tree method.
Show complete working.
[3+3 = 6]
a) T(n) = 4T(n-1) +1
b) You are given an array of n elements, and you notice that some of the elements are duplicates; that is, they
appear more than once in the array. How to remove all duplicates from the array in time O(nlgn). Give the
idea of your algorithm in 3-4 lines. [3]
c) Suppose you are given letter grades of students for the course Design and Analysis of Algorithms in the
form of an array and you are required to answer the following query. How many students have earned
grades from grade X to grade Y? Where X and Y can be any of the letter grades (A+, A, A-, B+, B, B-,
C+, C, C-, D+, D, F). We need an algorithm that can answer such queries in O (1). You can preprocess
the data to answer such queries. Give the idea of your algorithm in 3-4 lines. [4]
Examples
Input: L = {3, 1, 2, 4} and
T = {4, 1000, 2, 5}
Output: 3, 4, 1, 2
Explanation: We should first complete job 3, then jobs 4, 1, 2 respectively.
Input: L = {1, 2, 3, 5, 6}
T = {2, 4, 1, 3, 2}
Output: 3, 5, 4, 1, 2
Explanation: We should complete jobs 3, 5, 4, 1 and then 2 in this order.
The cost of an alignment is the number of columns in which the letters differ. And the edit distance between
two strings is the cost of their best possible alignment. Do you see that there is no better alignment of SNOWY
and SUNNY than the one shown here with a cost of 3? Edit distance is so named because it can also be
thought of as the minimum number of edits—insertions, deletions, and replacements of characters—needed
to transform the first string into the second. For instance, the alignment shown on the left corresponds to three
edits: insert U, replace O → N, and remove W.
The goal is to find the minimum edit distance ED (m, n) between two strings Xn = (x1, x2,...,xn) and Ym =
(y1,y2,..,ym).
Operation 1 (INSERT): Insert any character before or after any index of X
Operation 2 (REMOVE): Remove a character of X
Operation 3 (REPLACE): Replace a character at any index of X with some other character.
𝑚 𝑖𝑓 𝑛 = 0
𝑛 𝑖𝑓 𝑚 = 0
𝐸𝐷 (𝑋𝑛 , 𝑌𝑚 ) = 𝐸𝐷 (𝑋 − 1, 𝑌 − 1) 𝑖𝑓 𝑥𝑛 = 𝑦𝑚
𝑛 𝑚
{𝑀𝑖𝑛(𝐸𝐷(𝑋𝑛−1 , 𝑌𝑚−1 ), 𝐸𝐷(𝑋𝑛−1 , 𝑌𝑚 ), 𝐸𝐷(𝑋𝑛 , 𝑌𝑚−1 )) + 1 𝑖𝑓 𝑥𝑛 ≠ 𝑦𝑚
a) Compute the edit distance between two strings X = POLYN and Y = EXPON, by using the above DP
solution. Fill the following table for finding minimum edit distance. [3]
X P O L Y N
Y 0 1 2 3 4 5
E 1
X 2
P 3
O 4
N 5
c) Write down the pseudo code of a function that computed the edit distance of Xn and Ym . Also write its
time complexity[5]
D0 1 2 3 4 D3 1 2 3 4
1 1
2 2
3 3
4 4
D1 1 2 3 4 D4 1 2 3 4
1 1
2 2
3 3
4 4
D2 1 2 3 4
1
b) Let T be a minimum spanning tree of a graph G. Then for any two vertices u,v the path from u to v in T is a
shortest path from u to v in G. Is this statement True or False? Justify your answer with an example. [3]