CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

Similar documents
Assignment 5: Solutions

Dictionary: an abstract data type

Fundamental Algorithms

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

CS1800: Mathematical Induction. Professor Kevin Gold

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

Binary Search Trees. Motivation

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Fundamental Algorithms

Lecture 2: Proof of Switching Lemma

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Dictionary: an abstract data type

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

Administrivia. COMP9020 Lecture 7 Session 2, 2017 Induction and Recursion. Lecture 6 recap. Lecture 6 recap

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

CS173 Lecture B, November 3, 2015

CS60007 Algorithm Design and Analysis 2018 Assignment 1

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

CS 240 Data Structures and Data Management. Module 4: Dictionaries

INF2220: algorithms and data structures Series 1

CPSC 121: Models of Computation. Module 9: Proof Techniques (part 2) Mathematical Induction

Induction and Recursion

Proof Techniques (Review of Math 271)

CSC236 Week 3. Larry Zhang

Models of Computation,

Lecture 6 September 21, 2016

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

HW #4. (mostly by) Salim Sarımurat. 1) Insert 6 2) Insert 8 3) Insert 30. 4) Insert S a.

CS 395T Computational Learning Theory. Scribe: Mike Halcrow. x 4. x 2. x 6

Weight-balanced Binary Search Trees

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Module 9: Mathematical Induction

ICS141: Discrete Mathematics for Computer Science I

Weight-balanced Binary Search Trees

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Discrete Math, Spring Solutions to Problems V

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Data Structures and Algorithms " Search Trees!!

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Learning Decision Trees

The semantics of propositional logic

Problem: Data base too big to fit memory Disk reads are slow. Example: 1,000,000 records on disk Binary search might take 20 disk reads

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

AVL Trees. Properties Insertion. October 17, 2017 Cinda Heeren / Geoffrey Tien 1

Lecture 1: September 25, A quick reminder about random variables and convexity

Nearest Neighbor Search with Keywords

CMPUT 675: Approximation Algorithms Fall 2014

Data Structures and and Algorithm Xiaoqing Zheng

Finite Automata Theory and Formal Languages TMV027/DIT321 LP Recap: Logic, Sets, Relations, Functions

1. [10 marks] Consider the following two algorithms that find the two largest elements in an array A[1..n], where n >= 2.

CMSC250 Homework 9 Due: Wednesday, December 3, Question: Total Points: Score:

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

CS483 Design and Analysis of Algorithms

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Decision Trees. Lewis Fishgold. (Material in these slides adapted from Ray Mooney's slides on Decision Trees)

Heaps and Priority Queues

7.3 AVL-Trees. Definition 15. Lemma 16. AVL-trees are binary search trees that fulfill the following balance condition.

13 Dynamic Programming (3) Optimal Binary Search Trees Subset Sums & Knapsacks

Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of

The Trees of Hanoi. Joost Engelfriet

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Binary Decision Diagrams. Graphs. Boolean Functions

CS4311 Design and Analysis of Algorithms. Analysis of Union-By-Rank with Path Compression

Announcements. Problem Set Four due Thursday at 7:00PM (right before the midterm).

Functional Data Structures

Chapter 11. Min Cut Min Cut Problem Definition Some Definitions. By Sariel Har-Peled, December 10, Version: 1.

Today s Outline. CS 362, Lecture 4. Annihilator Method. Lookup Table. Annihilators for recurrences with non-homogeneous terms Transformations

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Reckhow s Theorem. Yuval Filmus. November 2010

} It is non-zero, and maximized given a uniform distribution } Thus, for any distribution possible, we have:

1. Prove: A full m- ary tree with i internal vertices contains n = mi + 1 vertices.

Induction on Failing Derivations

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

The Tree Evaluation Problem: Towards Separating P from NL. Lecture #6: 26 February 2014

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

CSC236H Lecture 2. Ilir Dema. September 19, 2018

Alpha-Beta Pruning: Algorithm and Analysis

We now introduce a powerful method for proving claims about inductively-defined sets...

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Notes on induction proofs and recursive definitions

Randomized Algorithms III Min Cut

COE428 Notes Week 4 (Week of Jan 30, 2017)

Alpha-Beta Pruning: Algorithm and Analysis

Announcements Wednesday, November 15

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

N/4 + N/2 + N = 2N 2.

Recap from Last Time

CVO103: Programming Languages. Lecture 2 Inductive Definitions (2)

Midterm 2 for CS 170

Bayesian Networks: Independencies and Inference

Chapter 10 Search Structures

Proving simple set properties...

Partitions and Covers

Preparing for the CS 173 (A) Fall 2018 Midterm 1

Transcription:

CS 151 Red Black Trees & Structural Induction 1

Announcements Majors fair tonight 4:30-6:30pm in the Root Room in Carnegie. Come and find out about the CS major, or some other major. Winter Term in CS info session will happen in the next week. Details TBA. 2 2

Red-Black Trees A red-black tree is another balanced BST, faster than AVL s in practice. A red-black tree is a binary search tree such that color property: every node is either red or black root property: the root node is black internal property: the children of a red node are black depth property: for each node v, all v -> null pointer paths contain the same number of black nodes 1 13 8 17 11 15 25 6 22 27 3 3

Structural Induction Review Structural induction is a proof technique used to prove boolean properties. That is, it s used to prove that a given boolean method always returns true. To use structural induction, you need a recursively defined structure S with both a base case, and a recursive case a boolean property / method P(S) defined on any instance of structure S To prove that P(S) returns / is true for all S that satisfy your definition, just prove that P(S) is true when S is one of the base cases, and prove that P(S) is true in the recursive case IF it s true for the substructures 4 4

Structural Induction Example A red-black tree T is either an empty tree, or a black root node r with left and right RB trees that may violate the root property (so root must be black if either child is red, otherwise no restrict n) For a RB tree T that possibly violates the root property, let P(T) be the boolean property that b(t ) 2 m(t ) 1. (Remember, you can think of P(T) as a boolean function that takes in a RB tree T that possibly violates the root property, and returns the value of the given boolean expression.) We will show that P(T) is always true, that is, that no matter what tree you pass to the method P(T), its expression always evaluates to true. This fact helps show that RB trees have height O(log n). (It isn t immediately obvious, but it helps nonetheless.) 5 5

Structural Induction Example For a RB tree T that possibly violates the root property, let P(T) be the boolean property that b(t ) 2 m(t ) 1. We will show that P(T) is true for all RB trees T by structural induction on T. Step 0: Play around and convince yourself P(T) is true for some specific T s. 6 6

Structural Induction Example For a RB tree T that possibly violates the root property, let P(T) be the boolean property that b(t ) 2 m(t ) 1. We will show that P(T) is true for all RB trees T by structural induction on T. Step 1: Show that P(T) is true when T is your base case. base case: T is an empty tree We want to show (WTS) that P(T) is true when T is an empty tree, i.e. that b(t ) 2 m(t ) 1 holds when T is an empty tree. When T is empty, b(t)=0 and m(t)=0. Then b(t )=0 because T has no nodes, red or black =1 1 by algebra =2 0 1 also by algebra =2 m(t ) 1 because there are no nodes on any root-leaf path So the base case is true, because P(T) is true when T is an empty RB tree. 7 7

Structural Induction Example For a RB tree T that possibly violates the root property, let P(T) be the boolean property that b(t ) 2 m(t ) 1. We will show that P(T) is true for all RB trees T by structural induction on T. Step 2: Show that P(T) is true when T is your recursive case. inductive step: T is a root r with subtrees L and R that may violate root prop. Suppose, hypothetically, that P(L) and P(R) are true, i.e. that b(l) 2 m(l) 1 and b(r) 2 m(r) 1 called the induction hypothesis We want to use these two hypothetical assumptions to show P(T) is true, ie. that b(t ) 2 m(t ) 1 is always true. To do this, we need to find a connection between b(t) and b(l), b(r) m(t) and m(l), m(r) 8 8

Structural Induction Example Algebraically connect b(t) to b(l), b(r) Algebraically connect m(t) to m(l), m(r) Let x(t) = 1 if the root of T is black, and let x(t) = 0 if it is red. Then b(t) = b(l) + b(r) + x(t) (because T contains black nodes of R and L) m(t) = m(l) + x(t) = m(r) + x(t) (because of the RB tree depth property) Recall that we want to show that b(t ) 2 m(t ) 1 is true, given our IH that b(l) 2 m(l) 1 and b(r) 2 m(r) 1 b(t )=b(l)+b(r)+x(t) because T contains nodes of R, L, and itself 2 m(l) 1+2 m(r) 1+x(T ) by our induction hypothesis (IH) =2 m(l)+1 2+x(T ) because m(l)=m(r), and by algebra Now there are 2 cases. If x(t)=0 then b(t ) 2 m(t )+1 2 2 m(t ) 1 If x(t)=1 then b(t ) 2 m(t ) 2+1=2 m(t ) 1 In both cases, we ve shown our inequality holds. The proof is done! 9 9

Red-Black Trees Heights are log n Fact: for every RB tree T, the length of the longest root-leaf path (i.e. T s height) is at most (twice that of the shortest root-leaf path + 1). (This fact also helps show that RB trees heights are O(log n).) Proof: Let S be the shortest root-leaf path, let L be the longest root-leaf path. WTS (want to show) that length(l) <= 2 length(s) + 1 Equivalently, WTS that numnodes(l) <= 2 numnodes(s) (by def of length) By the internal property, no path has 2 red nodes in a row, so numred(l) <= numblack(l), equivalently, numnodes(l) <= 2 numblack(l) By the depth property, numblack(l) = numblack(s) Putting this together: length(l) = numnodes(l) - 1 <= 2 numblack(l) - 1 = 2 numblack(s) - 1 <= 2 numnodes(s) - 1 = 2( numnodes(s) - 1 ) + 1 = 2 length(s) + 1 10 10

Red-Black Trees Heights are log n We already showed by induction that, for every RB tree T, b(t ) 2 m(t ) 1 On the previous slide we showed that Then: numnodes(t ) b(t ) 2 m(t ) 1 2 m(t ) numnodes(t )+1 by algebra m(t ) log(numnodes(t ) + 1) height(t ) 2 log(numnodes(t ) + 1) + 1 height(t ) 2m(T )+1 because numnodes = numred + numblack by the property we showed by induction by taking the log of both sides by property from previous slide height(t ) O(log(numNodes(T ))) by definition of big-oh 11 11