Download presentation
Presentation is loading. Please wait.
1
Heaps, Priority Queues, Compression
Compression is a high-profile application .zip, .mp3, .jpg, .gif, .gz, … What property of MP3 was a significant factor in what made Napster work (why did Napster ultimately fail?) What’s the difference between compression for .mp3 files and compression for .zip files? Between .gif and .jpg? What’s the source, what’s the destination? What is lossy vs. lossless compression? Are the differences important? Is it possible to compress (lossless compression rather than lossy) every file? Every file of a given size? What are repercussions?
2
Priority Queue Compression motivates the study of the ADT priority queue Supports three basic operations insert -– an element into the priority queue removeMin – the minimal element from the priority queue peekMin– find (don’t delete) minimal element getMin/delete analagous: stack top/pop, queue enqueue/dequeue Simple sorting using priority queue (see PQdemo.java), what is the complexity of this sorting method? void sort(ArrayList list) { int k; PriorityQueue pq = new ArrayPriorityQueue(); int size = list.size(); for(k=0; k < size; k++) pq.add(list.get(k)); for(k=0; k < size; k++) list.set(k,pq.removeMin()); }
3
Priority Queue implementations
Implementing priority queues: average and worst case Insert average remove (delete) worst Unsorted array Sorted array Search tree Balanced tree Heap O(n) O(1) O(n) O(1) log n O(n) log n O(1) log n Heap has O(1) peekMin (no delete) and O(n) build heap
4
Priority Queue implementation
The interface PriorityQueue is AP specific, not java.util Implementations provided: ArrayPriorityQueue What are complexities? Easy to implement? Could use TreeSet, see Set API for method first() How do we implement removeMin using peekMin? In general, elements must implement Comparable Changing method of comparison when calculating priority? Could provide Comparator in PriorityQueue Could wrap Comparable objects in other Comparable How do implement ReverseComparable?
5
Reverse Comparable: Decorator
class ReverseComparable implements Comparable { private Comparable myObject; public ReverseComparable(Comparable c) myObject = c ; } public int compareTo(Object o) ReverseComparable c = (ReverseComparable) o; return -1 * myObject.compareTo(c.getObject()); public Comparable getObject() return myObject;
6
Creating Heaps Heap is an array-based implementation of a binary tree used for implementing priority queues, supports: insert, findmin, deletemin: complexities? Using array minimizes storage (no explicit pointers), faster too --- children are located by index/position in array Heap is a binary tree with shape property, heap/value property shape: tree filled at all levels (except perhaps last) and filled left-to-right (complete binary tree) each node has value smaller than both children
7
Array-based heap store “node values” in array beginning at index 1
for node with index k left child: index 2*k right child: index 2*k+1 why is this conducive for maintaining heap shape? what about heap property? is the heap a search tree? where is minimal node? where are nodes added? deleted? 1 2 3 4 5 6 7 8 9 10 17 13 25 21 19 6 10 7 17 13 9 21 19 25
8
Thinking about heaps Where is minimal element? Root, why?
Where is maximal element? Leaves, why? How many leaves are there in an N-node heap (big-Oh)? O(n), but exact? What is complexity of find max in a minheap? Why? O(n), but ½ N? Where is second smallest element? Why? Near root? 6 10 7 17 13 9 21 19 25 1 2 3 4 5 6 7 8 9 10 17 13 25 21 19
9
Towards Compression Each ASCII character is represented by 8 bits, one byte bit is a binary digit, byte is a binary term compress text: use fewer bits for frequent characters (does this come free?) 256 character values, 28 = 256, how many bits needed for 7 characters? for 38 characters? for 125 characters? go go gophers: 8 different characters ASCII bits g o p h e r s sp ASCII: 13 x 8 = 104 bits 3 bit code: 13 x 3 = 39 bits compressed: ???
10
Huffman coding: go go gophers
ASCII bits Huffman g ?? o ?? p h e r s sp choose two smallest weights combine nodes + weights Repeat Priority queue? Encoding uses tree: 0 left/1 right How many bits? g o p h 1 e r s * 3 3 1 1 1 1 2 2 p 1 h 2 e 1 r 3 s 1 * 2 2 p 1 h e r 4 g 3 o 6
11
Huffman coding: go go gophers
ASCII bits Huffman g o p h e r s sp Encoding uses tree: 0 left/1 right How many bits? 37!! Savings? Worth it? g 3 o 6 13 3 2 p 1 h e r 4 s * 7 g 3 o 6 2 p 1 h e r 4 3 s 1 * 2
12
Properties of Huffman code
Prefix property, no code is prefix of another code optimal per character compression Where do frequencies come from? decode: need tree a t r s e *
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.