Final 2023_PartB_Solution
Final 2023_PartB_Solution
Instructions:
• Return the question paper.
• Read each question completely before answering it. There are 8 questions on 4 pages.
• In case of any ambiguity, you may make assumptions. But your assumption should not contradict
any statement in the question paper.
Question # 3 [CLO:1] Answer the following questions and explain in only 2-3 lines only. [5
marks]
Strict Marking for T/F. Rest can be partial depends upon the correct answer
a) [0.5 Point] Floyd-Warshall algorithm computes shortest path from every vertex to every other vertex
using O(n3) complexity, whereas Bellman-Ford can compute in O(n3), only from a single source to
other vertices. Why one needs to use Bellman-Ford algorithm, in presence of more information rich
Floyd-Warshall algorithm. (apart from detecting a negative cycle)
b) [0.5 Points] True / False: Does Directed Acyclic Graph (DAG) can have negative weight edges?
c) [0.5 Point] Describe the difference between tight and loose bounds in Big-O notation. Provide an
example scenario where a tight bound is preferable and explain why.
d) [0.5 Point] What do you mean by the optimal solution?
e) [1 Point] Problem A reduces to problem B if you can use an algorithm that solves B to help solve A.
Let’s say problem A is to find median and problem B uses any sorting method with O(n2) complexity.
What would be the complexity of Problem A
f) [0.5 Point] True or False: In dynamic programming, a subproblem do share sub-subproblems
g) [1.5 Points] Differentiate b/w Single-source, Single-pair and All-pairs shortest path and one algorithm
for each studied during this course
b c
(2,1)
(10,2) 5,1
(1,3)
d e h
(7,1) (8,1)
a (3,2)
(2,1) (9,1)
(1,3) (7,1)
(10,1)
g f
Sol:
Ideally, 1st normalize both bandwidth cost and delay constraint to give equal importance to both.
b-c = 0.5 * 2 + 0.5 * 1 = 1.5; Similarly for others and then apply either Kruskal or Prims
For both parts (a) and (b), only pseudocode is required but must be with clear formulas / loops/ if-
else statements not just plain text. You can show dry run for your own understanding, but dry run
would not have any weightage.
Solution:
Brute Force Algorithm
LPS(1..n) {
if (n == 1)
return 1
if (S[1] == S[n])
return 2 + LPS(2..n-1)
else
return max( LPS(1..n-1), LPS(2..n) )
}
DPLPS(char S[1..n]) {
for (i = 1; i <= n; i++)
LPS[i][i] = 1;
for (len = 1; len < n; len++) {
for (i = 1; i < n-len; i++) {
j = i + len;
if (S[j] == S[j])
LPS[i][j] = 2 + LPS[i+1[j-1];
else
LPS[i][j] = max{LPS[i][j-1], LPS[i+1][j]}
}
}
return LPS[1][n];
}
Given an array of integers arr[] of size N and an integer, the task is to rotate the array elements to
the left by d positions, (where d<=n).
a) Design an algorithm to solve the above task with O(n) time complexity, and O(n) space
complexity [2.5 Points].
b) Design an algorithm to solve the above task with O(n^2) time complexity, and O(1) space
complexity [2.5 Points]
Pseudocode in plain text is acceptable for this question but must be clear.
Example:
Input: arr[] = {3, 4, 5, 6, 7, 1, 2}, d=2
Output: 5 6 7 1 2 3 4
Solution:
Algorithm a (Using temp array):
• First store the elements from index d to N-1 into the temp array.
• Then store the first d elements of the original array into the temp array.
• Copy back the elements of the temp array into the original array
Marking Scheme: Correct Sol 2.5 Points; Partial: 0.5-2.0 Points, No Solution: 0
Marking Scheme: Correct Sol 2.5 Points; Partial: 0.5-2.0 Points, No Solution: 0
Calculate the Levenshtein-Distance for str1 = “PLASMA”, and str2 = “ALTRUISM” using
Dynamic Programming Approach of Levenshtein-Distance.
Pseudocode Levenshtein-Distance is given below.
Levenshtein-Distance is the minimum edit distance between two strings is the minimum number
of editing operations:
- Insertion
- Deletion
- Substitution
Needed to transform one into the other. Examples include Spell correction, computational
biology etc. Normally, each operation has cost of 1. However, in Levenshtein, the substitution
cost can be taken as 2.
For two strings X of length n Y of length m. We define D(i,j) the edit distance between X[1..i]
and Y[1..j] i.e., the first i characters of X and the first j characters of Y. The edit distance
between X and Y is thus D(n,m).
Algorithm
//Initialization D(i,0) = i
D(0,j) = j
//Recurrence Relation:
For each i = 1…n
For each j = 1…m
D(i,j)= min(
D(i-1, j) + 1,
D(i, j-1) + 1,
D(i-1, j-1) + 2, if X(i) ≠ Y(j)
D(i-1, j-1) + 0 if X(i) = Y(j) )
//Termination: D(n,m) is distance
Sol:
While visiting the Northern Areas of Pakistan you asked your travel guide to provide you
information regarding the famous tourist points. Your travel guide handed you the following table
showing four tourist points and the distances between these locations. You are determined to visit
all tourist points once and return to the starting location besides reducing your travel expenses.
Based on your knowledge in this course how would travel to these locations. Show all the
computations and steps to solve it
𝐴1 𝐴2 𝐴3 𝐴4
𝐴1 0 2 1 4
𝐴2 2 0 3 2
𝐴3 1 3 0 5
𝐴4 4 2 5 0
Marking Scheme: Correct Naming of Solution i.e. TSP: 2 Points followed by marks on correct
steps and solution b/w 0 -3
Any Greedy Solution e.g. Steiner Tree etc without within 2 times of optimal solution = 2 Points
(Maximum)
Question # 9 [CLO: 5] [5 marks]
Let φ = (x1 v x1 v x2) Λ (¬x1 v ¬x2 v ¬x2) Λ (¬x1 v x2 v x2) which is 3-CNF formula with m=2
variables and L=3 clauses.
a) Find the value of k = m + 2L [0.25 Points]
b) Prove that Φ is satisfiable iff GΦ has a vertex cover of size k [3.75 Points]
[Hint: You can directly think of a solution, or two step solution i) reduce 3SAT to
Independent Set ii) Reduce Independent Set to k-Vertex Cover]
c) From k-vertex cover solution above, find whether x1x2 = {00, 01, 10, 11} is satisfiable or
not [1 Point]
Solution:
Marking Scheme
Apply the Bellman-Ford shortest path algorithm on the following graph, starting from vertex "a".
Show all steps
Solution
4 Points: There is negative cycle. Students should complete and optionally show 2nd loop of the
algorithm. -0.5 for each wrong entry
# edges A b C d e
0 0 ∞ ∞ ∞ ∞
1 0 ∞ ∞ -2 5
2 0 8 0 -2 -5
3 0 -2 -10 -2 -5
4 -1 -2 -14 -2 -5