# sol1 - Introduction to Algorithms Solution Set 1 CS 482...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms Solution Set 1 CS 482, Spring 2008 (1) This problem can be solved using a greedy algorithm which always attempts to place the next cell phone tower as far east as possible without leaving a house uncovered. The algorithm is analyzed using the “greedy stays ahead” technique. The algorithm begins by sorting the houses in order from west to east. The algorithm keeps track of a variable, t , which denotes the distance from the west endpoint to the most recently placed cell phone tower, which is also the easternmost tower at that stage of the algorithm’s execution. It goes through the houses in order from west to east, placing a cell phone tower 4 miles east of the current house (and updating t accordingly) unless the current house is already covered by the tower at position t . Here’s how the algorithm looks in pseudocode. Sort the houses the from west to east. d i := distance from the road’s west endpoint to the i-th house in this ordering. L := length of the road /* First tower */ t := d 1 + 4 Place a tower at position t . /* Remaining towers */ for i = 1 , 2 ,...,n if d i > t + 4 t := min { d i + 4 ,L } Place a tower at position t . endif endfor Analysis of running time. Sorting the houses requires O ( n log n ) time. The main loop runs for n iterations, and each iteration takes only constant time. So the algorithm’s running time is O ( n log n ). Correctness. Suppose that the algorithm given above places towers at positions t 1 < t 2 < ··· < t k . Every house is covered by one of these towers, because for every house i , either there is a tower covering it at the start of the i-th loop iteration, or the algorithm places a tower at location d i + 4 (thereby covering the i-th house) during the i-th loop iteration. To prove that the algorithm uses the minimum number of towers to cover all the houses, consider any other set of locations s 1 < s 2 < ··· < s j such that every house is within 4 miles of one of these locations. The notion that “greedy stays ahead” is expressed by the following proposition. Proposition 1. The inequality s i ≤ t i holds for 1 ≤ i ≤ min { j,k } . Proof. The proof is by induction on i . The fact that towers at { s 1 ,s 2 ,...,s j } cover every house (including the house at d 1 ) implies that s 1 ≤ d 1 +4 = t 1 , establishing the base case i = 1. For the induction step, assume that s i ≤ t i . Let ‘ be the number of the westernmost house not covered by towers t 1 ,t 2 ,...,t i . Then we have d ‘ > t i +4 ≥ s i +4 which implies that s 1 ,s 2 ,...,s i also fail to cover d ‘ . Hence among { s 1 ,s 2 ,...,s j } there is at least one number s such that s i < s ≤ d ‘ +4; in particular, this means that s i +1 ≤ d ‘ + 4 = t i +1 . Now, assuming that j < k , we can derive a contradiction as follows. We know that t j + 4 < d n , because otherwise the greedy algorithm would not have placed any more towers after t j . Using the proposition, this implies that s j +4...
View Full Document

## This note was uploaded on 10/02/2008 for the course CS 482 taught by Professor Kleinberg during the Spring '08 term at Cornell.

### Page1 / 6

sol1 - Introduction to Algorithms Solution Set 1 CS 482...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online