MA/CSSE 473 Days 31-32 Optimal linked lists Optimal BSTs.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Other Dynamic Programming Problems
Introduction to Computer Science 2 Lecture 7: Extended binary trees
EECS 4101/5101 Prof. Andy Mirzaian. Lists Move-to-Front Search Trees Binary Search Trees Multi-Way Search Trees B-trees Splay Trees Trees Red-Black.
Lecture 3: Parallel Algorithm Design
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Greedy Algorithms Amihood Amir Bar-Ilan University.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
© Neeraj Suri EU-NSF ICT March 2006 Dependable Embedded Systems & SW Group Introduction to Computer Science 2 Binary.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
CS Data Structures Chapter 10 Search Structures (Selected Topics)
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures – LECTURE 10 Huffman coding
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Static Dictionaries Collection of items. Each item is a pair.  (key, element)  Pairs have different keys. Operations are:  initialize/create  get (search)
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
6 6.3 © 2012 Pearson Education, Inc. Orthogonality and Least Squares ORTHOGONAL PROJECTIONS.
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
CS 2133: Data Structures Binary Search Trees.
MA/CSSE 473 Days Optimal BSTs. MA/CSSE 473 Days Student Questions? Expected Lookup time in a Binary Search Tree Optimal static Binary Search.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
CS Data Structures Chapter 10 Search Structures.
MA/CSSE 473 Days Optimal linked lists Optimal BSTs Greedy Algorithms.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
We learnt what are forests, trees, bintrees, binary search trees. And some operation on the tree i.e. insertion, deletion, traversing What we learnt.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
Binary Tree. Contiguous versus Linked List Insertion in Contiguous list needs a lot of move. For big chunks of records it is time consuming. Linked List.
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees Student questions?
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
MA/CSSE 473 Day 28 Dynamic Programming Binomial Coefficients Warshall's algorithm Student questions?
Static Dictionaries Collection of items. Each item is a pair.  (key, element)  Pairs have different keys. Operations are:  initialize/create  get (search)
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
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.
MA/CSSE 473 Day 30 B Trees Dynamic Programming Binomial Coefficients Warshall's algorithm No in-class quiz today Student questions?
Copyright © Curt Hill Other Trees Applications of the Tree Structure.
MA/CSSE 473 Day 30 Optimal BSTs. MA/CSSE 473 Day 30 Student Questions Optimal Linked Lists Expected Lookup time in a Binary Tree Optimal Binary Tree (intro)
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
MA/CSSE 473 Day 05 More induction Factors and Primes Recursive division algorithm.
MA/CSSE 473 Day 27 Dynamic Programming Binomial Coefficients
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Binary Search Trees A binary search tree is a binary tree
MA/CSSE 473 Day 28 Optimal BSTs.
Advanced Algorithms Analysis and Design
MA/CSSE 473 Day 29 Optimal BST Conclusion
CSC 143 Binary Search Trees.
Dynamic Programming II DP over Intervals
Optimal Binary Search Tree. 1.Preface  OBST is one special kind of advanced tree.  It focus on how to reduce the cost of the search of the BST.  It.
Presentation transcript:

MA/CSSE 473 Days Optimal linked lists Optimal BSTs

MA/CSSE 473 Days Student Questions? Optimal Linked Lists Expected Lookup time in a Binary Tree Optimal Binary Tree –(intro Day 31, finish Day 32)

Warmup: Optimal linked list order Suppose we have n distinct data items x 1, x 2, …, x n in a linked list. Also suppose that we know the probabilities p 1, p 2, …, p n that each of the items is the one we'll be searching for. Questions we'll attempt to answer: –What is the expected number of probes before a successful search completes? –How can we minimize this number? –What about an unsuccessful search? Q1-2

Examples p i = 1/n for each i. –What is the expected number of probes? p 1 = ½, p 2 = ¼, …, p n-1 = 1/2 n-1, p n = 1/2 n-1 –expected number of probes: What if the same items are placed into the list in the opposite order? The next slide shows the evaluation of the last two summations in Maple. –Good practice for you? prove them by induction

Calculations for previous slide

What if we don't know the probabilities? 1.Sort the list so we can at least improve the average time for unsuccessful search 2.Self-organizing list: –Elements accessed more frequently move toward the front of the list; elements accessed less frequently toward the rear. –Strategies: Move ahead one position (exchange with previous element) Exchange with first element Move to Front (only efficient if the list is a linked list) Q3

Optimal Binary Search Trees Suppose we have n distinct data keys K 1, K 2, …, K n (in increasing order) that we wish to arrange into a Binary Search Tree This time the expected number of probes for a successful or unsuccessful search depends on the shape of the tree and where the search ends up Q4

Recap: Extended binary search tree It's simplest to describe this problem in terms of an extended binary search tree (EBST): a BST enhanced by drawing "external nodes" in place of all of the null pointers in the original tree Formally, an Extended Binary Tree (EBT) is either –an external node, or –an (internal) root node and two EBTs T L and T R In diagram, Circles = internal nodes, Squares = external nodes It's an alternative way of viewing a binary tree The external nodes stand for places where an unsuccessful search can end or where an element can be inserted An EBT with n internal nodes has ___ external nodes (We proved this by induction – Day 5 in Fall, 2012) See Levitin: page 183 [141] Q5

What contributes to the expected number of probes? Frequencies, depth of node For successful search, number of probes is _______________ depth of the corresponding internal node For unsuccessful, number of probes is __________ depth of the corresponding external node one more than equal to Q6-7

Optimal BST Notation Keys are K 1, K 2, …, K n Let v be the value we are searching for For i= 1, …,n, let a i be the probability that v is key K i For i= 1, …,n-1, let b i be the probability that K i < v < K i+1 –Similarly, let b 0 be the probability that v K n Note that We can also just use frequencies instead of probabilities when finding the optimal tree (and divide by their sum to get the probabilities if we ever need them) Should we try exhaustive search of all possible BSTs? Answer on next slide

Aside: How many possible BST's Given distinct keys K 1 < K 2 < … < K n, how many different Binary Search Trees can be constructed from these values? Figure it out for n=2, 3, 4, 5 Write the recurrence relation Solution is the Catalan number c(n) Verify for n = 2, 3, 4, 5. Q8-14 Wikipedia Catalan article has five different proofs of When n=20, c(n) is almost 10 10

Recap: Optimal Binary Search Trees Suppose we have n distinct data items x 1, x 2, …, x n (in increasing order) that we wish to arrange into a Binary Search Tree This time the expected number of probes for a successful or unsuccessful search depends on the shape of the tree and where the search ends up Let v be the value we are searching for For i= 1, …,n, let a i be the probability that v is item x i For i= 1, …,n-1, let b i be the probability that x i < v < x i+1 Similarly, let b 0 be the probability that v x n Note that but we can also just use frequencies when finding the optimal tree (and divide by their sum to get the probabilities if needed)

What not to measure Earlier, we introduced the notions of external path length and internal path length These are too simple, because they do not take into account the frequencies. We need weighted path lengths.

Weighted Path Length If we divide this by  a i +  b i we get the average search time. We can also define it recursively: C(  ) = 0. If T =, then C(T) = C(T L ) + C(T R ) +  a i +  b i, where the summations are over all a i and b i for nodes in T It can be shown by induction that these two definitions are equivalent (good practice problem). TLTL TRTR Note: y 0, …, y n are the external nodes of the tree

Example Frequencies of vowel occurrence in English : A, E, I, O, U a's: 32, 42, 26, 32, 12 b's: 0, 34, 38, 58, 95, 21 Draw a couple of trees (with E and I as roots), and see which is best. (sum of a's and b's is 390).

Strategy We want to minimize the weighted path length Once we have chosen the root, the left and right subtrees must themselves be optimal EBSTs We can build the tree from the bottom up, keeping track of previously-computed values

Intermediate Quantities Cost: Let C ij (for 0 ≤ i ≤ j ≤ n) be the cost of an optimal tree (not necessarily unique) over the frequencies b i, a i+1, b i+1, …a j, b j. Then C ii = 0, and This is true since the subtrees of an optimal tree must be optimal To simplify the computation, we define W ii = b i, and W ij = W i,j-1 + a j + b j for i<j. Note that W ij = b i + a i+1 + … + a j + b j, and so C ii = 0, and Let R ij (root of best tree from i to j) be a value of k that minimizes C i,k+1 + C kj in the above formula

Code

Results Constructed by diagonals, from main diagonal upward What is the optimal tree? How to construct the optimal tree? Analysis of the algorithm?

Most frequent statement is the comparison if C[i][k-1]+C[k][j] < C[i][opt-1]+C[opt][j]: How many times does it execute: Running time