Recitation 5 - 5

Document Sample
Recitation 5 -  5 Powered By Docstoc
					‫מבוא מורחב למדעי המחשב‬
    ‫בשפת ‪Scheme‬‬
        ‫תרגול 5‬
                     Outline
• Abstraction Barriers
  – Fractals
  – Mobile


• List and pairs manipulations
  – Insertion Sort




                                 2
    cons, car, cdr, list

(cons 1 2) is a pair => (1 . 2)                2

box and pointer diagram:
                                      1



nil = () the empty list (null in Dr. Scheme)

(list 1) = (cons 1 nil) => (1)
                                      1
                                                   3
(car (list 1 2))    1   2

=> 1
(cdr (list 1 2))
=> (2)
(cadr (list 1 2))
=> 2
(cddr (list 1 2))
=> ()
                            4
(list 1 (list (list 2 3) 4) (cons 5 (list 6 7)) 8)




    1                                          8



                       5      6        7




                       4




                                                   5
          2        3
(5 4 (3 2) 1)
(list 5 4 (list 3 2) 1)
(cons 5 (cons 4 (cons (cons 3 (cons 2 nil)) (cons 1 null))))




           5           4                        1




                                3           2

  How to reach the 3 with cars and cdrs?
                                                               6
  (car (car (cdr (cdr x))))
         cdr-ing down a list
          cons-ing up a list
(add-sort 4 (list 1 3 5 7 9))
(1 3 4 5 7 9)
(add-sort 5 ‘())
(5)
(add-sort 6 (list 1 2 3))
(1 2 3 6)
                                       cdr-ing down

 (define (add-sort n s)
    (cond ((null? s) (list n))
            ((< n (car s)) (cons n s))
            (else (cons (car s)
cons-ing up             (add-sort n (cdr s))))))
                                                7
                 Insertion sort
• An empty list is already sorted
• To sort a list with n elements:
    – Drop the first element
    – Sort remaining n-1 elements (recursively)
    – Insert the first element to correct place
•   (7 3 5 9 1)          (1 3 5 7 9)
•   (3 5 9 1)            (1 3 5 9)
•   (5 9 1)              (1 5 9)
•   (9 1)                (1 9)
•   (1)                  (1)         Time
•   ()                   ()          Complexity?   8
          Implementation
(define (insertion-sort s)
   (if (null? s) null
       (add-sort (car s)
                 (insertion-sort (cdr s)))))




                                           9
                               Fractals




Definitions:
• A mathematically generated pattern that is reproducible at any magnification or
reduction.
• A self-similar structure whose geometrical and topographical features are
recapitulated in miniature on finer and finer scales.
• An algorithm, or shape, characterized by self-similarity and produced by recursive
                                                                                 10
sub-division.
Sierpinski triangle




     • Given the three endpoints of a triangle, draw the
     triangle
     • Compute the midpoint of each side
     • Connect these midpoints to each other, dividing the
     given triangle into four triangles
     • Repeat the process for the three outer triangles
                                                      11
         Sierpinski triangle –
           Scheme version
(define (sierpinski triangle)
  (cond
    ((too-small? triangle) #t)
    (else
     (draw-triangle triangle)
     (sierpinski [outer triangle 1]   )
     (sierpinski [outer triangle 2]   )
     (sierpinski [outer triangle 3]   ))))




                                             12
                   Scheme triangle
Constructor:   (define   (make-triangle a b   c) (list a b c))
Selectors:     (define   (a-point triangle)   (car triangle))
               (define   (b-point triangle)   (cadr triangle))
               (define   (c-point triangle)   (caddr triangle))

Predicate:     (define (too-small? triangle)
                 (let ((a (a-point triangle))
                       (b (b-point triangle))
                       (c (c-point triangle)))
                   (or (< (distance a b) 2)
                       (< (distance b c) 2)
                       (< (distance c a) 2))))

 Draw:         (define (draw-triangle triangle)
                 (let ((a (a-point triangle))
                       (b (b-point triangle))
                       (c (c-point triangle)))
                   (and ((draw-line view) a b my-color)
                        ((draw-line view) b c my-color)           13
                        ((draw-line view) c a my-color))))
                             Points
Constructor:   (define (make-posn x y) (list x y))
Selectors:     (define (posn-x posn) (car posn))
               (define (posn-y posn) (cadr posn))

               (define (mid-point a b)
                 (make-posn
                   (mid (posn-x a) (posn-x b))
                   (mid (posn-y a) (posn-y b))))

               (define (mid x y)
                 (/ (+ x y) 2))

               (define (distance a b)
                 (sqrt (+ (square (- (posn-x a) (posn-x b)))
                          (square (- (posn-y a) (posn-y b))))))



                                                                  14
        Sierpinski triangle –
        Scheme final version
(define (sierpinski triangle)
  (cond
    ((too-small? triangle) #t)
    (else
     (let ((a (a-point triangle))
            (b (b-point triangle))
            (c (c-point triangle)))
        (let ((a-b (mid-point a b))
              (b-c (mid-point b c))
              (c-a (mid-point c a)))
          (and
    (draw-triangle triangle)
    (sierpinski (make-triangle a a-b c-a)))
    (sierpinski (make-triangle b a-b b-c)))
                                                    15
    (sierpinski (make-triangle c c-a b-c)))))))))
Abstraction barriers
    Programs that use Triangles
    Triangles in problem domain

      Too-small? draw-triangle

   Triangles as lists of three points

 make-triangle a-point b-point c-point

Points as lists of two coordinates (x,y)
      make-posn posn-x posn-y
            Points as lists
            cons list car cdr              16
Mobile




         17
                   Mobile
• Left and Right branches
• Constructor
  – (make-mobile left right)
• Selectors
  – (left-branch mobile)
  – (right-branch mobile)



                               18
                       Branch
• Length and Structure
  – Length is a number
  – Structure is…
     • Another mobile
     • A leaf (degenerate mobile)
  – Weight is a number
• Constructor
  – (make-branch length structure)
• Selectors
  – (branch-length branch)
  – (branch-structure branch)
                                     19
            Building mobiles
(define m
 (make-mobile
                                    4   8
  (make-branch 4 6)
                                                4   2
  (make-branch
                                6
    8                                       1           2

    (make-mobile
        (make-branch 4 1)
        (make-branch 2 2)))))

                                                    20
             Mobile weight
• A leaf’s weight is its value
• A mobile’s weight is:
  – Sum of all leaves =
  – Sum of weights on both sides
• (total-weight m)                 6
                                       1
  – 9 (6+1+2)                               2




                                           21
           Mobile weight
(define (total-weight mobile)
   (if (atom? mobile) mobile
   (+ (total-weight (branch-structure
                     (left-branch mobile)))
      (total-weight (branch-structure
                     (right-branch mobile)))
     )))

(define (atom? x)
   (and (not (pair? x)) (not (null? x))))
                                            22
       Complexity Analysis
• What does “n” represent?
  – Number of weights?
  – Number of weights, sub-mobiles and branches?
  – Number of pairs?
  – All of the above?
• Analysis
  – (n)
  – Depends on mobile’s size, not structure

                                              23
           Balanced mobiles
• Leaf
  – Always Balanced
• Rod                               4   8
  – Equal moments
                                             5   1
  – F = length x weight
                                6
• Mobile                                 1
                                                 2

  – All rods are balanced =
  – Main rod is balanced, and both sub-mobiles
• (balanced? m)
                                                 24
                 balanced?
(define (balanced? mobile)
  (or (atom? mobile)
      (let ((l (left-branch mobile))
            (r (right-branch mobile)))
           (and
             (= (* (branch-length l)
                   (total-weight
                     (branch-structure l)))
                (* (branch-length r)
                   (total-weight
                     (branch-structure r))))
             (balanced? (branch-structure l))
             (balanced? (branch-structure r))))))


                                                    25
              Complexity
• Worst case scenario for size n
  – Need to test all rods
  – May depend on mobile structure
• Upper bound
  – Apply total-weight on each sub-mobile
  – O(n2)
• Lower bound

                                            26
               Mobile structures
                       n

                           n-1

                                 n-2

                                       n-3

                                             ...




T(n) = T(n-1) + (n)       (for this family of mobiles)
T(n) = (n2)                                              27
             Mobile structures
                         n/2     n/2

             n/4   n/4                 n/4   n/4
         n/8 n/8    n/8 n/8     n/8 n/8      n/8 n/8




T(n) = 2T(n/2) + (n)          (for this family of mobiles)
T(n) = (nlogn)                                           28
          Implementation
Constructors
(define (make-mobile left right)
  (list left right))
(define (make-branch length structure)
  (list length structure))

Selectors
(define (left-branch mobile) (car mobile))
(define (right-branch mobile) (cadr mobile))
(define (branch-length branch) (car branch))
(define (branch-structure branch)
  (cadr branch))
                                           29
      Preprocessing the data
• Calculate weight on creation:
   – (define (make-mobile left right)
       (list left right
             (+ (total-weight
                  (branch-structure left))
                (total-weight
                  (branch-structure right)))))
• New Selector:
   – (define (mobile-weight mobile)
        (caddr mobile))
• Simpler total-weight:
   – (define (total-weight mobile)
       (if (atom? mobile) mobile
           (mobile-weight mobile)))

                                                 30
          Complexity revised
•   Complexity of new total-weight?
•   Complexity of new constructor?
•   Complexity of balanced?
•   Can we do even better?




                                      31

				
DOCUMENT INFO