Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science 2 Lecture 7: Extended binary trees
Advertisements

Lecture 4 (week 2) Source Coding and Compression
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
Lecture 2: Greedy Algorithms II Shang-Hua Teng Optimization Problems A problem that may have many feasible solutions. Each solution has a value In maximization.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
22C:19 Discrete Math Trees Fall 2011 Sukumar Ghosh.
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
1 Assignment 2: (Due at 10:30 a.m on Friday of Week 10) Question 1 (Given in Tutorial 5) Question 2 (Given in Tutorial 7) If you do Question 1 only, you.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
A Data Compression Algorithm: Huffman Compression
Lecture 6: Greedy Algorithms I Shang-Hua Teng. Optimization Problems A problem that may have many feasible solutions. Each solution has a value In maximization.
Data Structures – LECTURE 10 Huffman coding
Chapter 9: Huffman Codes
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
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.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
MA/CSSE 473 Days Optimal BSTs. MA/CSSE 473 Days Student Questions? Expected Lookup time in a Binary Search Tree Optimal static Binary Search.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
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.
4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Huffman coding Content 1 Encoding and decoding messages Fixed-length coding Variable-length coding 2 Huffman coding.
GREEDY ALGORITHMS UNIT IV. TOPICS TO BE COVERED Fractional Knapsack problem Huffman Coding Single source shortest paths Minimum Spanning Trees Task Scheduling.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
Huffman’s Algorithm 11/02/ Weighted 2-tree A weighted 2-tree T is an extended binary tree with n external nodes and each of the external nodes is.
Foundation of Computing Systems
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.
Huffman Codes. Overview  Huffman codes: compressing data (savings of 20% to 90%)  Huffman’s greedy algorithm uses a table of the frequencies of occurrence.
1Computer Sciences Department. 2 Advanced Design and Analysis Techniques TUTORIAL 7.
1 Huffman Codes. 2 ASCII use same size encoding for all characters. Variable length codes can produce shorter messages than fixed length codes Huffman.
Greedy Algorithms Analysis of Algorithms.
Greedy algorithms 2 David Kauchak cs302 Spring 2012.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
HUFFMAN CODES.
CSC317 Greedy algorithms; Two main properties:
Chapter 5 : Trees.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
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.
Chapter 8 – Binary Search Tree
Chapter 9: Huffman Codes
Huffman Coding.
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.
Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths.
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Trees Addenda.
Data Structure and Algorithms
Basic Data Structures - Trees
Podcast Ch23d Title: Huffman Compression
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Huffman Coding Greedy Algorithm
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Analysis of Algorithms CS 477/677
Presentation transcript:

Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng

Basic Data Structures - Trees Informal: a tree is a structure that looks like a real tree (up- side-down) Formal: a tree is a connected graph with no cycles.

Trees - Terminology x bem cda root leaf height=2 size=7 Every node must have its value(s) Non-leaf node has subtree(s) Non-root node has a single parent node value subtree nodes

Types of Tree Binary Tree m-ary Trees Each node has at most 2 sub-trees Each node has at most m sub-trees

Binary Search Trees A binary search tree:  … is a binary tree.  if a node has value N, all values in its left sub-tree are less than or equal to N, and all values in its right sub-tree are greater than N.

This is NOT a binary search tree

This is a binary search tree

Searching a binary search tree search(t, s) { If(s == label(t)) return t; If(t is leaf) return null If(s < label(t)) search(t’s left tree, s) else search(t’s right tree, s)} h Time per level O(1) Total O(h)

Searching a binary search tree search( t, s ) { while(t != null) { if(s == label(t)) return t; if(s < label(t) t = leftSubTree(t); else t = rightSubTree(t); } return null; h Time per level O(1) Total O(h)

Here’s another function that does the same (we search for label s): TreeSearch(t, s) while (t != NULL and s != label[t]) if (s < label[t]) t = left[t]; else t = right[t]; return t;

Insertion in a binary search tree: we need to search before we insert Time complexity ? Insert Insert O(height_of_tree) O(log n) if it is balanced n = size of the tree always insert to a leaf

Insertion insertInOrder(t, s) { if(t is an empty tree) // insert here return a new tree node with value s else if( s < label(t)) t.left = insertInOrder(t.left, s ) else t.right = insertInOrder(t.right, s) return t }

Comparison – Insertion in an ordered list Insert 6 Time complexity? O(n) n = size of the list insertInOrder(list, s) { loop1: search from beginning of list, look for an item >= s loop2: shift remaining list to its right, start from the end of list insert s } 6789

Try it!! Build binary search trees for the following input sequences 7, 4, 2, 6, 1, 3, 5, 7 7, 1, 2, 3, 4, 5, 6, 7 7, 4, 2, 1, 7, 3, 6, 5 1, 2, 3, 4, 5, 6, 7, 8 8, 7, 6, 5, 4, 3, 2, 1

Suppose we have 3GB character data file that we wish to include in an . Suppose file only contains 26 letters {a,…,z}. Suppose each letter  in {a,…,z} occurs with frequency f . Suppose we encode each letter by a binary code If we use a fixed length code, we need 5 bits for each character The resulting message length is Can we do better? Data Compression

Data Compression: A Smaller Example Suppose the file only has 6 letters {a,b,c,d,e,f} with frequencies Fixed length 3G= bits Variable length Fixed length Variable length

How to decode? At first it is not obvious how decoding will happen, but this is possible if we use prefix codes

Prefix Codes No encoding of a character can be the prefix of the longer encoding of another character: We could not encode t as 01 and x as since 01 is a prefix of By using a binary tree representation we generate prefix codes with letters as leaves

Decoding prefix codes Follow the tree until it reaches to a leaf, and then repeat! A message can be decoded uniquely!

Prefix codes allow easy decoding Decode: s sa san 0 sane

Some Properties Prefix codes allow easy decoding An optimal code must be a full binary tree (a tree where every internal node has two children) For C leaves there are C-1 internal nodes The number of bits to encode a file is where f(c) is the freq of c, length T (c) is the tree depth of c, which corresponds to the code length of c

Optimal Prefix Coding Problem Given is a set of n letters (c 1,…, c n ) with frequencies (f 1,…, f n ). Construct a full binary tree T to define a prefix code that minimizes the average code length

Greedy Algorithms Many optimization problems can be solved using a greedy approach The basic principle is that local optimal decisions may be used to build an optimal solution But the greedy approach may not always lead to an optimal solution overall for all problems The key is knowing which problems will work with this approach and which will not We study The problem of generating Huffman codes

Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment My everyday examples: Driving in Los Angeles, NY, or Boston for that matter Playing cards Invest on stocks Choose a university The hope: a locally optimal choice will lead to a globally optimal solution For some problems, it works Greedy algorithms tend to be easier to code

David Huffman’s idea A Term paper at MIT Build the tree (code) bottom-up in a greedy fashion Each tree has a weight in its root and symbols as its leaves. We start with a forest of one vertex trees representing the input symbols. We recursively merge two trees whose sum of weights is minimal until we have only one tree.

Building the Encoding Tree