Algorithm Design and Analysis Homework #5 Due: 1pm, Monday, December 26, === Homework submission instructions ===

Similar documents
Data Structure and Algorithm Homework #1 Due: 2:20pm, Tuesday, March 12, 2013 TA === Homework submission instructions ===

Today: Amortized Analysis

Lecture 7: Amortized analysis

How many hours would you estimate that you spent on this assignment?

Written Exam Linear and Integer Programming (DM554)

Written Exam Linear and Integer Programming (DM545)

FALL 2018 MATH 4211/6211 Optimization Homework 1

CS483 Design and Analysis of Algorithms

Data Structures and Algorithm. Xiaoqing Zheng

Chart 3 Data in Array

FALL 2018 MATH 4211/6211 Optimization Homework 4

Problem set 1. (c) Is the Ford-Fulkerson algorithm guaranteed to produce an acyclic maximum flow?

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: October 11. In-Class Midterm. ( 7:05 PM 8:20 PM : 75 Minutes )

CS246: Mining Massive Data Sets Winter Only one late period is allowed for this homework (11:59pm 2/14). General Instructions

Lecture Notes for Chapter 17: Amortized Analysis

Fall 2017 November 10, Written Homework 5

Graduate Mathematical Economics Lecture 1

Algebra 2 CP Semester 1 PRACTICE Exam

Maximum sum contiguous subsequence Longest common subsequence Matrix chain multiplication All pair shortest path Kna. Dynamic Programming

CE 221 Data Structures and Algorithms. Chapter 7: Sorting (Insertion Sort, Shellsort)

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

CS 170 Algorithms Fall 2014 David Wagner MT2

Homework 6: Image Completion using Mixture of Bernoullis

More on Asymptotic Running Time Analysis CMPSC 122

Today s Menu. Administrativia Two Problems Cutting a Pizza Lighting Rooms

MATH10212 Linear Algebra B Homework 6. Be prepared to answer the following oral questions if asked in the supervision class:

Amortized Analysis (chap. 17)

Algorithms Exam TIN093 /DIT602

CSCI Homework Set 1 Due: September 11, 2018 at the beginning of class

Course Staff. Textbook

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Carnegie Mellon University Department of Computer Science /615 - Database Applications C. Faloutsos & A. Pavlo, Fall 2016

Solution Set 7, Fall '12

NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt):

Online algorithms December 13, 2007

Introduction to Bioinformatics Algorithms Homework 3 Solution

Amortized Analysis. DistributeMoney(n, k) 1 Each of n people gets $1. 2 for i = 1to k 3 do Give a dollar to a random person

COS 341: Discrete Mathematics

CSE 417. Chapter 4: Greedy Algorithms. Many Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

Dynamic Programming: Shortest Paths and DFA to Reg Exps

ICS141: Discrete Mathematics for Computer Science I

Homework 1 Submission

Name: Matriculation Number: Tutorial Group: A B C D E

Mathematics for Decision Making: An Introduction. Lecture 8

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Searching Algorithms. CSE21 Winter 2017, Day 3 (B00), Day 2 (A00) January 13, 2017

CMU CS 462/662 (INTRO TO COMPUTER GRAPHICS) HOMEWORK 0.0 MATH REVIEW/PREVIEW LINEAR ALGEBRA

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

PRIMES Math Problem Set

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

Midterm Exam Information Theory Fall Midterm Exam. Time: 09:10 12:10 11/23, 2016

Designing Information Devices and Systems I Spring 2017 Babak Ayazifar, Vladimir Stojanovic Homework 4

CprE 281: Digital Logic

MATH10212 Linear Algebra B Homework Week 3. Be prepared to answer the following oral questions if asked in the supervision class

DS-GA 1003: Machine Learning and Computational Statistics Homework 6: Generalized Hinge Loss and Multiclass SVM

Graduate Analysis of Algorithms Dr. Haim Levkowitz

Designing Information Devices and Systems II Fall 2018 Elad Alon and Miki Lustig Homework 9

CS103 Handout 08 Spring 2012 April 20, 2012 Problem Set 3

AS 101: The Solar System (Spring 2017) Course Syllabus

Algorithm Design and Analysis (NTU CSIE, Fall 2017) Homework #3. Homework #3. Due Time: 2017/12/14 (Thu.) 17:20 Contact TAs:

CSEP 521 Applied Algorithms. Richard Anderson Winter 2013 Lecture 1

10708 Graphical Models: Homework 2

Dynamic Programming: Shortest Paths and DFA to Reg Exps

DEPARTMENT OF MATHEMATICS

PreCalculus: Chapter 9 Test Review

Worksheet 1: Integrators

Hon.Algorithms (CSCI-GA 3520), Professor Yap Fall 2012 MIDTERM EXAM. Oct 11, 2012 SOLUTION

Discriminative Training. March 4, 2014

Chapter 2. Ma 322 Fall Ma 322. Sept 23-27

DISCRETE RANDOM VARIABLES EXCEL LAB #3

Fall 2016 Test 1 with Solutions

Machine Learning: Homework 5

Class Notes: Solving Simultaneous Linear Equations by Gaussian Elimination. Consider a set of simultaneous linear equations:

Sorting and Searching. Tony Wong

Machine Learning and Computational Statistics, Spring 2017 Homework 2: Lasso Regression

CSE 421 Algorithms. Richard Anderson Autumn 2015 Lecture 1

Math 3339 Homework 2 (Chapter 2, 9.1 & 9.2)

Class President: A Network Approach to Popularity. Due July 18, 2014

Data selection. Lower complexity bound for sorting

Amortized analysis. Amortized analysis

ORF 363/COS 323 Final Exam, Fall 2017

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

Math 180 Written Homework Assignment #10 Due Tuesday, December 2nd at the beginning of your discussion class.

Introductory Probability

CS261: Problem Set #3

Assignment 5 Bounding Complexities KEY

Problem Set 4. General Instructions

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Topics in Algorithms. 1 Generation of Basic Combinatorial Objects. Exercises. 1.1 Generation of Subsets. 1. Consider the sum:

Data Structures and Algorithms Chapter 2

Chapter 10 Study Guide The Mole Section 10 1

About this class. Yousef Saad 1. Noah Lebovic 2. Jessica Lee 3. Abhishek Vashist Office hours: refer to the class web-page.

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Written Homework #1: Analysis of Algorithms

Advanced Linear Programming: The Exercises

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Transcription:

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 currencies, a trader can start with 1 U.S. dollar and buy 49 2 0.0107 = 1.0486 U.S. 1

dollars, thus turning a profit of 4.86 percent. Suppose that we are given n currencies c 1, c 2,..., c n and an n n table R of exchange rates, such that one unit of currency c i buys R[i, j] units of currency c j. We would like to find a sequence of currencies c i1, c i2,..., c ik such that R[i 1, i 2 ] R[i 2, i 3 ] R[i k 1, i k ] R[i k, i 1 ] is maximized, where k n. If max {R[i 1, i 2 ] R[i 2, i 3 ] R[i k 1, i k ] R[i k, i 1 ]} > 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 10 5. 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: i 1, i 2,..., i k, i 1 where i 1, i 2,..., i k, i 1 is the sequence of the indices of the currencies in the profitable exchange, where i 1 < i j, for j = 2,..., k. The indices from i 2 to i k 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: 2

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. To be announced later. Problem 2. (10%) Read section 17.4 on the textbook. Then solve problem 17.4-3 on page 471. (a) If the ith operation does not trigger a contraction, then ĉ i = c i + Φ i Φ i 1 = 1 + 2num i size i 2num i 1 size i 1 = 1 + 2num i size i 2(num i + 1) size i = 1 + 2num i size i 2num i size i + 2 1 + 2 = 3. 3

(b) If the ith operation does trigger a contraction, then num i < size i 1 /3 = size i /2, and thus 2num i size i < 0. Therefore, ĉ i = c i + Φ i Φ i 1 = (num i + 1) + 2num i size i 2num i 1 size i 1 = (num i + 1) + 2num i size i 2(num i + 1) 3size i /2 = (num i + 1) + (size i 2num i ) (size i 2num i + size i /2) + 2 = num i size i /2 + 3 3. For both of the cases, the amortized cost ĉ i is bounded by a constant. Problem 3. (20%) Solve problem 17-5 on page 476 in the textbook. 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 rank L (x) to find x in L and rank L (x) 1 to move x to the front of the list by transpositions, c i = 2 rank L (x) 1. c. Since it takes rank L 1 (x) to find x in L and t i to move x to the front of the list by transpositions, c i =rank L i 1 (x) + t i. d. Consider transposing two adjacent list elements x and y in L i, so that y precedes x after the transposition. If x precedes y in L i, the potential would increase by 2; for otherwise, the potential would decrease by 2. e. Since rank Li 1 (x) counts the number of elements precedes x including itself, and the elements in L i 1 that precedes x is either followed or preceded by x in L i 1, rank Li 1 (x) = A + B + 1. With the same argument, we can get rank L i 1 (x) = A + C + 1. f. The change in potential of Φ(L i ) Φ(L i 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 4

inversions and eliminate B inversions while there is no element precedes x, with no more than t i inversions occurs in the worst case while performing transpositions in L i, therefore Φ(L i ) Φ(L i 1 ) 2( A B + t i ). g. ĉ i = c i + Φ(L i ) Φ(L i 1 ) 2 rank Li 1 (x) 1 + 2( A B + t i ) = 2( A + B + 1) 1 + 2( A B + t i ) = 4 A + 1 + 2t i 4( A + C + 1 + t i ) = 4(rank L i 1 (x) + t i ) = 4c i h. Since c i ĉ i 4c i for i = 1 to n C MTF (σ) = m i=1 c i m i=1 4c i = 4 m i=1 c i = 4(C H (σ)). Problem 4. (20%) Solve problem 17-1 on page 472. Key sentence: bit-reversal permutation swaps elements whose indices have binary representations that are the reverse of each other. 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 rev k that runs in time Θ(k), the following procedure runs in time O(nk) to perform the bit-reversal permutation on A. 5

BIT-REVERSAL-PERMUTATION(A[0..n 1]) 1 for(i = 1 to n 1) 2 reverse=rev k (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] 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 (x 1, x 2,..., x d ) nests within another box with dimensions (y 1, y 2,..., y d ) if there exists a permutation π on 1, 2,..., d such that x π(1) < y 1, x π(2) < y 2,..., x π(d) < y d. 6

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 {B 1, B 2,..., B n }. Give an efficient algorithm to find the longest sequence B i1, B i2,..., B ik of boxes such that B ij nests within B ij+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.) 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) < y 1, x π1 (2) < y 2,..., x π1 (d) < y d, and y π2 (1) < z 1, y π2 (2) < z 2,..., y π2 (d) < z d. Let π = π 2 π 1. Then, x π (i) = x π2 (π 1 (i)) < y π2 (i) < z i, 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 x 1, x 2,..., x d and y 1, y 2,..., y d respectively tow get two sequences (x 1, x 2,..., x d ) and (y 1, y 2,..., y d ), where x i x j and y i y j, for all indicesi < j. Then x nests within y if and only if x i < y i, 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, v 1, v 2,..., v n }, where every v i V corresponds a box B i. For the edge set E, add (v i, v j ) in E if and only if B i nests within B j, each with weight equals to 1, and add other 2n edges (s, v i ) and (v i, t) with edge weight equals to 0, for i = 1,..., n. It takes O(dn 2 + nd log d) to construct this graph. Then, to find the longest sequence B i1, B i2,..., B ik such that B ij nests within B ij+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(n 2 ) 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(dn 2 + nd log d). 7