SICP Section 2.3.3   Leave a comment

The focus of this section is on representing mathematical sets with Scheme’s built-in data structures, and methods of combining these data structures to enable us to write faster algorithms for performing set operations. We go from a basic list based representation, to binary trees. I found this section to be quite a test of my abstract thinking skills; visualizing the operations on the representations was quite difficult for me.

;;Exercise 2.59
Implementing union-set is fairly straightforward for the unordered-list representation.

(define (union-set set1 set2)
  (cond ((null? set1) set2)
        ((null? set2) set1)
        ((element-of-set? (car set1) set2)
         (union-set (cdr set1) set2))
        (else (cons (car set1) (union-set (cdr set1) set2)))))

;;Exercise 2.60
This exercise was pretty interesting, it makes you think about situations in which what would seem to be an inefficient algorithm is actually the best for a job. Here’s my implementation of the set operations for an unordered set which allows duplicates.

(define (element-of-set? x set)
  (cond ((null? set) false)
        ((equal? x (car set)) true)
        (else (element-of-set? x (cdr set)))))

(define (adjoin-set x set)
  (cons x set))

(define (intersection-set set1 set2)
  (cond ((or (null? set1) (null? set2)) '())
        ((element-of-set? (car set1) set2)
         (cons (car set1)
               (intersection-set (cdr set1) set2)))
        (else (intersection-set (cdr set1) set2))))

(define (union-set set1 set2)
  (append set2 set1))

From the code we can see that, when duplicates are allowed,

  • element-of-set? is still O(n)
  • adjoin-set is O(1) from O(n)
  • intersection-set is still O(n^2)
  • union-set is O(n) from O(n^2)

By allowing duplicates, we have sped things up quite a bit, even if the algorithms are going to use up a lot more memory. In choosing which algorithm to use in a certain situation, we need to take into account the environment it will be used in. If we are, for example, on a machine with limited CPU speed, but a lot of memory (not uncommon in machines that have been upgraded), the duplicate representation would be best. On the other hand, if we were dealing with huge numbers, it would be a big drain on memory to use the duplicate representation, and the non-duplicate representation would be better for keeping overhead down.

;;Exercise 2.61
Now we’re starting to optimize the representations a bit more, it’s really neat to see how simple ideas like ordering the sets can produce fairly large performance gains :)

(define (adjoin-set x S)
  (cond ((null? S) (list x))
        ((< x (car S)) (cons x S))
        ((> x (car S)) (cons (car S) (adjoin-set x (cdr S))))
        ((= x (car S)) S)))

The ordered-list representation will take less time to adjoin, on the average, because we will not necessarily have to process the whole list in order to know if a duplicate exists.

;;Exercise 2.62
This particular exercise required a fair bit of thought. Here’s my explanation.
If the first element of set1 is less than the first element of set2, we make that element the car of a new list, and the cdr of the new list is the union of the cdr of set1 and set2…

(define (union-set set1 set2)
  (cond ((null? set1) set2)
        ((null? set2) set1)
        ((< (car set1) (car set2))
         (cons (car set1) (union-set (cdr set1) set2)))
        ((> (car set1) (car set2))
         (cons (car set2) (union-set set1 (cdr set2))))
        ((= (car set1) (car set2))
         (cons (car set1)
               (union-set (cdr set1)
                          (cdr set2))))))

;;Exercise 2.63
I think this exercise is designed to see how well we can transform a diagram into the actual representation of a tree as the most difficult part was writing down the trees :D

(define tree1 (make-tree 7
                         (make-tree 3
                                    (make-tree 1 '() '())
                                    (make-tree 5 '() '()))
                         (make-tree 9
                                    (make-tree 11 '() '()))))

(define tree2 (make-tree 3
                         (make-tree 1 '() '())
                         (make-tree 7
                                    (make-tree 5 '() '())
                                    (make-tree 9
                                               (make-tree 11 '() '())))))

(define tree3 (make-tree 5
                         (make-tree 3
                                    (make-tree 1 '() '()))
                         (make-tree 9
                                    (make-tree 7 '() '())
                                    (make-tree 11 '() '()))))

After running these through both functions, you’ll see that both the recursive and iterative procedures produce the same result, which is expected if they are to do the same job…

Yes, they both have the same order of growth, however tree->list-2 uses less memory than tree->list-3, and thus could be faster in certain situations.

;;Exercise 2.64
Partial-tree works in a very recursive fashion :)
The best way I can explain is to work through an example.
Let’s say we have the list '(1 2 3 4 5) to turn into a tree.
Partial-tree first assigns equal, or almost equal sizes for the left and right branch of the new tree. So for our tree left-size will be 2, and right-size will be 3 (because the size of the list is not even). Then, partial tree conses together the result of partial-tree on the left and right halves of the given list. It’s quite a bit for me to wrap my head around, to be sure!

The list looks like so:

;;Exercise 2.65
Still a work in progress for me…

;;Exercise 2.66
The lookup procedure is basically element-of-set?, except instead of returning true or false, we return the item requested by key, or false if it’s not found.

(define (lookup key records)
  (cond ((null? records) false)
        ((= key (entry records)) (record-value (entry records)))
        ((< key (entry records))
         (lookup key (left-branch records)))
        ((> key (entry records))
         (lookup key (right-branch records)))))

;;Where a record structure looks like
(define (make-record key value)
  (cons key value))

Posted 23/09/2010 by Emmanuel Jacyna in Scheme, SICP

Tagged with , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: