Tribhuwan University

Institute of Science and Technology

2081

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.
Differentiate between dynamic programming and memorization. Compute the shortest path between every pairs in the following graphs using Floyd Warshall algorithm.
question image
[10]

Part A: Difference between Dynamic Programming and Memoization

Aspect Dynamic Programming (DP) Memoization
Approach Bottom-up approach Top-down approach
Direction Solves smaller subproblems first, builds up to larger ones Starts from the main problem, breaks down recursively
Table Filling Fills the entire table iteratively Fills table entries only when needed (on demand)
Recursion No recursion; uses iteration (loops) Uses recursion with caching
Efficiency May solve subproblems that are not needed Solves only required subproblems
Space Uses a table/array (iterative) Uses a table/array + recursion stack
Control Programmer controls the order of computation Order determined by recursive calls
Overhead No function call overhead Has recursive function call overhead

Part B: Floyd-Warshall Algorithm — Shortest Path Between Every Pair

Edges from the graph:

  • A → B = 4
  • B → A = 1
  • B → C = 2
  • A → C = 7
  • C → A = 6

Initial Distance Matrix ($D^0$):

Using nodes: A, B, C (indexed as 1, 2, 3)

D0=[04710260]D^0 = \begin{bmatrix} 0 & 4 & 7 \\ 1 & 0 & 2 \\ 6 & \infty & 0 \end{bmatrix}

Floyd-Warshall Formula:

Dk[i][j]=min(Dk1[i][j],  Dk1[i][k]+Dk1[k][j])D^k[i][j] = \min(D^{k-1}[i][j], \; D^{k-1}[i][k] + D^{k-1}[k][j])


Iteration k = 1 (Intermediate vertex: A)

For each pair (i, j), check if going through A is shorter:

  • D1[2][3]=min(D0[2][3],  D0[2][1]+D0[1][3])=min(2,  1+7)=min(2,8)=2D^1[2][3] = \min(D^0[2][3], \; D^0[2][1] + D^0[1][3]) = \min(2, \; 1+7) = \min(2, 8) = 2
  • D1[3][2]=min(D0[3][2],  D0[3][1]+D0[1][2])=min(,  6+4)=10D^1[3][2] = \min(D^0[3][2], \; D^0[3][1] + D^0[1][2]) = \min(\infty, \; 6+4) = 10
  • D1[3][1]=min(6,  6+0)=6D^1[3][1] = \min(6, \; 6+0) = 6 (no change)
  • D1[2][1]=min(1,  1+0)=1D^1[2][1] = \min(1, \; 1+0) = 1 (no change)

D1=[0471026100]D^1 = \begin{bmatrix} 0 & 4 & 7 \\ 1 & 0 & 2 \\ 6 & 10 & 0 \end{bmatrix}


Iteration k = 2 (Intermediate vertex: B)

  • D2[1][3]=min(D1[1][3],  D1[1][2]+D1[2][3])=min(7,  4+2)=min(7,6)=6D^2[1][3] = \min(D^1[1][3], \; D^1[1][2] + D^1[2][3]) = \min(7, \; 4+2) = \min(7, 6) = 6
  • D2[3][1]=min(D1[3][1],  D1[3][2]+D1[2][1])=min(6,  10+1)=min(6,11)=6D^2[3][1] = \min(D^1[3][1], \; D^1[3][2] + D^1[2][1]) = \min(6, \; 10+1) = \min(6, 11) = 6
  • D2[1][2]=min(4,  4+0)=4D^2[1][2] = \min(4, \; 4+0) = 4 (no change)
  • D2[3][2]=min(10,  10+0)=10D^2[3][2] = \min(10, \; 10+0) = 10 (no change)

D2=[0461026100]D^2 = \begin{bmatrix} 0 & 4 & 6 \\ 1 & 0 & 2 \\ 6 & 10 & 0 \end{bmatrix}


Iteration k = 3 (Intermediate vertex: C)

  • D3[1][2]=min(D2[1][2],  D2[1][3]+D2[3][2])=min(4,  6+10)=min(4,16)=4D^3[1][2] = \min(D^2[1][2], \; D^2[1][3] + D^2[3][2]) = \min(4, \; 6+10) = \min(4, 16) = 4
  • D3[2][1]=min(D2[2][1],  D2[2][3]+D2[3][1])=min(1,  2+6)=min(1,8)=1D^3[2][1] = \min(D^2[2][1], \; D^2[2][3] + D^2[3][1]) = \min(1, \; 2+6) = \min(1, 8) = 1
  • D3[1][3]=min(6,  6+0)=6D^3[1][3] = \min(6, \; 6+0) = 6 (no change)
  • D3[3][2]=min(10,  0+10)=10D^3[3][2] = \min(10, \; 0+10) = 10 (no change)

D3=[0461026100]D^3 = \begin{bmatrix} 0 & 4 & 6 \\ 1 & 0 & 2 \\ 6 & 10 & 0 \end{bmatrix}


Final Shortest Path Matrix

D=[0461026100]D = \begin{bmatrix} 0 & 4 & 6 \\ 1 & 0 & 2 \\ 6 & 10 & 0 \end{bmatrix}

From\To A B C
A 0 4 6
B 1 0 2
C 6 10 0

Conclusion: The Floyd-Warshall algorithm computes all-pairs shortest paths in O(n3)O(n^3) time using dynamic programming by systematically considering each vertex as an intermediate node.

2.
What is the worst case of quick sort and how does randomize quick sort handle this problem? Sort the data {2,4,3,6,12,10,11,13,9}\{-2,4,-3,6,12,10,11,13,9\} using quick sort.[10]
3.
Does greedy algorithm guarantee optimal solution? Solve the Fractional knapsack problem to find maximum loot from given information.
Item1234567Value121020152350Weight (kgs)213212101\begin{array}{|c|c|c|c|c|c|c|c|}\hline \text{Item} & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\ \hline \text{Value} & 12 & 10 & 20 & 15 & 2 & 3 & 50 \\ \hline \text{Weight (kgs)} & 2 & 1 & 3 & 2 & 12 & 10 & 1 \\ \hline \end{array}
[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
Given a set A={5,7,10,12,15,18,20}A=\{5,7,10,12,15,18,20\}, find the subset that sum to 35 using backtracking. [5]
5.
Solve the following recurrence relations using master's method.
T(n)=2T(n2)+n3, n>1;T(n)=1, n=1T(n) = 2T\left(\frac{n}{2}\right) + n^3,\ n > 1;\quad T(n) = 1,\ n = 1
T(n)=2T(n4)+1, n>1;T(n)=1, n=1T(n) = 2T\left(\frac{n}{4}\right) + 1,\ n > 1;\quad T(n) = 1,\ n = 1
[5]
6.
Define order statistics problem. Find the edit distance between 'cat' and 'car' using dynamic programming. [5]
7.
Discuss about recursion and backtracking. Analyze the complexity of Miller Rabin Randomized Primality test. [5]
8.
Solve the following linear equation using Chinese Remainder Theorem. x = 1 MOD 3,x = 2 MOD 5,x = 0 MOD 7 [5]
9.
Explain the approximation algorithm for vertex cover of a connected graph with an example. [5]
10.
State cooks theorem. Discuss about problem reducibility. [5]
11.
Write short notes on: a) Big Oh, Big Omega, Big theta b) Class P, Class NP and NP-Complete [5]
12.
Write an algorithm to find the nthn^{th} fibonacci number with its time and space complexity. [5]