General 2 CS
General 2 CS
General 2
1. (10 points) Consider the recurrence equation T (n) = aT (n/3) + O(n2 ), where
a 2 N and a > 0. Find the range of values of a such that T (n) = O(n2 ).
Solution: using the Master Theorem we have T (n) = aT (n/3) + O(n2 ), b = 3
and d = 2. If a < 9, then = log3 a < 2 and we have T (n) = O(n2 ).
2. (25 points) A palindrome is a nonempty string over some alphabet that reads
the same forward and backward. Examples of palindromes are all strings of
length 1, the words “mom”, “kayak”, “abba” and “racecar”.
Give and justify a dynamic programming algorithm to find the length of the
longest palindrome contained in the input string. For example, given the input
“multileveled”, the algorithm should return 5, the length of “level”. The palin-
drome should be made of consecutive characters, so for example the first “att”
and the letter “a” in “attraction” do not make a palindrome. The algorithm
should have a running time of O(n2 ).
Solution: All substrings of length 1 are palindromes. Strings of 2 characters
are palindromes if and only if the two characters are equal. Strings of length
k 3 are palindromes if and only if their first and last characters are equal and
the remaining k 2 characters string is a palindrome. Then we set the “base
cases” for length 1 and 2 and proceed from length 3 up to length n storing in
pal[i][j] True, if the substring starting at index i and ending at index j is
a palindrome, or False otherwise. The outer and the inner loops have O(n)
iterations, so the total running time of the algorithm is O(n2 ).
Following is a python implementation:
def longest_pal(S):
n = len(S)
pal = [[False for i in range(n)] for j in range(n)]
max_pal = 1
for i in range(n):
# every 1-char string is palindrome
General 2 — June 10, 2021 2
pal[i][i] = True
# every 2-char string is palindrome if the char are equal
if i+1 < n and S[i] == S[i+1]:
max_pal = 2
pal[i][i+1] = True
return max_pal
y l 1- 392 293 ≥ -3
-
-
subject to
ya Y3
-
≥ 3
2x1 x2 + x3 4
Yi ≥ 0 , i 1
2,3
=
,
4x1 + 3x2 2
3x1 2x2 x3 5
xi 0, i = 1, 2, 3
The primal solution (0, 0, 4) has an objective value of 12. In addition, it has both
the second and the third constraints not binding. By complementary slackness,
this implies y2 = 0 and y3 = 0. Solving the dual problem with these additional
constraints gives
2y1 1, y1 3, y1 3
whose only solution is y1 = 3. Thus, the dual solution corresponding to the
primal (0, 0, 4) is (3, 0, 0) and at (3, 0, 0) the dual objective is 12. By the strong
duality theorem, this is indeed the optimal solution to both problems.
4. (15 points) A firm has n shops which are now (i.e., month 0) all closed. Each
month, for the maintenance of shop i, i = 1, . . . , n, the firm pays ci > 0 if the
shop is closed, and 0 if it’s already opened. Starting on month 1, the firm will
re-open its shops, one per month, ending after n months.
(a) Describe an algorithm that allows the firm to open their shops in such an
order that it minimizes the overall cost.
(b) Prove that your algorithm is correct.
(c) Analyze the running time of your algorithm.
(k + j)c⌧k + kc⌧k+j
and we have
⇥ ⇤
kc⌧k + (k + j)c⌧k+j (k + j)c⌧k + kc⌧k+j = j(c⌧k+j c⌧k ) > 0,
because c⌧k+j = c k
and c k
> c⌧k . Thus, the ⌧ order is not optimal.
The running time of the algorithm is that needed for ordering the costs,
O(n log n).
General 2 — June 10, 2021 4
5
S 3 3 T
4 4
B D
2
(a) Find the value of the maximum flow and a minimum cut.
(b) Is the minimum cut unique? If not, show another minimum cut.
6. (15 points) Let X and Y be two search problems. Suppose we know that X
reduces to Y , X Y . Which of the following is true? (answer listing only the
true statements, like “a), d) and f)”) Note: NPC = NP-complete.
(a) If Y is NPC then X is NPC too. FALSE. For X to be NPC we need both
X in NP and any NP problem to reduce to X.
General 2 — June 10, 2021 5
① NORMALISATION :
LP MIN should be
ruin 4×2+23 probe -3 constraints
→
:
2- = hxet ≥ .
Sit
✗ it 221-23<-2
2×11-22 = 3
274+22+3×373
21,7<2,2370
not
a) go over
first 2 constraints since
they are I
xp -
Xz -
Xz ≥ -2
2X it Xz ≥ 3
27C it ✗ z ≤ 3
µ 2×1+22=3
LP win
hxethkztkss.ly
:
2- =
,
-
Xi -
Xz -
73 ≥ -2
≈, ,
g, gy, ≥ ,
21,7<2,2370
Max w=
-2yd +
392-393+394 .
ya -1212-293+294 ≤ 4
-
ye +
YL Ys
+
Yu 4
- -
≤
-
ye +39h ≤ 1 .
9119493 ,Yn≥o
?⃝