# The Algorithm Design Manual: Chapter 1

Yeah, new book series! I had laying this book around for about two and an half years and only read about a quarter of it but never worked through it. So, I decided to work through it and post interesting problems and solutions online.
The book consists of two parts. The first part treats the algorithmic basics. The second part is just a reference of different algorithmic problems and ways to solve it. I won’t cover the second part, mainly because there are no exercises.

1-1. Show that a + b can be less than min(a, b).
Solution: 1-1. For any $a, b < 0: a + b < \text{min}(a,b)$. For example:
$a = -5, b = -3. a + b = -8 < \text{min}(-5, -3) = -5.$

1-2. Show that a × b can be less than min(a, b).
Solution For example for $a = -5$ and $b = 3$ the result is $-5 * 3 = -15 < \text{min}(-5, 3) = -5.$

1-5. The knapsack problem is as follows: given a set of integers $S = {s_1, s_2, . . . , s_n}$, 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.
(a) Put the elements of S in the knapsack in left to right order if they fit, i.e. the first-fit algorithm.
(b) Put the elements of S in the knapsack from smallest to largest, i.e. the best-fit algorithm.
(c) Put the elements of S in the knapsack from largest to smallest.
Solution:
(a) $S = {1, 2}, T = 2$
(b) $S = {1, 2}, T = 2$
(c) $S = {2, 3, 4}, T = 5$

1-16. Prove by induction that n3 + 2n is divisible by 3 for all n ≥ 0.
Solution: The base case is $n = 0, 0^3 + 2*0 = 0 \text{ mod } 3 = 0$ which is true.
We can assume that this holds up to n. For n + 1 we get:
$(n+1)^3 + 2(n+1) = n^3 + 3n^2 + 3n + 1 + 2n + 1$
$= n^3 + 3n^2 + 5n + 2 = (n^3 + 2n) + (3 (n^2 + n)).$
The first term is parenthesis is our assumption and the second term is obviously divisible by 3, therefore we showed that our assumption is true.

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

NearestNeighbour createst shorter paths for small input graphs. For midsize input paths and big input graphs closestPair creates smaller paths. However nearestNeighbour is much faster. In conclusion, it depends on the application which heuristic is more suitable.

1-28. Write a function to perform integer division without using either the / or * operators. Find a fast way to do it.
Solution: A simple way to perform integer division is substracting the divisor and counting each substraction. This only works for positive numbers. (more in the comments)

If we can find a bigger divisor we could speed it up. An easy way is to multipy the divisor by itself as long as it divides the denominator with a rest.

This works quite nice and can speed up the process substantially.

1. mJr

Thanks for your answers. Just a quick note about 1-28; your solution works as a ceil function, while the more common function is the floor function, which would need a tiny change to your while loop condition (instead of a > 0, it could be a >= b).

• tst

Thank you! I updated the post. Cheers, Tim

2. Victor

For 1-28, your solution will work only for positive integers. Take a = -15 and b = -2, your algorithm produces 0 which is clearly wrong.

• tst

Thanks for the comment and good job for spotting this error! Do you propose an general algorithm which works on all natural integers? My first intuition is working on absolute values and then fixing the sign afterwards.

3. dp

Thank you for sharing your solutions!

A typos about the exercise 1-2:
-5*-3=-15
Obviously -3 is 3 :)

• tst

You’re welcome and thanks for the correction :)

4. DP

Hi, thank you again for sharing your solutions, they are very useful as feedback :)

I’m writing here about the solution 1-28. I think that your first solution does not work for a=2 and b=2, it returns a / b = 2. It iterates two times. The first one when a==2, the second time when a==0, so at the end count is two.
I think that the line number three should be while (a>0) without the equal to zero.

In addition, when b is equal to zero the algorithm is an infinite loop …

5. UFO

Hi for problem 1-16 ==> n^3 + 3n^2 + 5n + 2 = (n^3 + 2n) + (3(n^2+n)) + 2… so it does not work for n =1. I think you missed the constant 2

6. UFO

Hi for problem 1-16 ==> n^3 + 3n^2 + 5n + 2 = (n^3 + 2n) + (3(n^2+n)) + 2… so it does not work for n =1. I think you missed the constant 2

But in reality (n +1)^3 + 2(n+1) = n^3 + 3n^2 + 5n + 3 = (n^3 + 2n) + (3(n^2+n)) + 3