CSE 143 Lecture 24 Priority Queues; Huffman Encoding

Slides:



Advertisements
Similar presentations
Lecture 4 (week 2) Source Coding and Compression
Advertisements

Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Trees Chapter 8.
Huffman Coding: An Application of Binary Trees and Priority Queues
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CSE 143 Lecture 18 Huffman slides created by Ethan Apter
Building Java Programs Priority Queues, Huffman Encoding.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
Priority Queues, Heaps & Leftist Trees
Building Java Programs
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSE Lectures 22 – Huffman codes
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
CSE 143 Lecture 22 Priority Queues; Huffman Encoding slides created by Marty Stepp, Hélène Martin, and Daniel Otero
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.
CSE 143 Lecture 23 Priority Queues and Huffman Encoding
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Huffman Coding. Huffman codes can be used to compress information –Like WinZip – although WinZip doesn’t use the Huffman algorithm –JPEGs do use Huffman.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queues, Trees, and Huffman Encoding CS 244 This presentation requires Audio Enabled Brent M. Dingle, Ph.D. Game Design and Development Program.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CSE 143 Lecture 24 Priority Queues; Huffman Encoding slides created by Marty Stepp and Daniel Otero
Building Java Programs Priority Queues, Huffman Encoding.
Main Index Contents 11 Main Index Contents Complete Binary Tree Example Complete Binary Tree Example Maximum and Minimum Heaps Example Maximum and Minimum.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
CSE 143 Lecture 22 Huffman slides created by Ethan Apter
Compression and Huffman Coding. Compression Reducing the memory required to store some information. Lossless compression vs lossy compression Lossless.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
CSE373: Data Structures & Algorithms Priority Queues
Design & Analysis of Algorithm Huffman Coding
Data Structures and Design in Java © Rick Mercer
HUFFMAN CODES.
Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21
Huffman Coding Based on slides by Ethan Apter & Marty Stepp
The Greedy Method and Text Compression
The Greedy Method and Text Compression
CSC 172– Data Structures and Algorithms
Heaps, Priority Queues, Compression
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.
Prioritization problems
Chapter 8 – Binary Search Tree
Part-D1 Priority Queues
CSE 373: Data Structures and Algorithms
Find in a linked list? first last 7  4  3  8 NULL
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Lecture 24: Priority Queues and Huffman Encoding
Priority Queues.
Ch. 8 Priority Queues And Heaps
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Huffman Coding CSE 373 Data Structures.
CSE 373 Java Collection Framework, Part 2: Priority Queue, Map
Priority Queues & Heaps
Heaps and Priority Queues
Priority Queues.
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues.
Priority Queues CSE 373 Data Structures.
Podcast Ch23d Title: Huffman Compression
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

CSE 143 Lecture 24 Priority Queues; Huffman Encoding slides adapted from Marty Stepp, Hélène Martin, and Daniel Otero http://www.cs.washington.edu/143/

Prioritization problems print jobs: The CSE lab printers constantly accept and complete jobs from all over the building. Suppose we want them to print faculty jobs before staff before student jobs, and grad students before undergraduate students, etc.? ER scheduling: You are in charge of scheduling patients for treatment in the ER. A gunshot victim should probably get treatment sooner than that one guy with a sore neck, regardless of arrival time. How do we always choose the most urgent case when new patients continue to arrive? Why can't we solve these problems efficiently with the data structures we have (list, sorted list, map, set, BST, etc.)?

Some poor choices list : store jobs in a list; remove min/max by searching (O(N)) problem: expensive to search sorted list : store in sorted list; binary search it in O(log N) time problem: expensive to add/remove (O(N)) binary search tree : store in BST, go right for max in O(log N) problem: tree becomes unbalanced

Priority queue ADT priority queue: a collection of ordered elements that provides fast access to the minimum (or maximum) element usually implemented using a tree structure called a heap priority queue operations: add adds in order; O(log N) worst peek returns minimum value; O(1) always remove removes/returns minimum value; O(log N) worst isEmpty, clear, size, iterator O(1) always

Java's PriorityQueue class public class PriorityQueue<E> implements Queue<E> Queue<String> pq = new PriorityQueue<String>(); pq.add("Helene"); pq.add("Marty"); ... Method/Constructor Description Runtime PriorityQueue<E>() constructs new empty queue O(1) add(E value) adds value in sorted order O(log N ) clear() removes all elements iterator() returns iterator over elements peek() returns minimum element remove() removes/returns min element size() number of elements in queue

Inside a priority queue Usually implemented as a heap, a kind of binary tree. Instead of sorted left  right, it's sorted top  bottom guarantee: each child is greater (lower priority) than its ancestors add/remove causes elements to "bubble" up/down the tree (take CSE 332 or 373 to learn about implementing heaps!) 10 20 80 40 60 85 90 50 99 65

Exercise: Fire the TAs We have decided that TA performance is unacceptably low. Write a class FiringSquad that reads a list of TAs from a file. Find all with  2 quarters experience, and fire/replace them. Print the final list of TAs to the console, sorted by experience. Input format: name quarters Zack 0 name quarters Sara 4 name quarters Tyler 2

Priority queue ordering For a priority queue to work, elements must have an ordering in Java, this means implementing the Comparable interface Reminder: public class Foo implements Comparable<Foo> { … public int compareTo(Foo other) { // Return positive, zero, or negative integer }

Homework 8 (Huffman Coding)

File compression compression: Process of encoding information in fewer bits. But isn't disk space cheap? Compression applies to many things: store photos without exhausting disk space reduce the size of an e-mail attachment make web pages smaller so they load faster reduce media sizes (MP3, DVD, Blu-Ray) make voice calls over a low-bandwidth connection (cell, Skype) Common compression programs: WinZip or WinRAR for Windows Stuffit Expander for Mac

ASCII encoding ASCII: Mapping from characters to integers (binary bits). Maps every possible character to a number ('A'  65) uses one byte (8 bits) for each character most text files on your computer are in ASCII format Char ASCII value ASCII (binary) ' ' 32 00100000 'a' 97 01100001 'b' 98 01100010 'c' 99 01100011 'e' 101 01100101 'z' 122 01111010

Huffman encoding Huffman encoding: Uses variable lengths for different characters to take advantage of their relative frequencies. Some characters occur more often than others. If those characters use < 8 bits each, the file will be smaller. Other characters need > 8, but that's OK; they're rare. Char ASCII value ASCII (binary) Hypothetical Huffman ' ' 32 00100000 10 'a' 97 01100001 0001 'b' 98 01100010 01110100 'c' 99 01100011 001100 'e' 101 01100101 1100 'z' 122 01111010 00100011110

Huffman's algorithm The idea: Create a "Huffman Tree" that will tell us a good binary representation for each character. Left means 0, right means 1. example: 'b' is 10 More frequent characters will be "higher" in the tree (have a shorter binary value). To build this tree, we must do a few steps first: Count occurrences of each unique character in the file. Use a priority queue to order them from least to most frequent.

What you will write HuffmanNode HuffmanTree Binary tree node (à la 20 Questions) Each storing a character and a count of its occurrences HuffmanTree Two ways to build a Huffman-based tree Output the Huffman codes to a file Decode a sequence of bits into characters

Huffman compression 1. Count the occurrences of each character in file ' '=2, 'a'=3, 'b'=3, 'c'=1, EOF=1 2. Place characters and counts into priority queue 3. Use priority queue to create Huffman tree  4. Traverse tree to find (char  binary) map {' '=00, 'a'=11, 'b'=10, 'c'=010, EOF=011}

1) Make code: “a dad cab” i 0 1 2 32 97 98 99 100 255 counts [0,0,0,...,2,..., 3, 1, 1, 2,..., 0, 0]

2) Create priority queue step 2: place characters and counts into a priority queue store a single character and its count as a Huffman node object the priority queue will organize them into ascending order

EOF We need a special character to say “STOP” Otherwise, we may read extra characters (can only write whole bytes – 8 bits – at a time) We call this the EOF – end-of-file character Add to the tree manually when we construct from the int[] counts What value will it have? Can’t represent any existing character +---+ +---+ +---+ +---+ +---+ +---+ pq --> | 1 | | 1 | | 1 | | 2 | | 2 | | 3 | b c eof ‘ ‘ d a

3) Build Huffman tree step 2: create "Huffman tree" from the node counts algorithm: Put all node counts into a priority queue. while P.Q. size > 1: Remove two rarest characters. Combine into a single node with these two as its children.

Build tree example

4) Tree to binary encodings The Huffman tree tells you the binary encodings to use. left means 0, right means 1 example: 'b' is 10 What are the binary encodings of: EOF, ' ', 'c', 'a'? What is the relationship between tree branch height, binary representation length, character frequency, etc.?

Encoding For each character in the file Determine its binary Huffman encoding Output the bits to an output file Already implemented for you Problem: how does one read and write bits?

Bit I/O streams We want to read/write one single bit at a time. Java's input/output streams read/write 1 byte (8 bits) at a time. We want to read/write one single bit at a time. BitInputStream: Reads one bit at a time from input. BitOutputStream: Writes one bit at a time to output. public BitInputStream(String file) Creates stream to read bits from given file public int readBit() Reads a single 1 or 0 public void close() Stops reading from the stream public BitOutputStream(String file) Creates stream to write bits to given file public void writeBit(int bit) Writes a single bit public void close() Stops reading from the stream

Encode (you don’t do this) a d a d c a b 32 (‘ ‘) 00 100 (d) 01 98 (b) 100 99 (c) 101 97 (a) 11

Remaking the tree 32 (‘ ‘) 00 100 (d) 01 256 (eof) 100 98 (b) 1011 99 (c) 1010 97 (a) 11

How do we decompress a file of Huffman-compressed bits? Decompressing How do we decompress a file of Huffman-compressed bits? useful "prefix property" No encoding A is the prefix of another encoding B I.e. never will have x  011 and y  011100110 the algorithm: Read each bit one at a time from the input. If the bit is 0, go left in the tree; if it is 1, go right. If you reach a leaf node, output the character at that leaf and go back to the tree root.

Decompressing Use the tree to decompress a compressed file with these bits: 1011010001101011011 Read each bit one at a time. If it is 0, go left; if 1, go right. If you reach a leaf, output the character there and go back to the tree root. Output: bac aca 1011010001101011011 b a c _ a c a

Public methods to write public HuffmanTree(int[] counts) Given character counts for a file, create Huffman tree public void write(PrintStream output) Write the character-encoding pairs to the output file public HuffanTree(Scanner input) Reconstruct the Huffman tree from a code file public void decode(BitInputStream input, PrintStream output, int eof) Use Huffman tree to decompress the input into the given output