Solutions to Problem Set 1

Similar documents
/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Selection (deterministic & randomized): finding the median in linear time

II (Sorting and) Order Statistics

Solutions to the Second Midterm Exam

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Computational Geometry

More about The Competition

Question 7.11 Show how heapsort processes the input:

Randomized Algorithms: Selection

Problem Set 1. Solution. CS4234: Optimization Algorithms. Solution Sketches

Lecture 3: Art Gallery Problems and Polygon Triangulation

Practice Problems for the Final

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

2 Geometry Solutions

Greedy algorithms is another useful way for solving optimization problems.

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

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

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

16 Greedy Algorithms

Problem Set 5 Solutions

COMP3121/3821/9101/ s1 Assignment 1

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

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

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

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

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

implementing the breadth-first search algorithm implementing the depth-first search algorithm

1 Dijkstra s Algorithm

Introduction to Algorithms I

(Refer Slide Time: 01.26)

Lecture 6 Sequences II. Parallel and Sequential Data Structures and Algorithms, (Fall 2013) Lectured by Danny Sleator 12 September 2013

Principles of Algorithm Design

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

6th Bay Area Mathematical Olympiad

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

15.4 Longest common subsequence

Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Handout 8

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Multidimensional Divide and Conquer 2 Spatial Joins

Randomized Algorithms, Hash Functions

Practice Sheet 2 Solutions

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

Recursion and Structural Induction

Binary Search to find item in sorted array

Computational Geometry

Basic Combinatorics. Math 40210, Section 01 Fall Homework 4 Solutions

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

EDAA40 At home exercises 1

Lecture 15 : Review DRAFT

Algorithms and Applications

EECS 2011M: Fundamentals of Data Structures

Catalan Numbers. Table 1: Balanced Parentheses

HW Graph Theory SOLUTIONS (hbovik) - Q

Some Extra Information on Graph Search

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Math 4410 Fall 2010 Exam 3. Show your work. A correct answer without any scratch work or justification may not receive much credit.

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Solutions to Problem Set 3

Chapter 3. Set Theory. 3.1 What is a Set?

Analysis of Algorithms

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

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

CSE 417 Network Flows (pt 4) Min Cost Flows

Planar convex hulls (I) Computational Geometry [csci 3250] Laura Toma Bowdoin College

7.3 A randomized version of quicksort

Treewidth and graph minors

CME 305: Discrete Mathematics and Algorithms Instructor: Reza Zadeh HW#3 Due at the beginning of class Thursday 02/26/15

Symmetric Product Graphs

Paths, Flowers and Vertex Cover

HMMT February 2018 February 10, 2018

Extremal Graph Theory: Turán s Theorem

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Approximation Algorithms for Geometric Intersection Graphs

6.854J / J Advanced Algorithms Fall 2008

Theorem 2.9: nearest addition algorithm

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Test 1 Review Questions with Solutions

COMP 250 Fall recurrences 2 Oct. 13, 2017

Figure 1: A directed graph.

Solution Outlines. SWERC Judges SWERC SWERC Judges Solution Outlines SWERC / 39

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

26 The closest pair problem

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

Dynamic Programming Homework Problems

Matching and Planarity

Computational Geometry

Pebble Sets in Convex Polygons

Problem Set 1 Solutions

Question 2 (Strongly Connected Components, 15 points). What are the strongly connected components of the graph below?

COMP 352 FALL Tutorial 10

Transcription:

CSCI-GA.3520-001 Honors Analysis of Algorithms Solutions to Problem Set 1 Problem 1 An O(n) algorithm that finds the kth integer in an array a = (a 1,..., a n ) of n distinct integers. Basic Idea Using a Pivot: Find an approximate median element x and use it as a pivot. Then partition the array a into two arrays around the pivot: the array a LT of elements less than x and the array a GR of elements greater than x. Let m be the size of a LT. If k = m + 1 then return the pivot x. If k m then (recursively) find the kth element in a LT, and if k > m + 1 then (recursively) find the (k m 1)st element in a GR. Finding the Pivot: We need to efficiently find an approximate median. To do so, divide the input array a consisting of n elements into n 5 groups of (at most) 5 elements each. We then sort each group, pick out the median of each group and make these medians into a new array b of size l = n 5. We then (recursively) find the median of of b using the algorithm outlined above by setting k = l 2. Correctness: Correctness is guaranteed no matter what the quality of the approximate median! It follows simply from the parameters given in the recursion. Also the algorithm terminates since the array size decreases by at least 1 in each iteration. Run-Time Analysis: Here we analyze the quality of the approximate median used as a pivot. For this pivot, there are at least (1/2) n/5 2 groups which have a larger median and each of these groups contribute at least 3 elements that are greater than the pivot (we discount the last group which might not have five elements). Hence there are at least 3n/10 6 elements which are larger than the pivot x. Similarly, it is easy to show that there are at least 3n/10 6 elements smaller than x. Hence the recursive call gets a problem instance which is of size at most 7n/10 + 6. This means that the running time can be expressed as a recurrence relation: T (n) T (n/5 + 1) + T (7n/10 + 6) + O(n), T (1) = O(1) We can prove by induction that the above is bounded by T (n) cn for some c and all large enough n. Essentially, if the inductive hypothesis holds then T (n) c((9/10)n + 7) + c n cn + ( cn/10 + 7c + c n) by setting n 0 and c correctly (based on the value of c ) we can ensure that for all n n 0 the latter term ( cn/10 + 7c + c n) is not positive. Problem 2 Assuming that only equality checks are allowed, design an O(n) time algorithm to check if there is an element which occurs more than n/2 times in an array containing n elements. PS1-1

Algorithm: Create an empty stack S. Go through the array one element at a time and, for each new element e: (1) if the stack is empty push e on the stack S (2) if the top of the stack e is equal to e then push e on the stack (3) otherwise pop the element e which is at the top of the stack S and ignore e. One this is done, if S is non-empty then let e be the element at the top of S. count the number of occurrences of e in the original array. If there are more than n/2 of them, return true, otherwise return false. Correctness: Firstly, we notice that any given point, all elements in S have the same value. This is because we only push an element onto the stack if it is equivalent to the top of the stack. Secondly, any element that does not end up in the stack must have either been ignored or popped in condition (3) above. We can uniquely pair up all such elements into disjoint pairs (e, e ) (where e is the element that was ignored in (3) and e is the one that got popped) so that e e. Lastly, if some value v occurs more than n/2 times, then it is impossible that all occurrences of v are paired up as above. Hence some element whose value is v must end up in the stack. Since we saw all elements in the stack have the same value, the top of the stack must have value v. This concludes the proof of correctness. (Note that the implication only goes in one direction and we cannot say that if some value ends up in the stack it occurs more than n/2 times. Therefore we need the final check). Run Time: O(n) We do at most two iterations over the array and hence it is clear that the run-time is Problem 3 Suppose a > b > 0 and c > 0 are constants and ( n ) T (n) = at + cn, T (a) c b Show that T (n) = O(n log b a ). I like to use the recursion tree method to do these problems. Think of the above recurrence relation as being for a recursive algorithm which makes a recursive calls, in each of which the problem size is n/b. In addition, it does cn work aside from the recursive calls. Then let us look at tree of problems being solved: Level Problem Size Number of Problems Work Per Problem Total Work for Level 0 n 1 cn cn 1 n/b a c(n/b) ac(n/b) 2 n/b 2 a 2 c(n/b 2 ) a 2 c(n/b 2 ) i n/b i a i c(n/b i ) a i c(n/b i ) Hence the total amount of work is T (n) = cn m i=1 ( a b ) i where, m = logb (n) is the number of levels (since the problem size at level m is n/b m = 1). Now we solve PS1-2

m ( a ) i (a/b) m+1 1 = b a/b 1 i=1 ( ) ( ) a/b a log b (n) = = c n log b (a) 1 a/b 1 n For some constant c. Plugging this in, we get T (n) = (cn)c n log b (a) 1 = c n log b (a) = O(n log b (a) ) as we wanted to show. Problem 4 Given n intervals of real numbers, [a 1, b 1 ], [a 2, b 2 ],..., [a n, b n ] design an O(n log n) time algorithm to decide whether there exists a pair of intervals that overlap. Sort the intervals by their starting point a i resulting in a sorted list [a π(1), b π(1) ],..., [a π(n), b π(n) ] for some permutation π. Then scan the successive sorted intervals to see if b π(i) a π(i+1) for any i and, if so, there is an overlap. Otherwise there is none. The algorithm only involves sorting and one scan so the run time (assuming a good sorting algorithm) is O(n log n). For correctness, it is easy to see that if some pair of intervals overlap then, when sorted, there will be a pair of successive intervals which overlap. Problem 5 Given a m n matrix of integers such that every row is strictly increasing (from left to right), and every column is strictly increasing (from top to bottom), design an O(m + n) time algorithm to test if a given integer b is contained in the matrix. The algorithm starts with the matrix M and the indices i = m, j = 1 (corresponding to the bottom left of the matrix) and in each iteration: (1) if M[i, j] = b return true, (2) if M[i, j] < b then set j := j + 1 (go right) (3) if M[i, j] > b then i := i 1 (go up). If i or j falls off the matrix (i = 0 or j = n + 1) then return false. For correctness, we can think of M[i, j] as a pivot and, in each iteration we have the invariant that b must be to the top and right of the pivot (or the pivot itself). This is true at the beginning and, each time we go up or right, we discard a row or a column which cannot contain b. It is easy to see that the run-time is O(n + m) since, after n + m iterations, one of i, j must fall off the matrix. Problem 6 Given a sequence of positive integers (a 1, a 2,..., a n ), design an O(n) time algorithm to find a shortest sub-sequence of consecutive integers (a i, a i+1,..., a j ) whose sum is at least a given integer M. PS1-3

We use a sliding window to find this subsequence. Start of with two indices i = 1, j = 1. In each iteration, we first do an increase step where we increase j until the sequence a i,..., a j adds up to M. Then we do a narrow step in which we increase i until we get the largest value for which a i,..., a j adds up to M. At the conclusion of these steps we have a candidate window (i, j). If it smaller than the current best (or there is no current best yet) we set (i, j) as the current best. We set j := j + 1 and continue with the next iteration (until j falls off the array). We return the current best solution at the end. It is clear that the run time is O(n) since, in each step, we either increase i or j. For correctness, we need to argue that, if (a i0,..., a j0 ) is the shortest subsequence then, at some point in the algorithm, we get the candidate window (i 0, j 0 ). To see this, we consider the first time that the index i reaches i 0. At the beginning of this iteration, j must have reached exactly j 0 (if it was less then j 0 then i cannot move up to i 0 and j cannot go beyond j 0 since this already adds up to M). Hence we consider the window (i 0, j 0 ) and we will find a sequence that is at least as short. Problem 7 (optional) A rectangle in plane is a set of the form [a, b] [c, d]. Given n rectangles, design an O(n log n) time algorithm to decide whether there exists a pair of rectangles that overlap (i.e. share a point). For each rectangle R, let us define x low (R) to be the least (left most) point along the x axis. Similarly, define x high (R), y low (R) and y high (R). For example, if a rectangle R in a plane is given by [a, b] [c, d], x low (R) = a, x high (R) = b, y low (R) = c and y high (R) = d. The algorithm we use falls under the popular class of linesweep algorithms. Algorithm: 1. Take the x low and x high values of all rectangles in a list and sort them. For each point, maintain the rectangle it is associated with and if this point is high/low for that rectangle. The points are called points of interest. 2. Initialise a self balancing binary search tree which is initially empty. The tree will eventually hold rectangles with the key value being y low. 3. Loop through the points of interest, at each point do the following. (a) If the point is x low (R) for some R, add the rectangle to the BST and check if the rectangle added intersects with either of its neighbors in the BST. If either is true, return true. (b) If the point is x high (R) for some R, remove the rectangle from the BST. 4. If the algorithm hasn t returned true at the end of all points of interest, return false. Notice that if 2 or more rectangles have the same x low, the order in which they are inserted into the BST doesn t matter. (why?) Runtime analysis: The sorting takes O(n log n) time. For each point of interest (there are 2n of them), we do at most 2 comparisons which takes constant time, and at most 1 insert/delete which takes O(logn) time. Therefore, the algorithm is O(n log n). PS1-4

Proof of correctness: The loop invariant is that none of the rectangles present in the BST (active rectangles) intersect. Observe that when we add a rectangle to the BST, if the newly added rectangle doesn t intersect with either of its neighbors, it cannot interesect with any other rectangle that is currently active. Further reading: 1.Klee s measure problem. 2. One might want to attempt the following slightly harder problem. Given n line segments in a 2-dimensional plane, check if any pair of them interesect. (Why is this harder? Can you think of a reduction? Can you think of what steps to add to the given algorithm to solve this?) PS1-5