Math236 Discrete Maths with Applications P. Ittmann UKZN, - - PowerPoint PPT Presentation

math236 discrete maths with applications
SMART_READER_LITE
LIVE PREVIEW

Math236 Discrete Maths with Applications P. Ittmann UKZN, - - PowerPoint PPT Presentation

Math236 Discrete Maths with Applications P. Ittmann UKZN, Pietermaritzburg Semester 1, 2012 Ittmann (UKZN PMB) Math236 2012 1 / 14 Applications of Trees We now study a particularly useful application of trees Consider the following


slide-1
SLIDE 1

Math236 Discrete Maths with Applications

  • P. Ittmann

UKZN, Pietermaritzburg

Semester 1, 2012

Ittmann (UKZN PMB) Math236 2012 1 / 14

slide-2
SLIDE 2

Applications of Trees

We now study a particularly useful application of trees Consider the following problem: You wish to encode a text message in a binary alphabet in such a way as to use as few bits as possible

Ittmann (UKZN PMB) Math236 2012 2 / 14

slide-3
SLIDE 3

Applications of Trees

One solution is to use an ASCII encoding Recall that in ASCII every letter is encoded with 8 bits This is inefficient, since some characters appear more frequently than

  • thers

For example e is encoded using the same number of bits as q It would be more efficient if e was encoded with as few bits as possible

Ittmann (UKZN PMB) Math236 2012 3 / 14

slide-4
SLIDE 4

Huffman Codes

One solution to this problem uses Huffman Codes As with ASCII, Huffman codes represent characters as binary strings But in Huffman codes, the length of the strings is variable The more frequently used letters are assigned shorter strings

Ittmann (UKZN PMB) Math236 2012 4 / 14

slide-5
SLIDE 5

Huffman Codes

A Huffman code is represented by a binary tree called a Huffman tree The binary tree has various characters as leaves But the frequently used characters are placed closer to the root than the infrequently used characters The edges of the tree are labelled either with a 0 or a 1 An edge joining a parent with a “left child” is labelled 0 An edge joining a parent with a “right child” is labelled 1 Each message generates its own Huffman tree, which we will cover later

Ittmann (UKZN PMB) Math236 2012 5 / 14

slide-6
SLIDE 6

Huffman Codes

Consider the following binary tree

r 1 1 1 1 E A S R C

Ittmann (UKZN PMB) Math236 2012 6 / 14

slide-7
SLIDE 7

Huffman Codes

Using the sample tree above we will decode the binary string 0101100 We start at the root r We follow the path indicated by the binary string When a leaf is reached we record its label and return to the root We then continue with the rest of the string

Ittmann (UKZN PMB) Math236 2012 7 / 14

slide-8
SLIDE 8

Huffman Codes

Example We apply the above technique to 0101100 From the root r we move left using the first 0 A leaf with label E is reached We record E and return to the root We now use the string 101100 From the root r we move right using 1 We then move to the left using the next 0 A leaf A is recorded and we return to the root

Ittmann (UKZN PMB) Math236 2012 8 / 14

slide-9
SLIDE 9

Huffman Codes

Example We now repeat the process with the string 1100 This produces S and finally another E Thus the string 0101100 represents EASE

Ittmann (UKZN PMB) Math236 2012 9 / 14

slide-10
SLIDE 10

Huffman Codes

We now examine how to encode a string using a Huffman tree Each character X is encoded by traversing from the root to the corresponding leaf labelled X The 0’s and 1’s on each edge traversed are recorded in order This record forms the encoding of X

Ittmann (UKZN PMB) Math236 2012 10 / 14

slide-11
SLIDE 11

Huffman Codes

Consider the following tree

a d c e b r 1 1 1 1

Ittmann (UKZN PMB) Math236 2012 11 / 14

slide-12
SLIDE 12

Huffman Codes

Suppose we wish to encode the string add a is encoded as 000, d is encoded as 001 So, add is encoded as 000001001 Likewise, bed is encoded as 1110001 Note that bed is encoded as a 24-bit ASCII string, but only a 7-bit string using this Huffman tree

Ittmann (UKZN PMB) Math236 2012 12 / 14

slide-13
SLIDE 13

Huffman Codes

Given a Huffman code, we can construct a Huffman tree from it Consider the following Huffman code Symbol Code a 000 b 001 c 010 d 011 e 100

Ittmann (UKZN PMB) Math236 2012 13 / 14

slide-14
SLIDE 14

Huffman Codes

It generates the following Huffman tree

1 1 1 1 1 1 r a b c d e

Ittmann (UKZN PMB) Math236 2012 14 / 14