Tribhuwan University

Institute of Science and Technology

2079

Bachelor Level / Third Year / Fifth Semester / Science

B.Sc in Computer Science and Information Technology (CSC325)

(Design and Analysis of Algorithms)

Full Marks: 60

Pass Marks: 24

Time: 3 Hours

Candidates are required to give their answers in their own words as for as practicable.

The figures in the margin indicate full marks.

Section A

Long Answers Questions

Attempt any TWO questions.
[2*10=20]
1.
Explain the divide and conquer strategy for problem solving. Describe the worst-case linear time selection algorithm and analyze its complexity.[10]

Divide and Conquer Strategy and Worst-Case Linear Time Selection Algorithm

Divide and Conquer is an algorithm design paradigm that solves a problem by recursively breaking it into smaller sub-problems, solving each sub-problem independently, and then combining their solutions to get the final answer.


Divide and Conquer Strategy

The strategy works in three steps:

  • Divide: Break the original problem into smaller sub-problems of the same type.
  • Conquer: Solve each sub-problem recursively. If the sub-problem is small enough, solve it directly (base case).
  • Combine: Merge the solutions of sub-problems to form the solution of the original problem.

General Recurrence:

T(n)=aT(nb)+f(n)T(n) = aT\left(\frac{n}{b}\right) + f(n)

Where aa = number of sub-problems, n/bn/b = size of each sub-problem, f(n)f(n) = cost of dividing and combining.

Examples: Merge Sort, Quick Sort, Binary Search, Strassen's Matrix Multiplication.


Worst-Case Linear Time Selection Algorithm (Median of Medians)

The Worst-Case Linear Time Selection algorithm (also called SELECT or Median of Medians) finds the i$-th smallest element in an unsorted array in $O(n) time in the worst case.

Unlike Randomized Select (which has O(n2)O(n^2) worst case), this algorithm guarantees linear time by choosing a good pivot.

Steps of the Algorithm:

  • Step A: Divide the nn elements into groups of 5 (last group may have fewer elements). This gives n/5\lceil n/5 \rceil groups.

  • Step B: Find the median of each group by sorting each group (takes constant time since group size is 5).

  • Step C: Recursively find the median of these medians using SELECT. Call this pivot xx.

  • Step D: Partition the array around pivot xx. Let xx be at position kk after partitioning.

  • Step E:

    • If i=ki = k, return xx.
    • If i<ki < k, recursively select the $i$-th smallest in the left partition.
    • If i>ki > k, recursively select the $(i - k)$-th smallest in the right partition.

Complexity Analysis

Key Insight: The median of medians guarantees that at least 30% of elements are smaller and at least 30% of elements are larger than the pivot. So the worst-case recursive call is on at most 70% of elements (i.e., $7n/10$).

Counting elements guaranteed to be eliminated:

  • At least half of the n/5\lceil n/5 \rceil medians are x\geq x, i.e., at least n/10\lceil n/10 \rceil groups.
  • Each such group contributes at least 3 elements x\geq x.
  • So at least 3×n/103n/103 \times \lceil n/10 \rceil \approx 3n/10 elements are x\geq x.
  • Similarly, 3n/103n/10 elements are x\leq x.
  • Worst case: recursive call on at most n3n/10=7n/10n - 3n/10 = 7n/10 elements.

Recurrence Relation:

T(n)=T(n5)+T(7n10)+O(n)T(n) = T\left(\frac{n}{5}\right) + T\left(\frac{7n}{10}\right) + O(n)

Where:

  • T(n/5)T(n/5) = finding median of medians recursively
  • T(7n/10)T(7n/10) = recursive selection in the larger partition
  • O(n)O(n) = partitioning and grouping

Solving the Recurrence:

Assume T(n)cnT(n) \leq cn for some constant cc:

T(n)cn5+c7n10+anT(n) \leq c\frac{n}{5} + c\frac{7n}{10} + an

T(n)cn(15+710)+an=cn(910)+anT(n) \leq cn\left(\frac{1}{5} + \frac{7}{10}\right) + an = cn\left(\frac{9}{10}\right) + an

T(n)9cn10+ancnT(n) \leq \frac{9cn}{10} + an \leq cn

This holds when ancn/10an \leq cn/10, i.e., c10ac \geq 10a.

Therefore:

T(n)=O(n)\boxed{T(n) = O(n)}


Conclusion

The Divide and Conquer strategy is a powerful paradigm for designing efficient algorithms. The Median of Medians algorithm cleverly applies this strategy to achieve guaranteed O(n)O(n) worst-case time for selection, improving over Quick Select's O(n2)O(n^2) worst case by ensuring a balanced partition through careful pivot selection.

2.
Write the dynamic programming algorithm for matrix chain multiplication. Find the optimal parenthesization for the matrix chain product ABCDABCD with size of each is given as A5×10,B10×15,C15×20,D20×30A_{5\times10}, B_{10\times15}, C_{15\times20}, D_{20\times30}.[10]
3.
What do you mean by Backtracking? Explain the backtracking algorithm for solving knapsack problem and find the solution for the problem given below and capacity of knapsack is 10 kg
[Items1234Weight (wi)2345Profit (Pi)35610]\begin{bmatrix} \text{Items} & 1 & 2 & 3 & 4 \\ \text{Weight (w}_i\text{)} & 2 & 3 & 4 & 5 \\ \text{Profit (P}_i\text{)} & 3 & 5 & 6 & 10 \end{bmatrix}
[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
Generate the prefix code for the string "CYBER CRIME" using Huffman algorithm and find the total number of bits required. [5]
5.
Find the edit distance between the string "ARTIFICIAL" and "NATURAL" Using dynamic programming. [5]
6.
Write short notes on: a) Best, Worst and average case complexity b) Greedy Strategy [5]
7.
Solve the following recurrence relations using masters method.
a. T(n)=2T(n/4)+kn2, n>1; T(1)=1\text{a. } T(n) = 2T(n/4) + kn^2,\ n > 1;\ T(1) = 1
b. T(n)=5T(n/4)+kn, n>1; T(1)=1\text{b. } T(n) = 5T(n/4) + kn,\ n > 1;\ T(1) = 1
[5]
8.
Solve the following linear congruences using Chinese Remainder Theorem.x1(mod2),x3(mod5),x6(mod7)x \equiv 1 \pmod{2},\quad x \equiv 3 \pmod{5},\quad x \equiv 6 \pmod{7} [5]
9.
Find the MST from following graph using Kruskal's algorithm.
question image
[5]
10.
Trace the quick sort algorithm for sorting the array A[]={15,7,6,23,18,34,25}A[] = \{15,7,6,23,18,34,25\} and write its best and worst complexity. [5]
11.
Explain the iterative algorithm to find the GCD of given two numbers and analyze its complexity. [5]
12.
Define tractable and intractable problem. Illustrate vertex cover problem with an example. [5]