math236 discrete maths with applications
play

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


  1. Math236 Discrete Maths with Applications P. Ittmann UKZN, Pietermaritzburg Semester 1, 2012 Ittmann (UKZN PMB) Math236 2012 1 / 14

  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

  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 others 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

  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

  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

  6. Huffman Codes Consider the following binary tree r 0 1 E 0 1 A 0 1 S 0 1 C R Ittmann (UKZN PMB) Math236 2012 6 / 14

  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

  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

  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

  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

  11. Huffman Codes Consider the following tree r 0 1 1 0 0 1 e b c 0 1 a d Ittmann (UKZN PMB) Math236 2012 11 / 14

  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

  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

  14. Huffman Codes It generates the following Huffman tree r 0 1 0 1 1 0 1 0 1 1 c d e a b Ittmann (UKZN PMB) Math236 2012 14 / 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend