class 10: abstracting procedures

27
Class 10: Abstracting Procedures cs1120 Fall 2011 David Evans 14 September 2011

Upload: david-evans

Post on 07-Dec-2014

291 views

Category:

Documents


0 download

DESCRIPTION

Abstracting List ProceduresEdit Distance

TRANSCRIPT

Page 1: Class 10: Abstracting Procedures

Class 10: Abstracting Procedures

cs1120 Fall 2011David Evans14 September 2011

Page 2: Class 10: Abstracting Procedures

Plan for Today

PS2 Due NowAbstracting List ProceduresPS2: edit-distance

PS3 posted now, due next Friday (Sept 23).

You may work with a partner you choose.If you don’t have a partner arranged, you need toemail me by 3:55pm tomorrow: - Subject: PS3 Partner Either: “I want a partner for ps3.” or “I prefer to work alone on ps3 because [ <your reason> ]”

Page 3: Class 10: Abstracting Procedures

3

Warm-up: list-sum

Define a procedure, list-sum, that takes a list of numbers as input and outputs the sum of the numbers in the input list. (list-sum (list 1 2 3)) 6

(list-sum null) 0

Page 4: Class 10: Abstracting Procedures

4

list-sum

(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))

Okay, what about list-product?

Page 5: Class 10: Abstracting Procedures

5

list-product

(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))

Okay, what about list-length?

Page 6: Class 10: Abstracting Procedures

6

list-length

(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))

Page 7: Class 10: Abstracting Procedures

7

Comparing List Procedures

(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))

(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))

(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))

(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))

Page 8: Class 10: Abstracting Procedures

8

Base Cases

(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))

(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))

(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))

(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))

Page 9: Class 10: Abstracting Procedures

9

Recursive Calls

(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))

(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))

(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))

(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))

What does each do with the car of the list?

Page 10: Class 10: Abstracting Procedures

Abstracted List Procedure(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))

(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))

Page 11: Class 10: Abstracting Procedures

Master of (Almost) All List Procedures

(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))(define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p)))))

(define (list-product p) (if (null? p) 1 (* (car p) (list-product (cdr p)))))

Page 12: Class 10: Abstracting Procedures

Master of (Almost) All List Procedures(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))

(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))

(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))

Page 13: Class 10: Abstracting Procedures

PS2: Edit Distance

http://mbgd.genome.ad.jp/CoreAligner/

Million Bases

Page 14: Class 10: Abstracting Procedures

Defining edit-distance

(define (edit-distance s1 s2) (if (and (null? s1) (null? s2)) 0 (if (null? s1) (length s2) (if (null? s2) (length s1) …

Base cases

Page 15: Class 10: Abstracting Procedures

(define (edit-distance s1 s2) (if (or (null? s1) (null? s2)) (+ (length s1) (length s2)) (min (if (eq? (car s1) (car s2)) ; match or mutate

(edit-distance (cdr s1) (cdr s2)) (+ 1 (edit-distance (cdr s1) (cdr s2)))) (+ 1 (edit-distance s1 (cdr s2))) ; insert in s1 (+ 1 (edit-distance (cdr s1) s2))))) ; delete from s1

Page 16: Class 10: Abstracting Procedures

(define (edit-distance s1 s2) (if (or (null? s1) (null? s2)) (+ (length s1) (length s2)) (min (+ (if (eq? (car s1) (car s2) 0 1) ; match or mutate (edit-distance (cdr s1) (cdr s2)) (+ 1 (edit-distance s1 (cdr s2))) ; insert in s1 (+ 1 (edit-distance (cdr s1) s2))))) ; delete from s1

This is the smallest correct edit-distance procedure I can find…but, if you can find a smaller one that is worth a gold star bonus! (Replacing + 1 with inc or s1/s2 with s/t doesn’t count.)

Page 17: Class 10: Abstracting Procedures

Executing edit-distance a c a t c a t g c

c a t g a t a c(edit-distance )

c a t c a t g c a t g a t a c

c a t c a t g c c a t g a t a c

a c a t c a t g c a t g a t a c

c a t c a t g c a t g a t a c

Page 18: Class 10: Abstracting Procedures

> (require racket/trace)> (trace edit-distance)> (edit-distance (list 1 2) (list 2))>(edit-distance '(1 2) '(2))> (edit-distance '(2) '())< 1> (edit-distance '(1 2) '())< 2> (edit-distance '(2) '(2))> >(edit-distance '() '())< <0> >(edit-distance '(2) '())< <1> >(edit-distance '() '(2))< <1< 0<11

7 calls to edit-distance

> (edit-distance (list 1 2) (list 2 3))>(edit-distance '(1 2) '(2 3))> (edit-distance '(2) '(3))> >(edit-distance '() '())< <0> >(edit-distance '(2) '())< <1> >(edit-distance '() '(3))< <1< 1> (edit-distance '(1 2) '(3))> >(edit-distance '(2) '())< <1> >(edit-distance '(1 2) '())< <2> >(edit-distance '(2) '(3))> > (edit-distance '() '())< < 0> > (edit-distance '(2) '())< < 1> > (edit-distance '() '(3))< < 1< <1< 2> (edit-distance '(2) '(2 3))> >(edit-distance '() '(3))< <1> >(edit-distance '(2) '(3))> > (edit-distance '() '())< < 0> > (edit-distance '(2) '())< < 1> > (edit-distance '() '(3))< < 1< <1> >(edit-distance '() '(2 3))< <2< 1<2

2

19 calls to edit-distance

Page 19: Class 10: Abstracting Procedures

(define (edit-distance s1 s2) (if (or (null? s1) (null? s2)) (+ (length s1) (length s2)) (min (+ (if (eq? (car s1) (car s2) 0 1) ; match or mutate (edit-distance (cdr s1) (cdr s2)) (+ 1 (edit-distance s1 (cdr s2))) ; insert in s1 (+ 1 (edit-distance (cdr s1) s2))))) ; delete from s1

Page 20: Class 10: Abstracting Procedures

(define (edit-distance-counter s1 s2) (if (or (null? s1) (null? s2)) 1 (+ 1 (edit-distance-counter (cdr s1) (cdr s2)) (edit-distance-counter s1 (cdr s2)) (edit-distance-counter (cdr s1) s2))))

(define (edit-distance s1 s2) (if (or (null? s1) (null? s2)) (+ (length s1) (length s2)) (min (+ (if (eq? (car s1) (car s2) 0 1) ; match or mutate (edit-distance (cdr s1) (cdr s2)) (+ 1 (edit-distance s1 (cdr s2))) ; insert in s1 (+ 1 (edit-distance (cdr s1) s2))))) ; delete from s1

Page 21: Class 10: Abstracting Procedures

Evaluation: Input Lists Size (each list) Calls to edit-distance

0 1

1 4

2 19

3 94

4 481

5 2,524

6 13,483

7 72,958

8 398,593

9 2,193,844

10 12,146,179

Page 22: Class 10: Abstracting Procedures

1 2 3 4 5 6 7 8 9 10 11 12 130

500,000,000

1,000,000,000

1,500,000,000

2,000,000,000

2,500,000,000

Input Size

Num

ber o

f ed

it-di

stan

ce A

pplic

ation

s

12,146,179

0

50,000,000

100,000,000

150,000,000

200,000,000

250,000,000

300,000,000Computing Power 1969-2011(in Apollo Control Computer Units)

Page 23: Class 10: Abstracting Procedures

a c a t c a t g c

0 1 2 3 4 5 6 7 8 9

c 1

a 2

t 3

g 4

a 5

t 6

g 7

s1

s2

Page 24: Class 10: Abstracting Procedures

a c a t c a t g c

0 1 2 3 4 5 6 7 8 9

c 1

a 2

t 3

g 4

a 5

t 6

g 7

s1

s2

Value in square [i, j] = min (1 + value in square [i-1, j], 1 + value in square [i, j-1], (0 or 1) + value in square [i-1, j-1])

Page 25: Class 10: Abstracting Procedures

a c a t c a t g c

0 1 2 3 4 5 6 7 8 9

c 1 1 1 2 3 4 5 6 7 8

a 2 2 2 1 2 3 4 5 6 7

t 3 3 3 2 1 3 4 5 6 7

g 4 4 4 4 2 2 3 4 5 6

a 5 5 5 5 4 3 2 3 4 5

t 6 6 6 6 5 4 3 2 3 4

g 7 7 7 7 6 5 4 3 2 3

s1

s2

Page 26: Class 10: Abstracting Procedures

Is this only used for genome analysis?

Page 27: Class 10: Abstracting Procedures

ChargeProblem Set 3: Posted now, due next Friday

Remember to either (1) have a partner you agree to work with in place, or (2) email me a partner/solo request by tomorrow