• 沒有找到結果。

Algorithm Design and Analysis Homework #5 Due: 1pm, Monday, December 26, 2011

N/A
N/A
Protected

Academic year: 2022

Share "Algorithm Design and Analysis Homework #5 Due: 1pm, Monday, December 26, 2011"

Copied!
7
0
0

加載中.... (立即查看全文)

全文

(1)

Algorithm Design and Analysis Homework #5

Due: 1pm, Monday, December 26, 2011

=== Homework submission instructions ===

• Submit the answers for writing problems (including your programming report) through the CEIBA system (electronic copy) or to the TA in R432 (hard copy).

Please write down your name and school ID in the header of your documents. You also need to submit your programming assignment (problem 1) to the Judgegirl System(http://katrina.csie.ntu.edu.tw/judgegirl/).

• Each student may only choose to submit the homework in one way; either all as hard copies or all through CEIBA except the programming assignment. If you submit your homework partially in one way and partially in the other way, you might only get the score of the part submitted as hard copies or the part submitted through CEIBA (the part that the TA chooses).

• If you choose to submit the answers of the writing problems through CEIBA, please combine the answers of all writing problems into only one file in the doc/docx or pdf format, with the file name in the format of “hw5 [student ID].{pdf,docx,doc}”

(e.g. “hw5 b99902010.pdf”); otherwise, you might only get the score of one of the files (the one that the TA chooses).

• For each problem, please list your references (they can be the names of the classmates you discussed the problem with, the URL of the information you found on the Internet, or the names of the books you read). The TA can deduct up to 100% of the score assigned to the problems where you don’t list your references.

Problem 1. (30%) Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency.

For example, suppose that 1 U.S. dollar buys 49 Indian rupees, 1 Indian rupee buys 2 Japanese yen, and 1 Japanese yen buys 0.0107 U.S. dollars. Then, by converting cur- rencies, a trader can start with 1 U.S. dollar and buy 49 × 2 × 0.0107 = 1.0486 U.S.

(2)

dollars, thus turning a profit of 4.86 percent. Suppose that we are given n currencies c1, c2, ..., cn and an n × n table R of exchange rates, such that one unit of currency ci buys R[i, j] units of currency cj. We would like to find a sequence of currencies hci1, ci2, ..., ciki such that R[i1, i2] · R[i2, i3] · · · R[ik−1, ik] · R[ik, i1] is maximized, where k ≤ n. If max {R[i1, i2] · R[i2, i3] · · · R[ik−1, ik] · R[ik, i1]} > 1, we call this set of currencies profitable. To result in successful arbitrage, a sequence of exchanges must begin and end with the same currency, but any starting currency may be considered.

Input:

The first line contain n, 2 ≤ n ≤ 50. The next n lines represent the exchange rate table R in the following format:

R[1, 1] R[1, 2] R[1, 3] . . . R[1, n]

R[2, 1] R[2, 2] R[2, 3] . . . R[2, n]

. . .

R[n, 1] R[n, 2] R[n, 3] . . . R[n, n]

with all numbers ranging from 10−5 to 105. Output:

For each intput table you must determine whether a sequence of exchanges exists that results in a profit of more than 1 percent (0.01). If a sequence exists you must print the sequence of exchanges that results in a profit. If there is more than one sequence that results in a profit of more than 1 percent you must print a sequence of minimal length and starting currency index, i.e., one of the sequences that uses the fewest exchanges of currencies to yield a profit. All profiting sequences must consist of n or fewer transactions where n is the dimension of the table giving exchange rates. You should output the profitable sequence in the following format:

i1, i2, ..., ik, i1

where i1, i2, ..., ik, i1 is the sequence of the indices of the currencies in the profitable ex- change, where i1 < ij, for j = 2, ..., k. The indices from i2 to ik are all distinct. If no profitable sequence can be found, output “Not profitable”. All numbers in this output line are separated by comma.

Sample Input 1:

(3)

3

1 0.6 0.8 1.6 1 1.2 1.2 0.7 1

Sample Output 1:

Not profitable Sample Input 2:

3

1 0.05 0.21 16 1 4 5.1 0.09 1

Sample Output 2:

1,3,1

Please write a program to solve this problem. Please also submit a report in which you give a clear description of your algorithm and analyze the running time of your algorithm.

Sol: To be announced later.

Problem 2. (10%) Read section 17.4 on the textbook. Then solve problem 17.4-3 on page 471.

Sol:

(a) If the ith operation does not trigger a contraction, then

ˆ

ci = ci+ Φi− Φi−1

= 1 + |2numi− sizei| − |2numi−1− sizei−1|

= 1 + |2numi− sizei| − |2(numi+ 1) − sizei|

= 1 + |2numi− sizei| − |2numi− sizei+ 2|

≤ 1 + 2 = 3.

(4)

(b) If the ith operation does trigger a contraction, then numi < sizei−1/3 = sizei/2, and thus 2numi− sizei < 0. Therefore,

ˆ

ci = ci+ Φi− Φi−1

= (numi+ 1) + |2numi− sizei| − |2numi−1− sizei−1|

= (numi+ 1) + |2numi− sizei| − |2(numi+ 1) − 3sizei/2|

= (numi+ 1) + (sizei− 2numi) − (sizei− 2numi+ sizei/2) + 2

= numi− sizei/2 + 3

≤ 3.

For both of the cases, the amortized cost ˆci is bounded by a constant.

Problem 3. (20%) Solve problem 17-5 on page 476 in the textbook.

Sol:

a. Each time when we encounter a key in the access sequence, the worst case is to check the whole list, taking O(n) time, thus is takes Ω(mn) time in the worst case.

b. Since it takes rankL(x) to find x in L and rankL(x) − 1 to move x to the front of the list by transpositions, ci = 2·rankL(x) − 1.

c. Since it takes rankL−1(x) to find x in L and ti to move x to the front of the list by transpositions, ci =rankL

i−1(x) + ti.

d. Consider transposing two adjacent list elements x and y in Li, so that y precedes x after the transposition. If x precedes y in Li, the potential would increase by 2; for otherwise, the potential would decrease by 2.

e. Since rankLi−1(x) counts the number of elements precedes x including itself, and the ele- ments in Li−1that precedes x is either followed or preceded by x in Li−1, rankLi−1(x) =

|A| + |B| + 1. With the same argument, we can get rankLi−1(x) = |A| + |C| + 1.

f. The change in potential of Φ(Li)−Φ(Li−1) counts the number of the maximum numbers of inversions occurring in the new round. Moving x to the front of L would cause |A|

(5)

inversions and eliminate |B| inversions while there is no element precedes x, with no more than ti inversions occurs in the worst case while performing transpositions in Li, therefore Φ(Li) − Φ(Li−1) ≤ 2(|A| − |B| + ti).

g.

ˆ

ci = ci+ Φ(Li) − Φ(Li−1)

≤ 2 · rankLi−1(x) − 1 + 2(|A| − |B| + ti)

= 2(|A| + |B| + 1) − 1 + 2(|A| − |B| + ti)

= 4|A| + 1 + 2ti

≤ 4(|A| + |C| + 1 + ti)

= 4(rankL

i−1(x) + ti)

= 4ci

h. Since ci ≤ ˆci ≤ 4ci for i = 1 to n

⇒ CMTF(σ) =Pm

i=1ci ≤Pm

i=14ci = 4Pm

i=1ci = 4(CH(σ)).

Problem 4. (20%) Solve problem 17-1 on page 472. Key sentence: bit-reversal permu- tation swaps elements whose indices have binary representations that are the reverse of each other.

Sol:

a. Let A[0..n − 1] denote the input array and S[0..n − 1] be the array with all elements initialled to be 0, where S[i] records whether A[i] has been swapped or not. Given revk that runs in time Θ(k), the following procedure runs in time O(nk) to perform the bit-reversal permutation on A.

(6)

BIT-REVERSAL-PERMUTATION(A[0..n − 1]) 1 for(i = 1 to n − 1)

2 reverse=revk(i) 3 if(S[index] =false)

4 swap A[i] and A[reverse]

5 S[i] =true 6 S[reverse] =true 7 end if

8 end for

b. The idea of the procedure BIT-REVERSED-INCREMENT(A) is very similar to INCREMENT(A) as taught in the class (p.454-455), the only difference is that we start the bit check

from the highest bit.

BIT-REVERSED-INCREMENT(A[0..k − 1]) 1 i = k − 1

2 while(A[i] 6= 0) 3 A[i] = 1 4 i = i − 1 5 end while

The amortized analysis of BIT-REVERSED-INCREMENT(A) is therefore also the same as of INCREMENT(A).

c. While we shift only one bit at a time in BIT-REVERSED-INCREMENT(A), the time complexity is O(n) under the restriction.

Problem 5. (20%) Nesting boxes

A d-dimensional box with dimensions (x1, x2, ..., xd) nests within another box with di- mensions (y1, y2, ..., yd) if there exists a permutation π on 1, 2, ..., d such that xπ(1) <

y1, xπ(2) < y2, ..., xπ(d) < yd.

(7)

1. Argue that the nesting relation is transitive.

2. Describe an efficient method to determine whether or not one d-dimensional box nests inside another.

3. Suppose that you are given a set of n d-dimensional boxes {B1, B2, ..., Bn}. Give an efficient algorithm to find the longest sequence hBi1, Bi2, ..., Biki of boxes such that Bij nests within Bij+1 for j = 1, 2, ..., k − 1. Express the running time of your algorithm in terms of n and d. (Hint: construct a directed graph where the vertices represents the boxes.)

Sol:

1. Suppose x nests within y and y nests within z. Let π1 and π2 be the permutations on 1, 2, . . . , d such that xπ1(1) < y1, xπ1(2) < y2, . . . , xπ1(d) < yd, and yπ2(1) < z1, yπ2(2) <

z2, . . . , yπ2(d) < zd. Let π0 = π2 ◦ π1. Then, xπ0(i) = xπ21(i)) < yπ2(i) < zi, for i = 1, . . . , d. That is, x nests within z. Therefore the nesting relation is transitive.

2. Given two d-dimensional boxes x and y. Sort on x1, x2, . . . , xd and y1, y2, . . . , yd respectively tow get two sequences (x01, x02, . . . , x0d) and (y01, y20, . . . , yd0), where x0i ≤ x0j and yi0 ≤ yj0, for all indicesi < j. Then x nests within y if and only if x0i < yi0, for i = 1, . . . , d. This method takes time O(d log d).

3. We transform this problem to a graph problem. First, construct a directed acyclic graph G = (V, E) with V = {s, t, v1, v2, . . . , vn}, where every vi ∈ V corresponds a box Bi. For the edge set E, add (vi, vj) in E if and only if Bi nests within Bj, each with weight equals to 1, and add other 2n edges (s, vi) and (vi, t) with edge weight equals to 0, for i = 1, . . . , n. It takes O(dn2 + nd log d) to construct this graph.

Then, to find the longest sequence hBi1, Bi2, ..., Biki such that Bij nests within Bij+1 for j = 1, 2, ..., k − 1, it is sufficient to find the longest path from s to t in G. Since it takes Θ(|V | + |E|) = O(n2) to find the longest lath in a directed acyclic graph (Section 24.2 and Exercise 24.2-3 in the textbook), the overall running time of this algorithm is O(dn2 + nd log d).

參考文獻

相關文件

▪ Given a file containing a sequence of 8-bit characters (256 characters), if the maximum character frequency is less than k of the minimum character frequency in the file, then

our class: four to six times harder than a normal one in NTU around seven homework sets (and a hard final project) homework due within two weeks.. even have homework 0 and 1 NOW

If you have stored the distance between every two cities in a table, what is the time complexity you need to find the optimal solution of travelling salesman problem.. Calculation

The Vertical Line Test A curve in the xy-plane is the graph of a function of x if and only if no vertical line intersects the curve more than once.. The reason for the truth of

That the sequence is increasing can be shown by induction... If not,

• Submit the answers for writing problems (including your programming report) through the CEIBA system (electronic copy) or to the TA in R432 (hard copy).. Please write down your

As a byproduct, we show that our algorithms can be used as a basis for delivering more effi- cient algorithms for some related enumeration problems such as finding

To do (9), you need to recall the exercise from hw 1 and hw 2 in Calculus I: (you do not need to turn in the following exercises) If you are not familiar with the exercises below,