#99/111: The Algorithm Design Manual

An other one of the technical books. After reading the introduction to comp sci, I wanted to deepen my knowledge a bit and I had stand this book in my shelve. I started working through it and highly enjoyed it. The Algorithm Design Manual got 9 chapters with about 30 exercises per chapter. Furthermore, its got a reference with different problems and applications for algorithms. There are solutions online which is great. I highly recommend this book if you want a great introduction into algorithms.

#93/111: Concrete Abstractions

Following the other basic books I wanted to strengthen my basic knowledge about computer science and this book is excellent for this task. It uses Scheme as the introductory language (like SICP) and does a great job in explaining basic data structures and algorithms. If you want to seriously learn about basic computer science, this is a nice book which is also a bit easy than SICP. You can actually read Concrete Abstractions for free which is quite nice.

Concrete Abstractions: Chapter 10

Exercise 10.2: Even when a category is directly testable by Scheme, using EBNF to express it at a more primitive level can help you appreciate the expressive power of EBNF. In this exercise you will use EBNF to describe certain kinds of numbers—a small subset of those allowed by Scheme.
a. Write a production for <unsigned-integer>. You can use the productions for <digit> given above.
b. Next write productions for <integer>; an <integer> may start with a – sign, a + sign, or neither.
c. Finally, write productions for <real-number>, which are (possibly) signed numbers that may have a decimal point. Note that if the real number has a decimal point, there must be at least one digit to the left or to the right (or both) of the decimal point. Thus, -43., .43, 43, 43.21, and 43.0 are all valid real numbers.

Solution: a. <unsigned integer> -> <digit>+
b. <integer> -> -<unsigned integer> | +<unsigned integer> | <unsigned integer>
c. <real-number> -> <integer>. | <integer>.<unsigned integer>* | .<unsigned integer>+ | <integer>

Exercise 10.3: In Section 8.3 we considered expression trees for simple arithmetic expressions. All such expressions are either numbers or lists having an operator (one of +, -, *, or /) and two operands. Actually, there are three important variants, depending on where the operator occurs: in the first position (prefix or Scheme notation), the second position (infix or standard notation), or the third position (postfix, also known as Reverse Polish notation, or RPN). Let’s consider how such expressions can be specified using EBNF.
a. Write productions for <arithmetic-prefix-expression>.

b. Write productions for <arithmetic-infix-expression>.

c. Write productions for <arithmetic-postfix-expression>.

d. As noted in Section 8.3, a postorder traversal of an expression tree re-
sults in a list of the nodes that is identical to the language specified by <arithmetic-postfix-expression>, except that subexpressions are not parenthesized. Revise the productions for <arithmetic-postfix-expression> so that subexpressions are not parenthesized. (The overall top-level expression needn’t be parenthesized either.)

Solution:  <arithmetic-expression> -> + | – | * | /
a. <arithmetic-prefix-expression> -> (<arithmetic-expression> <real-number> <real-number>)
b. <aritmethic-infix-expression> -> (<real-number> <arithmetic-expression> <real-number>)
c. <arithmetic-postfix-expression> -> (<real-number> <real-number> <arithmetic-expression>)

Exercise 10.4: Let’s consider two possible additions to our Micro-Scheme grammar involving regular Scheme expressions.
a. Write a production for let expressions. Remember that let expressions allow zero or more bindings (i.e., parenthesized name/expression pairs), and the body of the let contains one or more expressions. You should define a separate syntactic category for <binding>.
b. Write productions for cond expressions. Remember that cond expressions allow one or more branches, the last of which may be an else, and each branch has one or more expressions following the test condition.

Solution: a. <binding> –> (let ((<name> <expression>)) <expression>+>
b.
<condition> –> (cond ((<expression>+ <expression>))+ (else <expression>+ )
)

Exercise 10.6:

Solution: a. (if 3 1 5) is a <conditional>. Let’s check if 3, 1, and 5 are <expression>s. 3, 1 and 5  are <number>s which are <literal>s which are <constant>s which are <expression>s. So that’s valid.
b. (lambda x (+ x 2)) that isn’t valid because a <abstraction> needs parenthisis after lambda.
c. (((a ((b))) c)) that looks like an <application> let’s check if ((a ((b))) c) is an <expression>. Because an <application> is an <expression> we have to check if <em>(a ((b)))</em> and <em>c</em> is an <expression>. c is an <name> and therefore an <expression>. <em>(a ((b)))</em> is an <application> which have to consists of one or more expressions. So there’s <em>a ((b))</em> left to check. a is an <expression>. ((b)) is an <application> of an <application> of a <name> which is an <expression>. So that’s valid.
d. (lambda (lambda) 3). We have to check if lambda ist a <name> which is false. Not valid.
e. (lambda () lambda). We have to check if lambda is an <expression> which it isn’t.
f. (lambda (x) (if (> x 0) x (- x) 0)). Let’s check if <em>if</em> is an <conditional>. It isn’t because it has more than three <expression>s in its body.
g. (lambda () x). This is valid if x is an <expression>. A <name> is an <expression>, so this is valid.
h. (lambda () ). Not valid, because there’s no <expression> in the body.
i. (/). Not valid, because it needs at least two <arithmetic-expression>s in the body.
j. (#t #f). This is an <application> of two expressions. Are #f and #t <expression>s?  They aren’t definied, so this is not valid.

Exercise 10.31: Use EBNF to write a grammar for the language of all strings of one or more digits that simultaneously meet both of the following requirements:
a. The digits alternate between even and odd, starting with either.

b. The string of digits is the same backward as forward (i.e., is palindromic).

Solution:

<even> -> 0 | 2 | 4 | 6 | 8
<odd>  -> 1 | 3 | 5 | 7 | 9
<palindrome-even> -> 0<palindrome-odd>0 | 2<palindrome-odd>2 | 4<palindrome-odd>4 | 6<palindrome-odd>6 |
                     8<palindrome-odd>8 | <even>
<palindrome-odd> ->  1<palindrome-even>1 | 3<palindrome-even>3 | 5<palindrome-even>5 | 7<palindrome-even>7 | 
                     9<palindrome-even>9 | <odd>
<legal> -> <palindrome-even> | <palindrome-odd>

Concrete Abstractions: Chapter 8

Exercise 8.1: Write a procedure called minimum that will find the smallest element in a nonempty binary search tree of numbers.

Solution:

(define minimum
  (lambda (tree)
    (if (empty-tree? (left-subtree tree))
        (root tree)
        (minimum (left-subtree tree)))))

Exercise 8.2: Write a procedure called number-of-nodes that will count the number of elements in a binary search tree.

Solution:

(define number-of-nodes
  (lambda (tree)
    (if (empty-tree? tree)
        0
        (+ 1 
           (number-of-nodes (left-subtree tree))
           (number-of-nodes (right-subtree tree))))))

Exercise 8.6: Suppose we want to create a new binary search tree by adding another element to an already existing binary search tree. Where is the easiest place to add such an element? Write a procedure called insert that takes a number and a binary search tree of numbers and returns a new binary search tree whose elements consist of the given number together with all of the elements of the binary search tree. You may assume that the given number isn’t already in the tree.

Solution:

(define insert
  (lambda (tree x)
    (if (empty-tree? tree)
        (make-nonempty-tree x '() '())
    (cond ((< x (root tree))
           (make-nonempty-tree 
            (root tree) (insert (left-subtree tree) x) (right-subtree tree)))
          ((> x (root tree))
           (make-nonempty-tree
            (root tree ) (left-subtree tree) (insert (right-subtree tree) x)))))))

Exercise 8.7: Using the procedure insert, write a procedure called list->bstree that takes a list of numbers and returns a binary tree whose elements are those numbers. Try this on several different lists and draw the corresponding tree diagrams. What kind of list gives you a short bushy tree? What kind of list gives a tall skinny tree?

Solution:

(define list->bstree
  (lambda (lst)
    (if (null? lst)
        (make-empty-tree)
        (insert (list->bstree (cdr lst)) (car lst)))))

Exercise 8.11: In many applications, binary trees aren’t sufficient because we need more than two subtrees. An m-ary tree is a tree that is either empty or has a root and m subtrees, each of which is an m-ary tree. Generalize the previous results to m-ary trees.

Solution:
Ok. So let’s see if we can find a formular for 3-ary trees first.

So we can see that a tree with an height of two has 3^2 + 3^1 + 3^0 nodes. Or more general: A 3-ary tree with an height of n has sum_{i=0}^{n} 3^i nodes. This can be simplified as frac{1 - 3^{n+1}}{1-3}. So is this true? Let’s see. The height should be n. What’s happening if we increase the height by one? Each leave will get three children. We assumed that there are 3^n leaves. Therefore there will be 3^n * 3 = 3^{n+1} children. That is, there will be at most text{nodes}(n+1) = text{nodes}(n) + 3^{n+1} nodes which is equivalent to sum_{i=0}^{n} 3^i + 3^{n+1} = sum_{i=0}^{n+1} 3^i.
Therefore there are sum_{i=0}^{n} m^i nodes in a m-ary of height n.

Exercise 8.12: In Exercise 8.7, you wrote a procedure list->bstree that created a binary search tree from a list by successively inserting the elements into the tree. This procedure can lead to trees that are far from minimum height—surprisingly, the worst case occurs if the list is in sorted order. However, if you know the list is already in sorted order, you can do much better: Write a procedure sorted-list->min-height-bstree that creates a minimum height binary search tree from a sorted list of numbers. Hint: If the list has more than one element, split it into three parts: the middle element, the elements before the middle element, and the elements after. Construct the whole tree by making the appropriate recursive calls on these sublists and combining the results.

Solution:

(define sorted-list->min-height-bstree
  (lambda (lst)
    (if (null? lst)
        (make-empty-tree)
        (if (not (list? lst))
            lst
            (make-nonempty-tree (middle-item lst) 
                                (sorted-list->min-height-bstree (left-items lst)) 
                                (sorted-list->min-height-bstree (right-items lst)))))))


(define list-element-range
  (lambda (lst start end i)
    (if (null? lst)
        '()
        (cond ((> i end)
               '())
              ((= i end)
               (cons (car lst) (list-element-range '() start end (+ i 1))))
              ((or (= i start) (> i start))
               (cons (car lst) (list-element-range (cdr lst) start end (+ i 1))))
              (else
               (list-element-range (cdr lst) start end (+ i 1)))))))

(define middle-item
  (lambda (lst)
    (let ((len (length lst)))
      (if (even? len)
        (list-element-range lst (/ len 2) (/ len 2) 0)
        (list-element-range lst (- (/ (+ len 1) 2) 1) (- (/ (+ len 1) 2) 1) 0)))))

(define left-items
  (lambda (lst)
    (let ((len (length lst)))
          (if (even? len)
              (list-element-range lst 0 (- (/ len 2) 1) 0)
              (list-element-range lst 0 (- (/ (+ len 1) 2) 2) 0)))))

(define right-items
  (lambda (lst)
    (let ((len (length lst)))
          (if (even? len)
              (list-element-range lst (+ 1 (/ len 2)) (+ len 1) 0)
              (list-element-range lst (/ (+ len 1) 2) len 0)))))

Exercise 8.31: Write a procedure that takes as arguments a binary search tree of numbers, a lower bound, and an upper bound and counts how many elements of the tree are greater than or equal to the lower bound and less than or equal to the upper bound. Assume that the tree may contain duplicate elements. Make sure your procedure doesn’t examine more of the tree than is necessary.

Solution:

(define count-elements-in-boundary
   (lambda (tree lower upper)
     (if (empty-tree? tree)
         0
         (+ 
          (if (or (< (root tree) lower) (> (root tree) upper))
             0
             1) 
          (count-elements-in-boundary (left-subtree tree) lower upper)
          (count-elements-in-boundary (right-subtree tree) lower upper)))))