Huffman Coding Assignment For CS211, Bellevue College (rev. 2016)

Size: px
Start display at page:

Download "Huffman Coding Assignment For CS211, Bellevue College (rev. 2016)"

Transcription

1 Huffman Coding Assignment For CS, Bellevue College (rev. ) (original from Marty Stepp, UW CSE, modified by W.P. Iverson) Summary: Huffman coding is an algorithm devised by David A. Huffman of MIT in 95 for compressing text data to make a file occupy a smaller number of bytes. This relatively simple compression algorithm is powerful enough that variations of it are still used today in computer networks, fax machines, modems, HDTV, and other areas. The basic principle is to encode characters into a specialized tree structure, with specialized nodes. The complete description of Huffman compression is below. Basically, we re building a simple binary tree, where the node data is a count of the frequency of each character in the file to be compressed. And the tree of nodes is organized with the most frequent characters near the root: public class HuffmanNode implements Comparable<HuffmanNode> { public int frequency; public char character; public HuffmanNode left; public HuffmanNode right; In order to build your Huffman Tree you need some basic methods for file I/O: public HuffmanTree(Map<Character, Integer> counts) // Constructor public StringBuilder compress(inputstream inputfile) // inputfile is a text file public StringBuilder decompress(stringbuilder inputstring) //inputstring s & s public String printsideways() // as per the method presented in Chapter 7. The Huffman Node class also needs a boolean isleaf() method, plus a static method to actually provide a count of the characters in an input file, and place those counts into a Map, with character as the unique key mapped into the integer counts of that character: public static Map<Character, Integer> getcounts(fileinputstream input) The Basic Idea: The input file to be compressed can actually be any format, but plain text will allow us to see what we re doing with simple ASCII characters. The output compressed file will a series of s and s (bits) as per the description below, which theoretically allows massive file compression. In practice, JAVA has some serious limitations here, so this assignment is only for conceptual purposes. True file compression needs to be done in a low level language. Text data is stored in a standard format of 8 bits per character, commonly using an encoding called ASCII that maps every character to a binary integer value from -55. The idea of Huffman coding is to abandon the rigid 8-bits-per-character requirement and use different-length binary encodings for different characters. The advantage of doing this is that if a character occurs frequently in the file, such as the letter 'e', it could be given a shorter encoding (fewer bits), making the file smaller. The tradeoff is that some characters may need to use encodings that are longer than 8 bits, but this is reserved for characters that occur infrequently, so that extra cost is not significant. of 5

2 The table below compares ASCII values of various characters to possible Huffman encodings for some unknown text file. The first step is to count the number of occurrences of each character in the text. Frequent characters such as space and 'e' will have short encodings, while rare characters like 'z' have longer encodings. Character ASCII value ASCII (binary) Huffman (binary) 'e' 'z' The steps involved in Huffman coding a given text source file into a destination compressed file are the following:. Examine the source file's contents and count the number of occurrences of each character.. Place each character and its frequency (count of occurrences) into a sorted "priority" queue.. Convert the contents of this priority queue into a binary tree with a particular structure.. Traverse the tree to discover the binary encodings of each character. 5. Re-examine the source file's contents, and for each character, output the encoded binary version of that character to the destination file. Encoding a File: Suppose we have a file named example.txt with the following contents ( characters, so bytes file size): ab ab cab In the original file, this text occupies bytes (8 bits) of data. The th is a special "end-of-file" () byte. byte char ASCII binary N/A In Step of Huffman's algorithm, a count of each character is computed. (i.e. the getcounts method). The counts are represented as a map: {=, =, =, =, =} // Done back in an earlier chapter on sets and maps! Step of the algorithm places these counts into binary tree nodes (Huffman Nodes), each storing a character and a count of its occurrences, with the left and right references all null (initially). These nodes are put into a priority queue (PriorityQueue<HuffmanNode>()) which keeps them in sorted order with smaller counts at the front of the queue. front back Step (, 5,...) is to repeatedly remove the front two nodes from the queue (the two with the smallest frequencies) and join them into a new node whose frequency is their sum. The two nodes are placed as children of the new node; the first removed becomes the left child, and the second the right. The new node is re-inserted into the queue in sorted order: front back 'n'a This process is repeated until the queue contains only one binary tree node with all the others as its children. This will be the root of our finished Huffman tree. Remember that each of the nodes above is the special Huffman Nodes we ve invented for this assignment. The following diagram shows this process, for our simple aba b cab text file: of 5

3 Notice that the nodes with low frequencies end up far down in the tree, and nodes with high frequencies end up near the root of the tree. This structure can be used to create an efficient encoding. The Huffman code is derived from this tree by thinking of each left branch as a bit value of and each right branch as a bit value of : The code for each character can be determined by traversing the tree. To reach we go left twice from the root, so the code for is. The code for is, the code for is, the code for is and the code for is. By traversing the tree, we can produce another map from characters to their binary representations. For this tree: {=, =, =, =, =} Using this map, we can encode the file into a shorter binary representation. The text ab ab cab would be encoded as: char binary The overall encoded contents are, which is bits, less than bytes, compared to the original file which was bytes. Modern encoding algorithms are faster and more efficient, but this is where it all started over 5 years ago, and is often been elevated into other technology ( ). To display the tree above, the printsideways from Chapter 7 of Building Java Programs will be used, but in this case we build a string to return into a graphical text area. You should actually use the StringBuilder class, as it is mutable. of 5

4 Since the character encodings have different lengths, often the length of a Huffman-encoded file does not come out to an exact multiple of 8 bits. Files are stored as sequences of whole bytes, so in cases like this the remaining digits of the last bit are filled with s. You do not need to worry about this in the assignment; it is part of the underlying file system. Decoding a File: The decompress method will take that sequence of s and s, use the Huffman tree structure, which is different for every text file (becomes like an encryption key) and recreates the text file from those bits. You can use a Huffman tree to decode text that was compressed with its encodings. The decoding algorithm is to read each bit from the file, one at a time, and use this bit to traverse the Huffman tree. If the bit is a, you move left in the tree. If the bit is, you move right. You do this until you hit a leaf node. Leaf nodes represent characters, so once you reach a leaf, you output that character. Using the Huffman tree shown below, we d walk from the root until we find character, then we output that character and go back to the root for the next character. Try this with the we just completed. Now this image tough to produce in a text only program, so let s use a format like =char(97) to indicate there are three lower case a characters in this example. And the character will simply be char() as shown below: of 5

5 Another example (DIFFERENT TEXT NOW), here s a sequence of bits in a compressed file: First we read a (right), then a (left). We reach and output b. Back to the root. We read a (right), then a (right). We reach and output a. We read a (left), then a (right), then a (left). We reach and output c. We read a (left), then a (left). We reach and output a space. We read a (right), then a (right). We reach and output a. We read a (left), then a (right), then a (left). We reach and output c. We read a (right), then a (right). We reach and output a. So the overall decoded text is bac aca. The input source reaches after character of, so we stop. I hope you noticed this is NOT the ab ab cab that we started with. It s amazing that this process really works! I think it s far easier to code if you know how and why it works. The GUI provided: To manage these data structures, I have modified the original UW assignment. Do not use solutions from UW CSE, as they will not work here. At UW, you have to save the Tree structure separately, so you can decode. In my program, the Tree structure is retained in memory, so when you decode, it is easily recalled as an existing object. When the selected input files get very large (try Moby.txt) the display will take forever (maybe minutes) to load. This is a well know problem with the Java set text methods, as they require constant refresh, rebuffer, and reload. So I provide a check box on the GUI to skip the text displays, so you can see your compression can really run in under a second even for huge files. In real compression algorithms, the bit stream would actually go out over the wire as binary data, and not be converted into character, strings, and graphics (so slow). We normally don t use System.out calls in a GUI. But I understand they are useful during debugging. You must comment out all System.out calls before submitting your work. The client program (HuffmanGUI.java) is provided, and you need to meet these specifications listed above. I expect two files to be submitted (HuffmanNode.java and HuffmanTree.java) which I will copy into my Eclipse folder and run the provided GUI for testing. 5 of 5

CSE 143, Winter 2013 Programming Assignment #8: Huffman Coding (40 points) Due Thursday, March 14, 2013, 11:30 PM

CSE 143, Winter 2013 Programming Assignment #8: Huffman Coding (40 points) Due Thursday, March 14, 2013, 11:30 PM CSE, Winter Programming Assignment #8: Huffman Coding ( points) Due Thursday, March,, : PM This program provides practice with binary trees and priority queues. Turn in files named HuffmanTree.java, secretmessage.short,

More information

CS106X Handout 29 Winter 2015 February 20 th, 2015 Assignment 5: Huffman

CS106X Handout 29 Winter 2015 February 20 th, 2015 Assignment 5: Huffman CS106X Handout 29 Winter 2015 February 20 th, 2015 Assignment 5: Huffman Thanks to Owen Astrachan (Duke) and Julie Zelenski for creating this. Modifications by Keith Schwarz, Stuart Reges, Marty Stepp.

More information

CSE143X: Computer Programming I & II Programming Assignment #10 due: Friday, 12/8/17, 11:00 pm

CSE143X: Computer Programming I & II Programming Assignment #10 due: Friday, 12/8/17, 11:00 pm CSE143X: Computer Programming I & II Programming Assignment #10 due: Friday, 12/8/17, 11:00 pm This assignment is worth a total of 30 points. It is divided into two parts, each worth approximately half

More information

15 July, Huffman Trees. Heaps

15 July, Huffman Trees. Heaps 1 Huffman Trees The Huffman Code: Huffman algorithm uses a binary tree to compress data. It is called the Huffman code, after David Huffman who discovered d it in 1952. Data compression is important in

More information

CMPSCI 240 Reasoning Under Uncertainty Homework 4

CMPSCI 240 Reasoning Under Uncertainty Homework 4 CMPSCI 240 Reasoning Under Uncertainty Homework 4 Prof. Hanna Wallach Assigned: February 24, 2012 Due: March 2, 2012 For this homework, you will be writing a program to construct a Huffman coding scheme.

More information

CSE 143 Lecture 22. Huffman Tree

CSE 143 Lecture 22. Huffman Tree CSE 4 Lecture Huffman slides created by Ethan Apter http://www.cs.washington.edu/4/ Huffman Tree For your next assignment, you ll create a Huffman tree Huffman trees are used for file compression file

More information

CS02b Project 2 String compression with Huffman trees

CS02b Project 2 String compression with Huffman trees PROJECT OVERVIEW CS02b Project 2 String compression with Huffman trees We've discussed how characters can be encoded into bits for storage in a computer. ASCII (7 8 bits per character) and Unicode (16+

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 04 / 25 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Balanced Binary Search trees AVL trees / Compression Uses binary trees Balanced

More information

Priority Queues and Huffman Encoding

Priority Queues and Huffman Encoding Priority Queues and Huffman Encoding Introduction to Homework 7 Hunter Schafer Paul G. Allen School of Computer Science - CSE 143 I Think You Have Some Priority Issues ER Scheduling. How do we efficiently

More information

Priority Queues and Huffman Encoding

Priority Queues and Huffman Encoding Priority Queues and Huffman Encoding Introduction to Homework 7 Hunter Schafer Paul G. Allen School of Computer Science - CSE 143 I Think You Have Some Priority Issues ER Scheduling. How do we efficiently

More information

Building Java Programs. Priority Queues, Huffman Encoding

Building Java Programs. Priority Queues, Huffman Encoding Building Java Programs Priority Queues, Huffman Encoding Prioritization problems ER scheduling: You are in charge of scheduling patients for treatment in the ER. A gunshot victim should probably get treatment

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2015S-P2 Huffman Codes Project 2 David Galles Department of Computer Science University of San Francisco P2-0: Text Files All files are represented as binary digits

More information

Huffman, YEAH! Sasha Harrison Spring 2018

Huffman, YEAH! Sasha Harrison Spring 2018 Huffman, YEAH! Sasha Harrison Spring 2018 Overview Brief History Lesson Step-wise Assignment Explanation Starter Files, Debunked What is Huffman Encoding? File compression scheme In text files, can we

More information

ASCII American Standard Code for Information Interchange. Text file is a sequence of binary digits which represent the codes for each character.

ASCII American Standard Code for Information Interchange. Text file is a sequence of binary digits which represent the codes for each character. Project 2 1 P2-0: Text Files All files are represented as binary digits including text files Each character is represented by an integer code ASCII American Standard Code for Information Interchange Text

More information

ASCII American Standard Code for Information Interchange. Text file is a sequence of binary digits which represent the codes for each character.

ASCII American Standard Code for Information Interchange. Text file is a sequence of binary digits which represent the codes for each character. Project 2 1 P2-0: Text Files All files are represented as binary digits including text files Each character is represented by an integer code ASCII American Standard Code for Information Interchange Text

More information

COSC-211: DATA STRUCTURES HW5: HUFFMAN CODING. 1 Introduction. 2 Huffman Coding. Due Thursday, March 8, 11:59pm

COSC-211: DATA STRUCTURES HW5: HUFFMAN CODING. 1 Introduction. 2 Huffman Coding. Due Thursday, March 8, 11:59pm COSC-211: DATA STRUCTURES HW5: HUFFMAN CODING Due Thursday, March 8, 11:59pm Reminder regarding intellectual responsibility: This is an individual assignment, and the work you submit should be your own.

More information

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015 BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 10 is due on Thursday -midterm grades out tomorrow 3 last time 4 -a hash table is a general storage

More information

CIS 121 Data Structures and Algorithms with Java Spring 2018

CIS 121 Data Structures and Algorithms with Java Spring 2018 CIS 121 Data Structures and Algorithms with Java Spring 2018 Homework 6 Compression Due: Monday, March 12, 11:59pm online 2 Required Problems (45 points), Qualitative Questions (10 points), and Style and

More information

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials) CSE100 Advanced Data Structures Lecture 12 (Based on Paul Kube course materials) CSE 100 Coding and decoding with a Huffman coding tree Huffman coding tree implementation issues Priority queues and priority

More information

Digital Image Processing

Digital Image Processing Digital Image Processing Image Compression Caution: The PDF version of this presentation will appear to have errors due to heavy use of animations Material in this presentation is largely based on/derived

More information

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices. Trees Trees form the most widely used subclasses of graphs. In CS, we make extensive use of trees. Trees are useful in organizing and relating data in databases, file systems and other applications. Formal

More information

CS 200 Algorithms and Data Structures, Fall 2012 Programming Assignment #3

CS 200 Algorithms and Data Structures, Fall 2012 Programming Assignment #3 Compressing Data using Huffman Coding Due Oct.24 noon Objectives In this assignment, you will implement classes for data compression. You will write: () An implementation of the Huffman Coding using a

More information

Assignment 6: Huffman Encoding

Assignment 6: Huffman Encoding Assignment 6: Huffman Encoding Thanks to Owen Astrachan (Duke) and Julie Zelenski. Updates by Keith Schwarz, Stuart Reges, Marty Stepp, Chris Piech,, Chris Gregg, Marissa Gemma, and Brahm Capoor. Due:

More information

CS106B Handout 34 Autumn 2012 November 12 th, 2012 Data Compression and Huffman Encoding

CS106B Handout 34 Autumn 2012 November 12 th, 2012 Data Compression and Huffman Encoding CS6B Handout 34 Autumn 22 November 2 th, 22 Data Compression and Huffman Encoding Handout written by Julie Zelenski. In the early 98s, personal computers had hard disks that were no larger than MB; today,

More information

CMPSC112 Lecture 37: Data Compression. Prof. John Wenskovitch 04/28/2017

CMPSC112 Lecture 37: Data Compression. Prof. John Wenskovitch 04/28/2017 CMPSC112 Lecture 37: Data Compression Prof. John Wenskovitch 04/28/2017 What You Don t Get to Learn Self-balancing search trees: https://goo.gl/houquf https://goo.gl/r4osz2 Shell sort: https://goo.gl/awy3pk

More information

Text Compression through Huffman Coding. Terminology

Text Compression through Huffman Coding. Terminology Text Compression through Huffman Coding Huffman codes represent a very effective technique for compressing data; they usually produce savings between 20% 90% Preliminary example We are given a 100,000-character

More information

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms Efficient Sequential Algorithms, Comp39 Part 3. String Algorithms University of Liverpool References: T. H. Cormen, C. E. Leiserson, R. L. Rivest Introduction to Algorithms, Second Edition. MIT Press (21).

More information

Greedy Algorithms CHAPTER 16

Greedy Algorithms CHAPTER 16 CHAPTER 16 Greedy Algorithms In dynamic programming, the optimal solution is described in a recursive manner, and then is computed ``bottom up''. Dynamic programming is a powerful technique, but it often

More information

CS15100 Lab 7: File compression

CS15100 Lab 7: File compression C151 Lab 7: File compression Fall 26 November 14, 26 Complete the first 3 chapters (through the build-huffman-tree function) in lab (optionally) with a partner. The rest you must do by yourself. Write

More information

Data compression.

Data compression. Data compression anhtt-fit@mail.hut.edu.vn dungct@it-hut.edu.vn Data Compression Data in memory have used fixed length for representation For data transfer (in particular), this method is inefficient.

More information

More Bits and Bytes Huffman Coding

More Bits and Bytes Huffman Coding More Bits and Bytes Huffman Coding Encoding Text: How is it done? ASCII, UTF, Huffman algorithm ASCII C A T Lawrence Snyder, CSE UTF-8: All the alphabets in the world Uniform Transformation Format: a variable-width

More information

A New Compression Method Strictly for English Textual Data

A New Compression Method Strictly for English Textual Data A New Compression Method Strictly for English Textual Data Sabina Priyadarshini Department of Computer Science and Engineering Birla Institute of Technology Abstract - Data compression is a requirement

More information

Information Science 2

Information Science 2 Information Science 2 - Path Lengths and Huffman s Algorithm- Week 06 College of Information Science and Engineering Ritsumeikan University Agenda l Review of Weeks 03-05 l Tree traversals and notations

More information

Horn Formulae. CS124 Course Notes 8 Spring 2018

Horn Formulae. CS124 Course Notes 8 Spring 2018 CS124 Course Notes 8 Spring 2018 In today s lecture we will be looking a bit more closely at the Greedy approach to designing algorithms. As we will see, sometimes it works, and sometimes even when it

More information

CS201 Discussion 12 HUFFMAN + ERDOSNUMBERS

CS201 Discussion 12 HUFFMAN + ERDOSNUMBERS CS0 Discussion HUFFMAN + ERDOSNUMBERS Huffman Compression Today, we ll walk through an example of Huffman compression. In Huffman compression, there are three steps:. Create a Huffman tree. Get all the

More information

EE 368. Weeks 5 (Notes)

EE 368. Weeks 5 (Notes) EE 368 Weeks 5 (Notes) 1 Chapter 5: Trees Skip pages 273-281, Section 5.6 - If A is the root of a tree and B is the root of a subtree of that tree, then A is B s parent (or father or mother) and B is A

More information

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday) CS 215 Fundamentals of Programming II Spring 2017 Programming Project 7 30 points Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday) This project

More information

Programming Abstractions

Programming Abstractions Programming Abstractions C S 1 0 6 X Cynthia Lee Topics: Today we re going to be talking about your next assignment: Huffman coding It s a compression algorithm It s provably optimal (take that, Pied Piper)

More information

14.4 Description of Huffman Coding

14.4 Description of Huffman Coding Mastering Algorithms with C By Kyle Loudon Slots : 1 Table of Contents Chapter 14. Data Compression Content 14.4 Description of Huffman Coding One of the oldest and most elegant forms of data compression

More information

Lossless Compression Algorithms

Lossless Compression Algorithms Multimedia Data Compression Part I Chapter 7 Lossless Compression Algorithms 1 Chapter 7 Lossless Compression Algorithms 1. Introduction 2. Basics of Information Theory 3. Lossless Compression Algorithms

More information

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017 CS6 Lecture 4 Greedy Algorithms Scribe: Virginia Williams, Sam Kim (26), Mary Wootters (27) Date: May 22, 27 Greedy Algorithms Suppose we want to solve a problem, and we re able to come up with some recursive

More information

15-122: Principles of Imperative Computation, Spring 2013

15-122: Principles of Imperative Computation, Spring 2013 15-122 Homework 6 Page 1 of 13 15-122: Principles of Imperative Computation, Spring 2013 Homework 6 Programming: Huffmanlab Due: Thursday, April 4, 2013 by 23:59 For the programming portion of this week

More information

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees. So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees. According to the 161 schedule, heaps were last week, hashing

More information

14 Data Compression by Huffman Encoding

14 Data Compression by Huffman Encoding 4 Data Compression by Huffman Encoding 4. Introduction In order to save on disk storage space, it is useful to be able to compress files (or memory blocks) of data so that they take up less room. However,

More information

Algorithms and Data Structures CS-CO-412

Algorithms and Data Structures CS-CO-412 Algorithms and Data Structures CS-CO-412 David Vernon Professor of Informatics University of Skövde Sweden david@vernon.eu www.vernon.eu Algorithms and Data Structures 1 Copyright D. Vernon 2014 Trees

More information

CS 350 : Data Structures B-Trees

CS 350 : Data Structures B-Trees CS 350 : Data Structures B-Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction All of the data structures that we ve

More information

Graduate-Credit Programming Project

Graduate-Credit Programming Project Graduate-Credit Programming Project Due by 11:59 p.m. on December 14 Overview For this project, you will: develop the data structures associated with Huffman encoding use these data structures and the

More information

CSE100. Advanced Data Structures. Lecture 13. (Based on Paul Kube course materials)

CSE100. Advanced Data Structures. Lecture 13. (Based on Paul Kube course materials) CSE100 Advanced Data Structures Lecture 13 (Based on Paul Kube course materials) CSE 100 Priority Queues in Huffman s algorithm Heaps and Priority Queues Time and space costs of coding with Huffman codes

More information

CS-301 Data Structure. Tariq Hanif

CS-301 Data Structure. Tariq Hanif 1. The tree data structure is a Linear data structure Non-linear data structure Graphical data structure Data structure like queue FINALTERM EXAMINATION Spring 2012 CS301- Data Structure 25-07-2012 2.

More information

Basic data types. Building blocks of computation

Basic data types. Building blocks of computation Basic data types Building blocks of computation Goals By the end of this lesson you will be able to: Understand the commonly used basic data types of C++ including Characters Integers Floating-point values

More information

Administrative. Distributed indexing. Index Compression! What I did last summer lunch talks today. Master. Tasks

Administrative. Distributed indexing. Index Compression! What I did last summer lunch talks today. Master. Tasks Administrative Index Compression! n Assignment 1? n Homework 2 out n What I did last summer lunch talks today David Kauchak cs458 Fall 2012 adapted from: http://www.stanford.edu/class/cs276/handouts/lecture5-indexcompression.ppt

More information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree Chapter 4 Trees 2 Introduction for large input, even access time may be prohibitive we need data structures that exhibit running times closer to O(log N) binary search tree 3 Terminology recursive definition

More information

An Overview 1 / 10. CS106B Winter Handout #21 March 3, 2017 Huffman Encoding and Data Compression

An Overview 1 / 10. CS106B Winter Handout #21 March 3, 2017 Huffman Encoding and Data Compression CS106B Winter 2017 Handout #21 March 3, 2017 Huffman Encoding and Data Compression Handout by Julie Zelenski with minor edits by Keith Schwarz In the early 1980s, personal computers had hard disks that

More information

Algorithms Dr. Haim Levkowitz

Algorithms Dr. Haim Levkowitz 91.503 Algorithms Dr. Haim Levkowitz Fall 2007 Lecture 4 Tuesday, 25 Sep 2007 Design Patterns for Optimization Problems Greedy Algorithms 1 Greedy Algorithms 2 What is Greedy Algorithm? Similar to dynamic

More information

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, 2016 Huffman Codes CLRS: Ch 16.3 Ziv-Lempel is the most popular compression algorithm today.

More information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in

More information

Huffman Codes (data compression)

Huffman Codes (data compression) Huffman Codes (data compression) Data compression is an important technique for saving storage Given a file, We can consider it as a string of characters We want to find a compressed file The compressed

More information

Encoding. A thesis submitted to the Graduate School of University of Cincinnati in

Encoding. A thesis submitted to the Graduate School of University of Cincinnati in Lossless Data Compression for Security Purposes Using Huffman Encoding A thesis submitted to the Graduate School of University of Cincinnati in a partial fulfillment of requirements for the degree of Master

More information

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018 COMP 250 Fall 2018 26 - priority queues, heaps 1 Nov. 9, 2018 Priority Queue Recall the definition of a queue. It is a collection where we remove the element that has been in the collection for the longest

More information

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018 1 CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018 Shortest Paths The discussion in these notes captures the essence of Dijkstra s algorithm discussed in textbook

More information

Binary Trees and Huffman Encoding Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees Binary Trees and Huffman Encoding Binary Search Trees Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. Motivation: Maintaining a Sorted Collection of Data A data dictionary is a

More information

Homework 3 Huffman Coding. Due Thursday October 11

Homework 3 Huffman Coding. Due Thursday October 11 Homework 3 Huffman Coding Due Thursday October 11 Huffman Coding Implement Huffman Encoding and Decoding and the classes shown on the following slides. You will also need to use Java s stack class HuffmanEncode

More information

CSE 2123 Recursion. Jeremy Morris

CSE 2123 Recursion. Jeremy Morris CSE 2123 Recursion Jeremy Morris 1 Past Few Weeks For the past few weeks we have been focusing on data structures Classes & Object-oriented programming Collections Lists, Sets, Maps, etc. Now we turn our

More information

Red-Black, Splay and Huffman Trees

Red-Black, Splay and Huffman Trees Red-Black, Splay and Huffman Trees Kuan-Yu Chen ( 陳冠宇 ) 2018/10/22 @ TR-212, NTUST AVL Trees Review Self-balancing binary search tree Balance Factor Every node has a balance factor of 1, 0, or 1 2 Red-Black

More information

6. Finding Efficient Compressions; Huffman and Hu-Tucker

6. Finding Efficient Compressions; Huffman and Hu-Tucker 6. Finding Efficient Compressions; Huffman and Hu-Tucker We now address the question: how do we find a code that uses the frequency information about k length patterns efficiently to shorten our message?

More information

Analysis of Algorithms

Analysis of Algorithms Algorithm An algorithm is a procedure or formula for solving a problem, based on conducting a sequence of specified actions. A computer program can be viewed as an elaborate algorithm. In mathematics and

More information

Balanced Search Trees

Balanced Search Trees Balanced Search Trees Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. Review: Balanced Trees A tree is balanced if, for each node, the node s subtrees have the same height or have

More information

4/16/2012. Data Compression. Exhaustive search, backtracking, object-oriented Queens. Check out from SVN: Queens Huffman-Bailey.

4/16/2012. Data Compression. Exhaustive search, backtracking, object-oriented Queens. Check out from SVN: Queens Huffman-Bailey. Data Compression Exhaustive search, backtracking, object-oriented Queens Check out from SVN: Queens Huffman-Bailey Bailey-JFC 1 Teams for EditorTrees project Greedy Algorithms Data Compression Huffman's

More information

Greedy Algorithms and Huffman Coding

Greedy Algorithms and Huffman Coding Greedy Algorithms and Huffman Coding Henry Z. Lo June 10, 2014 1 Greedy Algorithms 1.1 Change making problem Problem 1. You have quarters, dimes, nickels, and pennies. amount, n, provide the least number

More information

UNIT III BALANCED SEARCH TREES AND INDEXING

UNIT III BALANCED SEARCH TREES AND INDEXING UNIT III BALANCED SEARCH TREES AND INDEXING OBJECTIVE The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions and finds in constant

More information

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills CSCI 136 Data Structures & Advanced Programming Lecture 22 Fall 2018 Instructor: Bills Last Time Lab 7: Two Towers Array Representations of (Binary) Trees Application: Huffman Encoding 2 Today Improving

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

B-Trees. Introduction. Definitions

B-Trees. Introduction. Definitions 1 of 10 B-Trees Introduction A B-tree is a specialized multiway tree designed especially for use on disk. In a B-tree each node may contain a large number of keys. The number of subtrees of each node,

More information

Heaps and Priority Queues

Heaps and Priority Queues CpSc2120 Goddard Notes Chapter 15 Heaps and Priority Queues 15.1 Priority Queue The (min)-priority queue ADT supports: insertitem(e): Insert new item e. removemin(): Remove and return item with minimum

More information

Bits, Words, and Integers

Bits, Words, and Integers Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

More information

Analysis of Algorithms - Greedy algorithms -

Analysis of Algorithms - Greedy algorithms - Analysis of Algorithms - Greedy algorithms - Andreas Ermedahl MRTC (Mälardalens Real-Time Reseach Center) andreas.ermedahl@mdh.se Autumn 2003 Greedy Algorithms Another paradigm for designing algorithms

More information

Binary Trees Case-studies

Binary Trees Case-studies Carlos Moreno cmoreno @ uwaterloo.ca EIT-4103 https://ece.uwaterloo.ca/~cmoreno/ece250 Standard reminder to set phones to silent/vibrate mode, please! Today's class: Binary Trees Case-studies We'll look

More information

CS350: Data Structures B-Trees

CS350: Data Structures B-Trees B-Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction All of the data structures that we ve looked at thus far have been memory-based

More information

Compression. storage medium/ communications network. For the purpose of this lecture, we observe the following constraints:

Compression. storage medium/ communications network. For the purpose of this lecture, we observe the following constraints: CS231 Algorithms Handout # 31 Prof. Lyn Turbak November 20, 2001 Wellesley College Compression The Big Picture We want to be able to store and retrieve data, as well as communicate it with others. In general,

More information

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees Reading materials Dale, Joyce, Weems: 9.1, 9.2, 8.8 Liang: 26 (comprehensive edition) OpenDSA:

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019 CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues Ruth Anderson Winter 201 Today Finish up Intro to Asymptotic Analysis New ADT! Priority Queues 1/11/201 2 Scenario What is the difference

More information

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees Linked representation of binary tree Again, as with linked list, entire tree can be represented with a single pointer -- in this

More information

CSE 2123: Collections: Priority Queues. Jeremy Morris

CSE 2123: Collections: Priority Queues. Jeremy Morris CSE 2123: Collections: Priority Queues Jeremy Morris 1 Collections Priority Queue Recall: A queue is a specific type of collection Keeps elements in a particular order We ve seen two examples FIFO queues

More information

Priority Queues. Chapter 9

Priority Queues. Chapter 9 Chapter 9 Priority Queues Sometimes, we need to line up things according to their priorities. Order of deletion from such a structure is determined by the priority of the elements. For example, when assigning

More information

Greedy algorithms part 2, and Huffman code

Greedy algorithms part 2, and Huffman code Greedy algorithms part 2, and Huffman code Two main properties: 1. Greedy choice property: At each decision point, make the choice that is best at the moment. We typically show that if we make a greedy

More information

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab ===Lab Info=== *100 points * Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab ==Assignment== In this assignment you will work on designing a class for a binary search tree. You

More information

CIS 121 Data Structures and Algorithms with Java Spring 2018

CIS 121 Data Structures and Algorithms with Java Spring 2018 CIS 121 Data Structures and Algorithms with Java Spring 2018 Homework 2 Thursday, January 18 Due Monday, January 29 by 11:59 PM 7 Required Problems (85 points), and Style and Tests (15 points) DO NOT modify

More information

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi. Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 18 Tries Today we are going to be talking about another data

More information

CSE wi: Practice Midterm

CSE wi: Practice Midterm CSE 373 18wi: Practice Midterm Name: UW email address: Instructions Do not start the exam until told to do so. You have 80 minutes to complete the exam. This exam is closed book and closed notes. You may

More information

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100 DUKE UNIVERSITY Department of Computer Science Test 2: CompSci 100 Name (print): Community Standard acknowledgment (signature): Problem 1 value 14 pts. grade Problem 2 9 pts. Problem 3 7 pts. Problem 4

More information

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we have to talk about the way in which we represent the

More information

COS 226 Midterm Exam, Spring 2009

COS 226 Midterm Exam, Spring 2009 NAME: login ID: precept: COS 226 Midterm Exam, Spring 2009 This test is 10 questions, weighted as indicated. The exam is closed book, except that you are allowed to use a one page cheatsheet. No calculators

More information

ENSC Multimedia Communications Engineering Topic 4: Huffman Coding 2

ENSC Multimedia Communications Engineering Topic 4: Huffman Coding 2 ENSC 424 - Multimedia Communications Engineering Topic 4: Huffman Coding 2 Jie Liang Engineering Science Simon Fraser University JieL@sfu.ca J. Liang: SFU ENSC 424 1 Outline Canonical Huffman code Huffman

More information

Lecture: Analysis of Algorithms (CS )

Lecture: Analysis of Algorithms (CS ) Lecture: Analysis of Algorithms (CS483-001) Amarda Shehu Spring 2017 1 The Fractional Knapsack Problem Huffman Coding 2 Sample Problems to Illustrate The Fractional Knapsack Problem Variable-length (Huffman)

More information

The type of all data used in a C++ program must be specified

The type of all data used in a C++ program must be specified The type of all data used in a C++ program must be specified A data type is a description of the data being represented That is, a set of possible values and a set of operations on those values There are

More information

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CSC 373 Lecture # 3 Instructor: Milad Eftekhar Huffman encoding: Assume a context is available (a document, a signal, etc.). These contexts are formed by some symbols (words in a document, discrete samples from a signal, etc). Each symbols s i is occurred

More information