Introduction to Algorithms Jiafen Liu Sept. 2013.

Slides:



Advertisements
Similar presentations
October 17, 2005 Copyright© Erik D. Demaine and Charles E. Leiserson L2.1 Introduction to Algorithms 6.046J/18.401J LECTURE9 Randomly built binary.
Advertisements

© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 17 L9.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 9 Prof. Charles E. Leiserson.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
UNC Chapel Hill Lin/Foskey/Manocha Binary Search Tree Her bir node u bir object olan bir linked data structure ile temsil edilebilir. Her bir node key,
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary Search Tree Qamar Abbas.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk.
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Binary Search Trees What is a binary search tree?
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Elementary Data Structures
Red-Black Trees.
Ch. 12: Binary Search Trees Ming-Te Chi
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
CS6045: Advanced Algorithms
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

Introduction to Algorithms Jiafen Liu Sept. 2013

Today’s Tasks Binary Search Trees(BST) Analyzing height Informal Analysis Formal Proof –Convexity lemma –Jensen’s inequality –Exponential height

Binary Search Tree Binary search tree is an popular data structure, it supports several dynamic operations. –Search –Minimum –Maximum –Predecessor –Successor –Insert –Delete –Tree Walk

Data structure of binary search tree A BST tree can be represented by a linked data structure in which each node is an structure. –Key field –Satellite data –Pointers: left, right, and parent For any node x, the keys in the left subtree of x less than or equal to key[x], and the keys in the right subtree of x are bigger than key[x].

Different Shapes of BST 2 binary search trees contains the same 6 keys. –(a) A binary search tree with height 3. –(b) A binary search tree with height 5. –Which one is better? Balanced Tree and Unbalanced Tree, What’s the worst case of BST?

What’s the cost of basic operations? It takes Θ(n) time to walk an n-node binary search tree. Other basic operations on a binary search tree take time proportional to the height of the tree Θ(h).

Tree Walk If x is the root of an n-node subtree, then the call INORDER-TREE-WALK(x) takes Θ(n) time. We will prove by induction.

Proof of the theorem Proof: Let T(n) denote the time taken this function. T(1) is a constant. For n > 1, suppose that function is called on a node x whose left subtree has k nodes and whose right subtree has n - k - 1 nodes. T(n) = T(k) + T(n - k - 1) + d How to Solve it? –substitution method

Search TREE-SEARCH (x, k) 1 if x= NIL or k = key[x] 2 then return x 3 if k < key[x] 4 then return TREE-SEARCH(left[x], k) 5 else return TREE-SEARCH(right[x], k)

Minimum and maximum TREE-MINIMUM (x) 1 while left[x] ≠ NIL 2 do x ← left[x] 3 return x TREE-MAXIMUM(x) 1 while right[x] ≠ NIL 2 do x ← right[x] 3 return x

Successor and predecessor TREE-SUCCESSOR(x) 1 if right[x] ≠ NIL 2 then return TREE-MINIMUM (right[x]) 3 y ← p[x] 4 while y ≠ NIL and x = right[y] 5 do x ← y 6 y ← p[y] 7 return y if the right subtree of node x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x.

Insertion TREE-INSERT(T, z) 1 y ← NIL 2 x ← root[T] 3 while x ≠ NIL 4 do y ← x 5 if key[z] < key[x] 6 then x ← left[x] 7 else x ← right[x] 8 p[z] ← y 9 if y = NIL 10 then root[T] ← z // Tree T was empty 11 else if key[z] < key[y] 12 then left[y] ← z 13 else right[y] ← z

Deletion

Which node is actually removed depends on how many children z has. (a)If z has no children, we just remove it. (b)If z has only one child, we splice out z. (c)If z has two children, we splice out its successor y, which has at most one child, and then replace z's key and satellite data with y's key and satellite data.

Sorting and BST if there is an array A, can we sort this array using binary search tree operations as a black box? –Build the binary search tree, and then traverse it in order.

Sorting and BST Example: A= [ ], what we will get? What's the running time of the algorithm?

Cost of build a BST of n nodes Can anybody guess an answer? –Θ(nlgn) in most case –Θ(n 2 ) in the worst case Does this looks familiar and remind you of any algorithm we've seen before? –Quicksort Process of Quicksort

BST sort and Quicksort BST sort performs the same comparisons as Quicksort, but in a different order! So, the expected time to build the tree is asymptotically the same as the running time of Quicksort.

Informal Proof The depth of a node equals to the number of comparisons made during TREE-INSERT. Assuming all input permutations are equally likely, we have Average node depth() = (comparison times to insert node i) = Θ(nlgn) = Θ(lgn) The expected running time of quicksort on n elements is?

Expected tree height Average node depth of a randomly built BST = Θ(lgn). Does this necessarily means that its expected height is also Θ(lgn)?

Outline of Formal Proof Prove Jensen’s inequality, which says that f(E[X]) ≤ E[f(X)] for any convex function f and random variable X. Analyze the exponential height of a randomly built BST on n nodes, which is the random variable Y n = 2 Xn, where X n is the random variable denoting the height of the BST. Prove that 2 E[Xn] ≤ E[2 Xn ] = E[Y n ] = O(n 3 ), and hence that E[X n ] = O(lgn).

Convex functions A function f is convex if for all α, β≥0 such that α+β=1, we have f(αx+ βy) ≤αf(x)+βf(y) for all x,y ∈ R.

Convexity lemma Lemma. Let f be a convex function, and let α 1, α 2, …, α n be nonnegative real numbers such that. Then, for any real numbers x 1, x 2, …, x n, we have How to prove that?

Proof of convexity lemma Proof. By induction on n. For n=1, we have α 1 =1 and hence f(α 1 x 1 ) ≤α 1 f(x 1 ) trivially. Inductive step: why we introduce 1-α n ? By convexity By induction

Convexity lemma: infinite case Lemma. Let f be a convex function, and let α 1, α 2, … be nonnegative real numbers such that. Then, for any real numbers x 1, x 2, …, we have assuming that these summations exist. We will not prove that, but intuitively its true.

Jensen’s inequality Jensen’s inequality, which says that f(E[X]) ≤ E[f(X)] for any convex function f and random variable X. Proof. By definition of expectation By Convexity lemma (infinite case)

Analysis of BST height Let X n be the random variable denoting the height of a randomly built binary search tree on n nodes, and let Y n = 2 X n be its exponential height. If the root of the tree has rank k, then X n = 1 + max{X k–1,X n–k } since each of the left and right subtrees are randomly built. Hence, we have Y n = 2·max{Y k–1,Y n–k }.

Analysis (continued) Define the indicator random variable Z nk as Thus, Pr{Z nk = 1} = E[Z nk ] = 1/n, and Take expectation of both sides.

Analysis (continued) Linearity of expectation. Independence of the rank of the root from the ranks of subtree roots. The max of two nonnegative numbers is at most their sum, and E[Z nk ] = 1/n. Each term appears twice, and reindex.

Analysis (continued) How to solve this? –Substitution Method Guess E[Y n ] ≤ cn 3 for some positive constant c. Initial conditions: for n=1, E[Y 1 ]=2 X1 =2≤c, we can pick c sufficiently large to handle.

Analysis (continued) Substitution: How to compute the expression on right? –Integral method.

The grand finale By Jensen’s inequality, since f(x) = 2 x is convex, we have What we just showed Taking lg of both sides yields

Post mortem Why bother with analyzing exponential height? Why not just develop the recurrence on X n = 1 + max{X k–1,X n–k } directly?

Answer The inequality max{a,b} ≤ a+b provides a poor upper bound, since the RHS approaches the LHS slowly as |a–b| increases. The bound allows the RHS to approach the LHS far more quickly as |a–b| increases. By using the convexity of f(x) = 2 x via Jensen’s inequality, we can manipulate the sum of exponentials, resulting in a tight analysis.