Top Banner
61A Extra Lecture 4 Thursday, February 19
112

61A Extra Lecture 4

Feb 10, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 61A Extra Lecture 4

61A Extra Lecture 4

Thursday, February 19

Page 2: 61A Extra Lecture 4
Page 3: 61A Extra Lecture 4

0100010101101110011000110110111101100100011010010110111001100111

Page 4: 61A Extra Lecture 4

0100010101101110011000110110111101100100011010010110111001100111(Encoding)

Page 5: 61A Extra Lecture 4

What’s the point?

3http://pixshark.com/confused-face-clip-art.htm

Page 6: 61A Extra Lecture 4

What’s the point?

• Why do we encode things?

3http://pixshark.com/confused-face-clip-art.htm

Page 7: 61A Extra Lecture 4

What’s the point?

• Why do we encode things?

• You don’t speak binary

3http://pixshark.com/confused-face-clip-art.htm

Page 8: 61A Extra Lecture 4

What’s the point?

• Why do we encode things?

• You don’t speak binary

• Computers don’t speak English

3http://pixshark.com/confused-face-clip-art.htm

Page 9: 61A Extra Lecture 4

What’s the point?

• Why do we encode things?

• You don’t speak binary

• Computers don’t speak English

3http://pixshark.com/confused-face-clip-art.htm

Page 10: 61A Extra Lecture 4

A First Attempt

4

Page 11: 61A Extra Lecture 4

A First Attempt

• Let’s use an encoding

4

Page 12: 61A Extra Lecture 4

A First Attempt

• Let’s use an encoding

4

Letter Binary Letter Binarya 0 n 1b 1 o 0c 0 p 1d 1 q 1e 1 r 0f 0 s 1g 0 t 0h 1 u 0i 1 v 1j 1 w 1k 0 x 1l 1 y 0

m 1 z 0

Page 13: 61A Extra Lecture 4

Analysis

5

Page 14: 61A Extra Lecture 4

Analysis

Pros

5

Page 15: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

5

Page 16: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

• Took a very small amount of space

5

Page 17: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

• Took a very small amount of space

Cons

5

Page 18: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

• Took a very small amount of space

Cons

• Decoding it was impossible

5

Page 19: 61A Extra Lecture 4

Decoding

6

Page 20: 61A Extra Lecture 4

Decoding

• Encoding by itself is useless

6

Page 21: 61A Extra Lecture 4

Decoding

• Encoding by itself is useless

• Decoding is also necessary

6

Page 22: 61A Extra Lecture 4

Decoding

• Encoding by itself is useless

• Decoding is also necessary

• So… we need more bits

6

Page 23: 61A Extra Lecture 4

Decoding

• Encoding by itself is useless

• Decoding is also necessary

• So… we need more bits

• How many bits do we need?

6

Page 24: 61A Extra Lecture 4

Decoding

• Encoding by itself is useless

• Decoding is also necessary

• So… we need more bits

• How many bits do we need?

• lowercase alphabet

6

Page 25: 61A Extra Lecture 4

Decoding

• Encoding by itself is useless

• Decoding is also necessary

• So… we need more bits

• How many bits do we need?

• lowercase alphabet

• 5 bits

6

Page 26: 61A Extra Lecture 4

A Second Attempt

7

Page 27: 61A Extra Lecture 4

A Second Attempt

• Let’s try another encoding

7

Page 28: 61A Extra Lecture 4

A Second Attempt

• Let’s try another encoding

7

Letter Binary Letter Binarya 00000 n 01101b 00001 o 01110c 00010 p 01111d 00011 q 10000e 00100 r 10001f 00101 s 10010g 00110 t 10011h 00111 u 10100i 01000 v 10101j 01001 w 10110k 01010 x 10111l 01011 y 11000

m 01100 z 11001

Page 29: 61A Extra Lecture 4

Analysis

8

Page 30: 61A Extra Lecture 4

Analysis

Pros

8

Page 31: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

8

Page 32: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

• Decoding was possible

8

Page 33: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

• Decoding was possible

Cons

8

Page 34: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

• Decoding was possible

Cons

• Takes more space…

8

Page 35: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

• Decoding was possible

Cons

• Takes more space…

• What restriction did we place that’s unnecessary?

8

Page 36: 61A Extra Lecture 4

Analysis

Pros

• Encoding was easy

• Decoding was possible

Cons

• Takes more space…

• What restriction did we place that’s unnecessary?

• Fixed length

8

Page 37: 61A Extra Lecture 4

Variable Length Encoding

9

Page 38: 61A Extra Lecture 4

Variable Length Encoding

• Problems?

9

Page 39: 61A Extra Lecture 4

Variable Length Encoding

• Problems?

• When do we start and stop?

9

Page 40: 61A Extra Lecture 4

Variable Length Encoding

• Problems?

• When do we start and stop?

• String of As and Bs: ABA

9

Page 41: 61A Extra Lecture 4

Variable Length Encoding

• Problems?

• When do we start and stop?

• String of As and Bs: ABA

• A - 00, B - 0

9

Page 42: 61A Extra Lecture 4

Variable Length Encoding

• Problems?

• When do we start and stop?

• String of As and Bs: ABA

• A - 00, B - 0

• Encode ABA: 00000

9

Page 43: 61A Extra Lecture 4

Variable Length Encoding

• Problems?

• When do we start and stop?

• String of As and Bs: ABA

• A - 00, B - 0

• Encode ABA: 00000

• Decode 00000:

9

Page 44: 61A Extra Lecture 4

Variable Length Encoding

• Problems?

• When do we start and stop?

• String of As and Bs: ABA

• A - 00, B - 0

• Encode ABA: 00000

• Decode 00000:

• ABA, AAB, BAA?

9

Page 45: 61A Extra Lecture 4

Variable Length Encoding

• Problems?

• When do we start and stop?

• String of As and Bs: ABA

• A - 00, B - 0

• Encode ABA: 00000

• Decode 00000:

• ABA, AAB, BAA?

• What lengths do we use?

9

Page 46: 61A Extra Lecture 4

A Second Look at Fixed Length

10

Page 47: 61A Extra Lecture 4

A Second Look at Fixed Length

10

Letter Binary Letter Binarya 00000 n 01101b 00001 o 01110c 00010 p 01111d 00011 q 10000e 00100 r 10001f 00101 s 10010g 00110 t 10011h 00111 u 10100i 01000 v 10101j 01001 w 10110k 01010 x 10111l 01011 y 11000

m 01100 z 11001

Page 48: 61A Extra Lecture 4

11

Trees!

Page 49: 61A Extra Lecture 4

11

Trees!

Letter Binary

A 00

B 01

C 1

Page 50: 61A Extra Lecture 4

11

Trees!

0 1

Letter Binary

A 00

B 01

C 1

Page 51: 61A Extra Lecture 4

11

Trees!

0 1

C

Letter Binary

A 00

B 01

C 1

Page 52: 61A Extra Lecture 4

11

Trees!

0 1

0 1C

Letter Binary

A 00

B 01

C 1

Page 53: 61A Extra Lecture 4

11

Trees!

0 1

B

0 1C

Letter Binary

A 00

B 01

C 1

Page 54: 61A Extra Lecture 4

11

Trees!

0 1

A B

0 1C

Letter Binary

A 00

B 01

C 1

Page 55: 61A Extra Lecture 4

12

What happens when…?

Letter Binary Letter Binarya 0 n 1b 1 o 0c 0 p 1d 1 q 1e 1 r 0f 0 s 1g 0 t 0h 1 u 0i 1 v 1j 1 w 1k 0 x 1l 1 y 0

m 1 z 0

Page 56: 61A Extra Lecture 4

12

What happens when…?

Letter Binary Letter Binarya 0 n 1b 1 o 0c 0 p 1d 1 q 1e 1 r 0f 0 s 1g 0 t 0h 1 u 0i 1 v 1j 1 w 1k 0 x 1l 1 y 0

m 1 z 0

• Rule 1: Each leaf only has 1 label

Page 57: 61A Extra Lecture 4

What happens when…?

13

Letter Binary

A 00

B 0

Page 58: 61A Extra Lecture 4

What happens when…?

13

• Rule 2: Only leaves get labels

Letter Binary

A 00

B 0

Page 59: 61A Extra Lecture 4

An Optimal Encoding

14

Page 60: 61A Extra Lecture 4

An Optimal Encoding

14

• Start with a tree

0 1

A B

0 1C

Page 61: 61A Extra Lecture 4

An Optimal Encoding

14

• Start with a tree

• What kinds of things do we want to encode with this?

0 1

A B

0 1C

Page 62: 61A Extra Lecture 4

An Optimal Encoding

14

• Start with a tree

• What kinds of things do we want to encode with this?

• What letter do we want to appear the most?

0 1

A B

0 1C

Page 63: 61A Extra Lecture 4

An Optimal Encoding

14

• Start with a tree

• What kinds of things do we want to encode with this?

• What letter do we want to appear the most?

• How about the least?

0 1

A B

0 1C

Page 64: 61A Extra Lecture 4

An Optimal Encoding

14

• Start with a tree

• What kinds of things do we want to encode with this?

• What letter do we want to appear the most?

• How about the least?

• This is called a Huffman Encoding

0 1

A B

0 1C

Page 65: 61A Extra Lecture 4

Huffman Encoding

15

Page 66: 61A Extra Lecture 4

Huffman Encoding

15

• Let’s pretend we want to come up with the optimal encoding:

Page 67: 61A Extra Lecture 4

Huffman Encoding

15

• Let’s pretend we want to come up with the optimal encoding:

• AAAAAAAAAABBBBBCCCCCCCDDDDDDDDD

Page 68: 61A Extra Lecture 4

Huffman Encoding

15

• Let’s pretend we want to come up with the optimal encoding:

• AAAAAAAAAABBBBBCCCCCCCDDDDDDDDD

• A appears 10 times

Page 69: 61A Extra Lecture 4

Huffman Encoding

15

• Let’s pretend we want to come up with the optimal encoding:

• AAAAAAAAAABBBBBCCCCCCCDDDDDDDDD

• A appears 10 times

• B appears 5 times

Page 70: 61A Extra Lecture 4

Huffman Encoding

15

• Let’s pretend we want to come up with the optimal encoding:

• AAAAAAAAAABBBBBCCCCCCCDDDDDDDDD

• A appears 10 times

• B appears 5 times

• C appears 7 times

Page 71: 61A Extra Lecture 4

Huffman Encoding

15

• Let’s pretend we want to come up with the optimal encoding:

• AAAAAAAAAABBBBBCCCCCCCDDDDDDDDD

• A appears 10 times

• B appears 5 times

• C appears 7 times

• D appears 9 times

Page 72: 61A Extra Lecture 4

Huffman Encoding

16

Page 73: 61A Extra Lecture 4

Huffman Encoding

16

• Start with the two smallest frequencies

Page 74: 61A Extra Lecture 4

Huffman Encoding

16

• Start with the two smallest frequencies

• A appears 10 times, B appears 5 times, C appears 7 times, D appears 9 times

Page 75: 61A Extra Lecture 4

Huffman Encoding

16

• Start with the two smallest frequencies

• A appears 10 times, B appears 5 times, C appears 7 times, D appears 9 times

A

B

C

D

Page 76: 61A Extra Lecture 4

Huffman Encoding

16

• Start with the two smallest frequencies

• A appears 10 times, B appears 5 times, C appears 7 times, D appears 9 times

A

B

C

D

Page 77: 61A Extra Lecture 4

Huffman Encoding

16

• Start with the two smallest frequencies

• A appears 10 times, B appears 5 times, C appears 7 times, D appears 9 times

A

B

C

D

0 1

A

D

B C

Page 78: 61A Extra Lecture 4

Huffman Encoding

17

Page 79: 61A Extra Lecture 4

Huffman Encoding

17

• Continue…

Page 80: 61A Extra Lecture 4

Huffman Encoding

17

• Continue…

• A appears 10 times, B & C appear a combined 12 times, D appears 9 times

Page 81: 61A Extra Lecture 4

Huffman Encoding

17

• Continue…

• A appears 10 times, B & C appear a combined 12 times, D appears 9 times

0 1

A

D

B C

Page 82: 61A Extra Lecture 4

Huffman Encoding

17

• Continue…

• A appears 10 times, B & C appear a combined 12 times, D appears 9 times

0 1

A

D

B C

Page 83: 61A Extra Lecture 4

Huffman Encoding

17

• Continue…

• A appears 10 times, B & C appear a combined 12 times, D appears 9 times

0 1

A

D

B C

0 1

B C

0 1

A D

Page 84: 61A Extra Lecture 4

Huffman Encoding

18

Page 85: 61A Extra Lecture 4

Huffman Encoding

18

• And finally…

Page 86: 61A Extra Lecture 4

Huffman Encoding

18

• And finally…

0 1

B C

0 1

A D

Page 87: 61A Extra Lecture 4

Huffman Encoding

18

• And finally…

0 1

B C

0 1

A D

Page 88: 61A Extra Lecture 4

Huffman Encoding

18

• And finally…

0 1

B C

0 1

A D

B D

0 1

C

0 1

A

0 1

Page 89: 61A Extra Lecture 4

Huffman Encoding

19

Page 90: 61A Extra Lecture 4

Huffman Encoding

19

• Another example…

Page 91: 61A Extra Lecture 4

Huffman Encoding

19

• Another example…

• AAAAAAAAAABCCD

Page 92: 61A Extra Lecture 4

Huffman Encoding

19

• Another example…

• AAAAAAAAAABCCD

• A appears 10 times

Page 93: 61A Extra Lecture 4

Huffman Encoding

19

• Another example…

• AAAAAAAAAABCCD

• A appears 10 times

• B appears 1 time

Page 94: 61A Extra Lecture 4

Huffman Encoding

19

• Another example…

• AAAAAAAAAABCCD

• A appears 10 times

• B appears 1 time

• C appears 2 times

Page 95: 61A Extra Lecture 4

Huffman Encoding

19

• Another example…

• AAAAAAAAAABCCD

• A appears 10 times

• B appears 1 time

• C appears 2 times

• D appears 1 time

Page 96: 61A Extra Lecture 4

Huffman Encoding

20

Page 97: 61A Extra Lecture 4

Huffman Encoding

20

• Start with the two smallest frequencies

Page 98: 61A Extra Lecture 4

Huffman Encoding

20

• Start with the two smallest frequencies

• A appears 10 times, B appears 1 time, C appears 2 times, D appears 1 time

Page 99: 61A Extra Lecture 4

Huffman Encoding

20

• Start with the two smallest frequencies

• A appears 10 times, B appears 1 time, C appears 2 times, D appears 1 time

A

B

C

D

Page 100: 61A Extra Lecture 4

Huffman Encoding

20

• Start with the two smallest frequencies

• A appears 10 times, B appears 1 time, C appears 2 times, D appears 1 time

A

B

C

D

Page 101: 61A Extra Lecture 4

Huffman Encoding

20

• Start with the two smallest frequencies

• A appears 10 times, B appears 1 time, C appears 2 times, D appears 1 time

A

B

C

D

0 1

A

C

B D

Page 102: 61A Extra Lecture 4

Huffman Encoding

21

Page 103: 61A Extra Lecture 4

Huffman Encoding

21

• Start with the two smallest frequencies

Page 104: 61A Extra Lecture 4

Huffman Encoding

21

• Start with the two smallest frequencies

• A appears 10 times, B & D appear a combined 2 times, C appears 2 times

Page 105: 61A Extra Lecture 4

Huffman Encoding

21

• Start with the two smallest frequencies

• A appears 10 times, B & D appear a combined 2 times, C appears 2 times

0 1

A

C

B D

Page 106: 61A Extra Lecture 4

Huffman Encoding

21

• Start with the two smallest frequencies

• A appears 10 times, B & D appear a combined 2 times, C appears 2 times

0 1

A

C

B D

Page 107: 61A Extra Lecture 4

Huffman Encoding

21

• Start with the two smallest frequencies

• A appears 10 times, B & D appear a combined 2 times, C appears 2 times

0 1

A

C

B D

0 1

C 0 1

B D

A

Page 108: 61A Extra Lecture 4

Huffman Encoding

22

Page 109: 61A Extra Lecture 4

Huffman Encoding

22

• And finally…

Page 110: 61A Extra Lecture 4

Huffman Encoding

22

• And finally…

0 1

C 0 1

B D

A

Page 111: 61A Extra Lecture 4

Huffman Encoding

22

• And finally…

0 1

C 0 1

B D

A

Page 112: 61A Extra Lecture 4

Huffman Encoding

22

• And finally…

0 1

C 0 1

B D

0 1

A

0 1

C 0 1

B D

A