Assignment #4, CS/531
Due Date: Mon. Nov. 7, 2011
UNSUPPORTED SOLUTIONS RECEIVE NO CREDIT.
Total points: 51
1 (7 pts). Maximum Contiguous Subsequence Sum Problem revisited.
Let
A
[1
..n
] be an array of numbers. The elements in
A
can be either positive or negative.
We want to find the indices
k,l
so that the sum
∑
l
i
=
k
A
[
i
] is maximum among all possible
choices of
k,l
. For example if
A
=
{−
3
,
12
,
−
6
,
10
,
−
5
,
2
}
, the answer is
k
= 2
,l
= 4, since
A
[2] +
A
[3] +
A
[4] = 12 + (
−
6) + 10 = 16 is the maximum sum of all possible choices.
This is exactly the problem 3 in HW2. However, this time you must describe an
O
(
n
) time
algorithm for solving this problem.
Answer:
Define an array
B
[0
,
· · ·
,n
], where each element
B
[
i
] is defined to be:
B
[
i
] = the maximum contiguous subsequence sum of subsequences in
A
[1
..i
] that
ends at
A
[
i
].
Then we have:
B
[
i
] =
braceleftBigg
0
if
i
= 0
max
{
0
,B
[
i
−
1] +
A
[
i
]
}
if
i >
0
Explanation:
When
i
= 0, there is no element in
A
[
∗
] that we can include in the subsequence. So
B
[0] is
the default value 0.
When
i >
0, there are two possibilities:
•
The maximum contiguous subsequence that ends at
A
[
i
] contains
A
[
i
]. In this case, the
entry is
B
[
i
−
1] +
A
[
i
].
•
The maximum contiguous subsequence that ends at
A
[
i
] contains no element from A at
all. (This can happen when all contiguous subsequences ending with
A
[
i
] have negative
sum. In this case, we just take empty subsequence as the answer). In this case
B
[
i
] = 0.
Since we don’t know which case gives the larger sum, we take the maximum of the two.
The algorithm is as follows:
Algorithm 1
MaxSubsequenceSum
1:
Max
:=
B
[0] := 0
2:
For
i
= 1 to
n
Do:
B
[
i
] = max
{
0
,B
[
i
−
1] +
A
[
i
]
}
Max
= max
{
B
[
i
]
,Max
}
3:
Output
Max
Clearly, the algorithm takes Θ(
n
) time.
Note: This problem was an interview problem when Bloomburg visited UB campus a few
years ago. For students who were only able to describe a Θ(
n
2
) time algorithm, they basicalled
said “ByeBye”. For students who were able to give a Θ(
n
log
n
) solution, Bloomburg kept them
in contact list. For those students who got the Θ(
n
) time algorithm, they offered them onsite
interviews.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
2. (1 pt) Consider the graph
G
= (
V,E
) shown in Figure 1. The integers near an edge is its
weight. Using Kruskal’s algorithm, compute the minimum spanning tree
T
1
of
G
. List the edges
of
T
1
in the order they are added into
T
1
.
Answer:
5
1
7
8
6
3
4
2
9
2
3
4
6
b
c
d
e
f
h
a
g
Figure 1: Kruskal’s Algorithm.
The MST is
{
ae,hf,eb,cd,eh,hg,dh
}
.
3. (6 pts) For a given undirected, edge weighted graph
G
= (
V,E
), there might be more than
one MST of
G
. This can happen if
G
has two edges
e
′
and
e
′′
such that
w
(
e
′
) =
w
(
e
′′
). In this
case, when we run Kruskal algorithm on
G
, the edges
e
′
and
e
′′
may be processed in different
order, and this might results in two different MSTs of
G
.
Suppose that all edge weights of
G
are different. (Namely for any two edges
e
′
and
e
′′
, we
have
w
(
e
′
)
negationslash
=
w
(
e
′′
).) Under this condition, show that
G
has a unique MST.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '11
 XINHE
 Algorithms, Graph Theory, optimal solution, base station, Maximum contiguous subsequence

Click to edit the document details