# Difference between revisions of "Chapter 1"

Jump to navigation
Jump to search

m (Protected "Chapter 1" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))) |
|||

(23 intermediate revisions by the same user not shown) | |||

Line 1: | Line 1: | ||

− | + | Below are the exercises at the end of chapter 1 of the third edition of the Algorithm Design Manual by Steven Skiena. Student proposed answers to odd number questions are available by clicking on the question number. | |

− | + | =Introduction to Algorithms= | |

+ | ===Finding Counter Examples=== | ||

− | :1. | + | :[[1.1]]. Show that <math>a + b</math> can be less than <math>\min(a,b)</math>. |

+ | [[1.1|Solution]] | ||

− | : | + | :1.2. Show that <math>a \times b</math> can be less than <math>\min(a,b)</math>. |

− | :1. | + | :[[1.3]]. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the fastest route between <math>a</math> and <math>b</math> is not the shortest route. |

+ | [[1.3|Solution]] | ||

− | : | + | :1.4. Design/draw a road network with two points <math>a</math> and <math>b</math> such that the shortest route between <math>a</math> and <math>b</math> is not the route with the fewest turns. |

− | |||

− | :: | + | :[[1.5]]. The ''knapsack problem'' is as follows: given a set of integers <math>S = \{s_1, s_2, \ldots, s_n\}</math>, and a target number <math>T</math>, find a subset of <math>S</math> which adds up exactly to <math>T</math>. For example, there exists a subset within <math>S = \{1, 2, 5, 9, 10\}</math> that adds up to <math>T=22</math> but not <math>T=23</math>. |

+ | :Find counterexamples to each of the following algorithms for the knapsack problem. That is, giving an <math>S</math> and <math>T</math> such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists. | ||

+ | #Put the elements of <math>S</math> in the knapsack in left to right order if they fit, i.e. the first-fit algorithm. | ||

+ | #Put the elements of <math>S</math> in the knapsack from smallest to largest, i.e. the best-fit algorithm. | ||

+ | #Put the elements of <math>S</math> in the knapsack from largest to smallest. | ||

+ | [[1.5|Solution]] | ||

− | |||

− | ::(c) | + | :1.6. The ''set cover problem'' is as follows: given a set <math>S</math> of subsets <math> S_1, ..., S_m</math> of the universal set <math>U = \{1,...,n\}</math>, find the smallest subset of subsets <math>T \subset S</math> such that <math>\cup_{t_i \in T} t_i = U</math>.For example, there are the following subsets, <math>S_1 = \{1, 3, 5\}</math>, <math>S_2 = \{2,4\}</math>, <math>S_3 = \{1,4\}</math>, and <math>S_4 = \{2,5\}</math> The set cover would then be <math>S_1</math> and <math>S_2</math>. |

+ | |||

+ | :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. | ||

+ | |||

+ | |||

+ | :[[1.7]]. The ''maximum clique problem'' in a graph <math>G = (V, E)</math> asks for the largest subset <math>C</math> of vertices <math>V</math> such that there is an edge in <math>E</math> between every pair of vertices in <math>C</math>. Find a counterexample for the following algorithm: Sort the vertices of <math>G</math> from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered. | ||

+ | [[1.7|Solution]] | ||

+ | |||

+ | |||

+ | ===Proofs of Correctness=== | ||

+ | |||

+ | :1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants <math> c \geq 2</math>. | ||

+ | multiply(<math>y,z</math>) | ||

+ | #Return the product <math>yz</math>. | ||

+ | ''if'' <math>z=0</math> ''then'' return(0) ''else'' | ||

+ | return(multiply(<math>cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c</math>)) | ||

+ | |||

+ | |||

+ | |||

+ | :[[1.9]]. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = <math>a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0</math> | ||

+ | horner(<math>A,x</math>) | ||

+ | <math>p = A_n</math> | ||

+ | for <math>i</math> from <math>n-1</math> to <math>0</math> | ||

+ | <math>p = p*x+A_i</math> | ||

+ | return <math>p</math> | ||

+ | [[1.9|Solution]] | ||

+ | |||

+ | |||

+ | :1.10. Prove the correctness of the following sorting algorithm. | ||

+ | bubblesort (<math>A</math> : list[<math>1 \dots n</math>]) | ||

+ | for <math>i</math> from <math>n</math> to <math>1</math> | ||

+ | for <math>j</math> from <math>1</math> to <math>i-1</math> | ||

+ | if (<math>A[j] > A[j+1]</math>) | ||

+ | swap the values of <math>A[j]</math> and <math>A[j+1]</math> | ||

+ | |||

+ | |||

+ | :[[1.11]]. The ''greatest common divisor of positive'' integers <math>x</math> and <math>y</math> is the largest integer <math>d</math> such that <math>d</math> divides <math>x</math> and <math>d</math> divides <math>y</math>. Euclid’s algorithm to compute <math>gcd(x, y)</math> where <math>x > y</math> reduces the task to a smaller problem: | ||

+ | |||

+ | :::::<math>gcd(x, y) = gcd(y, x mod y)</math> | ||

+ | |||

+ | :Prove that Euclid’s algorithm is correct. | ||

+ | [[1.11|Solution]] | ||

+ | |||

+ | |||

+ | ===Induction=== | ||

+ | |||

+ | :1.12. Prove that <math>\sum_{i=1}^n i</math>=<math>n(n+1)/2</math> for <math>n \geq 0</math>, by induction. | ||

+ | |||

+ | |||

+ | :[[1.13]]. Prove that <math>\sum_{i=1}^n i^2</math>=<math>n(n+1)(2n+1)/6</math> for <math>n \geq\ 0</math>, by induction. | ||

+ | [[1.13|Solution]] | ||

+ | |||

+ | |||

+ | :1.14. Prove that <math>\sum_{i=1}^n i^3</math>=<math>n^2(n+1)^2/4</math> for <math>n \geq 0</math>, by induction. | ||

+ | |||

+ | |||

+ | :[[1.15]]. Prove that <math> \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 </math> | ||

+ | [[1.15|Solution]] | ||

− | :1. | + | :1.16. Prove by induction on <math>n \geq 1</math> that for every <math>a \neq 1</math>, <math> \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1}</math> |

− | : | + | |

+ | |||

+ | :[[1.17]]. Prove by induction that for <math>n \geq 1</math>, <math> \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} </math> | ||

+ | [[1.17|Solution]] | ||

+ | |||

+ | |||

+ | :1.18. Prove by induction that <math>n^3+2n</math> is divisible by <math>3</math> for all <math>n \geq 0</math>. | ||

+ | |||

+ | |||

+ | :[[1.19]]. Prove by induction that a tree with <math>n</math> vertices has exactly <math>n-1</math> edges. | ||

+ | [[1.19|Solution]] | ||

+ | |||

+ | |||

+ | :1.20. Prove by mathematical induction that the sum of the cubes of the first <math>n</math> positive integers is equal to the square of the sum of these integers, i.e. | ||

+ | |||

+ | ::::::::::::<math> \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 </math> | ||

+ | |||

+ | |||

+ | ===Estimation=== | ||

+ | |||

+ | :[[1.21]]. Do all the books you own total at least one million pages? How many total pages are stored in your school library? | ||

+ | [[1.21|Solution]] | ||

− | : | + | :1.22. How many words are there in this textbook? |

− | :1. | + | :[[1.23]]. How many hours are one million seconds? How many days? Answer these questions by doing all arithmetic in your head. |

− | + | [[1.23|Solution]] | |

− | |||

− | |||

− | : | + | :1.24. Estimate how many cities and towns there are in the United States. |

− | |||

− | |||

− | |||

− | |||

− | |||

− | :1. | + | :[[1.25]]. 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. |

+ | [[1.25|Solution]] | ||

− | |||

− | :1. | + | :1.26. How many Starbucks or McDonald’s locations are there in your country? |

− | |||

− | :1. | + | :[[1.27]]. How long would it take to empty a bathtub with a drinking straw? |

+ | [[1.27|Solution]] | ||

− | |||

− | :1. | + | :1.28. 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. | + | :[[1.29]]. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . . |

+ | ::(a) if you believe that the algorithm takes time proportional to n2, and | ||

+ | ::(b) if you believe that the algorithm takes time roughly proportional to n log n? | ||

+ | [[1.29|Solution]] | ||

− | |||

− | + | ===Implementation Projects=== | |

− | : | + | :1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them? |

− | |||

− | :[[1. | + | :[[1.31]]. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets. |

+ | [[1.31|Solution]] | ||

− | |||

− | + | ===Interview Problems=== | |

− | :1. | + | :1.32. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it. |

− | |||

− | :1. | + | :[[1.33]]. There are twenty-five horses. At most, five 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. |

+ | [[1.33|Solution]] | ||

− | |||

− | :1. | + | :1.34. How many piano tuners are there in the entire world? |

− | |||

− | :1. | + | :[[1.35]]. How many gas stations are there in the United States? |

+ | [[1.35|Solution]] | ||

− | |||

− | :1. | + | :1.36. How much does the ice in a hockey rink weigh? |

− | |||

− | :1. | + | :[[1.37]]. How many miles of road are there in the United States? |

+ | [[1.37|Solution]] | ||

− | |||

− | :1.38 | + | :1.38. On average, how many times would you have to flip open the Manhattan phone book at random in order to find a specific name? |

Back to [[Chapter List]] | Back to [[Chapter List]] |

## Latest revision as of 18:05, 1 October 2020

Below are the exercises at the end of chapter 1 of the third edition of the Algorithm Design Manual by Steven Skiena. Student proposed answers to odd number questions are available by clicking on the question number.

## Contents

# Introduction to Algorithms

### Finding Counter Examples

- 1.1. Show that [math]\displaystyle{ a + b }[/math] can be less than [math]\displaystyle{ \min(a,b) }[/math].

- 1.2. Show that [math]\displaystyle{ a \times b }[/math] can be less than [math]\displaystyle{ \min(a,b) }[/math].

- 1.3. Design/draw a road network with two points [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math] such that the fastest route between [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math] is not the shortest route.

- 1.4. Design/draw a road network with two points [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math] such that the shortest route between [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math] is not the route with the fewest turns.

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

- Put the elements of [math]\displaystyle{ S }[/math] in the knapsack in left to right order if they fit, i.e. the first-fit algorithm.
- Put the elements of [math]\displaystyle{ S }[/math] in the knapsack from smallest to largest, i.e. the best-fit algorithm.
- Put the elements of [math]\displaystyle{ S }[/math] in the knapsack from largest to smallest.

- 1.6. The
*set cover problem*is as follows: given a set [math]\displaystyle{ S }[/math] of subsets [math]\displaystyle{ S_1, ..., S_m }[/math] of the universal set [math]\displaystyle{ U = \{1,...,n\} }[/math], find the smallest subset of subsets [math]\displaystyle{ T \subset S }[/math] such that [math]\displaystyle{ \cup_{t_i \in T} t_i = U }[/math].For example, there are the following subsets, [math]\displaystyle{ S_1 = \{1, 3, 5\} }[/math], [math]\displaystyle{ S_2 = \{2,4\} }[/math], [math]\displaystyle{ S_3 = \{1,4\} }[/math], and [math]\displaystyle{ S_4 = \{2,5\} }[/math] The set cover would then be [math]\displaystyle{ S_1 }[/math] and [math]\displaystyle{ S_2 }[/math].

- 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.

- 1.7. The
*maximum clique problem*in a graph [math]\displaystyle{ G = (V, E) }[/math] asks for the largest subset [math]\displaystyle{ C }[/math] of vertices [math]\displaystyle{ V }[/math] such that there is an edge in [math]\displaystyle{ E }[/math] between every pair of vertices in [math]\displaystyle{ C }[/math]. Find a counterexample for the following algorithm: Sort the vertices of [math]\displaystyle{ G }[/math] from highest to lowest degree. Considering the vertices in order of degree, for each vertex add it to the clique if it is a neighbor of all vertices currently in the clique. Repeat until all vertices have been considered.

### Proofs of Correctness

- 1.8. Prove the correctness of the following recursive algorithm to multiply two natural numbers, for all integer constants [math]\displaystyle{ c \geq 2 }[/math].

multiply([math]\displaystyle{ y,z }[/math]) #Return the product [math]\displaystyle{ yz }[/math].if[math]\displaystyle{ z=0 }[/math]thenreturn(0)elsereturn(multiply([math]\displaystyle{ cy,\lfloor z/c \rfloor)+y \cdot (z\,\bmod\,c }[/math]))

- 1.9. Prove the correctness of the following algorithm for evaluating a polynomial. P(x) = [math]\displaystyle{ a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0 }[/math]

horner([math]\displaystyle{ A,x }[/math]) [math]\displaystyle{ p = A_n }[/math] for [math]\displaystyle{ i }[/math] from [math]\displaystyle{ n-1 }[/math] to [math]\displaystyle{ 0 }[/math] [math]\displaystyle{ p = p*x+A_i }[/math] return [math]\displaystyle{ p }[/math]

- 1.10. Prove the correctness of the following sorting algorithm.

bubblesort ([math]\displaystyle{ A }[/math] : list[[math]\displaystyle{ 1 \dots n }[/math]]) for [math]\displaystyle{ i }[/math] from [math]\displaystyle{ n }[/math] to [math]\displaystyle{ 1 }[/math] for [math]\displaystyle{ j }[/math] from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ i-1 }[/math] if ([math]\displaystyle{ A[j] \gt A[j+1] }[/math]) swap the values of [math]\displaystyle{ A[j] }[/math] and [math]\displaystyle{ A[j+1] }[/math]

- 1.11. The
*greatest common divisor of positive*integers [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] is the largest integer [math]\displaystyle{ d }[/math] such that [math]\displaystyle{ d }[/math] divides [math]\displaystyle{ x }[/math] and [math]\displaystyle{ d }[/math] divides [math]\displaystyle{ y }[/math]. Euclid’s algorithm to compute [math]\displaystyle{ gcd(x, y) }[/math] where [math]\displaystyle{ x \gt y }[/math] reduces the task to a smaller problem:

- [math]\displaystyle{ gcd(x, y) = gcd(y, x mod y) }[/math]

- Prove that Euclid’s algorithm is correct.

### Induction

- 1.12. Prove that [math]\displaystyle{ \sum_{i=1}^n i }[/math]=[math]\displaystyle{ n(n+1)/2 }[/math] for [math]\displaystyle{ n \geq 0 }[/math], by induction.

- 1.13. Prove that [math]\displaystyle{ \sum_{i=1}^n i^2 }[/math]=[math]\displaystyle{ n(n+1)(2n+1)/6 }[/math] for [math]\displaystyle{ n \geq\ 0 }[/math], by induction.

- 1.14. Prove that [math]\displaystyle{ \sum_{i=1}^n i^3 }[/math]=[math]\displaystyle{ n^2(n+1)^2/4 }[/math] for [math]\displaystyle{ n \geq 0 }[/math], by induction.

- 1.15. Prove that [math]\displaystyle{ \sum_{i=1}^n i(i+1)(i+2)=n(n+1)(n+2)(n+3)/4 }[/math]

- 1.16. Prove by induction on [math]\displaystyle{ n \geq 1 }[/math] that for every [math]\displaystyle{ a \neq 1 }[/math], [math]\displaystyle{ \sum_{i=0}^n a^i =\frac{a^{n+1}-1}{a-1} }[/math]

- 1.17. Prove by induction that for [math]\displaystyle{ n \geq 1 }[/math], [math]\displaystyle{ \sum_{i=1}^n \frac{1}{i(i+1)} = \frac{n}{n+1} }[/math]

- 1.18. Prove by induction that [math]\displaystyle{ n^3+2n }[/math] is divisible by [math]\displaystyle{ 3 }[/math] for all [math]\displaystyle{ n \geq 0 }[/math].

- 1.19. Prove by induction that a tree with [math]\displaystyle{ n }[/math] vertices has exactly [math]\displaystyle{ n-1 }[/math] edges.

- 1.20. Prove by mathematical induction that the sum of the cubes of the first [math]\displaystyle{ n }[/math] positive integers is equal to the square of the sum of these integers, i.e.

- [math]\displaystyle{ \sum_{i=1}^n i^3 = ( \sum_{i=1}^n i )^2 }[/math]

### Estimation

- 1.21. Do all the books you own total at least one million pages? How many total pages are stored in your school library?

- 1.22. How many words are there in this textbook?

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

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

- 1.25. 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.

- 1.26. How many Starbucks or McDonald’s locations are there in your country?

- 1.27. How long would it take to empty a bathtub with a drinking straw?

- 1.28. 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.29. A sorting algorithm takes 1 second to sort 1,000 items on your machine. How long will it take to sort 10,000 items. . .
- (a) if you believe that the algorithm takes time proportional to n2, and
- (b) if you believe that the algorithm takes time roughly proportional to n log n?

### Implementation Projects

- 1.30. Implement the two TSP heuristics of Section 1.1 (page 5). Which of them gives better solutions in practice? Can you devise a heuristic that works better than both of them?

- 1.31. Describe how to test whether a given set of tickets establishes sufficient coverage in the Lotto problem of Section 1.8 (page 22). Write a program to find good ticket sets.

### Interview Problems

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

- 1.33. There are twenty-five horses. At most, five 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.

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

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

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

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

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

Back to Chapter List