0% found this document useful (0 votes)
30 views6 pages

General 2 CS

This document contains solutions to 6 problems related to computer science topics like algorithms, complexity analysis, and graph theory. The solutions provide detailed explanations and justifications for determining the runtime of an algorithm to find the longest palindrome in a string, solving a linear programming problem and its dual, finding the minimum cost order to open shops to minimize costs, analyzing the maximum flow and minimum cut in a network, and determining reductions between search problems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

General 2 CS

This document contains solutions to 6 problems related to computer science topics like algorithms, complexity analysis, and graph theory. The solutions provide detailed explanations and justifications for determining the runtime of an algorithm to find the longest palindrome in a string, solving a linear programming problem and its dual, finding the minimum cost order to open shops to minimize costs, analyzing the maximum flow and minimum cut in a network, and determining reductions between search problems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Bocconi University— 30540: Computer Science II General 2

Fabrizio Iozzi and Luca Trevisan June 10, 2021

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

# strings with more than 2 chars are palindrome if


# leftmost and rightmost are equal and
# removing leftmost and rightmost results in a palindrome

# k = length of substring otg examine


for k in range(3,n):
# i = start of the substring examined
for i in range(n-k+1):
if S[i] == S[i+k-1] and pal[i+1][i+k-2]:
max_pal = k
pal[i][i+k-1] = True

return max_pal

3. (20 points) Consider the linear program


Max :
try it 292 +593

max x1 3x2 + 3x3 2g , 492 +391 ≥ 1


-

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

(a) Write the dual program


(b) Is the primal solution x⇤ = (0, 0, 4) optimal? Explain your answer.

Solution: The dual program is

min 4y1 + 2y2 + 5y3


subject to
2y1 4y2 + 3y3 1
y1 + 3y2 2y3 3
y1 y3 3
yi 0, i = 1, 2, 3
General 2 — June 10, 2021 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.

Solution: Open the shops in decreasing maintenance cost order. This is an


optimal order. To prove this, let 1 , . . . , n be the permutation of {1, . . . , n}
that gives the decreasing order of maintenance cost. That is, c j c k whenever
j < k. Let
C = c 1 + 2c 2 + 3c 3 + · · · + nc n
be the overall cost of the greedy solution.
Now suppose there is a permutation ⌧1 , . . . , ⌧n that is distinct from . We
show this is not optimal. Let k 2 {1, . . . , n} be the smallest integer such that
c k > c⌧k . This means c k appears as c⌧k+j for some j > 0. In the ⌧ order, the
cost of the two shops is
kc⌧k + (k + j)c⌧k+j .
If, in the ⌧ order, we swap the two shops the new cost is

(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. (15 points) Consider the following network:


2
A C
4 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.

Solution: The maximum flow is 6, obtained, for example, adding a flow of 2


along SACT , a flow of 2 along SBDT and a flow of 2 along SBCDT . There
are other paths for maximum flow. The only vertex reachable from S in the
residual graph is A and the corresponding minimum cut is {SA} {BCDT }.
There are no other minimum cuts because in the residual graph B, C and D
can all reach T .

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.


(b) If X is NPC and Y is in NP then Y is NPC.
(c) If X is in P, then Y is in P.
(d) If Y is in P, then X is in P.

Solution: Only b) and d). X reduces to Y means that there is a polynomial


time algorithm that transforms an instance of X into an instance of Y in such
a way that the answer YES to Y corresponds to the answer YES to X and the
answer NO to Y corresponds to the answer NO to X.
In other words, if you had a black box to solve Y efficiently, you could use it to
solve X efficiently. X is no harder than Y .

(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

(b) If X is NPC and Y is in NP then Y is NPC. TRUE. Let Z 2 N P . Then


Z  X (because X is NPC) and X  Y so that Z  Y . Moreover,
Y 2 N P . Then Y is NPC, since every problem in NP reduces to Y and
Y 2 NP .
(c) If X is in P, then Y is in P. FALSE. Y is at least as hard as X, not as
hard as X.
(d) If Y is in P, then X is in P. TRUE. X could be solved using the polynomial
time algorithm that solves Y applied for a polynomial number of times.
HOW TO WRITE THE DUAL OF AN LP

① 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

↑wi1h fixed constraints .

LP win

hxethkztkss.ly
:
2- =

,
-
Xi -
Xz -
73 ≥ -2

•* + % NORMAL MIN ᵈᵈᵈ will be NORMAL MAX


92
274 x2 ≥ -3
yz
- -

≈, ,
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
?⃝

You might also like