Huffman Codes Let A1,...,An be a set of items.

Slides:



Advertisements
Similar presentations
PROOF BY CONTRADICTION
Advertisements

Introduction to Computer Science 2 Lecture 7: Extended binary trees
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
CompSci 102 Discrete Math for Computer Science April 19, 2012 Prof. Rodger Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily.
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Data Structures – LECTURE 10 Huffman coding
1 More Applications of the Pumping Lemma. 2 The Pumping Lemma: Given a infinite regular language there exists an integer for any string with length we.
Chapter 9: Huffman Codes
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Greedy Algorithms Huffman Coding
CS420 lecture eight Greedy Algorithms. Going from A to G Starting with a full tank, we can drive 350 miles before we need to gas up, minimize the number.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
Mathematical Induction. F(1) = 1; F(n+1) = F(n) + (2n+1) for n≥ F(n) n F(n) =n 2 for all n ≥ 1 Prove it!
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.
Data Structures Week 6: Assignment #2 Problem
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
Weight balance trees (Nievergelt & Reingold 73)
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
 Rooted tree and binary tree  Theorem 5.19: A full binary tree with t leaves contains i=t-1 internal vertices.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
5.2 Trees  A tree is a connected graph without any cycles.
1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.
5.5.3 Rooted tree and binary tree  Definition 25: A directed graph is a directed tree if the graph is a tree in the underlying undirected graph.  Definition.
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
Foundation of Computing Systems
Bahareh Sarrafzadeh 6111 Fall 2009
The bin packing problem. For n objects with sizes s 1, …, s n where 0 < s i ≤1, find the smallest number of bins with capacity one, such that n objects.
Huffman Codes. Overview  Huffman codes: compressing data (savings of 20% to 90%)  Huffman’s greedy algorithm uses a table of the frequencies of occurrence.
1 Chapter 16: Greedy Algorithm. 2 About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm.
Great Theoretical Ideas in Computer Science for Some.
COMPSCI 102 Introduction to Discrete Mathematics.
5.6 Prefix codes and optimal tree Definition 31: Codes with this property which the bit string for a letter never occurs as the first part of the bit string.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
ENTROPY Entropy measures the uncertainty in a random experiment. Let X be a discrete random variable with range S X = { 1,2,3,... k} and pmf p k = P X.
Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.
Communication & Entropy
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
We consider the problem of optimally arranging files on a tape
Applied Algorithmics - week7
Proving the Correctness of Huffman’s Algorithm
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Chapter 9: Huffman Codes
Chapter 16: Greedy Algorithms
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Algorithms (2IL15) – Lecture 2
Discrete Mathematics and its Applications
Chapter 11 Data Compression
PROOF BY CONTRADICTION
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
The Lower Bounds of Problems
Greedy Algorithms Alexandra Stefan.
Chapter 16: Greedy algorithms Ming-Te Chi
More Applications of the Pumping Lemma
Discrete Mathematics for Computer Science
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Lecture 2: Greedy Algorithms
Given: the cost of two items is more than $50.
CSE 589 Applied Algorithms Spring 1999
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Proving the Correctness of Huffman’s Algorithm
Analysis of Algorithms CS 477/677
Presentation transcript:

Huffman Codes Let A1,...,An be a set of items. Let P1,...,Pn be their probabilities. We want to find a set of lengths L1,...,Ln that will produce a minimal 2. 1

Huffman's algorithm If (n==2) then {0,1} Else combine 2 smallest probabilities Pn,Pn-1 solve for P1,P2,...,Pn-2,Pn-1+Pn if Pn-1+Pn is represented by  then Pn-1 will be represented by 0 Pn will be represented by 1 2. 2

Example of Huffman coding In a given language: A - 20%, B - 10%, C - 10%, D - 30%, E - 30% 11 E(30) 10 D(30) 00 A(20) 011 C(10) 010 B(10) E(30) D(30) A(20) CB(20) CBA(40) E(30) D(30) ED(60) CBA(40) E D C B A 1 2. 3

Implementation Sorting is O(n log n). Finding where to insert a new item is O(n). There are n-2 items to find, so the total order is O(n2). Total order is O(nlogn)+O(n2) i.e. O(n2). 2. 4

Improved implementation Two queues. false true a1>b2 a2 a1 a3 a4 b2 b1 b3 b4 b1>a2 b1,b2 true false a1,b1 a1,a2 2. 5

Improved implementation (cont.) Sort all items into queue "a". Queue "b" is empty. While there are values in the queues, Put sum of 2 low values in queue "b". The order of the numbers created by the additions are in non-decreasing order. The algorithm is O(nlogn)+O(n) i.e. O(nlogn) 2. 6

Huffman is optimal An optimal tree is a tree for which is minimal Lemmas: In an Optimal tree: The tree is full so at least 2 nodes on lowest level. 2 nodes with lowest weights are on lowest level. 2 lowest weights can be on brother nodes. 2. 7

Theorem Let T1 be an optimal tree with probabilities P1,...,Pn. The two lowest probabilities Pn,Pn-1 are on lowest level and they are brothers. Let  be the father of Pn,Pn-1. Let T2 be the same tree as T1 without Pn,Pn-1 but with  that has a probability of Pn+Pn-1 Theorem: T2 is an optimal tree. 2. 8

Theorem (cont.) T1 T2  Pn+Pn-1 Pn Pn-1 2. 9

Theorem - proof Let us denote M1= . M1=P1L1+...+Pn-1Ln-1+PnLn-1 M2=M1-(Pn-1Ln-1+PnLn-1)+(Pn-1+Pn)(Ln-1-1) =M1-Pn-1-Pn Brothers have the same level Probability of  2. 10

Theorem - proof (cont.) Suppose T2 is not Optimal. There is a tree T3T2 which is optimal. M3<M2. There is a leaf  in T3 which has a probability of Pn-1+Pn. Let T4 be a tree with same nodes as T3 but instead of  it has two leaves: Pn-1, Pn 2. 11

Theorem - proof (cont.) T3 T4   Pn Pn-1 2. 12

Theorem - proof (end) T2 is an optimal tree. T4 is a tree for the probablities P1,...,Pn. M4=M3+(Pn-1Ln-1+PnLn-1)-(Pn-1+Pn)(Ln-1-1) =M3+Pn-1+Pn M4=M3+Pn-1+Pn<M2+Pn-1+Pn=M1 M4<M1 But M1 is an optimal tree. contradiction! T2 is an optimal tree. 2. 13

Theorem Let T be an optimal tree for any n leaves. T is equivalent to a Huffman tree with same leaves. i.e. they both have same . 2. 14

Theorem - Proof Induction on number of leaves. for n=2 there is just one option so the trees are equivalent. P1 P2 2. 15

Theorem proof (end) Let us assume for n-1 and prove for n. Let T1 be an optimal tree. According to previous theorem T2 is optimal too. According to induction's assumption T2 which has n-1 leaves, is equivalent to a Huffman tree. T1 was built according to Huffman's algorithm so T1 is equivalent to a Huffman Tree too. 2. 16

Optimal trees Huffman trees are optimal as proved but there are other trees which are optimal but not Huffman. Example: A - 20%, B - 10%, C - 10%, D - 30%, E - 30% E A C B D 1 2. 17

Results of Huffman Examples for text files: 2. 18