1 Huffman Codes. 2 ASCII use same size encoding for all characters. Variable length codes can produce shorter messages than fixed length codes Huffman.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 15 - Case Study: Huffman Codes.
Advertisements

Introduction to Computer Science 2 Lecture 7: Extended binary trees
Lecture 4 (week 2) Source Coding and Compression
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Huffman code and ID3 Prof. Sin-Min Lee Department of Computer Science.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture3.
Greedy Algorithms (Huffman Coding)
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Huffman Coding: An Application of Binary Trees and Priority Queues
Optimal Merging Of Runs
A Data Compression Algorithm: Huffman Compression
Data Structures – LECTURE 10 Huffman coding
Chapter 9: Huffman Codes
CSE 143 Lecture 18 Huffman slides created by Ethan Apter
Data Compression Basics & Huffman Coding
Data Compression and Huffman Trees (HW 4) Data Structures Fall 2008 Modified by Eugene Weinstein.
x x x 1 =613 Base 10 digits {0...9} Base 10 digits {0...9}
Huffman Codes Message consisting of five characters: a, b, c, d,e
Data Structures and Algorithms Huffman compression: An Application of Binary Trees and Priority Queues.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Huffman Codes. Encoding messages  Encode a message composed of a string of characters  Codes used by computer systems  ASCII uses 8 bits per character.
Huffman Encoding Veronica Morales.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
CS-2852 Data Structures LECTURE 13B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures Week 6: Assignment #2 Problem
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Compression.  Compression ratio: how much is the size reduced?  Symmetric/asymmetric: time difference to compress, decompress?  Lossless; lossy: any.
Huffman Coding. Huffman codes can be used to compress information –Like WinZip – although WinZip doesn’t use the Huffman algorithm –JPEGs do use Huffman.
Lossless Compression CIS 465 Multimedia. Compression Compression: the process of coding that will effectively reduce the total number of bits needed to.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Huffman coding Content 1 Encoding and decoding messages Fixed-length coding Variable-length coding 2 Huffman coding.
Trees (Revisited) CHAPTER 15 6/30/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Huffman Code and Data Decomposition Pranav Shah CS157B.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
Huffman’s Algorithm 11/02/ Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is.
Foundation of Computing Systems
Bahareh Sarrafzadeh 6111 Fall 2009
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
1 Huffman Codes Drozdek Chapter Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Lossless Decomposition and Huffman Codes Sophia Soohoo CS 157B.
Huffman Coding The most for the least. Design Goals Encode messages parsimoniously No character code can be the prefix for another.
1 Data Compression Hae-sun Jung CS146 Dr. Sin-Min Lee Spring 2004.
Huffman encoding.
CSE 143 Lecture 22 Huffman slides created by Ethan Apter
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
Compression and Huffman Coding. Compression Reducing the memory required to store some information. Lossless compression vs lossy compression Lossless.
Huffman Codes ASCII is a fixed length 7 bit code that uses the same number of bits to define each character regardless of how frequently it occurs. Huffman.
HUFFMAN CODES.
COMP261 Lecture 22 Data Compression 2.
Assignment 6: Huffman Code Generation
Chapter 5 : Trees.
Greedy Technique.
ISNE101 – Introduction to Information Systems and Network Engineering
Huffman Coding Based on slides by Ethan Apter & Marty Stepp
Data Compression If you’ve ever sent a large file to a friend, you may have compressed it into a zip archive like the one on this slide before doing so.
Chapter 8 – Binary Search Tree
The Huffman Algorithm We use Huffman algorithm to encode a long message as a long bit string - by assigning a bit string code to each symbol of the alphabet.
Chapter 9: Huffman Codes
Huffman Coding CSE 373 Data Structures.
Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths.
Trees Addenda.
Greedy Algorithms Alexandra Stefan.
Podcast Ch23d Title: Huffman Compression
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
CSE 589 Applied Algorithms Spring 1999
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Presentation transcript:

1 Huffman Codes

2 ASCII use same size encoding for all characters. Variable length codes can produce shorter messages than fixed length codes Huffman Codes constructs an optimal variable bit length code for an alphabet with known frequency probability for each character

Average Length for a Character Set ACDDE probability length23441 Expected length = 0.2* *3+0.1*4+0.15*4+0.45*1

4 Immediate Decodability Prefix codes: No valid code symbol is a prefix of another valid code symbol. – Codes that are immediately decodable!

5 Huffman's code Optimal Codes – For a given character set with known frequency probability – Immediately decodable. – Average message length for a large number of messages is minimal

6 Huffman's Algorithm Huffman_tree_node: ch, freq, *left and *right Initialize a list of Huffman_tree_node – One node for each character containing the character and the frequency, left, right is NULL. While there is more than one node in the list: – Find two nodes with minimal frequency. – Remove those nodes from the list, make a new node, the new node’s left and right subtrees are the nodes just removed, the new node frequency is the sum of left and right nodes’ frequency – Label the arc to the left subtree with 0. – Label the arc to the right subtree with 1. – Add the new node to the list.

Example ABCDE probability BCDAE 0.1 NULL 0.1 NULL 0.15 NULL 0.2 NULL 0.45 NULL Remove two nodes Band C which minimal freq, Make a new Huffman_tree_node: BC: left =B, right –C freq= =0.2, put the new node into the list, next select D A, visualize the tree, Keep doing this until there is no more than 1 node in the list DABCE 0.15 NULL 0.2 NULL 0.2 B C 0.45 NULL

Huffm an code A 011 B 000 C 001 D 010 E1E1 ABCDE probability Example Result

9 Huffman Decoding Algorithm HuffmanDecode(Huffman_Tree ht, String s), Initialize pointer p to the root of Huffman tree. While end of message string not reached: move p to the left child if next bit is 0 otherwise move p to the right child If p points to a leaf Display the character at that leaf. Reset p to the root of the Huffman tree.

10 Huffman Decoding Algorithm Decoding Remember that Huffman code is immediately decodable: B E A D

11 Implementing a Huffman Code Program class Huffman_tree_node { //functions to set ch, freq, left and right char ch; double freq; Huffman_tree_node *left, *right; }

12 Huffman_Tree class Huffman_Tree { public: Huffman_Tree(void); ~Huffman_Tree(void) {}; // Add a single node tree to the list. void Add(char c, double frequency); Make_Huffman_code_Tree(void) private: list node_list; };

13 main.cpp Huffman_Tree huffman_tree; int main(void) { cout << "This is the Huffman code program.\n\n"; huffman_tree.Add('a',0.2); huffman_tree.Add('b',0.1); huffman_tree.Add('c',0.1); huffman_tree.Add('d',0.15); huffman_tree.Add('e',0.45); }

14 Implementing Huffman’s Algorithm function Make_Huffman_code_Tree Repeatedly – Sort the list of trees by frequency – Remove the first two trees – Create a new node with these trees as subtrees. Frequency is sum of their frequencies – Add the new node to the list. Continue until there is only one node on the list.

15 Huffman_Tree.cpp function Make_Huffman_code_Tree () while (node_list.size() > 1) { Huffman_tree_node* cf1 = new Huffman_tree_node(node_list.front()); node_list.pop_front(); Huffman_tree_node* cf2 = new Huffman_tree_node(node_list.front()); node_list.pop_front(); Huffman_tree_node cf3(0, cf1->Freq()+cf2->Freq(), cf1, cf2); node_list.push_back(cf3); node_list.sort(); }