Tutorial 7 Solutions
Tutorial 7 Solutions
Dynamic Programming 2
Solution Sketch
1 Number of parenthesis
Count the number of expressions containing n pairs of parentheses that are correctly matched. For n = 3,
possible expressions are ((())), ()(()), ()()(), (())(), (()()).
Note: The above number is also called Catalan number which can be computed faster, but think of DP
approach to count number of possibilities.
Solution. Think of the first opening brace ‘(’. Of the n brace pairs, i would be before its matching ‘)’,
Pn−1
rest n-i-1 would be after. So for each i we can have n − 1 such options. Sol[n] = i=0 Sol[i] × Sol[n − i − 1].
The maximum of values stored at the root equals the size of the maximum independent set.
To construct the actual solution set, traceback using the following rules. Set the maximum of s(r) and
s̄(r) as active and the other as non-active for root r. If s(r) is active, output r, and continue traceback from
its children setting s̄(w) active at each child w of the root. If s̄(r) is active, set a maximum of s(w) and s̄(r)
as active and the other as non-active, for each child w of the root, possibly output each w where s(w) is set
to active, and continue traceback on all active values. After reaching all leaves, a maximum independent set
is outputed. Time complexity O(n).
L[j, k ′ ] = length of LIS in A[1..j] of k ′ contiguous subsarrays and ending with A[j].
Then
max(M, L[j − 1, k ′ ] + 1)
′ if A[j − 1] < A[j]
L[j, k ] =
M otherwise
where M = max1≤i<j {L[i, k ′ − 1] + 1 : A[i] < A[j]}.
As the base case, L[j, k ′ ] = 0 if k ′ = 0 or j < k ′ . Time Complexity: O(n2 k), and since it depends only
of previous k, space can be optimized to O(n) by computing in reverse order of j. Note that computing
LIS of original problem and then taking maximum length k subarrays does not give optimal solution. Say
A = {1, 2, 5, 3, 4}. LIS is {(1, 2), (3, 4)}, but if k = 1 solution is (1, 2, 5) which is not a subset of original LIS.
dist1 [v, w(u, v)] = min {dist1 [u, w(x, u)] + w(u, v)}
(x,u)∈E,w(x,u)<w(u,v)
dist2 [v, w(u, v)] = min {dist1 [u, w(x, u)] + w(u, v), dist2 [u, w(x, u)] + w(u, v))}
(x,u)∈E,w(x,u)>w(u,v)
The length of the shortest bitonic path to v would be dist[v] = min(dist1 [v, ·], dist2 [v, ·]).
Algorithm. Process vertices in topological order. For dist1 process incoming edges and outgoing edges in
increasing order, maintaining the shortest path through incoming edges as we use edges smaller than each
outgoing edge while processing it. For dist2 process incoming edges and outgoing edges in decreasing order,
maintaining the shortest path through incoming edges as we use edges larger than each outgoing edge while
processing it. This requires O(deg(v)) time to process a vertex v, taking overall O(m) time. Additionally,
we need O(m log n) time for sorting adjacency list. Space complexity O(m + n).
5 Programming
Solve and submit on codechef by end of the week.
• https://www.codechef.com/SEP12/problems-old/CROWD
https://discuss.codechef.com/t/crowd-editorial/562
• https://www.codechef.com/problems/CR195
https://discuss.codechef.com/t/cr195-editorial/22145