CS60003 Algorithm Design and Analysis, Autumn

Similar documents
CMSC 754 Computational Geometry 1

Computational Geometry

Elementary maths for GMT. Algorithm analysis Part II

FINAL EXAM SOLUTIONS

Computing intersections in a set of line segments: the Bentley-Ottmann algorithm

Formal Model. Figure 1: The target concept T is a subset of the concept S = [0, 1]. The search agent needs to search S for a point in T.

Lecture 3: Art Gallery Problems and Polygon Triangulation

Assignment 1 (concept): Solutions

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

The Hunt-Szymanski Algorithm for LCS

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

Connected Components of Underlying Graphs of Halving Lines

CSE 332, Spring 2010, Midterm Examination 30 April 2010

BMO Round 1 Problem 6 Solutions

1 Lower bound on runtime of comparison sorts

Algorithms and Applications

11. APPROXIMATION ALGORITHMS

CS 372: Computational Geometry Lecture 3 Line Segment Intersection

15-451/651: Design & Analysis of Algorithms November 4, 2015 Lecture #18 last changed: November 22, 2015

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

On Merging Straight Skeletons

(Refer Slide Time: 00:50)

Lecture 11 (Application of Integration) Areas between Curves Let and be continuous and on. Let s look at the region between and on.

CMPSCI 105: Lecture #12 Searching, Sorting, Joins, and Indexing PART #1: SEARCHING AND SORTING. Linear Search. Binary Search.

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

Stabbers of line segments in the plane

CS261: Problem Set #1

Computational Geometry

ARC 088/ABC 083. A: Libra. DEGwer 2017/12/23. #include <s t d i o. h> i n t main ( )

DDS Dynamic Search Trees

We assume uniform hashing (UH):

Interleaving Schemes on Circulant Graphs with Two Offsets

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

The Running Time of Programs

Table of Contents. Chapter 1: Introduction to Data Structures... 1

The Geodesic Integral on Medial Graphs

Multi-Way Search Trees

26 The closest pair problem

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

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Space vs Time, Cache vs Main Memory

Sorting Pearson Education, Inc. All rights reserved.

Multi-Way Search Trees

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Algorithm Design and Time Analysis

Optimization and approximation on systems of geometric objects van Leeuwen, E.J.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Advanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret

Orthogonal line segment intersection. Computational Geometry [csci 3250] Laura Toma Bowdoin College

Data Structures Lesson 9

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

( ) n 5. Test 1 - Closed Book

The Encoding Complexity of Network Coding

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Approximation Algorithms

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

CS 532: 3D Computer Vision 11 th Set of Notes

What is a minimal spanning tree (MST) and how to find one

Line segment intersection (I): Orthogonal line segment intersection. Computational Geometry [csci 3250] Laura Toma Bowdoin College

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

such a manner that we are able to understand, grasp and grapple with the problem at hand in a more organized fashion.

Computer Science Foundation Exam

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

Properties of red-black trees

Lecture 1. 1 Notation

On the number of distinct directions of planes determined by n points in R 3

Approximation Algorithms for the Unit Disk Cover Problem in 2D and 3D

Algorithms and Data Structures

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Planar Point Location

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Data Structures Lecture 3 Order Notation and Recursion

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Randomized incremental construction. Trapezoidal decomposition: Special sampling idea: Sample all except one item

Discrete Mathematics I So Practice Sheet Solutions 1

Geometric and Thematic Integration of Spatial Data into Maps

Binary Search Trees, etc.

PACKING DIGRAPHS WITH DIRECTED CLOSED TRAILS

On the Max Coloring Problem

Notes and Answers to Homework Exam 1, Geometric Algorithms, 2017

Lecture 9 March 4, 2010

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

Approximation Algorithms

Test 1 - Closed Book Last 4 Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Solutions to problem set 1

1/60. Geometric Algorithms. Lecture 1: Introduction. Convex Hulls

CSE332 Summer 2010: Midterm Exam Sample Solutions

Computational Geometry

Topology Homework 3. Section Section 3.3. Samuel Otten

Algorithm Design and Analysis

arxiv: v1 [cs.ds] 3 Mar 2015

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Search Trees: BSTs and B-Trees

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Red-Black-Trees and Heaps in Timestamp-Adjusting Sweepline Based Algorithms

Multidimensional Divide and Conquer 2 Spatial Joins

Logarithmic-Time Point Location in General Two-Dimensional Subdivisions

Transcription:

CS60003 Algorithm Design and Analysis, Autumn 2009 10 Mid-Semester Test Maximum marks: 45 September 20, 2009 Total time: 2 hours Roll no: Name: [ Write your answers in the question paper itself. Be brief and precise. Answer all questions. ] 1. Consider the following function that accepts a positive integer n as input. playwith (n) { while (n > 1) { Increment n by 1. while (n is even), set n = n/2. (a) Prove that the function terminates for every input integer n 1. (5) Solution Clearly, the function terminates for n = 1. Except perhaps in the first iteration of the outer while loop, the loop body starts with an odd value of n. In the loop body, the value of n changes to (n + 1)/2 k for some k 1. We have (n + 1)/2 k (n + 1)/2 < n for all n > 1, that is, each iteration strictly reduces the value of n. (b) Determine a tight bound on the running time of the above function. You should supply an argument to corroborate that your bound is tight (that is, achievable). (5) Solution The worst case occurs when the reduction of n (by division) occurs only once in the inner while loop. But for all n > 3, we have (n + 1)/2 < (2/3)n. The first iteration may change n to n + 1 (if the initial value of n is even). After that, at most log 3/2 (n + 1) iterations of the outer loop reduce n to a value 3. For n = 3, the outer while loop is executed only once (twice for n = 2, but this case does not arise unless we start with n = 2). Since the order notation hides constant multiplicative factors, we can change the base of logarithms from 3/2 to any other suitable constant value. Moreover, since each increment and division can be performed in unit time, the running time of playwith(n) is O(log n). This bound is tight, since Ω(log n) iterations are needed, for instance, for n = 2 t + 1. Page 1 of 6

2. (a) Let S and T be strings each of length n. Your task is to determine whether T can be obtained by cyclically rotating S. For example, the string star can be obtained by cyclically rotating the string tars, whereas the string arts cannot be obtained by cyclically rotating the string tars. Supply an O(n)-time algorithm to solve this problem. (5) Solution Let S = a 0 a 1...a n 1. Search for T in the string a 0 a 1... a n 1 a 0 a 1...a n 2 of length 2n 1. Use the KMP string matching algorithm. (b) Let S and T be strings of lengths m and n respectively. Your task is to determine whether T is a sub-sequence of S, that is, whether the symbols of T occur in S in the same order as they appear in T, but not necessarily contiguously. For example, the string grim is a sub-sequence of the string algorithm, whereas the string gram is not. Supply an O(m + n)-time algorithm to solve this problem. (5) Solution Initialize i = j = 0. while (i < m) { If (S[i] equals T[j]) { increment j by 1. If (j equals n), return success. Increment i by 1. Return failure. Page 2 of 6

3. Consider the line-sweep algorithm for computing line-segment intersections. Suppose that some of the input segments are allowed to be vertical (that is, parallel to the y-axis). You may, however, assume that no two given vertical segments are collinear. (a) Define a new type of event Vertical Segment to deal with this situation. Describe how you can efficiently handle this event. You are required to maintain the running time of the original algorithm. (5) Solution The event queue Q contains a single event for each vertical segment L i. When this event occurs, we delete the event from Q. Suppose that the sweep line information S is maintained as a height-balanced binary search tree with top-to-bottom ordering of active segments. We locate the insertion position of the upper end point of L i in S. From this position, we keep on looking at successors in S, until the successor lies below the bottom end point of L i. Let all these successors be L j1, L j2,..., L jr. We report the intersection of L i with each of L j1, L j2,..., L jr. (b) Prove that you achieve the original running time of O((n + h) log n) in presence of Vertical Segment events. You may assume that a height-balanced binary search tree on k nodes can be so implemented that the predecessor or successor of a node in the tree can be located in O(log k) time. (5) Solution Suppose that there are h 1 intersection points among non-vertical segments, and there are h 2 intersection points involving vertical segments. The effort spent to identify the former h 1 intersection points remains O((n + h 1 )log n) as in the original algorithm. For a vertical segment L i, the determination of r intersection points (with L j1, L j2,..., L jr ) takes O(r log n) time, since the size of S is always n. So the total effort associated with handling all Vertical Segment events is O(h 2 log n). Consequently, the total running time is O((n + h 1 + h 2 )log n), that is, O((n + h)log n). Page 3 of 6

4. You are given a set of n real numbers x 1,x 2,...,x n. Your task is to cover these points by intervals of unit length (that is, by intervals of the form [a,a + 1] = {x R a x a + 1 for real numbers a). Your goal is to minimize the number of intervals in the cover. (Here is a practical application of this problem. Suppose that the points x i represent houses on a straight road. You want to cover all the houses by a set of cellular-phone towers each with a maximum range of 1/2 km in each direction. Naturally, you attempt to serve all the houses with as few towers as possible.) (a) Consider the following greedy strategy. Choose an interval of unit length to cover the maximum number of points in the given collection. Output this interval, and remove the points covered by this interval from the collection. (Serve the maximum possible number of houses by a single tower.) Repeat until no points are left. Give an example to demonstrate that this greedy algorithm may fail to provide an optimal solution. (5) Solution Consider the points 1, 0.5, 0.1, 0.1, 0.5, 1. It is easy to see that no unit interval covers five or more of these points. The greedy strategy first chooses the interval [ 0.5, 0.5] to cover the most congested part (that is, the four central points). This is indeed the only unit interval which contains four of the six given points. But then two other intervals are needed, one to cover 1, and the other to cover 1. That leads to a collection of three intervals. On the contrary, only two intervals [ 1, 0] and [0, 1] suffice to cover all of the given six points. (b) Although the greedy strategy of Part (a) fails, there exist other greedy strategies that efficiently compute optimal solutions. Describe such a strategy. The running time of your greedy algorithm must be bounded by a polynomial in n. (5) Solution First, sort the input points in increasing order. This takes O(n log n) time. Suppose that the sorted sequence is x 1, x 2,...,x n itself. Let x i be the leftmost point yet to be covered (initially, i = 1). Output the interval [x i, x i + 1], and remove all the points that lie in the interval. Repeat until no points are left. The algorithm can be implemented to run in O(n) time (after the initial sorting phase, of course). Page 4 of 6

(c) Prove that your greedy algorithm correctly computes an optimal solution. (5) Solution Suppose that an optimal solution is provided to us. Assume that x 1 x 2 x n. There exists (at least) one interval I 1 in the optimal solution, that covers x 1. Take I 1 = [x 1, x 1 + 1]. Clearly, I 1 covers no fewer points than I 1. Suppose that x i is the leftmost point not covered by I 1. In the optimal solution, (at least) one interval I 2 covers x i. We must have I 2 I 1, since otherwise I 1 would have covered x i. Take I 2 = [x i, x i + 1]. Clearly, I 1 I 2 covers no fewer points than I 1 I 2. Proceeding in this fashion, we can convert the optimal solution to a greedy solution without ever increasing the number of intervals. Since the original collection of intervals was optimal, the converted greedy collection must be optimal too (and must contain the same number of intervals as the original optimal collection.) Page 5 of 6

ROUGH WORK Page 6 of 6