1 / 30

Lecture #10

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

brone
Download Presentation

Lecture #10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture #10 Sets (2.3.3) Huffman Encoding Trees (2.3.4) מבוא מורחב

  2. 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) מבוא מורחב

  3. Implementation • We will see three ways to implement sets. • With lists • With sorted lists • 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. First Implementation: Adjoin (define (adjoin-set x set) (if (element-of-set? x set) set (cons x set))) מבוא מורחב

  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 (n) Element-of-set Adjoin-set Intersection-set Union-set (n) (n2) (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. unsorted Element-of-set Adjoin-set Intersection-set Union-set (n) (n) (n2) (n2) Complexity sorted (n) (n) (n) (n) מבוא מורחב

  16. 3 7 7 1 9 3 5 9 12 5 1 12 Version 3: Binary Trees Depth = (log n) Balanced Tree Unbalanced Tree מבוא מורחב

  17. 7 9 3 12 5 1 Interface of Binary Trees. Constructor: (define (make-tree entry left right) (list entry left right)) Selectors: (define (entry tree) (car tree)) (define (left-branch tree) (cadr tree)) (define (right-branch tree) (caddr tree)) מבוא מורחב

  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. unsorted sorted Element-of-set Adjoin-set Intersection-set Union-set (n) (n) (n) (n) (n) (n2) (n) (n2) Complexity trees (log(n)) (log(n)) (n log(n)) (n log(n)) מבוא מורחב

  20. Huffman encoding trees מבוא מורחב

  21. Data Transmission “sos” A B 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 A000 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. 42 bits (20% shorter) Variable Length Code Make use of frequencies. Frequency of A=8, B=3, others1. A 0 B 100 C 1010 D 1011 E 1100 F 1101 G 1110 H 1111 Example: BACADAEAFABBAAAGAH 100010100101101100011010100100000111001111 But how do we decode? מבוא מורחב

  24. 0 1 0 1 A 0 1 0 1 0 1 1 0 0 1 B C D E F G H Prefix code  Binary tree Prefix code: No codeword is a prefix of the other A 0 B 100 C 1010 D 1011 E 1100 F 1101 G 1110 H 1111

  25. 0 1 0 1 A 0 1 0 1 0 1 1 0 0 1 B C D E F G H Decoding Example 10001010 10001010 B 10001010 BA 10001010 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 {A,B,C,D,E,F,G,H} 17 A 8 9 {B,C,D,E,F,G,H} 4 5 {B,C,D} {E,F,G,H} 2 2 B 2 3 {C,D} {E,F} {G,H} C D E F G H 1 1 1 1 1 1

  29. 0 1 0 1 8 A 8 0 1 0 1 A 1 0 1 0 0 1 0 1 D 0 1 3 1 0 0 1 0 1 1 0 1 0 1 B C E F G H 1 3 1 1 1 1 B 1 1 1 1 1 1 C D E F G H Huffman Tree = Optimal Length Code Optimal: no code has better weighted average length

  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. מבוא מורחב

More Related