CSE 373 Analysis of Algorithms, Fall Homework #3 Solutions Due Monday, October 18, 2003

Similar documents
12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

Dynamic Programming (Part #2)

/463 Algorithms - Fall 2013 Solution to Assignment 3

Unit-5 Dynamic Programming 2016

14 Dynamic. Matrix-chain multiplication. P.D. Dr. Alexander Souza. Winter term 11/12

Lecture 13: Chain Matrix Multiplication

Lecture 8. Dynamic Programming

CMSC351 - Fall 2014, Homework #4

Write an algorithm to find the maximum value that can be obtained by an appropriate placement of parentheses in the expression

Partha Sarathi Manal

Give a short answer (i.e. 1 to 3 sentences) for the following questions:

Chain Matrix Multiplication

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

5.1 The String reconstruction problem

Optimization II: Dynamic Programming

CS 231: Algorithmic Problem Solving

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

Dynamic Programming II

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

We ve done. Now. Next

Solutions to Midterm 2 - Monday, July 11th, 2009

Introduction to Algorithms I

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

1 Non greedy algorithms (which we should have covered

Framework for Design of Dynamic Programming Algorithms

8. The Postman Problems

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

CSE 21 Spring 2016 Homework 5. Instructions

Recitation 12. Dynamic Programming Announcements. SegmentLab has been released and is due Apr 14.

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018

Computer Project #2 (Matrix Operations)

Backtracking and Branch-and-Bound

Algorithm Design Techniques part I

So far... Finished looking at lower bounds and linear sorts.

2. CONNECTIVITY Connectivity

Test 1 Review Questions with Solutions

Algorithms: COMP3121/3821/9101/9801

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Notes for Lecture 24

(Refer Slide Time: 02.06)

Introduction to Algorithms

MAL 376: Graph Algorithms I Semester Lecture 1: July 24

Lecture 3. Brute Force

CSC 373: Algorithm Design and Analysis Lecture 8

P Is Not Equal to NP. ScholarlyCommons. University of Pennsylvania. Jon Freeman University of Pennsylvania. October 1989

II. Functions. 61. Find a way to graph the line from the problem 59 on your calculator. Sketch the calculator graph here, including the window values:

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

Chapter 3 Dynamic programming

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

UML CS Algorithms Qualifying Exam Spring, 2004 ALGORITHMS QUALIFYING EXAM

Computer Algorithms-2 Prof. Dr. Shashank K. Mehta Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

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

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

Simple Graph. General Graph

Question Points Score TOTAL 55 TOTAL (Calibrated) 50

S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 165

Hypercubes. (Chapter Nine)

Outline. CS38 Introduction to Algorithms. Fast Fourier Transform (FFT) Fast Fourier Transform (FFT) Fast Fourier Transform (FFT)

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

Solutions for Operations Research Final Exam

CS2 Algorithms and Data Structures Note 9

(Refer Slide Time: 06:01)

Homework3: Dynamic Programming - Answers

Algorithms for Data Science

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Algorithms: Dynamic Programming

Dynamic Programming Matrix-chain Multiplication

Dynamic Programming Group Exercises

10/24/ Rotations. 2. // s left subtree s right subtree 3. if // link s parent to elseif == else 11. // put x on s left

Design and Analysis of Algorithms

Dynamic Programming. Lecture Overview Introduction

Elementary maths for GMT. Algorithm analysis Part II

Design and Analysis of Algorithms

Computer Sciences Department 1

Chapter 11: Graphs and Trees. March 23, 2008

Finite Math - J-term Homework. Section Inverse of a Square Matrix

6.854J / J Advanced Algorithms Fall 2008

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

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Assignment 5: Solutions

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

MATH20902: Discrete Maths, Problem Set 6

UCSD CSE 21 Winter 2017 Midterm 2 Practice Problems

Dynamic programming II - The Recursion Strikes Back

MC302 GRAPH THEORY SOLUTIONS TO HOMEWORK #1 9/19/13 68 points + 6 extra credit points

CS 170 Algorithms Spring 2009 David Wagner MT2

Ideally your algorithms for both parts should run in linear time. You will receive partial credit for a polynomial-time algorithm.

Lecture 4: Dynamic programming I

Module 11. Directed Graphs. Contents

TIM 206 Lecture Notes Integer Programming

COMP3121/3821/9101/ s1 Assignment 1

CMPS 102 Solutions to Homework 7

2.2 Set Operations. Introduction DEFINITION 1. EXAMPLE 1 The union of the sets {1, 3, 5} and {1, 2, 3} is the set {1, 2, 3, 5}; that is, EXAMPLE 2

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

CS 161 Fall 2015 Final Exam

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Lecture 9 March 4, 2010

14 More Graphs: Euler Tours and Hamilton Cycles

Transcription:

Piyush Kumar CSE 373 Analysis of Algorithms, Fall 2003 Homework #3 Solutions Due Monday, October 18, 2003 Problem 1 Find an optimal parenthesization of a matrix chain product whose sequence of dimensions is < 5, 10, 3, 12, 5 > We can use the Dynamic Programming algorithm described in textbook pp273 to solve this problem. Let m[i, j] be the minimum number of scalar multiplications needed to compute the matrix A i...j. 0 if i = j m[i, j] = min i k<j {m[i, k] + m[k + 1, j] + d i 1 d k d j } if i < j And we use another array s[i, j] to keep the value of k at which we can split the product A i A i+1... A j to obtain an optimal parenthesization. The two tables m[i, j] and s[i, j] for this problems are as follows. 1 2 3 4 1 0 150 330 405 2 0 360 330 3 0 180 4 0 2 3 4 1 1 2 2 2 2 2 3 3 From the table s[i, j], we know we can split the product A 1... A 4 at d 2 with the cheapest cost. So the optimal parenthesization is ((A 1 A 2 )(A 3 A 4 )). Problem 2 (Problem from Brassard and Bratley) There are n trading posts along a river. At any of the posts you can rent a canoe to be returned at any other post downstream. (It is next to impossible to paddle against the current.) For each possible departure point i and each possible arrival point j the cost of a rental from i to j is known. However, it can happen that the cost of renting from i to j is higher than the total cost of a series of shorter rentals. In this case you can return the first canoe at some post k between i and j and continue your journey in a second canoe. There is no extra charge for changing canoes in this way.

Give an efficient algorithm to determine the minimum cost of a trip by canoe from each possible departure point i to each possible arrival point j. In terms of n, how much time is needed by your algorithm? Let A[i, j] represent the cost fo renting a canoe at post i and returning it at post j. There are n subproblems: Let T [1, k] be the cheapest cost of traveling from post 1 to post k, for k = 1... n. Our goal is to find T [1, k], 1 < k n. here is the dynamic program: T [1, 1] = 0 for k = 2 to n do T [1, k] = min j=1...k 1 {T [1, j] + A[j, k]} The running time is O(n 2 ). Similarly, we can find T [2, k], 2 < k n, T [3, k], 3 < k n,..., T [n 1, n]. Totaly, the running time is O(n 3 ). Problem 3 Suppose you are driving around Chicago. Your goal is to drive on every single street. (Ignore that there are 1-way streets.) Can you drive on every street at most twice? If so, how? If not, why not? Yes We can use Depth First Search(DFS) to drive each street at most twice. Let us consider each street to be an edge and each intersection to be a vertex. Then a DFS moves along streets crossing intersections until we reach an intersection we have encountered before. At this point we back track down streets and explore any streets we have not taken before in exactly the same manner. If we proceed in this manner we will have explored every street exactly twice, one straight and one backtrack. Finally we will end up at our starting point. Problem 4 You are standing on a street looking for a particular house. But you do not know if you should go east or west. The house is at distance L from you, but you do not know what L is. (a) Give an algorithm to find the house. The algorithm should run in time O(L). (b) Draw a picture to illustrate your idea. (If you have a very good picture, then you do not even need to write words for part (a).) Use technique of repeated doubling. Walk distance 2 i in each direction, where i = 1, 2,. After the first i such that 2 i > L, we have found the house. Make sure you understand why the running time is O(L)!!!

Problem 5 Suppose you are given an integer A (usually say 32/64 bit integer). And you are only allowed the operations < statement > return {True False} < statement > z = { AND x, y OR x, y NOT x z + 1 z 1} < statement > if(x <>= y) then < statement > else < statement > on A and a constant number of other variables. How would you determine if A has only 1 bit on without using a loop. For instance A = 100000 returns true whereas A = 101000 returns false. Note: This is an O(1) operation. You can write the solution in three statements! And if you know C/C++, one line of C should suffice in writing this whole function! #define IsPowerOf2(x) (!(x & (x-1)) Problem 6 Consider the knapsock problem discussed in class: For set S = {s 1, s 2,, s n } of integers and target integer K, is there a subset T S, such that t i = K? t i T (1) Recall the algorithm from class. Provide the subproblems, then present pseudocode from class. (2) The algorithm from (1) only answers the question with a yes or no. What part of the algorithm would we modify so that we can compute the set T? What are the modifications? (1)Suppose we use Boolean ExistSubSet(i, K) represent whether or not there exists a subset T s 1, s 2,..., s i such that t i T t i = K. Then T RUE if i or K = 0 ExistSubSet(i, K) = ExistSubSet(i 1, K s i ) ExistSubSet(i 1, K) if K 0 algorithm as follows: function Boolean ExistSubSet(n,K) 1: for i = 0... n do ExistSubSet(i, 0) = T RUE; 2: for j = 1... K do ExistSubSet(0, j) = F ALSE; 3: for i = 1... n do 4: for j = 1... K do

5: ExistSubSet(i, j) = ExistSubSet(i 1, K s i ) ExistSubSet(i 1, K); 6: end for 7: end for 8: return ExistSubSet(n,K); (2)In fact, from the algorithm for (1), we can get an array ExistSubSet[0... n, 0... K]. Suppose we use a table to describe this array, then begin at ExistSubSet[n, K] and trace through the table following the elements which equal TRUE, we can compute the set T easily. For example, suppose we want to know whether s i belongs to set T from the condition ExistSubSet[i, j]=true, we discuss in three cases. Case 1: ExistSubSet[i 1, j s i ] = T RUE ExistSubSet[i 1, j] = F ALSE, then s i T. Then we repeat this process on ExistSubSet[i 1, j s i ] = T RUE to decide whether or not s i 1 T. Case 2:ExistSubSet[i 1, j s i ] = F ALSE ExistSubSet[i 1, j] = T RUE, then s i T. Then we repeat this process on ExistSubSet[i 1, j] = T RUE to decide whether or not s i 1 T. Case 3: ExistSubSet[i 1, j s i ] = T RUE ExistSubSet[i 1, j] = T RUE, then s i can T. But it can T too. we select one possibility randomly, then continue our trace as case 1 or case 2. Note: From case 3, we can conclude: trace different path, maybe we can get different set T. Problem 7 Professor Greedy suggests the following alternative algorithm for determining the minimum cost multiplication of a chain of matrices, A 1, A 2,..., A n. Recall (from class) that we are given a sequence d 0, d 1,..., d n of dimensions where matrix A i is a d i 1 d i matrix. The problem is the same as we did in class, to determine the order in which to multiply these matrices so that the the number of multiplications (or unit operations) is least. Professor Greedy suggest a greedy algorithm: find the index k such that 1 k n 1 where d k is maximum (You may assume that all d i s are distinct). The algorithm replaces the two matrices A k and A k+1 with a single matrix which is their product, A k A k+1. This is repeated on the remaining n 1 matrices until only a single matrix remains. (The intuition is that you want to get rid of large dimensions as quickly as possible, since they result in higher multiplication cost). Lets do an example of this algorithm: d =< 5, 3, 7, 2, 6 >. Since 7 is the largest inner dimension we first multiply C = A 2 A 3 whose multiplication cost is 3 7 2. The new resulting dimension vector is < 5, 3, 2, 6 >. We now multiply D = A 1 C with a cost of 5 3 2 and then finally we multiply DA 4 with a cost of 5 2 6. The total cost is 42 + 30 + 60 = 132. Show that this algorithm is not optimal. In particular give a sequence of matrix di-

mensions; show the result of the execution of the above algorithm and show that a better multiplication order exists. (One example is all that is needed to show that the algorithm is not correct). Shorter counterexamples are better than longer ones. This can be done with 4 matrices whose dimensions are in the range from 1 to 3. Counterexample: We can give a sequence of dimensions as follows < 1, 3, 5, 4, 2 > If use the method of Professor Greedy, we get the multiplication order (A 1 ((A 2 A 3 )A 4 )), the total cost is 60+24+6=90. However, if the product A 1 A 2 A 3 A 4 is parenthesized in this way ((A 1 A 2 )A 3 )A 4 ), the total cost is only 15+20+8=43. So this algorithm is not optimal.