CS502 – Fundamentals of Algorithms: Midterm Important Subjective (39 Solved Q&As)
Computer Science • Read every solved subjective question with its model answer directly on this page. Free, no login required.
All 664 Midterm Packs · CS502 Course Page · CS502 MCQ Pack (239 MCQs)
-
Whether each of the following sorting algorithms is stable and in-place or not? Fill the table: Merge Sort, Heapsort, Quicksort, Counting Sort, Bubble Sort — Stable (Yes/No), In-Place (Yes/No). (Marks: 5)
Topic: Sorting Algorithms: Identification, Stability & Decision Trees
Merge Sort — Stable: Yes; In-Place: No. Merge sort preserves the relative order of equal elements (when merging, the element from the left half is taken first on a tie), but it needs an auxiliary array of size Θ(n) for merging, so it is not in-place. Heapsort — Stable: No; In-Place: Yes. Heap operations (extract-max and heapify) swap elements over long distances, which can change the relative order of equal keys; it sorts within the array itself using only O(1) extra space. Quicksort — Stable: No; In-Place: Yes. Partitioning swaps elements across the pivot, so equal elements may be reordered; it needs no extra array (only O(log n) stack space for recursion on average). Counting Sort — Stable: Yes; In-Place: No. It is made stable by placing elements into the output array from right to left using the cumulative counts, but it requires a separate output array and a count array, so it is not in-place. Bubble Sort — Stable: Yes; In-Place: Yes. Adjacent elements are swapped only when they are strictly out of order, so equal elements never cross each other, and sorting happens inside the same array.
-
What will be the sorting techniques by analyzing the followings? (i) Whenever two consecutive items are found in an array that is out of order, swap them. (ii) Assume that A[1...i-1] have already been sorted. Add A[i] into its proper position in this sub array. Create this position by shifting all larger elements to the right. (iii) Assume that A[1...i-1] contain the i-1 smallest elements in sorted order. Find the smallest element in A[i...n]. Swap it with A[i]. (Marks: 3)
Topic: Sorting Algorithms: Identification, Stability & Decision Trees
(i) Bubble Sort — it works by repeatedly comparing each pair of adjacent (consecutive) items and swapping them if they are out of order, so the largest unsorted element 'bubbles up' to its correct position in every pass. (ii) Insertion Sort — the array is divided into a sorted part A[1...i-1] and an unsorted part; the next element A[i] is inserted into its proper position in the sorted part by shifting all larger elements one position to the right. (iii) Selection Sort — in each step the smallest element is selected from the unsorted part A[i...n] and swapped with A[i], so after i-1 steps A[1...i-1] hold the i-1 smallest elements in sorted order.
-
Consider sorting three numbers a1, a2 and a3 with comparison-based sorting algorithms, then how many combinations are possible? Write the possible combinations. (Marks: 2–3 (2 marks in File 2, 3 marks in File 3))
Topic: Sorting Algorithms: Identification, Stability & Decision Trees
There are 3! = 6 possible combinations (permutations) of the three numbers: 1) a1 a2 a3 2) a1 a3 a2 3) a2 a1 a3 4) a2 a3 a1 5) a3 a1 a2 6) a3 a2 a1 A comparison-based sorting algorithm must be able to distinguish all 6 possible orderings of the input, so its decision tree must have at least 6 leaves. Therefore at least ceil(log2 6) = 3 comparisons are necessary in the worst case to sort three numbers.
-
What is the essential constraint for the Counting Sort? (Marks: 2)
Topic: Counting Sort & Linear-Time Sorting
The essential constraint is that the values to be sorted must be integers (or keys that can be mapped to integers) lying in a known, limited range, say 0 to k, where k is not much larger than the number of elements n. Counting sort counts the occurrences of each value, so it needs an auxiliary count array of size k+1; if the range k is very large compared with n, the Θ(n + k) time and Θ(k) space make it impractical. When k = O(n), it runs in linear time Θ(n).
-
What is the idea behind counting sort algorithm, to sort the elements without comparisons in linear time? (Marks: 2)
Topic: Counting Sort & Linear-Time Sorting
The basic idea is to sort by counting rather than by comparing elements. For each input element we count how many elements are equal to it (frequency count), and then compute the cumulative (prefix) sums of these counts, which directly give the exact position of each element in the sorted output. Each element is then placed into its computed position in an output array. Because positions are calculated arithmetically from the counts — no key comparisons are ever made — the comparison-sort lower bound of Ω(n log n) does not apply, and the algorithm runs in Θ(n + k) time, i.e. linear time when k = O(n).
-
Solve it: T(n) = (1/2) * Σ (q = 1 to 2) of [ T(q - 1) + T(2 - q) + 2 ] (Marks: 3)
Topic: Recurrences, Catalan Numbers & Complexity Analysis
This is the average-case recurrence of quicksort written for n = 2. Expanding the summation: q = 1: T(0) + T(1) + 2 q = 2: T(1) + T(0) + 2 T(2) = (1/2) [ (T(0) + T(1) + 2) + (T(1) + T(0) + 2) ] = (1/2) [ 2T(0) + 2T(1) + 4 ] = T(0) + T(1) + 2. Using the base cases T(0) = 0 and T(1) = 0 (an array of size 0 or 1 is already sorted and needs no comparisons): T(2) = 0 + 0 + 2 = 2.
-
The following summation form represents quick sort, so what will be the outcome after solving this recurrence in terms of asymptotic notation? Σ (i = 1 to n+1) of i = (n + 1)(n + 2) / 2 (Marks: 5)
Topic: Recurrences, Catalan Numbers & Complexity Analysis
Expanding the closed form: (n + 1)(n + 2) / 2 = (n² + 3n + 2) / 2 = (1/2)n² + (3/2)n + 1. Keeping only the dominant (highest-order) term and dropping constants, the result is Θ(n²). This summation comes from the worst case of quicksort: when the pivot is always the smallest or the largest element, the partition is maximally unbalanced (0 and n-1 elements), the partitioning costs add up as n + (n-1) + ... which is Θ(n²). Hence quicksort in the worst case runs in Θ(n²) time (its average case is Θ(n log n)).
-
What are Catalan numbers? Give the formula to find Catalan numbers. (Marks: 3)
Topic: Recurrences, Catalan Numbers & Complexity Analysis
Catalan numbers form a sequence of natural numbers (1, 1, 2, 5, 14, 42, 132, ...) that counts many combinatorial structures. In this course they appear as the number of ways to fully parenthesize a product of n+1 matrices (the number of alternative ways of parenthesizing a chain in the matrix chain multiplication problem), and they also count the number of binary tree shapes with n nodes, the number of ways to triangulate a polygon, etc. Formula: C(n) = (1 / (n + 1)) * binom(2n, n) = (2n)! / ( (n + 1)! n! ) Equivalently, by the recurrence: C(0) = 1 and C(n) = Σ (i = 0 to n-1) of C(i) * C(n - 1 - i). Catalan numbers grow exponentially (roughly 4^n / n^(3/2)), which is why a brute-force search over all parenthesizations of a matrix chain is infeasible and dynamic programming is used instead.
-
Write down the steps of dynamic programming strategy. (Marks: 5)
Topic: Dynamic Programming: Strategy, Knapsack & Matrix Chain
Dynamic programming is applied in four steps: 1. Characterize the structure of an optimal solution — show that the problem has optimal substructure, i.e. an optimal solution can be built from optimal solutions of its subproblems. 2. Recursively define the value of an optimal solution — write a recurrence expressing the value of the optimal solution in terms of the values of smaller subproblems. 3. Compute the value of an optimal solution in a bottom-up fashion — fill a table starting from the smallest subproblems, so that when a subproblem is solved all its (overlapping) sub-subproblems are already computed; each distinct subproblem is solved only once. 4. Construct an optimal solution from the computed information — trace back through the table (using an auxiliary choice table, where kept) to recover the actual solution, not just its value.
-
True or False: A sequence of values in a row of the dynamic programming table for an instance of the knapsack problem is always non-decreasing. Give a brief description. (Marks: 3)
Topic: Dynamic Programming: Strategy, Knapsack & Matrix Chain
True. In the 0/1 knapsack table, the entry V[i, w] is the maximum value obtainable using the first i items with capacity w. Within a fixed row i, as the capacity w increases, the maximum achievable value can never decrease: any selection of items that fits in a smaller capacity also fits in a larger capacity (the extra space may simply be left unused), and a larger capacity can only add more feasible choices. Hence the values along every row of the table are non-decreasing with w.
-
What is the time complexity of the following algorithm? KNAPSACK(n, W): for w = 0 to W do V[0, w] ← 0; for i = 0 to n do V[i, 0] ← 0; for i = 1 to n do for w = 0 to W do if (w_i ≤ w and v_i + V[i-1, w-w_i] > V[i-1, w]) then V[i, w] ← v_i + V[i-1, w-w_i] else V[i, w] ← V[i-1, w]. (Marks: 5)
Topic: Dynamic Programming: Strategy, Knapsack & Matrix Chain
The algorithm fills a table V with (n + 1) rows and (W + 1) columns, i.e. Θ(nW) entries in total. Filling each entry requires only a constant amount of work (one comparison and taking the maximum of two already-computed values), and the initialization loops also cost Θ(n + W). Hence the total running time is Θ(nW), and the space is also Θ(nW) for the table. Note that Θ(nW) is only pseudo-polynomial: it is polynomial in the numeric value W, but exponential in the size (number of bits, log W) needed to represent W.
-
How we proceed with m entries in cost table for chain matrix multiplication problem? (Marks: 2)
Topic: Dynamic Programming: Strategy, Knapsack & Matrix Chain
The cost table m[1..n, 1..n] is filled only in its upper-triangular part (entries m[i, j] with i ≤ j), and the entries are computed diagonal by diagonal, in order of increasing chain length. First the main diagonal is set: m[i, i] = 0 (a chain of a single matrix needs no multiplication). Then chains of length 2 are computed, then length 3, and so on up to length n, using m[i, j] = min over k (i ≤ k < j) of { m[i, k] + m[k+1, j] + p(i-1) * p(k) * p(j) }. Filling in increasing-length order guarantees that the smaller subchain costs needed for an entry are already available. The final answer (minimum number of scalar multiplications) is m[1, n]; a companion table s[i, j] records the value of k at which the optimum is achieved, so that the optimal parenthesization can be reconstructed.
-
Which one is the better approach to cope with chain matrix multiplication? (Marks: 2)
Topic: Dynamic Programming: Strategy, Knapsack & Matrix Chain
Dynamic programming (bottom-up tabulation) is the better approach. A plain recursive / brute-force search tries all possible parenthesizations — their number is given by the Catalan numbers and grows exponentially — and it recomputes the same overlapping subproblems again and again. Dynamic programming instead solves each of the Θ(n²) distinct subchains exactly once and stores the results in the cost table m, giving an overall Θ(n³) time and Θ(n²) space. (A top-down recursion with memoization achieves the same efficiency.)
-
Draw the final Max-Heap structure for the following array: 50, 31, 45, 30, 2, 7, 40, 12, 28, 1. You can show the final result (tree) only. (Marks: 5)
Topic: Heaps & Heap Sort
First check the heap property level by level (parent ≥ children): 50 ≥ 31, 45; 31 ≥ 30, 2; 45 ≥ 7, 40; 30 ≥ 12, 28; 2 ≥ 1. Every parent is already greater than or equal to its children, so the given array already satisfies the max-heap property and build-max-heap leaves it unchanged. The final max-heap (array form: 50, 31, 45, 30, 2, 7, 40, 12, 28, 1) as a tree is: 50 / \ 31 45 / \ / \ 30 2 7 40 / \ / 12 28 1
-
How edit distance is used for speech recognition? (Marks: 2)
Topic: Edit Distance & Its Applications
In speech recognition, the sound of a spoken word is first converted into a sequence of phonemes (basic sound units). To decide which word was spoken, the system computes the edit distance between this phoneme sequence and the stored phoneme sequence of every candidate word in its dictionary/vocabulary — i.e. the minimum number of insertions, deletions and substitutions needed to convert one sequence into the other. The dictionary word with the smallest edit distance is selected as the recognized word. Because a small distance tolerates minor errors in pronunciation or in the captured sounds, this makes recognition robust (the same idea is used in spell checkers).
-
Describe three solution paths for edit distance of these two given words: 1. MATHS 2. ARTS (Marks: 5)
Topic: Edit Distance & Its Applications
The edit distance between MATHS and ARTS is 3. Three different optimal solution paths (each using exactly 3 operations) are: Path 1 — Delete, Substitute, Substitute: Delete M → ATHS; substitute T with R → ARHS; substitute H with T → ARTS. (1 deletion + 2 substitutions = 3 operations) Path 2 — Delete, Delete, Insert: Delete M → ATHS; delete H → ATS; insert R after A → ARTS. (2 deletions + 1 insertion = 3 operations) Path 3 — Substitute, Substitute, Delete: Substitute M with A → AATHS; substitute the second A with R → ARTHS; delete H → ARTS. (2 substitutions + 1 deletion = 3 operations) In every path the matching letters A, T (in order) and S are kept, and no solution with fewer than 3 operations exists, so the edit distance E(MATHS, ARTS) = 3.
-
What is an algorithm? Write down the essential elements (properties) of an algorithm. (Marks: 3)
Topic: Core Concepts: Algorithms, Analysis & Strategies
An algorithm is a finite, step-by-step procedure (a well-defined sequence of instructions) for solving a problem, which takes some input and produces the desired output. The essential elements/properties of an algorithm are: (1) Input — it takes zero or more inputs; (2) Output — it produces at least one output; (3) Definiteness — every step is precisely and unambiguously defined; (4) Finiteness — it must terminate after a finite number of steps; (5) Effectiveness — every operation is basic enough to be carried out exactly, in finite time. An algorithm should also be general enough to solve a class of problems, not just one instance.
-
Define the asymptotic notations Big-O, Big-Omega (Ω) and Big-Theta (Θ), and give one example of each. (Marks: 3)
Topic: Core Concepts: Algorithms, Analysis & Strategies
Big-O (O): an upper bound on the growth rate. f(n) = O(g(n)) if there exist constants c > 0 and n0 such that f(n) ≤ c·g(n) for all n ≥ n0. Example: 3n² + 5n = O(n²). Big-Omega (Ω): a lower bound on the growth rate. f(n) = Ω(g(n)) if there exist constants c > 0 and n0 such that f(n) ≥ c·g(n) for all n ≥ n0. Example: 3n² + 5n = Ω(n²). Big-Theta (Θ): a tight bound — both an upper and a lower bound. f(n) = Θ(g(n)) if f(n) = O(g(n)) and f(n) = Ω(g(n)). Example: 3n² + 5n = Θ(n²).
-
What is the divide and conquer strategy? Write its steps and give examples. (Marks: 3)
Topic: Core Concepts: Algorithms, Analysis & Strategies
Divide and conquer is an algorithm design strategy that solves a problem in three steps: (1) Divide — break the problem into smaller subproblems of the same type; (2) Conquer — solve the subproblems recursively (a subproblem small enough is solved directly as a base case); (3) Combine — merge the solutions of the subproblems into the solution of the original problem. Examples: merge sort (combine step does the real work), quicksort (divide step — partitioning — does the real work), binary search, and Strassen's matrix multiplication.
-
Differentiate between the greedy approach and dynamic programming. (Marks: 3)
Topic: Core Concepts: Algorithms, Analysis & Strategies
Greedy approach: at each step it makes the locally optimal (best-looking) choice and never reconsiders it; it does not require optimal substructure solutions to be combined from all possibilities, and it is not guaranteed to give an optimal solution for every problem (e.g. it fails on the 0/1 knapsack). It is simple and fast. Dynamic programming: it solves overlapping subproblems once, stores their results in a table, and combines optimal subproblem solutions to build the optimal solution of the whole problem (bottom-up or memoized). It always gives the optimal solution when the problem has optimal substructure and overlapping subproblems, at the cost of extra table space (e.g. matrix chain multiplication, 0/1 knapsack, edit distance).
-
What is the sieve technique? How does it differ from divide and conquer? (Marks: 3)
Topic: Core Concepts: Algorithms, Analysis & Strategies
The sieve technique is a special case of divide and conquer in which we are interested in only ONE of the subproblems produced by the division, and the others are simply discarded ('sieved out') without being solved. At each step the input is partitioned into groups/subsets, a decision is made about which single subset contains the answer, and the rest are thrown away. Example: finding the k-th smallest element (selection) — after partitioning around a pivot, only the side containing the k-th element is processed further. Difference from divide and conquer: in ordinary divide and conquer ALL subproblems are solved and their solutions are combined; in the sieve technique only one subproblem is pursued and no combine step is needed, which makes it more efficient for selection-type problems.
-
Solve the recurrence T(n) = 2T(n/2) + Θ(n), which is the recurrence of merge sort. (Marks: 3)
Topic: Core Concepts: Algorithms, Analysis & Strategies
Using the Master Theorem: here a = 2, b = 2 and f(n) = Θ(n). Compute n^(log_b a) = n^(log2 2) = n^1 = n. Since f(n) = Θ(n) = Θ(n^(log_b a)), Case 2 of the Master Theorem applies, giving T(n) = Θ(n log n). (The same result follows from a recursion tree: there are log2 n levels, each level costs Θ(n) in total merging work, so the total cost is Θ(n) × Θ(log n) = Θ(n log n).) Hence merge sort runs in Θ(n log n) time.
-
Define optimal substructure and overlapping subproblems. Why are these two properties essential for dynamic programming? (Marks: 3)
Topic: Core Concepts: Algorithms, Analysis & Strategies
Optimal substructure: a problem has optimal substructure if an optimal solution to the problem can be constructed from optimal solutions of its subproblems (e.g. the optimal parenthesization of a matrix chain contains optimal parenthesizations of its subchains). Overlapping subproblems: the same subproblems recur many times in the recursion, so a naive recursive algorithm would solve them repeatedly (exponential work), while the total number of distinct subproblems is only polynomial. These two properties are exactly what dynamic programming exploits: optimal substructure lets us define the value recursively and combine sub-solutions, and overlapping subproblems make it worthwhile to compute each distinct subproblem once and reuse the stored result, turning an exponential recursion into a polynomial-time algorithm.
-
What is the worst case of quicksort, when does it occur, and how can it be avoided? (Marks: 3)
Topic: Core Concepts: Algorithms, Analysis & Strategies
The worst case of quicksort is Θ(n²). It occurs when the pivot is always the smallest or the largest element of the subarray (for example when the first/last element is chosen as pivot and the input is already sorted or reverse sorted), so every partition is maximally unbalanced — one side has 0 elements and the other has n-1 — giving the recurrence T(n) = T(n-1) + Θ(n) = Θ(n²). It can be avoided (with high probability) by choosing the pivot randomly (randomized quicksort) or by using the median-of-three rule, which makes the expected/average running time Θ(n log n) regardless of the input order.
-
How would you modify QUICKSORT to sort into non-increasing order? (Marks: 2)
Topic: Sorting Algorithms: Identification, Stability & Decision Trees
To make QUICKSORT sort in non-increasing (descending) order, we only need to modify the PARTITION procedure. In the ordinary PARTITION, elements smaller than the pivot are placed in the left sub-array and elements larger than the pivot in the right sub-array. For non-increasing order we reverse this: elements larger than the pivot are placed in the left sub-array and elements smaller than the pivot in the right sub-array (i.e., the comparisons in PARTITION are reversed). The rest of the QUICKSORT procedure remains exactly the same, and the array ends up sorted in non-increasing order.
-
Describe bubble sort. (Marks: 2)
Topic: Sorting Algorithms: Identification, Stability & Decision Trees
In bubble sort, we scan the array and whenever two consecutive items are found that are out of order, we swap them. The scan is repeated again and again until all consecutive items are in order, i.e., a complete pass is made with no swaps. In each pass the largest remaining element 'bubbles up' to its correct position at the end of the array. Bubble sort is simple to implement but slow: its worst-case (and average) running time is Θ(n^2). It is in-place and stable.
-
What is the necessary assumption for the average-case analysis of quicksort? (Marks: 2)
Topic: Sorting Algorithms: Identification, Stability & Decision Trees
Average-case analysis normally depends on some assumption about the distribution of inputs. In the case of quicksort, however, the analysis does not depend on the distribution of the input at all. It only depends upon the random choices of pivots that the algorithm makes. The necessary assumption is that the pivot is chosen randomly (equivalently, that all permutations of the input are equally likely), which makes the expected running time of quicksort Θ(n log n).
-
What is sorting? Describe slow running sorting algorithms. (Marks: 5)
Topic: Sorting Algorithms: Identification, Stability & Decision Trees
Sorting is the process of arranging items in some sequence (in increasing or decreasing order) according to a key value. There are a number of well-known slow sorting algorithms that run in O(n^2) time in the worst case. These include: Bubble sort: Scan the array. Whenever two consecutive items are found that are out of order, swap them. Repeat until all consecutive items are in order. Insertion sort: Assume that A[1..i-1] have already been sorted. Insert A[i] into its proper position in this sub-array, creating this position by shifting all larger elements to the right. Selection sort: Assume that A[1..i-1] contain the i-1 smallest elements in sorted order. Find the smallest element in A[i..n] and swap it with A[i]. These algorithms are easy to implement, but they run in Θ(n^2) time in the worst case, so they are practical only for small inputs.
-
Suggest how we can make improvements in the Quick Sort algorithm. (Marks: 5)
Topic: Sorting Algorithms: Identification, Stability & Decision Trees
Quick sort can be improved in several ways: 1. Better pivot selection: choose the pivot randomly, or use the median-of-three (first, middle and last elements), so that badly unbalanced partitions — and hence the Θ(n^2) worst case — become very unlikely. 2. Use insertion sort for small sub-arrays: when a sub-array becomes small (say fewer than 10–20 elements), insertion sort is faster in practice because of its low overhead. 3. Recurse on the smaller sub-array first and handle the larger one iteratively (tail recursion), which keeps the recursion stack depth to O(log n). 4. Use three-way partitioning (splitting the array into elements less than, equal to, and greater than the pivot) when the input contains many duplicate keys, so equal elements are not partitioned again and again.
-
How do we heapify? (Marks: 2)
Topic: Heaps & Heap Sort
If an element in the heap is not at its proper place, i.e., it is violating the heap order, the HEAPIFY procedure is used to fix it and place it at its proper position. In HEAPIFY, the element is compared with its two children; if it is smaller than the larger child, it is swapped with that larger child. This step is repeated (the element sifts down) until the element becomes larger than both of its children or it reaches a leaf. Each level takes Θ(1) time for the comparison, so HEAPIFY takes O(log n) time overall.
-
Describe the heap sort algorithm. (Marks: 3)
Topic: Heaps & Heap Sort
Heap sort works in two phases: 1. Build a max-heap from the unsorted array by calling HEAPIFY on all internal nodes, starting from the last parent node up to the root (BUILD-HEAP). After this step, the largest element is at the root of the heap. 2. Swap the root (the maximum element) with the last element of the heap, decrease the heap size by one, and call HEAPIFY on the new root to restore the heap property. Repeat this step until the heap contains only one element. At the end, the array is sorted in increasing order. Heap sort runs in Θ(n log n) time; it is in-place but not stable.
-
What is a heap and what is heap order? (Marks: 2)
Topic: Heaps & Heap Sort
A heap is a left-complete binary tree — a binary tree in which all levels are completely filled, except possibly the last level, which is filled from left to right — that conforms to the heap order. The heap order property states that the key stored at a node is greater than or equal to the keys stored in its children (max-heap order), so the largest element is always at the root. In a min-heap, the order is reversed: the key at a node is smaller than or equal to the keys of its children.
-
Define worst-case time and average-case time. (Marks: 2)
Topic: Recurrences, Catalan Numbers & Complexity Analysis
Worst-case time is the maximum running time of an algorithm over all (legal) inputs of size n. Average-case time is the average running time of an algorithm over all inputs of size n (taken over some assumed distribution of inputs).
-
How can we avoid unnecessary repetitions for recursive calls? (Marks: 2)
Topic: Dynamic Programming: Strategy, Knapsack & Matrix Chain
We can avoid these unnecessary repetitions by writing down (storing) the results of recursive calls and looking them up again if we need them later, instead of computing them again. This process is called memoization. It is the key idea that turns an exponential-time recursive algorithm with overlapping sub-problems (such as the naive Fibonacci algorithm) into an efficient one.
-
Write a pseudo code for Fibonacci with memoization. (Marks: 3)
Topic: Dynamic Programming: Strategy, Knapsack & Matrix Chain
MEMOFIB(n) 1. if (n < 2) then return n 2. if (F[n] is undefined) then F[n] ← MEMOFIB(n - 1) + MEMOFIB(n - 2) 3. return F[n] The array F stores each Fibonacci number the first time it is computed. When the value is needed again, it is simply looked up in the array, so every value is computed only once and the total running time becomes Θ(n).
-
How is edit distance used in spelling correction? (Marks: 3)
Topic: Edit Distance & Its Applications
If a text contains a word that is not in the dictionary, a 'close' word, i.e., one with a small edit distance from the typed word, may be suggested as a correction. Most word processing applications, such as Microsoft Word, have a spelling checking and correction facility: when such an application finds an incorrectly spelled word, it searches the dictionary for words at a minimum edit distance (obtained by insertions, deletions and substitutions of characters) from the typed word and suggests them as the most likely corrections.
-
What is the total number of entries in the matrix for edit distance? (Marks: 2)
Topic: Edit Distance & Its Applications
There are Θ(n^2) entries in the edit distance matrix (for two strings of length n; in general (m+1) x (n+1) entries for strings of lengths m and n). Each entry E(i, j) takes Θ(1) time to compute, so the total running time of the edit distance algorithm is Θ(n^2).
-
Illustrate how Radix Sort works on the following words: COW, DOW, SEA, RUG, ROW, MOB, BOX, TAB, BAR, EAR, TAR, DIG, BIG, TEA, NOW, FOX. Show the result of the first 2 passes only. (Marks: 5)
Topic: Counting Sort & Linear-Time Sorting
Radix sort sorts the words letter by letter, starting from the least significant (last) letter, using a stable sort in each pass. Original list: COW, DOW, SEA, RUG, ROW, MOB, BOX, TAB, BAR, EAR, TAR, DIG, BIG, TEA, NOW, FOX Pass 1 (sort by the 3rd letter): SEA, TEA, MOB, TAB, RUG, DIG, BIG, BAR, EAR, TAR, COW, DOW, ROW, NOW, BOX, FOX Pass 2 (sort by the 2nd letter): TAB, BAR, EAR, TAR, SEA, TEA, DIG, BIG, MOB, COW, DOW, ROW, NOW, BOX, FOX, RUG (A third pass on the 1st letter would complete the sorting.)
-
What is a better approach of multiplication rather than the straight form of multiplication? Name and describe it. (Marks: 3)
Topic: Divide and Conquer
The better approach is the divide and conquer approach to (large integer) multiplication. Each n-digit number is split into two halves: a high part and a low part of n/2 digits each. The product of the two numbers is then expressed in terms of products of these halves, which are computed recursively. The straight form of this idea needs four multiplications of half size, but with an algebraic trick only three half-size multiplications are needed, giving the recurrence T(n) = 3T(n/2) + Θ(n), whose solution is Θ(n^1.58) — clearly better than the Θ(n^2) time of the straight (grade-school) multiplication method.