Upload
shiang-yun-yang
View
207
Download
0
Embed Size (px)
Citation preview
Advanced Algorithms
Exercise 2
Group 2
陳右緯、楊翔雲、蔡宗衛、BINAYA KAR、林淑慧
2013/11/6 1Q1 Q8
Q3
• Given an integer (not necessary a decimal
number) with n digits, we want to remove m
(m < n) digits from this number such that the
resulting number is as large as possible.
Design an O(n) time algorithm to solve it.
• Ex. n = 9, m = 3
input : 987645821
remove bold place, maximum result = 987821
2013/11/6 2Q1 Q8
Q3 – efficient algorithm
2013/11/6 Q1 Q8 3
s[] : digit as [0, n-1]
r[] : maximum result (stack)
for(i = top = 0; i < n; i++)
if(top + n – i <= n)
while(i < n)
r[top++] = s[i++]
break;
while(top > 0 && s[i] > r[top-1])
top--;
if(top + n – i <= n)
break;
r[top++] = s[i]
return r[];
Q4
• Let x1, x2, … , xn be a sequence of integers
and k be another given integer. Design an
algorithm to find a subsequence xi , xi+1, …,
xj (of consecutive elements) such that the sum
of the numbers in it is exactly k if there does
exist such a subsequence.
2013/11/6 4Q1 Q8
Q4 – efficient algorithm
2013/11/6 Q1 Q8 5
s[] : s[i] = sigma(x[j]), j <= i
x[], k : input
hash[] : infinity.
hash[0] = 0
for(i = 1; i <= n; i++)
hash[s[i]] = min(hash[s[i]], i);
for(i = 1; i <= n; i++)
index = hash[s[i]-k]
if(index <= i)
return true // pair(index+1, i)
return false
Q5
• Given a 2-dimensional n x n array (or matrix)
of 0/1 integers, design a linear time algorithm
to find a sub-square (a sub-square matrix with
consecutive indices in both dimensions) with
the largest size such that all the elements in the
sub-square are equal to 1.
2013/11/6 6Q1 Q8
Q5 – efficient algorithm
2013/11/6 Q1 Q8 7
m[][], n : input
dp[][] : init zero
r = 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(m[i][j] == 1)
dp[i][j]=min(dp[i-1][j],
dp[i][j-1],dp[i-1][j-1])+1;
r = max(r, dp[i][j])
return r;
Q6
• Given an undirected graph G, a vertex subset I is
called an independent set of G if for every pair of
vertices u and v in I, uv not in E(G). The independent
set problem is to find an independent set with
maximum size. Design a linear time algorithm to
solve the problem on undirected trees. Can you
extend your algorithm to solve the weighted case?
(That is, each node is associated with a positive
weight, and you are asked to find an independent set
with maximum weight sum.)
2013/11/6 8Q1 Q8
Q6 – efficient algorithm
2013/11/6 Q1 Q8 9
weight[] : input
dp[n][2] : init zero.
dfs(1, -1)
dfs(nd, p) {
for(neighbor e for nd) {
if(e == p)
continue
dfs(e, nd)
dp[nd][0]+=max(dp[e][0], dp[e][1])
dp[nd][1]+=dp[e][0];
}
dp[nd][1] += weight[nd];
}
return max(dp[1][0], dp[1][1])
Q7
• Let x1, x2, … , xn be a sequence of real numbers (not
necessarily positive). Design an algorithm to find a
substring xi , xi+1, …, xj such that the product of the
numbers in it is maximum over all substrings. The
product of the empty substring is defined as 1.
2013/11/6 10Q1 Q8
Q7 – efficient algorithm
2013/11/6 Q1 Q8 11
x[], n : input
mndp[] :
mxdp[] :
mndp[0] = +oo
mxdp[0] = -oo
r = 1
for(i = 1; i <= n; i++) {
mxdp[i] = max(x[i], mxdp[i-1]*x[i],
mndp[i-1]*x[i])
mndp[i] = min(x[i], mxdp[i-1]*x[i],
mndp[i-1]*x[i])
r = max(r, mxdp[i], mndp[i])
}
return r
Q8
• Given a sequence of real numbers x1, x2, …, xn
(not necessarily positive), n > 1, and an
integer L, 1 <= L <= n. Design an algorithm
to find a substring xi, xi+1, …, xj such that its
length j - i+1 <= L and the sum of the
numbers in it is maximum over all substrings
with length not greater than L. Your algorithm
should run in O(n) time (note: not O(nL)).
2013/11/6 12Q1 Q8
Q8 – efficient algorithm
2013/11/6 Q1 Q8 13
x[], n, L : input
Q[] : monotone queue
s[] : s[0] = 0
head = tail = 0
r = -oo
for(i = 1; i <= n; i++)
s[i] = s[i-1] + x[i]
while(head < tail && s[Q[head]] >= s[i])
tail--;
Q[tail++] = s[i];
r = max(r, s[i], s[i]-s[Q[head]]);
while(head < tail && Q[head] <= i-L)
head++;
return r;
Q9
• Given a sequence of objects where each object
is associated with a value and a weight, design
an algorithm to find a subsequence such that
its corresponding value sequence is increasing
and its weights’ sum is maximized.
2013/11/6 14Q1 Q8
Q9 – efficient algorithm
2013/11/6 Q1 Q8 15
x[], w[], n: input
discretization for x[] in [1, n] --- O(nlgn)
dynamic_RMQ_struct S : init zero.
r = w[1]
for(i = 1; i <= n; i++)
v = S.query_max(0, x[i]-1) --- O(lg n)
S.updateVal(x[i], v+w[i]) --- O(lg n)
r = max(r, v+w[i])
return r
Q10
• Design an algoritm to solve Problem 10154 “Weights and Measures” in the web site: http://acm.uva.es/problemset/.
2013/11/6 16Q1 Q8
Q10 – efficient algorithm(1)
2013/11/6 Q1 Q8 17
A[].first – strong, A[].second – weight
sort(A, A+n)
dp[0] = 0
for(i = 1; i <= n; i++)
dp[i] = +oo;
for(i = 0; i < n; i++)
for(j = n; j >= 1; j--)
if(dp[j-1]+A[i].w <= A[i].s)
dp[j] = min(dp[j], dp[j-1]+A[i].w)
for(i = n; ; i--)
if(dp[i] != +oo)
return i;
Q10 – efficient algorithm(2)
2013/11/6 Q1 Q8 18
A[].first – weight, A[].second – strong
sort(A, A+n)
using pattern as inserting sort.
for(i = 1; i <= n; i++) {
..xx..
}