Review for Exam 2

CS 223
Fall 2004

Chapters in book

•      Carrano/Savitch

–    Chapter 28 Balanced Search Trees

•      Neapolitan/Naimipour

–    Chapter 1 Algorithms: Efficiency, Analysis, and Order

•    Sections 1.1 & 1.2

–    Chapter 2 Divide-and-Conquer

•    Sections:  all but Sect 2.4

–    Chapter 3 Dynamic Programming

•    Section 3.1The Binomial Coefficient

•    Section 3.3 Dynamic Programming & Optimization Problems

•    Section 3.4 Chained Matrix Multiplication

•    Section 3.5 Optimal Binary Search Trees

 

Topics

•      Balanced Search Trees

–    AVL Trees

–    Multiway search trees

–    Red-Black trees

•      Ways to solve problems--algorithms

•      Divide and Conquer Algorithms

–    Recurrence relations

–    Merge Sort

–    Multiplying large integers

–    Matrix multiplication—Strassen’s method

•      Dynamic Programming algorithms

–    Binary Coefficients

–    Chained matrix multiplication

–    Optimal Binary Search Trees

 

AVL trees

•      An AVL tree is a binary search tree that is either empty or has the following two properties

•      1) the heights of the left and right subtrees differ by at most 1

•      2) the left and right subtrees are AVL trees

•      Build a AVL tree with this input
90  100  50   30   70   80

 

Multiway search trees

•      Multi-way search trees generalize binary search trees into m-ary search trees

•      They allow more than one key to be stored in a  node

•      There will always be one more child pointer than key

•      Build this  2-3 three
      20  40  60  10  80  100  5

•      Analysis of multi-way search trees

•      The advantage is their easy-to-maintain balance

•      Not their shorter height

•      The reduction in height is offset by the increased number of comparisons that the search may require at each node

 

Red-Black trees

•       Red-black tree is a BST that is empty, or in which the root node is colored black, and every other node is colored red or black, and the following properties are satisfied:

–     Red rule: If an node is red, it parents must be black;

–     Path rule: The number of black nodes must be the same in all paths from the root to a node with no children or with one child.

 

Summary

•       Case 1 uncle—red;   parent -- red left child;   CurNode -- either      L or R 

–     Recolor parent, uncle, grandparent

•       Case 2  CurNode--right child;  parent--a red left child, uncle – black or none

–     Rotate (parent, CurNode) and reset CurNode

–     It then becomes case 3

•       Case 3 CurNode -- left child; parent--red left child;   uncle–black or none

–     Change color of parent and grandparent

–     Rotate(parent, grandparent)

 

Merge Sort and recurrence relations

•       Analyzing Merge sort

–     With merge sort, we divide the list in half, then recursively merge sort each half; then we have to put the list back together

•       So T(n) = T(n/2) + T(n/2) + n

–     Collecting terms this is T(n) = 2T(n/2) + n

•       This is the recurrence relation for Merge Sort

•       One way to solve it is to substitute in the rule, starting with what we know

•       T(1) = 1;

•       N = 2;   T(2) = 2T(1) + 2

                         2 (1) +  2 = 4       or 2*1 + 2    

•       N = 4;   T(4) = 2T(2) + 4

                           2(4)  +  4 = 12       or 4*2+4

•       N = 8;   T(8) = 2T(4) + 8

                         2(12) + 8                or 8 * 3 + 8  

                                                         n(log n) + n

 

Multiplying large integers

•      The natural way to multiply integers too large to fit into a long is to break it recursively into a high and low part, then to multiply the high parts, the low parts

–    This requires four multiplications

–    T(n) = 4T(n/2) + cn;   this is an O(n2) algorithm

•       A more efficient way notices that the middle term can be done with one multiplication instead of two

•       Note that the middle term:
 XhYl + XlYh = (Xh+Xl) (Yl+Yh ) - XhYh - XlYl

 

Strassen’s method of multiplication

•      The standard method to multiply matrices takes 8 multiplications and 4 add/sub

•      Strassen’s method takes 7 multiplications and 18 add/sub

•      In a 2 X 2 matrix, this is not worthwhile, but  it can be used on larger matrices that are divided into four submatrices

•      This works only because commutativity of multiplication is not used

•      When the matrices have been divided to a point where they reach a threshold, standard multiplication is used since the overhead on a small matrix is not worthwhile

 

Binary Coeffieients

•        They are the coefficients when expanding a binomial like (x + y)n

–    n is the power to which the binomial is expanded

•      k is the number of the term of the expansion

•      The coefficients of a binomial expansion are also the terms in Pascal’s triangle

•      They are also the solution to the number of combinations possible with n total elements, taken k at a time

•      Since binomial coefficients represent three different things, we have a choice of how to program a way to find them

–    We could do it recursively, but that refigures the same thing over and over

–    So instead, we start from the smallest, and save the results in a table

–    This is typical of dynamic programming

 

Chained matrix multiplication

•      The problem is to figure out the order in which to multiply a chain of matrices

•      The solution is to find the minimum number of multiplications necessary to form chain of shorter length, save them in a table , and use them to find optimal chains of longer length

•      We keep the size of the matrices to be multiplied in a one D array

•      We keep the minimum number of multiplications necessary to form shorted chains a in 2-D array

•      A formula is used to find the minimum number of multiplications for chains of each length

•      Ni,j = min{ Ni,k + Nk+1,j + di-1*dk*dj }
     
where i <= k < j

 

Optimal BSTs

•      The calculations and storage of data is much like the chained matrix multiplication algorithm

•      The main reason for presenting this is so you know that other options are available when doing certain searches

•      To use an optimal BST, certain information must be available:

–    An ordered array with all the possiaable keys

–    The probability for each key that it will be found in the tree

–    Insertions and deletion after the tree is built are not possible without rebuilding the tree