0% found this document useful (0 votes)
21 views

Tutorial 7 Solutions

The document discusses solutions to 5 dynamic programming problems: 1) counting valid parenthesis expressions, 2) finding the maximum independent set in a tree, 3) finding the longest increasing subsequence with at most k contiguous subarrays, 4) finding the shortest bitonic path in a DAG, and 5) programming challenges on CodeChef to solve by the end of the week.

Uploaded by

Sara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Tutorial 7 Solutions

The document discusses solutions to 5 dynamic programming problems: 1) counting valid parenthesis expressions, 2) finding the maximum independent set in a tree, 3) finding the longest increasing subsequence with at most k contiguous subarrays, 4) finding the shortest bitonic path in a DAG, and 5) programming challenges on CodeChef to solve by the end of the week.

Uploaded by

Sara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

[CSN212] Study Group 7

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].

2 Maximum Independent Set in Tree.


Given a tree T = (V, E), design and analyze an algorithm to find a maximum size subset I ⊆ V such that
for all u, v ∈ I it holds (u, v) ∈
/ E. This is called the Maximum independent set problem, which is NP-hard
for general graphs. Hint. Two possible states of every node v are being in V ′ and not being in V ′ .
Solution. Dynamic Programming: For each node v store two values:
• s(v) for the size of the maximum independent in the subtree rooted at v with v as part of the solution.
• s̄(v) for the size of the maximum independent in the subtree rooted at v without v as part of the
solution.

Compute these values bottom-up as follows:


• s(v) = 1 + w:(v,w)∈E,w is child of v s̄(w)
P

• s̄(v) = w:(v,w)∈E,w is child of v max(s(w), s̄(w))


P

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).

3 Longest Increasing Subsequence Variant.


Given an array A[n], we have studied an algorithm to report the length of its longest increasing subsequence.
Design and analyze (time and space) an O(n2 k) algorithm to compute the length of the longest increasing
subsequence that consists of at most k contiguous subarrays. Eg. A = {1, 3, 5, 2, 6, 7, 2, 4, 9, 12, 15}, and
k = 3, the optimal solution is [1, 3, 5], [6, 7], [9, 12, 15]. With k = 2 the optimal solution is [1, 3, 5], [9, 12, 15].
Solution. Define

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.

4 Bitonic shortest path in DAG.


Given a directed acyclic graph G = (V, E) having positive edge weights, a bitonic path has subsequent edge
weights first strictly increasing up to some vertex and then strictly decreasing (strictly implies adjacent edges
cannot have equal weights). Design and analyze an algorithm to compute the shortest bitonic paths from a
given source s to all vertices.
Solution. Use same DP formulation as for general graphs but instead of solving using variant of
Dijikstra’s solve using Topological ordering, processing sorted incoming and outgoing edges of a node like
merging step of merge.
dist1 [v, w]= Length of the shortest bitonic s, v path in increasing phase with last edge weight w.
dist2 [v, w]= Length of the shortest bitonic s, v path in decreasing phase with last edge weight w.

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

You might also like