CS 310 Unit 16 Optimal Binary Search Trees, and Huffman Codes, and Greedy Programming

CS 310 Unit 16 Optimal Binary Search Trees, and Huffman Codes, and Greedy Programming

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 310 Unit 16 Optimal Binary Search Trees, Huffman Codes, and Greedy Programming Furman Haddix Ph.D. Assistant Professor Minnesota State University, Mankato Spring 2008
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Unit 16 Objectives Optimal Binary Search Trees Elements of the Greedy Strategy Huffman Codes Variable Length Codes Prefix Codes Constructing a Huffman Code Justification of Optimality of Huffman Codes Dynamic Programming compared to Greedy Methods Knapsack Problems Text Chapters 15.5, 16.3, 16.2
Background image of page 2
Optimal Binary Search Tree If we have a binary search tree that looks up C++ reserved words, performance would vary depending on whether specific words were located near the root or near the bottom level, e.g., if union were near the root and int were near the bottom level. The binary search tree that we will work on has two kinds of nodes: key nodes holding a valid key dummy nodes representing all of the possible but invalid keys before, after, or in between valid keys.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Optimality Metric for Binary Search Tree The cost of a search may be approximated as the number of nodes traversed (level of termination + 1). A search terminating at the root (level 0) would cost 1 A search terminating at level 3 would cost 4 A search terminating at level 4 would cost 5 Whether the search is successful (terminating at a key node) or unsuccessful (terminating at a dummy node does not affect the cost computation. Based on the above the following relationships exist: Σ i=1 n p i + Σ i=0 n q i = 1, where p i = probability of searching for key k i and q i = probability of searching for an invalid key and encountering dummy d i (in between k i and k i+1 ) n
Background image of page 4
The Structure of an Optimal Binary Search Tree 1. Characterize the structure of an optimal solution There are n possible roots if n is the number of keys For each possible root, the tree is composed of the root and two optimal binary search subtrees Each optimal binary search subtree has j – i + 1 possible roots, where i is the index of the smallest key and j is the index of the largest key. Similarly, the optimal subtree must be composed of a root and two optimal subtrees. k-level optimal subtrees may be components of multiple optimal subtrees.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Recursive Formulation of Optimal Binary Search Tree 1. Recursively define the value of an optimal solution Define the expected search cost e(i, j) for the subtree which includes keys i through j. If we have a leaf node, then j = i-1 and e(i, j) = q j For a subtree, the sum of the probabilities is w(i, j) = Σ k=i j p k + Σ k=i-1 j q k This is the amount by which a subtree’s search cost increases when it becomes a subtree of a node. Thus, a reflexive definition of the cost e(i,j) if rooted at a node r, such that i < r < j, is e(i,j) = p r + e(i, r-1) + w(i, r-1) + e(r+1, j) + w(r+1,j) Since w(i, j) = p r + w(i, r-1) + w(r+1, j) e(i, j) = e(i, r-1) + e(r+1, j) + w(i,j) Recursive Form: q j if j = i – 1
Background image of page 6
Optimal Binary Search Tree 3. Compute the value of an optimal solution in bottom-up fashion
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2008 for the course CS 310 taught by Professor Furmanhaddix during the Spring '08 term at Minnesota State University, Mankato.

Page1 / 58

CS 310 Unit 16 Optimal Binary Search Trees, and Huffman Codes, and Greedy Programming

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online