Trees1 Trees and Codes a bc de 000100111011 abcde.

Slides:



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

Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Huffman code and ID3 Prof. Sin-Min Lee Department of Computer Science.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Huffman Encoding Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 10 : Huffman Encoding Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University Lecture notes : courtesy.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
Red-Black Trees 4/16/2017 8:38 AM Splay Trees v z Splay Trees.
Lecture04 Data Compression.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Huffman Encoding 16-Apr-17.
CSC401 – Analysis of Algorithms Lecture Notes 7 Multi-way Search Trees and Skip Lists Objectives: Introduce multi-way search trees especially (2,4) trees,
© 2004 Goodrich, Tamassia, Dickerson Splay Trees v z.
© 2004 Goodrich, Tamassia Greedy Method and Compression1 The Greedy Method and Text Compression.
A Data Compression Algorithm: Huffman Compression
DL Compression – Beeri/Feitelson1 Compression דחיסה Introduction Information theory Text compression IL compression.
6/26/2015 7:13 PMTries1. 6/26/2015 7:13 PMTries2 Outline and Reading Standard tries (§9.2.1) Compressed tries (§9.2.2) Suffix tries (§9.2.3) Huffman encoding.
Chapter 9: Huffman Codes
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
Lecture 4 Source Coding and Compression Dr.-Ing. Khaled Shawky Hassan
Fundamentals of Multimedia Chapter 7 Lossless Compression Algorithms Ze-Nian Li and Mark S. Drew 건국대학교 인터넷미디어공학부 임 창 훈.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
1 Lossless Compression Multimedia Systems (Module 2) r Lesson 1: m Minimum Redundancy Coding based on Information Theory: Shannon-Fano Coding Huffman Coding.
x x x 1 =613 Base 10 digits {0...9} Base 10 digits {0...9}
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
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.
© 2004 Goodrich, Tamassia Tries1. © 2004 Goodrich, Tamassia Tries2 Preprocessing Strings Preprocessing the pattern speeds up pattern matching queries.
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
Communication Technology in a Changing World Week 2.
1 Source Coding and Compression Dr.-Ing. Khaled Shawky Hassan Room: C3-222, ext: 1204, Lecture 5.
ICS 220 – Data Structures and Algorithms Lecture 11 Dr. Ken Cosh.
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 13.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
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.
1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.
Huffman coding Content 1 Encoding and decoding messages Fixed-length coding Variable-length coding 2 Huffman coding.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
Bahareh Sarrafzadeh 6111 Fall 2009
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Lossless Decomposition and Huffman Codes Sophia Soohoo CS 157B.
1 Huffman Codes. 2 ASCII use same size encoding for all characters. Variable length codes can produce shorter messages than fixed length codes Huffman.
Huffman code and Lossless Decomposition Prof. Sin-Min Lee Department of Computer Science.
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
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.
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
Tries 07/28/16 11:04 Text Compression
Algorithms for iSNE Dr. Kenneth Cosh Week 13.
ISNE101 – Introduction to Information Systems and Network Engineering
Chapter 8 – Binary Search Tree
Chapter 9: Huffman Codes
Advanced Algorithms Analysis and Design
Huffman Coding CSE 373 Data Structures.
Communication Technology in a Changing World
Communication Technology in a Changing World
Data Structures and Algorithms
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
Trees Addenda.
Data Structure and Algorithms
Greedy Algorithms Alexandra Stefan.
Huffman Encoding.
Red-black tree properties
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Huffman Coding Greedy Algorithm
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Presentation transcript:

Trees1 Trees and Codes a bc de abcde

Trees2 Binary Trees and Prefix Codes Imagine that you have an alphabet of symbols e.g. A, B, …, Z, a, b, …, z, ‘ ’, ‘.’ We wish to represent a string of these symbols as a string of bits e.g. “This is a string of characters” becomes “ ”

Trees3 Method 1: Use a fixed number for each symbol Map A -> 1, B -> 2, … “This is a string of characters” becomes a string of numbers “20,34,35,…” I required commas to separate the characters! Use a fixed width, padded with leading 0’s instead “ …”

Trees4 Method 1: Fixed width How wide do my characters need to be Using a string of bits How many bits are needed to represent the largest character? ceil(log 2 (n)) bits Use that many bits for each character This is the system used within the computer with 8 bits for each ASCII code

Trees5 Method II: Prefix codes For each symbol, we’ll use a code with a special property No code is the prefix of any other code How does this work? Decoding: We read in the codes one bit at a time When we have a code we recognise, it must be the end of a symbol  It cannot be part of a longer symbol because no code is the prefix of another code

Trees6 Method II: Prefix codes Example A:00, B:010, C:011, D:10, E:11 ADBECABADE A D B E C A B A D E The string decodes

Trees7 Making a Prefix Code We want the code to be efficient No strings longer than necessary No wasted strings A code is a set of strings of binary digits, such that no string corresponding to one symbol is the prefix of a string corresponding to another symbol In a tree, leaf nodes have no children No path from the root to a leaf is the prefix of a path from the root to another node

Trees8 Binary Trees and Prefix Codes Binary trees are in one to one correspondence with Prefix Codes A:00, B:010, C:011, D:10, E:11 A BC DE

Trees9 Prefix Trees Binary Trees The left child corresponds to 0, the right to 1 Each leaf contains a symbol The code for a symbol corresponds to the path from the root to the leaf containing that symbol

Trees10 Encoding and Decoding Imagine the encoder and decoder running in parallel Encoding Start from the root While you are not at the symbol’s leaf  If the symbol you wish to send is a left decendant, send 0 and move to your left child, else send 1 and move to your right child Decoding Start from the root While you are not at a leaf  Read a bit. If it is 0 then move to your left chile, else move to your right child

Trees11 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding:

Trees12 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding: 0

Trees13 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding:

Trees14 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding: 0

Trees15 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A

Trees16 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A

Trees17 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A 0

Trees18 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A

Trees19 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A 1

Trees20 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A

Trees21 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A 1

Trees22 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC

Trees23 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC

Trees24 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC 1

Trees25 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC

Trees26 Encoding and Decoding: ACD A BC DEA BC DE Encoding:DDecoding:AC 1

Trees27 Encoding and Decoding: ACD A BC DEA BC DE Encoding:Decoding:ACD

Trees28 Back to Method I: Balanced Tree Method I was to used fixed length code words Each path from the root to a leaf is the same length: a balanced tree Balanced trees are good for worst case path length. Are they good for coding? Yes, if you assume the worst case But we can normally do better…

Trees29 Statically optimal codes Want common symbols to have short codes This will make uncommon symbols have longer codes In a tree with a fixed number of leave/symbols, moving one leaf/symbol closer to the root will move others further away

Trees30 Huffman codes From Shannon’s information theory, The optimal static code assigns -log 2 (p) bits to a symbol that occurs with probability p It is possible to make a Huffman code tree with this property Will look at this later in the course

Trees31 Adaptive Codes As long as the same change is made in both sending and receiving trees/codes, there is no reason why the tree/code must remain static Send a character using the initial tree Update the tree using that character  Can also be updated in the receiver as it already has the character Send the next character

Trees32 Encoding and Decoding: ACD A BC DEA BC DE Encoding:ACDDecoding:

Trees33 Encoding and Decoding: ACD A BC DEA BC DE Encoding:CDDecoding:A 00

Trees34 Encoding and Decoding: ACD Encoding:CDDecoding:A A BCDE A BCDE Make same change in both trees: Rotate A’s parent

Trees35 Encoding and Decoding: ACD Encoding:CDDecoding:A A BCDE A BCDE

Trees36 Encoding and Decoding: ACD Encoding:DDecoding:CA A BCDE A BCDE 101

Trees37 Encoding and Decoding: ACD Encoding:DDecoding:CA A B C DE A B C DE

Trees38 Encoding and Decoding: ACD Encoding:DDecoding:CA A B C DE A B C DE

Trees39 Encoding and Decoding: ACD Encoding:Decoding:CAD A B C DE A B C DE 1110

Trees40 Splay Trees do Rotations after Every Operation new operation: splay splaying moves a node to the root using rotations right rotation makes the left child x of a node y into y’s parent; y becomes the right child of x y x T1T1 T2T2 T3T3 y x T1T1 T2T2 T3T3 left rotation makes the right child y of a node x into x’s parent; x becomes the left child of y y x T1T1 T2T2 T3T3 y x T1T1 T2T2 T3T3 (structure of tree above y is not modified) (structure of tree above x is not modified) a right rotation about ya left rotation about x

Trees41 Splaying: is x the root? stop is x a child of the root? right-rotate about the root left-rotate about the root is x the left child of the root? is x a left-left grandchild? is x a left-right grandchild? is x a right-right grandchild? is x a right-left grandchild? right-rotate about g, right-rotate about p left-rotate about g, left-rotate about p left-rotate about p, right-rotate about g right-rotate about p, left-rotate about g start with node x “ x is a left-left grandchild” means x is a left child of its parent, which is itself a left child of its parent p is x ’s parent; g is p ’s parent no yes no yes zig-zig zig-zag zig-zig zig

Trees42 Visualizing the Splaying Cases zig-zag y x T2T2 T3T3 T4T4 z T1T1 y x T2T2 T3T3 T4T4 z T1T1 y x T1T1 T2T2 T3T3 z T4T4 zig-zig y z T4T4 T3T3 T2T2 x T1T1 zig x w T1T1 T2T2 T3T3 y T4T4 y x T2T2 T3T3 T4T4 w T1T1