CS383, Algorithms Spring 2009 HW6 Solutions

Size: px
Start display at page:

Download "CS383, Algorithms Spring 2009 HW6 Solutions"

Transcription

1 Prof. Sergio A. Alvarez alvarez/ 21 Campanella Way, room 569 Computer Science Department voice: (617) Boston College fax: (617) Chestnut Hill, MA 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

2 (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/ 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/ (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/ 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/ 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/ 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

3 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/ n]) (7) return GLUE(a[1...n], (S L, i L, i L ), (S R, i R, i R )) 3

4 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

5 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

6 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

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 2 Analysis of Algorithms Insertion Sort Loop invariants Asymptotic analysis Sofya Raskhodnikova and Adam Smith The problem of sorting Input: sequence a 1,

More information

Reasoning and writing about algorithms: some tips

Reasoning and writing about algorithms: some tips Reasoning and writing about algorithms: some tips Theory of Algorithms Winter 2016, U. Chicago Notes by A. Drucker The suggestions below address common issues arising in student homework submissions. Absorbing

More information

Algorithms - Ch2 Sorting

Algorithms - Ch2 Sorting Algorithms - Ch2 Sorting (courtesy of Prof.Pecelli with some changes from Prof. Daniels) 1/28/2015 91.404 - Algorithms 1 Algorithm Description Algorithm Description: -Pseudocode see conventions on p. 20-22

More information

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

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G. 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G. A shortest s-t path is a path from vertex to vertex, whose sum of edge weights is minimized. (b) Give the pseudocode

More information

1 Non greedy algorithms (which we should have covered

1 Non greedy algorithms (which we should have covered 1 Non greedy algorithms (which we should have covered earlier) 1.1 Floyd Warshall algorithm This algorithm solves the all-pairs shortest paths problem, which is a problem where we want to find the shortest

More information

CMPSCI 311: Introduction to Algorithms Practice Final Exam

CMPSCI 311: Introduction to Algorithms Practice Final Exam CMPSCI 311: Introduction to Algorithms Practice Final Exam Name: ID: Instructions: Answer the questions directly on the exam pages. Show all your work for each question. Providing more detail including

More information

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

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment Class: V - CE Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology Sub: Design and Analysis of Algorithms Analysis of Algorithm: Assignment

More information

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

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 Study: Chapter 4 Analysis of Algorithms, Recursive Algorithms, and Recurrence Equations 1. Prove the

More information

Chapter 3, Algorithms Algorithms

Chapter 3, Algorithms Algorithms CSI 2350, Discrete Structures Chapter 3, Algorithms Young-Rae Cho Associate Professor Department of Computer Science Baylor University 3.1. Algorithms Definition A finite sequence of precise instructions

More information

Multiple-choice (35 pt.)

Multiple-choice (35 pt.) CS 161 Practice Midterm I Summer 2018 Released: 7/21/18 Multiple-choice (35 pt.) 1. (2 pt.) Which of the following asymptotic bounds describe the function f(n) = n 3? The bounds do not necessarily need

More information

Technical University of Denmark

Technical University of Denmark Technical University of Denmark Written examination, May 7, 27. Course name: Algorithms and Data Structures Course number: 2326 Aids: Written aids. It is not permitted to bring a calculator. Duration:

More information

CS2223: Algorithms D-Term, Assignment 5

CS2223: Algorithms D-Term, Assignment 5 CS2223: Algorithms D-Term, 2015 Assignment 5 Teams: To be done individually Due date: 05/01/2015 (1:50 PM) Note: no late submission of HW5 will be accepted; we will talk about the solution of HW5 during

More information

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

Midterm 1 Solutions. (i) False. One possible counterexample is the following: n n 3 CS 170 Efficient Algorithms & Intractable Problems, Spring 2006 Midterm 1 Solutions Note: These solutions are not necessarily model answers. Rather, they are designed to be tutorial in nature, and sometimes

More information

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

Final Exam. If you plan to solve a problem using a standard graph algorithm then you should clearly NAME: CS 241 Algorithms and Data Structures Spring Semester, 2003 Final Exam May 2, 2003 Do not spend too much time on any problem. The point value approximates the time I expect you to need for the problem.

More information

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

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]: CSE 101 Homework 1 Background (Order and Recurrence Relations), correctness proofs, time analysis, and speeding up algorithms with restructuring, preprocessing and data structures. Due Thursday, April

More information

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

NAME: March 4. and ask me. To pace yourself, you should allow on more than 1 minute/point. For CS 241 Algorithms and Data Structures Spring Semester, 2004 Midterm Exam NAME: March 4 If you have any questions about a problem, quietly come to the front of the classroom and ask me. To pace yourself,

More information

Introduction to the Analysis of Algorithms. Algorithm

Introduction to the Analysis of Algorithms. Algorithm Introduction to the Analysis of Algorithms Based on the notes from David Fernandez-Baca Bryn Mawr College CS206 Intro to Data Structures Algorithm An algorithm is a strategy (well-defined computational

More information

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

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1 DIVIDE & CONQUER Definition: Divide & conquer is a general algorithm design strategy with a general plan as follows: 1. DIVIDE: A problem s instance is divided into several smaller instances of the same

More information

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

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 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 Course Info course webpage: - from the syllabus on http://cs.gmu.edu/

More information

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

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

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

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010 University of Toronto Department of Electrical and Computer Engineering Midterm Examination ECE 345 Algorithms and Data Structures Fall 2010 Print your name and ID number neatly in the space provided below;

More information

Theorem 2.9: nearest addition algorithm

Theorem 2.9: nearest addition algorithm There are severe limits on our ability to compute near-optimal tours It is NP-complete to decide whether a given undirected =(,)has a Hamiltonian cycle An approximation algorithm for the TSP can be used

More information

Math 381 Discrete Mathematical Modeling

Math 381 Discrete Mathematical Modeling Math 381 Discrete Mathematical Modeling Sean Griffin Today: -Equipment Replacement Problem -Min Spanning Tree Problem -Clustering Friday s Plan Intro to LPSolve software Download before class (link on

More information

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

Solutions to Problem 1 of Homework 3 (10 (+6) Points) Solutions to Problem 1 of Homework 3 (10 (+6) Points) Sometimes, computing extra information can lead to more efficient divide-and-conquer algorithms. As an example, we will improve on the solution to

More information

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

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk 8/29/06 CS 6463: AT Computational Geometry 1 Convex Hull Problem Given a set of pins on a pinboard and a rubber band around them.

More information

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

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm INTRODUCTION A. Preliminaries: Purpose: Learn the design and analysis of algorithms Definition of Algorithm: o A precise statement to solve a problem on a computer o A sequence of definite instructions

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS For COMPUTER SCIENCE DATA STRUCTURES &. ALGORITHMS SYLLABUS Programming and Data Structures: Programming in C. Recursion. Arrays, stacks, queues, linked lists, trees, binary

More information

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

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

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

Table of Contents. Course Minutiae. Course Overview Algorithm Design Strategies Algorithm Correctness Asymptotic Analysis 2 / 32 Intro Lecture CS 584/684: Algorithm Design and Analysis Daniel Leblanc1 1 Senior Adjunct Instructor Portland State University Maseeh College of Engineering and Computer Science Spring 2018 1 / 32 2 / 32

More information

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

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012 1 University of Toronto Department of Electrical and Computer Engineering Midterm Examination ECE 345 Algorithms and Data Structures Fall 2012 Print your name and ID number neatly in the space provided

More information

UNIT 1 BASICS OF AN ALGORITHM

UNIT 1 BASICS OF AN ALGORITHM UNIT 1 BASICS OF AN ALGORITHM Basics of an Algorithm Structure Page Nos. 1.0 Introduction 5 1.1 Objectives 6 1.2. Analysis and Complexity of Algorithms 6 1.3 Basic Technique for Design of Efficient Algorithms

More information

Vertex Cover Approximations

Vertex Cover Approximations CS124 Lecture 20 Heuristics can be useful in practice, but sometimes we would like to have guarantees. Approximation algorithms give guarantees. It is worth keeping in mind that sometimes approximation

More information

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

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018 COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018 Name: ID: Answer the questions directly on the exam pages. Show all your work for each question. More detail including comments

More information

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

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems. 2.3 Designing algorithms There are many ways to design algorithms. Insertion sort uses an incremental approach: having sorted the subarray A[1 j - 1], we insert the single element A[j] into its proper

More information

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

EE/CSCI 451 Spring 2018 Homework 8 Total Points: [10 points] Explain the following terms: EREW PRAM CRCW PRAM. Brent s Theorem. EE/CSCI 451 Spring 2018 Homework 8 Total Points: 100 1 [10 points] Explain the following terms: EREW PRAM CRCW PRAM Brent s Theorem BSP model 1 2 [15 points] Assume two sorted sequences of size n can be

More information

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

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +... Design and Analysis of Algorithms nd August, 016 Problem Sheet 1 Solutions Sushant Agarwal Solutions 1. A d-ary tree is a rooted tree in which each node has at most d children. Show that any d-ary tree

More information

Graph Algorithms (part 3 of CSC 282),

Graph Algorithms (part 3 of CSC 282), Graph Algorithms (part of CSC 8), http://www.cs.rochester.edu/~stefanko/teaching/11cs8 Homework problem sessions are in CSB 601, 6:1-7:1pm on Oct. (Wednesday), Oct. 1 (Wednesday), and on Oct. 19 (Wednesday);

More information

Elementary maths for GMT. Algorithm analysis Part II

Elementary maths for GMT. Algorithm analysis Part II Elementary maths for GMT Algorithm analysis Part II Algorithms, Big-Oh and Big-Omega An algorithm has a O( ) and Ω( ) running time By default, we mean the worst case running time A worst case O running

More information

CSE 101 Homework 5. Winter 2015

CSE 101 Homework 5. Winter 2015 CSE 0 Homework 5 Winter 205 This homework is due Friday March 6th at the start of class. Remember to justify your work even if the problem does not explicitly say so. Writing your solutions in L A TEXis

More information

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

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are

More information

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.

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. It is recommended that you read through the exam before you begin. Answer all questions in the space provided. Name: Answer whether the following statements are true or false and briefly explain your answer

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms CSE341T 09/13/2017 Lecture 5 Divide and Conquer Algorithms We have already seen a couple of divide and conquer algorithms in this lecture. The reduce algorithm and the algorithm to copy elements of the

More information

EE/CSCI 451 Midterm 1

EE/CSCI 451 Midterm 1 EE/CSCI 451 Midterm 1 Spring 2018 Instructor: Xuehai Qian Friday: 02/26/2018 Problem # Topic Points Score 1 Definitions 20 2 Memory System Performance 10 3 Cache Performance 10 4 Shared Memory Programming

More information

Writing Parallel Programs; Cost Model.

Writing Parallel Programs; Cost Model. CSE341T 08/30/2017 Lecture 2 Writing Parallel Programs; Cost Model. Due to physical and economical constraints, a typical machine we can buy now has 4 to 8 computing cores, and soon this number will be

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be if not careful with data structures 3 Definitions an undirected graph G = (V, E) is a

More information

Test 1 Review Questions with Solutions

Test 1 Review Questions with Solutions CS3510 Design & Analysis of Algorithms Section A Test 1 Review Questions with Solutions Instructor: Richard Peng Test 1 in class, Wednesday, Sep 13, 2017 Main Topics Asymptotic complexity: O, Ω, and Θ.

More information

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

The divide and conquer strategy has three basic parts. For a given problem of size n, 1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often

More information

Recurrences and Divide-and-Conquer

Recurrences and Divide-and-Conquer Recurrences and Divide-and-Conquer Frits Vaandrager Institute for Computing and Information Sciences 16th November 2017 Frits Vaandrager 16th November 2017 Lecture 9 1 / 35 Algorithm Design Strategies

More information

Approximation Algorithms

Approximation Algorithms Approximation Algorithms Subhash Suri June 5, 2018 1 Figure of Merit: Performance Ratio Suppose we are working on an optimization problem in which each potential solution has a positive cost, and we want

More information

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

Design and Analysis of Algorithms CS404/504. Razvan Bunescu School of EECS. Welcome Design and Analysis of Algorithms Razvan Bunescu School of EECS bunescu@ohio.edu 1 Course Description Course Description: This course provides an introduction to the modern study of computer algorithms.

More information

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

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions U.C. Berkeley CS70 : Algorithms Midterm 2 Solutions Lecturers: Sanjam Garg and Prasad aghavra March 20, 207 Midterm 2 Solutions. (0 points) True/False Clearly put your answers in the answer box in front

More information

Subset sum problem and dynamic programming

Subset sum problem and dynamic programming Lecture Notes: Dynamic programming We will discuss the subset sum problem (introduced last time), and introduce the main idea of dynamic programming. We illustrate it further using a variant of the so-called

More information

END-TERM EXAMINATION

END-TERM EXAMINATION (Please Write your Exam Roll No. immediately) Exam. Roll No... END-TERM EXAMINATION Paper Code : MCA-205 DECEMBER 2006 Subject: Design and analysis of algorithm Time: 3 Hours Maximum Marks: 60 Note: Attempt

More information

Practice Problems for the Final

Practice Problems for the Final ECE-250 Algorithms and Data Structures (Winter 2012) Practice Problems for the Final Disclaimer: Please do keep in mind that this problem set does not reflect the exact topics or the fractions of each

More information

Lecture 2: Getting Started

Lecture 2: Getting Started Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,

More information

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM NAME: This exam is open: - books - notes and closed: - neighbors - calculators ALGORITHMS QUALIFYING EXAM The upper bound on exam time is 3 hours. Please put all your work on the exam paper. (Partial credit

More information

Introduction to Algorithms and Complexity Theory

Introduction to Algorithms and Complexity Theory Introduction to Algorithms and Complexity Theory Iris Cong UCLA Math Circle - HS II Example 1. Algorithm to find the maximum of a list of N positive integers (call this list L 0, L N-1 ): initialize accumulator

More information

Algorithms Activity 6: Applications of BFS

Algorithms Activity 6: Applications of BFS Algorithms Activity 6: Applications of BFS Suppose we have a graph G = (V, E). A given graph could have zero edges, or it could have lots of edges, or anything in between. Let s think about the range of

More information

Module 2: Classical Algorithm Design Techniques

Module 2: Classical Algorithm Design Techniques Module 2: Classical Algorithm Design Techniques Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Module

More information

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

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, 2016 Huffman Codes CLRS: Ch 16.3 Ziv-Lempel is the most popular compression algorithm today.

More information

CMPSCI611: Approximating SET-COVER Lecture 21

CMPSCI611: Approximating SET-COVER Lecture 21 CMPSCI611: Approximating SET-COVER Lecture 21 Today we look at two more examples of approximation algorithms for NP-hard optimization problems. The first, for the SET-COVER problem, has an approximation

More information

Binary Search to find item in sorted array

Binary Search to find item in sorted array Binary Search to find item in sorted array January 15, 2008 QUESTION: Suppose we are given a sorted list A[1..n] (as an array), of n real numbers: A[1] A[2] A[n]. Given a real number x, decide whether

More information

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

U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Umesh Vazirani February 13, Midterm 1 U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Umesh Vazirani February 13, 2018 Name: Targaryen Midterm 1 SID: 0123456789 Name and SID of student to your left: Lannister Name

More information

CSE 21 Spring 2016 Homework 4 Answer Key

CSE 21 Spring 2016 Homework 4 Answer Key 1 CSE 21 Spring 2016 Homework 4 Answer Key 1. In each situation, write a recurrence relation, including base case(s), that describes the recursive structure of the problem. You do not need to solve the

More information

CS 373: Combinatorial Algorithms, Spring 1999

CS 373: Combinatorial Algorithms, Spring 1999 CS 373: Combinatorial Algorithms, Spring 1999 Final Exam (May 7, 1999) Name: Net ID: Alias: This is a closed-book, closed-notes exam! If you brought anything with you besides writing instruments and your

More information

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

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I MCA 312: Design and Analysis of Algorithms [Part I : Medium Answer Type Questions] UNIT I 1) What is an Algorithm? What is the need to study Algorithms? 2) Define: a) Time Efficiency b) Space Efficiency

More information

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

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D SOLUTIONS EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D This exam consists of 6 questions, worth 10 points in total, and a bonus question for 1 more point. Using

More information

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

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms Computer Science 385 Design and Analysis of Algorithms Siena College Spring 2019 Topic Notes: Brute-Force Algorithms Our first category of algorithms are called brute-force algorithms. Levitin defines

More information

Searching Algorithms/Time Analysis

Searching Algorithms/Time Analysis Searching Algorithms/Time Analysis CSE21 Fall 2017, Day 8 Oct 16, 2017 https://sites.google.com/a/eng.ucsd.edu/cse21-fall-2017-miles-jones/ (MinSort) loop invariant induction Loop invariant: After the

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)

More information

Algorithm Analysis Part 2. Complexity Analysis

Algorithm Analysis Part 2. Complexity Analysis Algorithm Analysis Part 2 Complexity Analysis Introduction Algorithm Analysis measures the efficiency of an algorithm, or its implementation as a program, as the input size becomes large Actually, an estimation

More information

CS 161 Summer 2009 Homework #1 Sample Solutions

CS 161 Summer 2009 Homework #1 Sample Solutions CS 161 Summer 2009 Homework #1 Sample Solutions Regrade Policy: If you believe an error has been made in the grading of your homework, you may resubmit it for a regrade. If the error consists of more than

More information

Framework for Design of Dynamic Programming Algorithms

Framework for Design of Dynamic Programming Algorithms CSE 441T/541T Advanced Algorithms September 22, 2010 Framework for Design of Dynamic Programming Algorithms Dynamic programming algorithms for combinatorial optimization generalize the strategy we studied

More information

CSCE 411 Design and Analysis of Algorithms

CSCE 411 Design and Analysis of Algorithms CSCE 411 Design and Analysis of Algorithms Set 4: Transform and Conquer Slides by Prof. Jennifer Welch Spring 2014 CSCE 411, Spring 2014: Set 4 1 General Idea of Transform & Conquer 1. Transform the original

More information

Lecture 2: Divide and Conquer

Lecture 2: Divide and Conquer Lecture 2: Divide and Conquer Paradigm Convex Hull Median finding Paradigm Given a problem of size n divide it into subproblems of size n, a 1, b >1. Solve each b subproblem recursively. Combine solutions

More information

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

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is

More information

Solutions for Algorithm Design Exercises and Tests

Solutions for Algorithm Design Exercises and Tests Chapter 4 Solutions for Algorithm Design Exercises and Tests 4.1 Divide and Conquer 4.1.1 Solutions for Selected Exercises Solution for Exercise #1 in Section 1.9 Solution for Part (a): This problem requires

More information

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

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, 2016 Instructions: CS1800 Discrete Structures Final 1. The exam is closed book and closed notes. You may

More information

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

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Thursday 1st June 2017 Time: 14:00-16:00 COMP 26120 Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE Algorithms and Imperative Programming Date: Thursday 1st June 2017 Time: 14:00-16:00 Please answer THREE Questions from the FOUR

More information

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

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started CS4311 Design and Analysis of Algorithms Lecture 1: Getting Started 1 Study a few simple algorithms for sorting Insertion Sort Selection Sort Merge Sort About this lecture Show why these algorithms are

More information

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

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15 600.363 Introduction to Algorithms / 600.463 Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15 14.1 Introduction Today we re going to talk about algorithms for computing shortest

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Spring 2019 Alexis Maciel Department of Computer Science Clarkson University Copyright c 2019 Alexis Maciel ii Contents 1 Analysis of Algorithms 1 1.1 Introduction.................................

More information

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov Divide and Conquer Design and Analysis of Algorithms Andrei Bulatov Algorithms Divide and Conquer 4-2 Divide and Conquer, MergeSort Recursive algorithms: Call themselves on subproblem Divide and Conquer

More information

Divide and Conquer 4-0

Divide and Conquer 4-0 Divide and Conquer 4-0 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain

More information

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

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 07 Single-Source Shortest Paths (Chapter 24) Stephen Scott and Vinodchandran N. Variyam sscott@cse.unl.edu 1/36 Introduction

More information

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

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 CS 124 Quiz 2 Review 3/25/18 1 Format You will have 83 minutes to complete the exam. The exam may have true/false questions, multiple choice, example/counterexample problems, run-this-algorithm problems,

More information

Lecture 9: Sorting Algorithms

Lecture 9: Sorting Algorithms Lecture 9: Sorting Algorithms Bo Tang @ SUSTech, Spring 2018 Sorting problem Sorting Problem Input: an array A[1..n] with n integers Output: a sorted array A (in ascending order) Problem is: sort A[1..n]

More information

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1 Algorithm Analysis Spring Semester 2007 Programming and Data Structure 1 What is an algorithm? A clearly specifiable set of instructions to solve a problem Given a problem decide that the algorithm is

More information

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?

More information

Chapter 3 Dynamic programming

Chapter 3 Dynamic programming Chapter 3 Dynamic programming 1 Dynamic programming also solve a problem by combining the solutions to subproblems. But dynamic programming considers the situation that some subproblems will be called

More information

Trees, Trees and More Trees

Trees, Trees and More Trees Trees, Trees and More Trees August 9, 01 Andrew B. Kahng abk@cs.ucsd.edu http://vlsicad.ucsd.edu/~abk/ How You ll See Trees in CS Trees as mathematical objects Trees as data structures Trees as tools for

More information

Algorithms for Data Science

Algorithms for Data Science Algorithms for Data Science CSOR W4246 Eleni Drinea Computer Science Department Columbia University Thursday, October 1, 2015 Outline 1 Recap 2 Shortest paths in graphs with non-negative edge weights (Dijkstra

More information

CSE 373 Final Exam 3/14/06 Sample Solution

CSE 373 Final Exam 3/14/06 Sample Solution Question 1. (6 points) A priority queue is a data structure that supports storing a set of values, each of which has an associated key. Each key-value pair is an entry in the priority queue. The basic

More information

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

Tutorial 7: Advanced Algorithms. (e) Give an example in which fixed-parameter tractability is useful. CS 161 Summer 2018 8/9/18 Tutorial 7: Advanced Algorithms Final Review Advanced Algorithms 1. (Warmup with Advanced Algorithms) (Difficulty: Easy to Medium) (a) T/F All NP-complete decision problems are

More information

CS2351 Data Structures. Lecture 1: Getting Started

CS2351 Data Structures. Lecture 1: Getting Started CS2351 Data Structures Lecture 1: Getting Started About this lecture Study some sorting algorithms Insertion Sort Selection Sort Merge Sort Show why these algorithms are correct Analyze the efficiency

More information

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

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance Algorithm (03F) Lecture3: Algorithm Analysis A step by step procedure to solve a problem Start from an initial state and input Proceed through a finite number of successive states Stop when reaching a

More information

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

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017 CS 6, Lecture 5 Quicksort Scribe: Sam Keller (05), Seth Hildick-Smith (06), G. Valiant (07) Date: January 5, 07 Introduction Today we ll study another sorting algorithm. Quicksort was invented in 959 by

More information

Ma/CS 6a Class 27: Shortest Paths

Ma/CS 6a Class 27: Shortest Paths // Ma/CS a Class 7: Shortest Paths By Adam Sheffer Naïve Path Planning Problem. We are given a map with cities and noncrossing roads between pairs of cities. Describe an algorithm for finding a path between

More information

Review for Midterm Exam

Review for Midterm Exam Review for Midterm Exam 1 Policies and Overview midterm exam policies overview of problems, algorithms, data structures overview of discrete mathematics 2 Sample Questions on the cost functions of algorithms

More information

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

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri 1 Introduction Today, we will introduce a fundamental algorithm design paradigm,

More information