CS383, Algorithms Spring 2009 HW6 Solutions

Similar documents
Data Structures and Algorithms CSE 465

Reasoning and writing about algorithms: some tips

Algorithms - Ch2 Sorting

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

1 Non greedy algorithms (which we should have covered

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Chapter 3, Algorithms Algorithms

Multiple-choice (35 pt.)

Technical University of Denmark

CS2223: Algorithms D-Term, Assignment 5

Midterm 1 Solutions. (i) False. One possible counterexample is the following: n n 3

Final Exam. If you plan to solve a problem using a standard graph algorithm then you should clearly

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

NAME: March 4. and ask me. To pace yourself, you should allow on more than 1 minute/point. For

Introduction to the Analysis of Algorithms. Algorithm

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

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

Theorem 2.9: nearest addition algorithm

Math 381 Discrete Mathematical Modeling

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

DATA STRUCTURES AND ALGORITHMS

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Table of Contents. Course Minutiae. Course Overview Algorithm Design Strategies Algorithm Correctness Asymptotic Analysis 2 / 32

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

UNIT 1 BASICS OF AN ALGORITHM

Vertex Cover Approximations

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

EE/CSCI 451 Spring 2018 Homework 8 Total Points: [10 points] Explain the following terms: EREW PRAM CRCW PRAM. Brent s Theorem.

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Graph Algorithms (part 3 of CSC 282),

Elementary maths for GMT. Algorithm analysis Part II

CSE 101 Homework 5. Winter 2015

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

CS 350 Final Algorithms and Complexity. It is recommended that you read through the exam before you begin. Answer all questions in the space provided.

Divide and Conquer Algorithms

EE/CSCI 451 Midterm 1

Writing Parallel Programs; Cost Model.

Chapter 9 Graph Algorithms

Test 1 Review Questions with Solutions

The divide and conquer strategy has three basic parts. For a given problem of size n,

Recurrences and Divide-and-Conquer

Approximation Algorithms

Design and Analysis of Algorithms CS404/504. Razvan Bunescu School of EECS.

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Subset sum problem and dynamic programming

END-TERM EXAMINATION

Practice Problems for the Final

Lecture 2: Getting Started

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

Introduction to Algorithms and Complexity Theory

Algorithms Activity 6: Applications of BFS

Module 2: Classical Algorithm Design Techniques

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes

CMPSCI611: Approximating SET-COVER Lecture 21

Binary Search to find item in sorted array

U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Umesh Vazirani February 13, Midterm 1

CSE 21 Spring 2016 Homework 4 Answer Key

CS 373: Combinatorial Algorithms, Spring 1999

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Searching Algorithms/Time Analysis

Chapter 9 Graph Algorithms

Algorithm Analysis Part 2. Complexity Analysis

CS 161 Summer 2009 Homework #1 Sample Solutions

Framework for Design of Dynamic Programming Algorithms

CSCE 411 Design and Analysis of Algorithms

Lecture 2: Divide and Conquer

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Solutions for Algorithm Design Exercises and Tests

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Thursday 1st June 2017 Time: 14:00-16:00

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Algorithms and Data Structures

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Divide and Conquer 4-0

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

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Lecture 9: Sorting Algorithms

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Chapter 3 Dynamic programming

Trees, Trees and More Trees

Algorithms for Data Science

CSE 373 Final Exam 3/14/06 Sample Solution

Tutorial 7: Advanced Algorithms. (e) Give an example in which fixed-parameter tractability is useful.

CS2351 Data Structures. Lecture 1: Getting Started

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Ma/CS 6a Class 27: Shortest Paths

Review for Midterm Exam

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Transcription:

Prof. Sergio A. Alvarez http://www.cs.bc.edu/ alvarez/ 21 Campanella Way, room 569 alvarez@cs.bc.edu Computer Science Department voice: (617) 552-4333 Boston College fax: (617) 552-6790 Chestnut Hill, MA 02467 USA CS383, Algorithms Spring 2009 HW6 s 1. The computational task of computing the maximum consecutive subsequence sum (MSS) of a numerical array of length n is described below, together with a simple algorithm (Algorithm 1) that performs this task. For example, the correct output for the MSS task for the input instance a = { 5, 3, 12, 8, 6, 4} is S = 17, i = 2, i = 6. Your main goal will be to develop an efficient divide and conquer algorithm for the MSS task. Algorithm 1: Simple Maximum Consecutive Subsequence Sum Input: Nonempty array a[1...n] of numbers. Output: Maximum consecutive sum S = i i i a[i], associated indices i, i. SimpleMSS(a[1...n]) (1) S = a[1], i = 1, i = 1 (2) foreach i = 1 to n (3) foreach j = i to n (4) S = i k j a[k] (5) if S > S (6) S = S, i = i, i = j (7) return S, i, i (a) Find the worst-case asymptotic running time of Algorithm 1 in big Θ notation. Explain carefully. Let n denote the length of the input array a[1...n]. Ignoring the time required for arithmetic operations (they are Θ(1) for operands that fit within a machine word), the asymptotic running time of Algorithm 1 is determined by the number of evaluations of a[k] in line 4. This number is the number of elements in the following set of index triples: {(i, j, k) 1 i n, i j n, i k j} Since the number of values of k between i and j is j 1 + 1, the total number of index triples equals (j i + 1) = j = (n i + 1)(n i + 2)/2 1 i n i j n 1 i n 1 j n i+1 1 i n Summing over all values of i, the result is a polynomial in n with leading term n 3 /6. It follows that Algorithm 1 runs in time Θ(n 3 ). 1

(b) Implement (in detailed pseudocode) two functions MSLeft(a[1...n]) and MSRight(a[1...n]) that run in time Θ(n) and that return the maximum consecutive subsequence sums (and corresponding index ranges) over ranges of the restricted forms i... n/2 and n/2 +1...i, respectively (note that these ranges reach the midpoint of the array). For example, for the input array a = { 5, 3, 12, 8, 6, 4}, MSLeft(a) and MSRight(a) would return the values S = 15, i = 2, i = 3 and S = 2, i = 4, i = 6, respectively. Algorithm 2: Boundary Sum from Below Input: Array a[1...n]. Output: S, i, i to restricted MSS task for lower half a[1... n/2 ], with 1 i i = n/2. MSSLeft(a[1...n]) (1) S = a[ n/2 ], i = i = n/2 (2) foreach i = n/2 1...1 (3) if S + a[i] > S (4) S = S + a[i], i = i (5) return S, i, i Algorithm 3: Boundary Sum from Above Input: Array a[1...n]. Output: S, i, i to MSS task for upper half a[ n/2 + 1...n], with n/2 + 1 = i i n. MSSRight(a[1...n]) (1) S = a[ n/2 + 1], i = i = n/2 + 1 (2) foreach i = n/2 + 1...n (3) if S + a[i] > S (4) S = S + a[i], i = i (5) return S, i, i (c) Design a Θ(n) gluing function for a divide and conquer algorithm for the MSS task. Provide detailed pseudocode. Gluing inputs include the MSS solutions S L, i L, i L and S H, i H, i H for the lower and upper halves a[1... n/2 ] and a[ n/2 + 1...n], respectively, as well as the full array a[1...n]. For example, for a = { 5, 3, 12, 8, 6, 4}, one would have S L = 15, i L = 2, i L = 3 and S H = 10, i H = 5, i H = 6. The output of the gluing function should be a solution S, i, i to the MSS task on the full array a[1...n]. Your gluing function may call the functions defined in 1b. See Algorithm 4. (d) Write detailed pseudocode for a divide and conquer algorithm that solves the MSS task using the gluing function from 1c. What is your algorithm s running time? Explain. 2

Algorithm 4: Gluing Step Input: Array a[1...n]; MSS half-solutions S L, i L, i L, S H, i H, i H (see above). Output: Global solution S, i, i to MSS task for full array a[1...n]. glue(a[1...n], S L, i L, i L, S H, i H, i H.) (1) (S ML, i ML, i ML ) = MSLeft(a[1...n]) (2) (S MH, i MH, i MH ) = MSRight(a[1...n]) (3) S M = S ML + S MH (4) if S M S L and S M S H (5) return (S M, i ML, i MR ) (6) else if S L S M and S L S H (7) return (S L, i L, i L ) (8) else (9) return (S H, i H, i H ) See Algorithm 5. Since the gluing step (Algorithm 4) runs in time Θ(n), the running time of Algorithm 5, T (n), satisfies the recurrence relation T (n) = 2T ( n/2 ) + Θ(n) By the Master Theorem (critical gluing time a = 2, b = 2, p = 1), we see that the asymptotic running time of Algorithm 5 is T (n) = Θ(n log n) Algorithm 5: Divide and Conquer MSS Input: Maximum consecutive sum S = i i i a[i], associated indices i, i. Output: Array of numbers a[1...n]. DCMSS(a[1...n]) (1) if n = 0 (2) return 0 (3) if n = 1 (4) return a[1] (5) (S L, i L, i L ) = DCMSS(a[1... n/2 ]) (6) (S R, i R, i R ) = DCMSS(a[ n/2 + 1...n]) (7) return GLUE(a[1...n], (S L, i L, i L ), (S R, i R, i R )) 3

2. Apply Dijkstra s algorithm to the weighted graph G that has the vertices A, B, C, D, E, F and the edge weights given below in adjacency list format (edges not listed have weight ). Let A be the start vertex. w(a, B) = 20, w(a, C) = 30, w(a, F ) = 100 w(b, C) = 30, w(b, D) = 20, w(b, F ) = 100 w(c, D) = 40, w(c, E) = 15, w(c, F ) = 60 w(d, E) = 50, w(d, F ) = 5 w(e, F ) = 60 Provide a complete iteration table that shows the contents of the distance and predecessor arrays at each stage, and that indicates what vertex is selected for expansion in each case. Each entry in the following table shows the distance and predecessor values for the vertex in the corresponding column. The rightmost column shows what vertex is expanded as a result of the distance updates in that row. A B C D E F expand 0, null, null, null, null, null, null A 0, null 20, A 30, A, null, null 100, A B 0, null 20, A 30, A 40, B, null 100, A C 0, null 20, A 30, A 40, B 45, C 90, C D 0, null 20, A 30, A 40, B 45, C 45, D E 0, null 20, A 30, A 40, B 45, C 45, D F 3. You re starting a new business that requires delivering orders from your warehouse to customers in various rural towns. You have a fleet of delivery trucks and a known grid of roads that they can travel on. There is a single road between every pair of towns. Road conditions sometimes shut down travel along a stretch of road. Such events are unpredictable, but you have for each road an estimate of the probability that the road will be closed at any given time. You are interested in determining routes that will maximize the probability that a delivery attempt will not run into a closed road anywhere along the route. Assuming that road closings occur independently of one another, this all clear probability along a route t 0, t 1,...t k, where the t i are the towns along the route, is the product of the individual probabilities p ti,t i+1 that each of the roads between consecutive towns t i and t i+1 will be open. (a) You wish to compute, for each of the n towns 1,...n, the route from the warehouse to that town that maximizes the all clear probability described above. The probability p i,j that the road from town i to town j will be clear (open) is known in advance for every pair of towns. The warehouse is considered to be town 0. Cast this as a computational task involving a graph. Precisely describe the graph involved, and give a detailed inputoutput specification of the computational task. Make explicit note of any constraints on the inputs. 4

The relevant graph here is a directed weighted graph with vertices 0...n and an edge from i to j with edge weight p i,j for every pair of vertices (i, j). The target computational task involving this graph is described below. I ll use the term multiplicative value for the product of the individual edge weights along a path in a weighted graph. A path of greatest multiplicative value between a given pair of vertices is one that has a multiplicative value that is not less than that of any other path between the same vertices. Algorithm 6: Greatest Multiplicative Value Paths Input: Weighted graph G with vertices 0...n and edge weights p i,j with 0 p i,j 1. Output: Array pred[1...n] such that for each vertex v 0 of G, there is a path from 0 to v of greatest multiplicative value that visits pred[v] immediately before v. (b) Describe an algorithm that solves the computational task described in 3a. detailed pseudocode with explanations. Provide The pseudocode appears below. We transform the graph G slightly and apply the standard Dijkstra single-source shortest paths algorithm to the modified graph G. The transformation simply replaces each edge weight by the negative of itslogarithm (which results in a non-negative value since the original edge weights are all less than or equal to 1). Since the log of a product is the sum of the logs of the factors, this transformation has the property that the multiplicative value of a path in the original graph may be obtained as the inverse logarithm of the standard additive path cost in the transformed graph. Furthermore, because of the negative sign, a path has greatest multiplicative value in the original graph G if and only if the path has least cost in the transformed graph G. Hence, the standard Dijkstra algorithm applied to the transformed graph finds the desired paths of greatest multiplicative value in the original graph. Algorithm 7: Dijkstra for Paths of Greatest Multiplicative Value Input: Weighted graph G with vertices 0...n and edge weights p i,j with 0 p i,j 1. Output: Array pred[1...n] such that for each vertex v 0 of G, there is a path from 0 to v of greatest multiplicative value that visits pred[v] immediately before v. productdijsktra(g) (1) Define G to be a graph with vertices 0...n and edge weights log p i,j, i, j = 0...n. (2) return standarddijkstra(g, 0) 4. Suppose that a store offers that for each item purchased at full price, you can get another item of the same price or less for free. You would like to buy a total of 2n items, in a way that maximizes the money saved through this offer. You know the price of each item, and you wish to pair them in the form (P 1, F 1 ), (P 2, F 2 ), (P n, F n ), where you pay for item P i and get item F i for free based on the purchase of P i (so F i costs no more than P i ). (a) Describe an algorithm that takes a list of 2n items and their prices as inputs, and returns an optimal pairing (P 1, F 1 ), (P 2, F 2 ), (P n, F n ). Optimality here means that 5

the total amount paid should be minimized (equivalently, the total amount saved should be maximized). Give detailed pseudocode. Algorithm 8 describes a simple greedy solution for this task: simply sort the items in decreasing order of price, and pair every two consecutive items, starting with the two highest priced items. Algorithm 8: Maximum Savings on Pay One Item, Get Second Item Free Input: Even-length array I[1...2n] of pairs (x, price(x)) representing items x with their prices. Output: Pairs[1...n] with Pairs[i] = (P i, F i ), where P i is to be purchased at full price and F i (price(f i ) price(p i )) is free, such that the savings n i=1 price(f i) is maximized by this pairing. maxsavings(i[1...2n]) (1) sort I in decreasing order of price (2) foreach i = 1...n (3) Pairs[i] = (I[2i 1], I[2i]) (4) return Pairs (b) Prove that your algorithm from 4a returns an optimal solution. Give a carefully reasoned and complete explanation. It is clear that the total discount depends only on which items are discounted. Thus, we focus our attention on the indices of the discounted items in the sorted array. We call such indices the discounted indices, and we list them in increasing order as i 1 < i 2 <... < i n. Each pairing of the items produces a set of discounted indices. The pairing produced by Algorithm 8 satisfies i k = 2k for all k = 1,.., n. We claim that for any admissible pairing, one must have i k >= 2k for all k. We will prove this below. Observe that this claim implies that the proposed pairing is optimal, since the value of the i-th item is a decreasing function of the index i, so that the savings associated with any admissible pairing will be less than the savings associated with the proposed pairing, which achieves the minimum allowable values of the discounted indices. Proving that i k >= 2k for any admissible pairing is quite easy. Admissibility reduces to the constraint that each discounted index be paired with a nondiscounted index corresponding to an item of equal or greater value, i.e. with a nondiscounted index which is less than or equal to the given discounted index. Thus, since there are precisely k discounted indices which are less than or equal to the k-th discounted index i k, there must also be at least k associated nondiscounted indices less than or equal to i k. But the total number of nondiscounted indices in the range 1,..., i k is precisely i k k. We must therefore have i k k >= k, or i k >= 2k. 6