Lecture #10

Post on 18-Jan-2016

26 views 0 download

description

Lecture #10. Sets (2.3.3) Huffman Encoding Trees (2.3.4). Sets. A set is a collection of distinct items. Methods: ( element-of-set? x set) (adjoin-set x set) (union-set s1 s2) (intersection-set s1 s2). Implementation. We will see three ways to implement sets. With lists - PowerPoint PPT Presentation

Transcript of Lecture #10

מבוא מורחב1

Lecture #10

Sets (2.3.3)

Huffman Encoding Trees (2.3.4)

מבוא מורחב2

Sets

Methods:

(element-of-set? x set)

(adjoin-set x set)

(union-set s1 s2)

(intersection-set s1 s2)

A set is a collection of distinct items.

מבוא מורחב3

Implementation

We will see three ways to implement sets.

1. With lists2. With sorted lists3. With trees

And compare the three methods.

מבוא מורחב4

First implementation: Lists

Empty set empty list ‘()

Adding an element cons

Set Union append

Set Intersection

But: we also need to remember to remove duplicats.

מבוא מורחב5

First implementation: Lists

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

equal? : Like eq? for symbols. Works for numbers

Works recursively for compounds.

(eq? (list ‘a ‘b) (list ‘a ‘b)) #f

(equal? (list ‘a ‘b) (list ‘a ‘b)) #t

מבוא מורחב6

(define (adjoin-set x set)

(if (element-of-set? x set)

set

(cons x set)))

First Implementation: Adjoin

מבוא מורחב7

Intersection

(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))))

מבוא מורחב8

Union

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

(define (union-set set1 set2) (cond ((null? set1) set2)) (else (adjoin-set (car set1) (union-set (cdr set1) set2)))))

מבוא מורחב9

Analysis

Element-of-set

Adjoin-set

Intersection-set

Union-set

(n)

(n2)

(n)

(n2)

מבוא מורחב10

Second implementation: Sorted Lists

Empty set empty list ‘()

Adding an element We add the element to the list so that the list is sorted.

Set Union sorted list of union.

Set Intersection sorted list of intersection.

מבוא מורחב11

Membership?

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

(n) steps.

adjoin-set is similar, try it yourself

מבוא מורחב12

Intersection

(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))))

Can we do it better ?

מבוא מורחב13

Better Intersection

(define (intersection-set set1 set2) (if (or (null? set1) (null? set2)) '() (let ((x1 (car set1)) (x2 (car set2))) (cond ((= x1 x2) (cons x1 (intersection-set (cdr set1) (cdr set2)))) ((< x1 x2) (intersection-set (cdr set1) set2)) ((< x2 x1) (intersection-set set1 (cdr set2)))))))

מבוא מורחב14

Example

set1 set2 intersection

(1 3 7 9) (1 4 6 7) (1

(3 7 9) (4 6 7) (1

(7 9) (4 6 7) (1

(7 9) (6 7) (1

(7 9) (7) (1

(9) () (1 7)

Time and space (n)

Union -- similar

מבוא מורחב15

Complexity

Element-of-set

Adjoin-set

Intersection-set

Union-set

(n)

(n2)

(n)

(n2)

unsorted sorted

(n)

(n)

(n)

(n)

מבוא מורחב16

Version 3: Binary Trees

7

3

1 5

9

12

3

1

5

12

7

9

Balanced Tree Unbalanced Tree

Depth = (log n)

מבוא מורחב17

Interface of Binary Trees.

7

3

1 5

9

12 Selectors:

(define (entry tree) (car tree))

(define (left-branch tree) (cadr tree))

(define (right-branch tree) (caddr tree))

Constructor: (define (make-tree entry left right) (list entry left right))

מבוא מורחב18

Element-of-set

(define (element-of-set? x set) (cond ((null? set) false) ((= x (entry set)) true) ((< x (entry set)) (element-of-set? x (left-branch set))) ((> x (entry set)) (element-of-set? x (right-branch set)))))

Complexity: (d)

If tree is balanced d log(n)

מבוא מורחב19

Complexity

Element-of-set

Adjoin-set

Intersection-set

Union-set

(n)

(n2)

(n)

(n2)

unsorted sorted

(n)

(n)

(n)

(n)

trees

(log(n))

(log(n))

(n log(n))

(n log(n))

מבוא מורחב20

Huffman encoding trees

מבוא מורחב21

Data Transmission

A B

“sos”

We wish to send information efficiently from A to B

מבוא מורחב22

Fixed Length Codes

Represent data as a sequence of 0’s and 1’s

Example: BACADAEAFABBAAAGAH

A 000 B 001 C 010 D 011

E 100 F 101 G 110 H 111

001000010000011000100000101000001001000000000110

000111

This is a fixed length code. Sequence is 18x3=54 bits long.

Can we make the sequence of 0’s and 1’s shorter ?

מבוא מורחב23

Variable Length Code

A 0 B 100 C 1010 D 1011

E 1100 F 1101 G 1110 H 1111

Example: BACADAEAFABBAAAGAH

100010100101101100011010100100000111001111

Make use of frequencies. Frequency of A=8, B=3, others 1.

But how do we decode?

42 bits (20% shorter)

24

Prefix code Binary tree

A 0 B 100 C 1010 D 1011

E 1100 F 1101 G 1110 H 1111

Prefix code: No codeword is a prefix of the other

A

B

C D E F G H

0 1

1

1

1

0

0

0

0

00

1

11

25

Decoding Example

10001010A

B

C D E F G H

0 1

1

1

1

0

0

0

0

00

1

11

10001010 B10001010 BA10001010 BAC

מבוא מורחב26

Decoding a Message

(define (choose-branch bit branch) (cond ((= bit 0) (left-branch branch)) ((= bit 1) (right-branch branch)) (else (error "bad bit -- CHOOSE-BRANCH" bit))))

מבוא מורחב27

Decoding a Message

(define (decode bits Huffman_tree current_branch) (if (null? bits) '() (let ((next-branch (choose-branch (car bits) current_branch))) (if (leaf? next-branch) (cons (symbol-leaf next-branch) (decode (cdr bits) Huffman_tree Huffman_tree)) (decode (cdr bits) Huffman_tree next-branch)))))

28

The cost of a weighted Tree

17

2 2

9

45

2

A

B

C D E F G H

8

3

1 1 1 1 1 1

{G,H}{E,F}{C,D}

{B,C,D} {E,F,G,H}

{B,C,D,E,F,G,H}

{A,B,C,D,E,F,G,H}

29

Huffman Tree = Optimal Length Code

Optimal: no code has better weighted average length

A

B

C D E F G H

0 1

1

1

1

0

0

0

0

00

1

11

8

3

1 1 1 1 1 1

A

D

B C E F G H

0 1

1

1

1

0

0

0

0

00

1

11

8

3 1 1 1 1 1

1

מבוא מורחב30

Next Time

• We will describe an efficient algorithm that given the weights, constructs the Huffman tree.

• We will describe the interface it requires and how to implement it.