Minimum Cost Binary Trees Speaker: Dana Moshkovitz.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Incremental Linear Programming Linear programming involves finding a solution to the constraints, one that maximizes the given linear function of variables.
Introduction to Computer Science 2 Lecture 7: Extended binary trees
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
QuickSort Average Case Analysis An Incompressibility Approach Brendan Lucier August 2, 2005.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
Graphs III (Trees, MSTs) (Chp 11.5, 11.6)
Bounds on Code Length Theorem: Let l ∗ 1, l ∗ 2,..., l ∗ m be optimal codeword lengths for a source distribution p and a D-ary alphabet, and let L ∗ be.
Outline. Theorem For the two processor network, Bit C(Leader) = Bit C(MaxF) = 2[log 2 ((M + 2)/3.5)] and Bit C t (Leader) = Bit C t (MaxF) = 2[log 2 ((M.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Discrete Structure Li Tak Sing( 李德成 ) Lectures
CPSC 668Set 10: Consensus with Byzantine Failures1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
Greedy method for inferring tandem duplication history Louxin Zhang, Bin Ma, Lusheng Wang and Ying Xu. BIOINFORMATICS 2003 reference: 1.Elemento,O.,(2002)
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
Tirgul 5 AVL trees.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
MAE 552 – Heuristic Optimization Lecture 26 April 1, 2002 Topic:Branch and Bound.
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
Data Structures – LECTURE 10 Huffman coding
Chapter 9: Huffman Codes
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
4. There once lived a king, named Hagar, who had thousands of sons and daughters, still giving more births to his wives. One of his serious problems was.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
May 1, 2002Applied Discrete Mathematics Week 13: Graphs and Trees 1News CSEMS Scholarships for CS and Math students (US citizens only) $3,125 per year.
Multiway Trees. Trees with possibly more than two branches at each node are know as Multiway trees. 1. Orchards, Trees, and Binary Trees 2. Lexicographic.
1 Greedy algorithm 叶德仕 2 Greedy algorithm’s paradigm Algorithm is greedy if it builds up a solution in small steps it chooses a decision.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
 Rooted tree and binary tree  Theorem 5.19: A full binary tree with t leaves contains i=t-1 internal vertices.
1 1 © 2003 Thomson  /South-Western Slide Slides Prepared by JOHN S. LOUCKS St. Edward’s University.
Lectures on Greedy Algorithms and Dynamic Programming
Graph Colouring L09: Oct 10. This Lecture Graph coloring is another important problem in graph theory. It also has many applications, including the famous.
10. Lecture WS 2006/07Bioinformatics III1 V10: Network Flows V10 follows closely chapter 12.1 in on „Flows and Cuts in Networks and Chapter 12.2 on “Solving.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Chapter 9 Finding the Optimum 9.1 Finding the Best Tree.
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
Chapter 8 Maximum Flows: Additional Topics All-Pairs Minimum Value Cut Problem  Given an undirected network G, find minimum value cut for all.
Bushy Binary Search Tree from Ordered List. Behavior of the Algorithm Binary Search Tree Recall that tree_search is based closely on binary search. If.
Great Theoretical Ideas in Computer Science for Some.
Approximation Algorithms based on linear programming.
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.
Lossless Compression-Statistical Model Lossless Compression One important to note about entropy is that, unlike the thermodynamic measure of entropy,
Sorting by placement and Shift Sergi Elizalde Peter Winkler By 資工四 B 周于荃.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
EMGT 6412/MATH 6665 Mathematical Programming Spring 2016
St. Edward’s University
CSCE 411 Design and Analysis of Algorithms
(edited by Nadia Al-Ghreimil)
Minimal Spanning Trees
Chapter 9: Huffman Codes
James B. Orlin Presented by Tal Kaminker
Multi-Way Search Trees
Algorithms (2IL15) – Lecture 2
3.5 Minimum Cuts in Undirected Graphs
The algorithm of Garsia and Wachs
Applied Combinatorics, 4th Ed. Alan Tucker
(edited by Nadia Al-Ghreimil)
Advanced Algorithms Analysis and Design
Huffman Coding Greedy Algorithm
Algorithms Tutorial 27th Sept, 2019.
Presentation transcript:

Minimum Cost Binary Trees Speaker: Dana Moshkovitz

Outline Minimum Cost Binary Trees: The problem’s description The Garsia-Wachs algorithm Kingston’s proof for the Garsia-Wachs algorithm

Minimum Cost Binary Trees: Description Given a list of n weights (nonnegative real numbers), p 1,...,p n, let us look at the class of binary trees, which these weights are assigned to their leaves in the same order given. we would like to find the tree with the minimum weighted external path length, i.e. that tree T, s.t is minimal (h i, namely the level of p i, is the number of arcs in T along the path from the root to the leaf, whose weight is p i ) Let’s observe an example...

Suppose these are the weights we get: We can construct the following tree: Now let us add to each leave its weighted external path length Minimum Cost Binary Trees Presenting the problem Example Total: 106

Total: 106 Clearly, this tree was not in fact minimal: Total: 101 This is because we can construct a “better” tree. Such tree is this one: Minimum Cost Binary Trees Presenting the problem Example

Minimum Cost Binary Trees Solving the problem Intuition Our first observation is that the smaller the weight, the lower it should be in the tree. The above obvious observation is all we needed in the unordered version. Let us recall Huffman’s simple greedy algorithm. The general idea behind his solution was to construct the tree from the bottom and always choose the two smallest weights to be siblings. Let us demonstrate...

Minimum Cost Binary Trees Solving the problem Intuition - Back to Huffman’s Algorithm 6 Again we have these weights: We choose the two less expensive fatherless weights and create them a father Total: 100

Minimum Cost Binary Trees Solving the problem Intuition The question arising here is whether and how can we apply the same idea to the ordered case. The problem now is that we cannot arbitrarily choose the two cheapest leaves. The two leaves we choose must be neighbors. Unfortunately, this demand makes the naive greedy algorithm incorrect (For example, suppose the weights are 4,3,4,4. The correctness of the result is depended on whether we choose or to be the minimal pair). So, what do we do??

Minimum Cost Binary Trees Solving the problem Toward a Solution The feel is that we have some pretty useful ideas: Constructing the tree from the bottom Finding siblings in each iteration However, as we have already seen, they still need some refinement.

We need an additional observation: Suppose we have weights p 1,...,p n and levels h 1,...,h n. There is only one binary tree (at most) determined by this data, and that tree can be computed efficiently. Let’s demonstrate and explain this: Minimum Cost Binary Trees Solving the problem Toward a Solution

Minimum Cost Binary Trees Solving the problem Toward a Solution Given feasible weights p 1,...,p n and levels h 1,...,h n, let us see how can we construct the binary tree they represent Algorithmically we can obtain this by preserving an array, which will hold in its i-th entry a pointer to a node in the i-th level lacking a right son.

Minimum Cost Binary Trees Solving the problem Toward a Solution We conclude, that it suffices to solve the unordered version under the constraint that the level of each leaf in the resulting tree equals the level of the corresponding leaf of an ordered solution. What we need to explain now is how do we solve the unordered problem under this constraint. The question is how do we choose the right pair of neighbors.

Let us define the following: A pair of leaves p i-1,p i is right minimal (briefly R.M.) if (i) 1<i  n (ii) p i-2 +p i-1  p i-1 +p i (iii) p i-1 +p i i Minimum Cost Binary Trees Solving the problem Right Minimal Pairs In other words, two neighbors are right minimal if their sum is minimal among the sums to their right, but this does not hold for the pairs to their left. Remark: From now on, we shall treat our list of weights as though it consists of additional two sentinels, namely  and  +1, placed in the leftmost and rightmost positions in the list respectively. This is done for the ease of the discussion, proving the modification does not alter the solution is rather trivial (see Gilbert and Moore’s result).

Lemma 1: Suppose we have a sequence of at least three weights p a,p a+1,...,p b, s.t. p j-1 + p j < p j + p j+1, for a<j<b, then h a  h a+1 ...  h b-1 in every minimal tree containing these weights < 8 < 9 < 15 < 16 Minimum Cost Binary Trees Solving the problem Some Interesting Facts about R.M. Pairs

Proof. Suppose h j-1 <h j for some a<j<b. p j must be a left child. Then the transformation: is both legal and less expensive: since p j+1 must be in R, |R|  p j+1 >p j-1. Therefore the lemma holds.  pjpj p j-1 pjpj R R Minimum Cost Binary Trees Solving the problem Some Interesting Facts about R.M. Pairs

Lemma 2: if p i-1,p i is the rightmost R.M. pair, then h i-1  h i ...  h n in every minimal tree. Proof. Directly follows from the choice of the pair and from lemma 1.  Further explanation: notice that when a pair is the rightmost R.M pair, the sequence of pairs to its right constructs a monotone series. (Otherwise the pair that “breaks” the monotony would be the rightmost R.M pair). Minimum Cost Binary Trees Solving the problem Some Interesting Facts about R.M. Pairs

Lemma 3: if p i-1,p i is the rightmost R.M. pair, then h i-1 = h i in some minimal tree. Proof. By lemma 2 it suffices to show that h i-1  h i in some minimal tree. Suppose we have a minimal tree such that h i-1 >h i. In this tree p i-1 is a right child, so we can use the following transformation to get another minimal tree: p i-1 pipi R pipi R Again, |R|  p i-2  p i, so the new tree is necessarily minimal.  Minimum Cost Binary Trees Solving the problem Some Interesting Facts about R.M. Pairs

Minimum Cost Binary Trees Solving the problem... And finally... After we clarified the most important notion for the purpose of this lecture, namely R.M. pairs, and proved some interesting properties related to such, let us finally observe the desired algorithm. Afterwards we shall prove the correctness of our solution.

Minimum Cost Binary Trees Solving the problem The Algorithm Execute the following two steps n-1 times: (1) Locate the rightmost R.M pair of entries. (2) Find the first entry to its right, which is greater than/equals it, and move the pair to the left of this entry.

Minimum Cost Binary Trees Solving the problem Simulating the algorithm We start with the same input We locate the rightmost R.M pair, pair them and move them to their right place The heights yield the tree we have already seen:

Lemma 4: Let p i+k+1 be the first node to the right of the rightmost R.M pair p i-1,p i, s.t p i+k+1 > p i-1 +p i. In some minimal tree T for which h i-1 = h i, either (a) h i+k = h i -1,or (b) h i+k = h i, and p i+k is a right child. Proof. Begin with T the minimal tree of lemma3. By lemma 2 we merely need to show that h i+k  h i -1 in T. Let us suppose h i+k < h i -1. Let p m (m  i+k) be the first node to the right of p i, s.t. h m < h i -1. p m <p i-1 +p i. Minimum Cost Binary Trees Proving the Solution

There are two possibilities: (a) p i-1,p i are siblings in T (b) p i,p i+1 are siblings in T Let p,q denote the two siblings. We can transform T as follows and improve the cost by decreasing p+q-p m  p i-1 +p i -p m >0. This contradicts the hypothesis T was minimal. Hence h i+k  h i -1. pq pmpm... pq pmpm Minimum Cost Binary Trees Proving the Solution

It remains to prove, that when h i+k = h i, p i+k can be made a right child. If p i+k is already a right child, we are done. Otherwise, if k>0, we can transform T: p i-1 pipi... p i+k p i+k+1 p i-1 pipi... p i+k p i+k+1 Minimum Cost Binary Trees Proving the Solution

p i-1 pipi p i+k+1 p i-1 pipi p i+k+1 While if k=0, we can use this transformation: In both cases, our claim holds.  Minimum Cost Binary Trees Proving the Solution

Minimum Cost Binary Trees Proving the solution Lemma 5: For every rearrangement done in an iteration of the algorithm, the cost for the new series cannot exceed the cost for the original series of weights. Proof. Let k be the number of places we move the R.M. pair in a specific iteration. We will exhibit a tree T’ for the new arrangement, whose leaves has the same levels as in T, the minimum tree of lemma 4. This - of- course - will prove the lemma. If k=0, p i-1 and p i are siblings in T, so we may take T’=T.

Minimum Cost Binary Trees Proving the solution Otherwise k>0. First we will transform T, so p i-1 and p i (the current rightmost R.M. pair) are siblings (If they they are not so already): p i-1 pipi p i+1 p i-1 pipi p i+1 Lemma 4 states, that they have the same level Lemma 4 states p i+1 is a right child

Since k>0, the first weight greater than/equal to p i-+ p i, namely p i+k+1, is still to the right of p i. By lemma 4, there are merely two possibilities: h i+k =h i or h i+k =h i -1. In each case all we need to do is “slid” the new node to the right until it passes p i+k.  Minimum Cost Binary Trees Proving the solution p i-1 pipi p i+k... p i-1 pipi p i+k... p i-1 pipi p i+k... p i-1 pipi... p i+k

Minimum Cost Binary Trees Proving the solution Lemma 6: For every rearrangement done in an iteration of the algorithm, the cost for the new series is at least the cost for the original series of weights, and if equality holds, then the two solutions have corresponding levels. Proof. Now - given a minimum tree T’ for the new arrangement - we need to construct a minimum tree T for the original series (while preserving the levels). Again we denote the number of places we move the pair by k. If k=0, T=T’.

Minimum Cost Binary Trees Proving the solution Otherwise k>0. We know that p i+k <p i-1 +p i  p i+k+1, so we can use lemma 1 in T’ with p i+1,...,p i+k,p x, p i+k+1 (p x is the father of the R.M. pair) to obtain: h i+1 ...  h i+k  h x in T’. Hence, either TxTx p i-1 pipi p i+1... TxTx p i-1 pipi p i+1... or In the first case the treatment is simple: we need to move T x to the left.

Minimum Cost Binary Trees Proving the solution The treatment of the second case consists of two phases: (a) Slid T x to the left across all the leaves in its level. (b) Rotate all the nodes two places to the right. TxTx p i-1 pipi p i+1... TxTx p i-1 pipi p i+1... (a)

TxTx p i-1 pipi p i+1 Minimum Cost Binary Trees Proving the solution How do we perform the rotation? A simulation pipi p i-1 p i+1 pipi p i-1 p i+1

Minimum Cost Binary Trees Proving the solution For every level the pair drops downward (possibly 0), some other pair moves up one level. But we have taken a R.M. pair (for which all the pairs with greater indices weight more than it does). This implies that w(T’)=w(T a )  w(T), so the weight of T’ is at least the weight of a minimum cost tree for the original series of weights. If equality holds, the level of the pair is not changed by the rotation, so T is a minimum cost tree for the original series, which preserves the levels of T’. 

Minimum Cost Binary Trees Proving the solution Those two final lemmas allow us to finally state the following: The algorithm we presented is correct. 

Minimum Cost Binary Trees Solving the problem Another way to view this algorithm We start with the same input We locate the rightmost R.M pair, pair them and move them to their right place Which yield the tree we have already seen:

Minimum Cost Binary Trees References This overview was based on: [1]J. H. Kingston. A new proof for the Garcia-Wachs algorithm. J. of Algorithms, 9: ,1988 The Garsia-Wachs algorithm: [2]A. M. Garsia and M. L. Wachs, A new algorithm for minimum cost binary trees. Sicomp, 6(4), , 1977 Huffman’s algorithm: [3]D. Huffman, A method for the construction of minimum- redundancy codes, Proc. IRL, , 1952

Minimum Cost Binary Trees References You might be interested in previous results concerning this problem: The original O(n 3 ) algorithm: [4]E. N. Gilbert and E.F. Moore, Variable length binary encodings, Bell Systems Tech, 38, , An O(n 2 ) algorithm: [5]D. E. Knuth, Optimum binary search trees, Acta Inform. 1, 14-25, The Hu-Tucker algorithm (O(nlogn)): [6]T.C. Hu and C. Tucker, Optimum computer search trees, SIAM J. Appl. Math., , [7]T.C. Hu, A new proof of the T-C algorithm, SIAM J. Appl. Math. 25, 83-94, 1973.