Docsity

Algorithm Design and Analysis - Assignment 2 Solutions | CSE 202, Assignments of Computer Science

Toggle sidebar
bg1
CSE 202 Homework 2: Solutions
Combinatorial Search and Optimization:
Dynammic Programming and Greedy Algorithms
1. Bitonic euclidean traveling-salesperson problem: CLR 16-1
Solution(s)
We will consider two approaches to the problem that result in
O
(
n
2
)
algorithms. The first consists of
O
(
n
2
)
subproblems that are generated in constant time each, the second has
O
(
n
)
subproblems that require
O
(
n
)
time
to compute.
The first solution maintains subproblems,
D
,where
D
(
i; j
)
;j < i
, is the length of an optimal bitonic path with
endpoints
i
and
j
visiting all points
k<i
. Note that this path is unidirectional on points
k>j
since
j
is an
endpoint. The base case
D
(1
;
2)
is just the distance
j
p
1
p
2
j
. Recursively,
D
(
i; j
)=
min
(
D
(
i; j
1) +
j
p
j
p
j
1
j
;D
(
j; j
1) +
j
p
j
1
p
j
+1
j
+
P
j
+1
k<i
j
p
k
p
k
+1
j
)
Intuitively this compares the case where point
j
1
lies on the path leading to endpoint
j
to that where
j
1
leads
to
i
. Then, the final tour is the minimum over
j
of
D
(
n; j
)
with the closing edge added:
T
=
min
j<n
D
(
n; j
)+
j
p
n
p
j
j
.
To prove this is the optimal tour length we must show that the base case is optimal, the recursive formulation
maintains optimality, and the final solution can be obtained from the subproblems. By our definition of the
subproblem,
D
(1
;
2) =
j
p
1
p
2
j
is clearly optimal since the direct line is the only path to consider. Now, when
calculating
D
(
i; j
)
, node
k
(
j < k < i
)
is necessarily connected to
k
+1
because the path is bitonic. The
first node we have to make a decision about is
j
1
which has two choices. Consider the optimal bitonic path
with endpoints
i
and
j
; point
j
1
must come between 1 and
j
or between 1 and
i
, not both since
j
and
i
are
endpoints. In the first case,
j
1
is necessarily directly connected to
j
and the optimal path
D
(
i; j
1)
finishes
the path
D
(
i; j
)
. In the second,
j
1
is connected to
j
+1
and the path is
D
(
j; j
1)
with the monotonic path
from
j
1
to
n
(skipping only
j
) added. Optimality is inductively maintained. Finally, the optimal tour must
necessarily connect
(
n; n
1)
in one direction and some
(
n; j
)
in the other and thus follow an optimal bitonic
path with endpoints
n
and
j
. Minimizing over these path lengths plus the connecting edge
(
n; j
)
will give an
optimal bitonic tour.
There are
O
(
n
2
)
subproblems. Each is evaluated by a constant number of references to other subproblems, a
reference to an edge distance, and a monotonic path length (which can be calculated in
O
(
n
2
)
time beforehand
and written down). The evaluation ordering on subproblems (for
i
=1to
n
(for
j
=1to
i
) ... ) ensures referenced
subproblems have alreadybeen calculated. So, the total time is
O
(
n
2
)
.
Another approach (thanks to Nathan Segerlind and crew, and just summarized here) is to considerbitonic paths
with endpoints
i
and
i
+1
, containing all points
j < i
. The base case
D
(1
;
2)
is again just the edge (1,2).
Recursively,
D
(
i; i
+1) =
min
j
(
D
(
j; j
+1) +
j
p
j
p
i
j
+
j
p
i
+1
p
i
1
j
+
P
j
+1
<k<i
j
p
k
1
p
k
j
)
. Intuitively
this recursion considers all possible immediate predecessors to
i
, and completes the bitonic path with the optimal
subproblem already known plus the implicit monotonic segments. To find the final solution, the final closing
edge must also be considered. The optimality is inductively guaranteed because all possible bitonic tours with
endpoints
i; i
+1
have some node
j<i
1
directly connected to
i
combined with a bitonic tour with endpoints
j; j
+1
and a monotonic path from
j
+1
to
i
+1
. Further, all possible bitonic tourshave some immediate
predecessor,
j
, on the side not through
n
1
and therefore contain a bitonic pathwith endpoints
j
and
j
+1
.There
are
O
(
n
)
subproblems and each is calculated as the minimum over
O
(
n
)
previously calculated subproblems.
This algorithm is therefore
O
(
n
2
)
time.
2. Problem 3-6 in Skiena. In the United States, coins are minted with denominations of 1,5,10,25, and 50 cents.
Now consider a country whose coins are minted with denominationsof
f
d
1
; :::; d
k
g
units. They seek an algo-
rithm that will enable them to make change of
n
units using the minimum number of coins.
a) The greedy algorithm for making change repeatedly uses the biggest coin smaller than the amount to be
changed until it is zero. Provide a greedy algorithm for making change of
n
units using US denominations.
Prove its correctness and analyze its time complexity.
1
pf3
pf4

Partial preview of the text

Download Algorithm Design and Analysis - Assignment 2 Solutions | CSE 202 and more Assignments Computer Science in PDF only on Docsity!

CSE 202 Homework 2: Solutions Combinatorial Search and Optimization: Dynammic Programming and Greedy Algorithms

  1. Bitonic euclidean traveling-salesperson problem: CLR 16-

Solution(s) We will consider two approaches to the problem that result in O (n^2 ) algorithms. The first consists of O (n^2 ) subproblems that are generated in constant time each, the second has O (n) subproblems that require O (n) time to compute. The first solution maintains subproblems, D , where D (i; j ); j < i, is the length of an optimal bitonic path with endpoints i and j visiting all points k < i. Note that this path is unidirectional on points k > j since j is an endpoint. The base case D (1; 2) is just the distance jp 1 p 2 j. Recursively, D (i; j ) = min (D (i; j 1) + jpj pj 1 j; D (j; j 1) + jpj 1 pj +1 j +

P

j +1k <i jpk^ ^ pk^ +1^ j) Intuitively this compares the case where point j 1 lies on the path leading to endpoint j to that where j 1 leads to i. Then, the final tour is the minimum over j of D (n; j ) with the closing edge added: T =minj <n D (n; j ) + jpn pj j. To prove this is the optimal tour length we must show that the base case is optimal, the recursive formulation maintains optimality, and the final solution can be obtained from the subproblems. By our definition of the subproblem, D (1; 2) = jp 1 p 2 j is clearly optimal since the direct line is the only path to consider. Now, when calculating D (i; j ), node k (j < k < i) is necessarily connected to k + 1 because the path is bitonic. The first node we have to make a decision about is j 1 which has two choices. Consider the optimal bitonic path with endpoints i and j ; point j 1 must come between 1 and j or between 1 and i, not both since j and i are endpoints. In the first case, j 1 is necessarily directly connected to j and the optimal path D (i; j 1) finishes the path D (i; j ). In the second, j 1 is connected to j + 1 and the path is D (j; j 1) with the monotonic path from j 1 to n (skipping only j ) added. Optimality is inductively maintained. Finally, the optimal tour must necessarily connect (n; n 1) in one direction and some (n; j ) in the other and thus follow an optimal bitonic path with endpoints n and j. Minimizing over these path lengths plus the connecting edge (n; j ) will give an optimal bitonic tour. There are O (n^2 ) subproblems. Each is evaluated by a constant number of references to other subproblems, a reference to an edge distance, and a monotonic path length (which can be calculated in O (n^2 ) time beforehand and written down). The evaluation ordering on subproblems (for i = 1 to n (for j = 1 to i) ... ) ensures referenced subproblems have already been calculated. So, the total time is O (n^2 ). Another approach (thanks to Nathan Segerlind and crew, and just summarized here) is to consider bitonic paths with endpoints i and i + 1 , containing all points j < i. The base case D (1; 2) is again just the edge (1,2). Recursively, D (i; i + 1) = minj (D (j; j + 1) + jpj pi j + jpi+1 pi 1 j +

P

j +1<k <i jpk^ ^1 ^ pk^ j). Intuitively this recursion considers all possible immediate predecessors to i, and completes the bitonic path with the optimal subproblem already known plus the implicit monotonic segments. To find the final solution, the final closing edge must also be considered. The optimality is inductively guaranteed because all possible bitonic tours with endpoints i; i + 1 have some node j < i 1 directly connected to i combined with a bitonic tour with endpoints j; j + 1 and a monotonic path from j + 1 to i + 1. Further, all possible bitonic tours have some immediate predecessor, j , on the side not through n 1 and therefore contain a bitonic path with endpoints j and j + 1. There are O (n) subproblems and each is calculated as the minimum over O (n) previously calculated subproblems. This algorithm is therefore O (n^2 ) time.

  1. Problem 3-6 in Skiena. In the United States, coins are minted with denominations of 1,5,10,25, and 50 cents. Now consider a country whose coins are minted with denominations of fd 1 ; :::; dk g units. They seek an algo- rithm that will enable them to make change of n units using the minimum number of coins. a) The greedy algorithm for making change repeatedly uses the biggest coin smaller than the amount to be changed until it is zero. Provide a greedy algorithm for making change of n units using US denominations. Prove its correctness and analyze its time complexity.

b) Show that the greedy algorithm does not always give the minimum number of coins in a country whose denominations are f 1 ; 6 ; 10 g.

c) Give an efficient algorithm that correctly determines the minimum number of coins needed to make change of n units using denominations fd 1 ; :::; dk g. Analyze its running time.

Solution:

a)

Inputs: number of units to make change for n

Outputs: number of half dollars, quarter, dimes, nickels, and pennies to use (c 50 ; c 25 ; c 10 ; c 5 ; c 1 ).

M AKE C HANGE (n)

(a) c 50 = n div 50 (b) l ef tov er = n mo d 50 (c) c 25 = n div 25 (d) l ef tov er = n mo d 25 (e) c 10 = n div 10 (f) l ef tov er = n mo d 10 (g) c 5 = n div 5 (h) l ef tov er = n mo d 5 (i) c 1 = n div 1 (j) l ef tov er = n mo d 1 (k) return (c 50 ; c 25 ; c 10 ; c 5 ; c 1 )

Because the algorithm always performs 10 calculations, it’s worst-case running time is (1).

Proof of Optimality

Assume that the best non-greedy solution for a given instance of the problem is (b 50 ; b 25 ; b 10 ; b 5 ; b 1 ), where n = 50 b 50 + 25 b 25 + 10 b 10 + 5 b 5 + b 1. We show that the greedy solution is as good as or better than the best solution. The greedy solution is (c 50 ; c 25 ; c 10 ; c 5 ; c 1 ). We want to show that c 50 + c 25 + c 10 + c 5 + c 1  b 50 + b 25 + b 10 + b 5 + b 1.

Since the best solution is not greedy at some point there will be fewer coins of some denomination in the best solution vs. the greedy solution. We will show that any combination of coins with lower denominations which make up for the difference could be replaced with fewer coins. Therefore, the best solution must be equivalent to the greedy solution.

If b 5 0 < c 50 then 25 b 25 + 10 b 10 + 5 b 5 + b 1  50. To satisfy the given inequality b 2 5 could be  2. If that is the case then each 2 quarters could be replaced by one half-dollar thus using fewer coins. For this inequality all the cases are listed below.

(a) if b 25  2 , replace with 1 half-dollar (b) if b 25 = 1 we must also have either 2 dimes and 1 nickel, 1 dime and 3 nickels, etc., any of these combi- nations can be replaced with 1 half-dollar therefore using fewer coins (c) if b 25 = 0 we must also have either 5 dimes, 4 dimes and 2 nickels, etc., any of these combinations can be replaced with 1 half-dollar

If b 50 = c 50 and b 25 < c 25 then 10 b 10 + 5 b 5 + b 1  25.

(a) if b 10  3 , replace with 1 quarter and 1 nickel (b) if b 10 = 2 we must also have either 1 nickels or 5 pennies, all of which can be replaced with 1 quarter

(s) while total > 0 (t) cdcoin[total] = cdcoin[total] + 1 (u) total = total dcoin[total] (v) return (cd 1 ; cd 2 ; :::; cdk )

The running time of the above algorithm is O (nk ). Notice that n doesn’t properly describe our input size. The input parameter n can be represented with lg n bits. Assume that all di denominations are represented with a constant number of bits. The true input size is O (p + k ), where p = lg n. In terms of the true input size the running time of the algorithm is O (2p^ k ).