introduction to algorithm design.chapter 1 skiena

24
Introduction to Algorithm Design Finding Counterexamples 1-1. Show that a + b can be less than min(a,b). (Solution 1.1) If both a and b are negative, a + b < min(a,b). For example a = − 5 b = − 7 a + b = − 5 + ( − 7) = − 12 min( − 5, − 7) = − 7 1-2. Show that can be less than min(a,b). (Solution 1.2) a = − 2 b = 3

Upload: arpita-sengupta

Post on 23-Oct-2015

1.097 views

Category:

Documents


5 download

DESCRIPTION

Algorithm - Skiena , Solution Manual

TRANSCRIPT

Page 1: Introduction to Algorithm Design.chapter 1 Skiena

Introduction to Algorithm Design Finding Counterexamples

1-1. Show that a + b can be less than min(a,b).

(Solution 1.1)

If both a and b are negative, a + b < min(a,b). For example

a = − 5 b = − 7 a + b = − 5 + ( − 7) = − 12 min( − 5, − 7) = − 7

1-2. Show that can be less than min(a,b).

(Solution 1.2)

a = − 2b = 3

min(a,b) = − 2a * b = ( − 2) * 3 = − 6

Page 2: Introduction to Algorithm Design.chapter 1 Skiena

1-3. Design/draw a road network with two points a and b such that the fastest route between a and b is not the shortest route.

(Solution 1.3)

a ----------- c ----------- b \ / \--------- d ---------- /

If the distance from a to b going through d is less than the distance from a to b going through c but there is a busy traffic intersection at d with a stop sign that is always backed up, then the route from a to b through c is faster, but the route through d is shorter.

For example,

dist(a,c) = 10 miles

dist(c,b) = 5 miles

So the distance from a to b through c is 15 miles. Assuming you drive 30 miles per hour, the time to travel this would be 30 minutes

dist(a,d) = 5 miles

dist(c,b) = 5 miles

So the distance from a to b through d is 10 miles. Assuming you drive 30 miles per hour, the time to travel this would be 20 minutes, but due to the busy intersection at d, you are delayed 15 minutes, the total time would be 35 minutes.

1-4. Design/draw a road network with two points a and b such that the shortest route between a and b is not the route with the fewest turns.

Page 3: Introduction to Algorithm Design.chapter 1 Skiena

1-5. The knapsack problem is as follows: given a set of integers , and a target number T, find a subset of S which adds up exactly to T. For example, there exists a subset within S = {1,2,5,9,10} that adds up to T = 22 but not T = 23. Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an S and T such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists.

1. Put the elements of S in the knapsack in left to right order if they fit, i.e. the first-fit algorithm.

2. Put the elements of S in the knapsack from smallest to largest, i.e. the best-fit algorithm. 3. Put the elements of S in the knapsack from largest to smallest.

(Solution 1.5)

First-fit algorithm counterexample:

S = {1,2,3} T = 5

Best-fit algorithm counterexample:

S = {1,2,3} T = 5

Largest to smallest algorithm counterexample:

S = {4,3,2} T = 5

Another counterexample:

If t = 11, the solution would be the subset {2,9} or {1,10} which none of the algorithms found.

Using first-fit algorithm (left to right)

Item added Subset of S Sum1 {1} 1 < 112 {1,2} 3 < 115 {1,2,5} 8 < 119 {1,2,5,9} 17 > 11

Page 4: Introduction to Algorithm Design.chapter 1 Skiena

Using best-fit algorithm (smallest to largest)

Item added Subset of S Sum1 {1} 1 < 112 {1,2} 3 < 115 {1,2,5} 8 < 119 {1,2,5,9} 17 > 11

Using largest to smallest

Item added Subset of S Sum10 {10} 10 < 119 {9,10} 19 > 11

1-6. The set cover problem is as follows: given a set of subsets S1,...,Sm of the universal set U =

{1,...,n}, find the smallest subset of subsets such that . For example, there are the following subsets, S1 = {1,3,5}, S2 = {2,4}, S3 = {1,4}, and S4 = {2,5} The set cover would then be S1 and S2. Find a counterexample for the following algorithm: Select the largest subset for the cover, and then delete all its elements from the universal set. Repeat by adding the subset containing the largest number of uncovered elements until all are covered.

(Solution 1.6)

One counter-example consists of a series of subsets that increase in size exponentially, plus 2 additional subsets that each cover half of the elements. Example:

S1 = {1,2} S2 = {3,4,5,6} S3 = {7,8,9,10,11,12,13,14,15,16} S4 = {1,2,3,4,5,6,7,8} S5 = {9,10,11,12,13,14,15,16}

The greedy algorithm will choose S3,S2,S1, while the optimal solution is simply S4,S5

Page 5: Introduction to Algorithm Design.chapter 1 Skiena

Proofs of Correctness

1-7. Prove the correctness of the following recursive algorithm to multiply two natural numbers,

for all integer constants .

function multiply(y,z) comment Return the product yz. 1. if z = 0 then return(0) else

2. return(multiply( ))

(Solution 1.7)

Base Case

The base case (z = 0) is true since it returns zero

if z = 0 then return(0)

Assumptions

multiply(y,z) gives the correct answer where:

z <= n c >= 2 y >= 0

We will also assume the following. A brief proof will follow:

Page 6: Introduction to Algorithm Design.chapter 1 Skiena

Proof

Where z = n+1, c >= 2, y >= 0

First we break the result of multiply into two parts:

    A =

    B =

    multiply(y,z) = A + B

Now, because c >= 2 we know that .Based on that, we know that the call to multiply in "A" returns the correct result.

    

So now let's transform A into something more useful:

         (Note: We transformed n+1 back into z for simplicity)

Based on our earlier assumption, we can transform this further:

    

    

    

And now we add B back into the mix:

    

Subproof

Show that where c >= 2

Page 7: Introduction to Algorithm Design.chapter 1 Skiena

We can prove this statement using a general example.

First, assume that

Now, due to the definition of floor, we know the following:

    

This is because the remainder can't possibly be taken into account during a "floor" operation.

Based on that, we can restate the equation as:

    (z − a) / c * c + a = (z − a) + a = z

1-8. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) =

function horner(A,x) p = An

for i from n − 1 to 0 p = p * x + Ai

return p

1-9. Prove the correctness of the following sorting algorithm.

function bubblesort (A : list[ ]) var int i,j for i from n to 1 for j from 1 to i − 1 if (A[j] > A[j + 1]) swap the values of A[j] and A[j + 1]

(Solution 1.9)

n = 1

The single-element List is sorted.

Assume the algorithm is true for n<=k

i.e.,

Page 8: Introduction to Algorithm Design.chapter 1 Skiena

for i from k to 1 for j from 1 to i − 1 if (A[j] > A[j + 1]) swap the values of A[j] and A[j + 1]

produces a sorted list[1...k].

Let n = k+1

for i from k+1 to 1 for j from 1 to i − 1 if (A[j] > A[j + 1]) swap the values of A[j] and A[j + 1]

Here, the inner loop (having counter j) just "bubbles out" the maximum element to the i-th position in the list.

So, in the first iteration with i = k+1 , on the completion of the inner loop, A[k+1] contains the maximum element of the list.

At this point, i = k and we are left with the task of sorting a k-element list which is already assumed to be performed correctly by the algorithm.

After the completion of all the iterations we have :

(i) A k-element sorted list (ascending order) :

A[1] < A[2] < A[3] <......... < A[k]

(ii) Also we have A[k+1] as the maximum element of the list. This implies A[k] < A[k+1]

From (i) and (ii) we get

A[1] < A[2] < A[3] <......... < A[k] < A[k+1]

which is a sorted list[1...(k+1)].

QED

Induction

Page 9: Introduction to Algorithm Design.chapter 1 Skiena

1-10. Prove that =n(n + 1) / 2 for , by induction.

1-11. Prove that =n(n + 1)(2n + 1) / 6 for , by induction.

(Solution 1.11)

The basis case is when n = 0

and using n = 0 in the formula you get:

Since these are equal, the basis case is true.

Now, I will show that on the assumption that the summation is true for n, it follows that it is true for n + 1

Which should be equal to the formula when n = n + 1:

Page 10: Introduction to Algorithm Design.chapter 1 Skiena

1-12. Prove that =n2(n + 1)2 / 4 for , by induction.

1-13. Prove that

(Solution 1.13)

Call the statement Sn and the general term an

Step 1: Show that the statement holds for the basis case n = 0

Since an = Sn, the basis case is true.

Step 2: Assume that n = k holds.

Step 3: Show that on the assumption that the summation is true for k, it follows that it is true for k + 1.

Page 11: Introduction to Algorithm Design.chapter 1 Skiena

It's easier to factor than expand. Notice the common factor of (k + 1)(k + 2)(k + 3).

This should be equal to the formula when k = k + 1:

Since both the basis and the inductive step have been proved, it has now been proved by mathematical induction that S_n holds for all natural n

1-14. Prove by induction on that for every , }

1-15. Prove by induction that for ,

(Solution 1.15)

Step 1: Show that the statement holds for the basis case n = 1

Page 12: Introduction to Algorithm Design.chapter 1 Skiena

Since 1 / 2 = 1 / 2, the basis case is true.

Step 2: Assume that that summation is true up to n.

Step 3: Show that on the assumption that the summation is true for n, it follows that it is true for n + 1.

QED

1-16. Prove by induction that n3 + 2n is divisible by 3 for all . }{

(Solution 1.16)

First we must assume that , where .

The case when n = 0 is pretty obvious, so let's go straight to n = n + 1:

We get: .

Page 13: Introduction to Algorithm Design.chapter 1 Skiena

Now let's rearrange the parts like this: , and we can see, that the left-side part equals a, and the right-side is clearly divisible by 3, thus the n3 + 2n is proven to be divisible by 3.

1-17. Prove by induction that a tree with n vertices has exactly n − 1 edges.

(Solution 1.17)

Step 1: Show that the statement holds for the basis case n = 1

E(n) = n − 1E(1) = 1 − 1 = 0. A tree with one node has zero edges

Step 2: Assume that that summation is true up to n.

Step 3: Show that on the assumption that the summation is true for n, it follows that it is true for n + 1.

When adding one node to a tree one edge is added as well

QED

1-18. Prove by mathematical induction that the sum of the cubes of the first n positive integers is

equal to the square of the sum of these integers, i.e.

(Solution 1.18)

Page 14: Introduction to Algorithm Design.chapter 1 Skiena

Step 1: Show that the statement holds for the basis case n = 1

Step 2: Assume that n holds.

Step 3: Show that on the assumption that the summation is true for n, it follows that it is true for n + 1.

This is the case of n assumed to be true in step 2, so n + 1 holds as well.

Page 15: Introduction to Algorithm Design.chapter 1 Skiena

Above the arithmetic series was used:

QED

Estimation

1-19. Do all the books you own total at least one million pages? How many total pages are stored in your school library? Schaffer, chapter 2 problem 2.21

1-20. How many words are there in this textbook?

1-21. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head.

(Solution 1.21)

1 million seconds = 277.777778 hours

1 million seconds = 11.5740741 days

1-22. Estimate how many cities and towns there are in the United States.

1-23. Estimate how many cubic miles of water flow out of the mouth of the Mississippi River each day. Do not look up any supplemental facts. Describe all assumptions you made in arriving at your answer.

(Solution 1.23)

Page 16: Introduction to Algorithm Design.chapter 1 Skiena

I estimate the mouth of the Mississippi at 1 mile wide and 100 feet, or 0.02 miles, deep. If the water were moving at 10 miles an hour, that means that 10 miles x 0.02 miles x 1 miles flow through the mouth every hour.

10 x 0.02 x 1 = 0.2, times 24 hours/day = 4.8 cubic miles of water/day.

One assumption is that the tide currents of the ocean can be neglected, because within each 24-hour-period the tide rises and falls twice thus cancelling out itself.

1-24. Is disk drive access time normally measured in milliseconds (thousandths of a second) or microseconds (millionths of a second)? Does your RAM memory access a word in more or less than a microsecond? How many instructions can your CPU execute in one year if the machine is left running all the time?

1-25. A sorting algorithm takes 1 second to sort 1,000 items on your local machine. How long will it take to sort 10,000 items ...

1. if you believe that the algorithm takes time proportional to n2, and 2. if you believe that the algorithm takes time roughly proportional to nlogn?

(Solution 1.25)

1. If there are 10 times as many items, and it is proportional to n2, it will take 102 times as long or 100 seconds. 2. If it proportional to nlogn, it will take 10log210 as long or about 33.2 seconds.

Implementation Projects

1-26. Implement the two TSP heuristics of tsp-robot. Which of them gives better-quality solutions in practice? Can you devise a heuristic that works better than both of them?

1-27. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of lotto. Write a program to find good ticket sets.

(Solution 1.27)

Interview Problems

Page 17: Introduction to Algorithm Design.chapter 1 Skiena

1-28. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it.

(Solution 1.28)

// Note: This only works for positive values!int divide(int numerator, int denominator) { int quotient = 0;

while(numerator >= denominator) { numerator -= denominator; quotient++; }

return quotient;}

1-29. There are 25 horses. At most, 5 horses can race together at a time. You must determine the fastest, second fastest, and third fastest horses. Find the minimum number of races in which this can be done.

(Solution 1.29)

Answer: Seven races.

First 5 races: Divide 25 horses into 5 groups and that gives you 5 winners.

Sixth race: Now race 5 of them that will give you winner and which two are the bottom most group. Reject bottom two group

Now consider first 3 groups, G1, G2 and G3

G3 Group (the horse that came 3rd in 6th race) - can only bid for 3rd place so we take him for next level i.e. G31

G2 Group (the horse that came 2nd in 6th race) - can bid for 2& 3rd spots we take top two from this group for next level i.e G21, G22

G1 Group (the horse that came 1st in 6th race) - can bid for 1,2,3rd place; but we have already got our first; so we need only to take 2 from this group for next level i.e. G11, G12, G13

Page 18: Introduction to Algorithm Design.chapter 1 Skiena

Seventh Race:: G12, G13, G21, G22, G31 Race them to get 2nd and 3rd winner G11 is already won as 1st winner.

--Max 07:06, 16 June 2010 (EDT)

I get the same answer but want to clarify the notation and solution approach.

Answer: Seven races.

First five races: First 5 exclusive groups of 5 horses.

Sixth race: Between all the first place winners in first five races (leaders). Letter the groups by the order the leaders in the sixth race (e.g. a1 is the fastest of the leaders while e1 is the slowest leader).

Let's list the constraints implied by these results (where >> means faster).

X(n) >> X(n+1), a1 >> b1, b1 >> c1, c1 >> d1, d1 >> e1

These constraints allow us to sketch a tree of continuations for the possible ordering:

a1| \ a2 b1--c1--d1--e1| | | | |a3 b2 c2 d2 e2

Now, we need to devise the next race to give us an answer. Reasoning by constraints and seeking three fastest we can knockout anyone lower than c1 since that would imply that c2 >> b1 >> c1 which is a contradiction.

That leaves us with one more race between a2, a3, b1, b2, c1 to determine the second and third fastest horses.

Argument: In the above solution we are assuming that the winners of each of the groups are better than all the remaining horses in every group. But that might not be true. For example, let us denote the speed (20) of a horse A as A(20). Now let us consider a collection of 9 horses:

Group I A(10) B(20) C(30) winner: C(30)

Group II D(40) E(50) F(60) winner: F(60)

Group III G(70) H(80) I(90) winner: I(90)

Page 19: Introduction to Algorithm Design.chapter 1 Skiena

We clearly see that if we continue with ONLY the winner of the groups, we get an incorrect answer. We need to pick the top 3 of each group to get the correct answer.

1-30. How many piano tuners are there in the entire world?

1-31. How many gas stations are there in the United States?

(Solution 1.31)

ssumptions: aprox 400000 cars each car needs to refuel once a week each gas station is open 10 hours a day and refuels 10 cars an hour There are enough stations to refuel all cars once per week

Calculation: cars that can be fueled by 1 station in 1 week 10*10*7=700 number of gas stations (rounded up): ceil(400000/700)=572

A slightly different approach:

aprox 300 mln cars in US (1 car per each citizen). each station is open 12hr a day, has 6 places for taking fuel, each fueling takes about 6

min

Amount of cars using given gas station daily:

Gas station (at least in Europe) are used always used, so: gas stations.

1-32. How much does the ice in a hockey rink weigh?

1-33. How many miles of road are there in the United States?

Page 20: Introduction to Algorithm Design.chapter 1 Skiena

(Solution 1.33)

I'm envisioning the United States as a rectangle 1000 miles high and 3000 miles long. I'm not including Alaska, because, although it's large, it doesn't have many roads. Much of the U.S. is rural, so roads are fairly sparse. In the semi-rural area of New York State where I grew up, it was common to go a mile or so without hitting a road. So it's reasonable to imagine the U.S. as a grid of roads, each one mile from the other.

That means that if all roads were evenly distributed, the U.S. would be covered with a grid of 1000 roads of 3000 miles each, and 3000 roads of 1000 miles each, for a total of 6,000,000 miles of roads.

Editor's Note: Not bad, according to National Atlas there are about 4,000,000 miles of roads in the U.S.

1-34. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name?