Chapter 6: Transform and Conquer
Chapter 6: Transform and Conquer
16
AVL Illustration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Analysis of AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Chapter 6: Transform and Conquer Horner’s Rule and Fast Exponentiation 19
Horner’s Rule. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Horner’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
You can never solve a problem Fast Exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
on the level on which it was cre- Fast Exponentiation Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
ated. (Albert Einstein)
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Presorting Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Gaussian Elimination 4
System of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Example of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Forward Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Forward Elimination Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Backward Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Backward Substitution Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Comments on Gaussian Elimination. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Balanced Search Trees: AVL Trees 11
Binary Search Tree Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Binary Search Tree Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
R-Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
LR-Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1 2
Introduction Gaussian Elimination 4
For example, the linear least squares method in statistics needs to solve this kind
Presorting Example of system.
CS 3343 Analysis of Algorithms Chapter 6: Slide – 4
Presorting (sorting the data in advance) makes it easier to:
Check element uniqueness. Example of Linear Equations
Compute the mode.
Search the data. For example, consider this system of 3 equations:
Find the minimum, maximum, median, or any order statistic.
2x1 − x2 + 3x3 = 4
CS 3343 Analysis of Algorithms Chapter 6: Slide – 3
3x1 + 2x2 + x3 = 6
−5x1 + x2 − 2x3 = 3
This system has the solution x1 = −5/4, x2 = 13/4, and x3 = 13/4.
3 4
Forward Elimination Backward Substitution
2 −1 3 4 Solving last equation 4x3 = 13 yields x3 = 13/4.
3 2 1 6 row 2 − 3/2 row 1
−5 1 −2 3 row 3 + 5/2 row 1 Substitute for x3 in second equation:
7/2x2 − 7/2x3 = 0
2 −1 3 4
0 7/2 −7/2 0 7/2x2 = 0 + 7/2x3 = 0 + 91/8 = 91/8
x2 = (91/8)/(7/2) = 13/4
0 −3/2 11/2 13 row 3 + 3/7 ∗ row 2
Substitute for x2 and x3 in first equation:
2 −1 3 4
0 7/2 −7/2 0 2x1 − x2 + 3x3 = 4
0 0 4 13 2x1 = 4 + x2 − 3x3 = 4 + 13/4 − 3 ∗ (13/4)
2x1 = −10/4
CS 3343 Analysis of Algorithms Chapter 6: Slide – 6 x1 = −5/4
CS 3343 Analysis of Algorithms Chapter 6: Slide – 8
5 6
Comments on Gaussian Elimination AVL Trees
Gaussian Elimination requires that A be nonsingular. See your linear algebra For each node in binary search tree, all keys on its left are ≤ to its key, and all
book. keys on its right are ≥ to its key.
Gaussian Elimination is Θ(n3). Note triple loop in ForwardElim. In a balanced search tree, the height is Θ(log n), where n is the number of
Better numerical properties can be obtained through partial pivoting. See nodes.
book. For each node in an AVL tree, the heights of the left and right subtree are
LU decomposition is an extension of Gaussian elimination. After Θ(n3) equal or differ by one.
preprocessing of the A matrix, only Θ(n2) is needed for any b vector. See The balance factor is the left height minus the right height.
book. Tree rotations are used to transform the tree into balance.
Similar processing can be used to compute matrix inverse and determinant. CS 3343 Analysis of Algorithms Chapter 6: Slide – 12
7 8
R-Rotation Balancing AVL Trees
An R-rotation is used when the left child is too high, and the left-left grandchild is
algorithm AVLBalance(x)
as high or higher than the left-right one.
// Input: Node x
// Output: x and nodes above it are balanced
while x 6= null do
if x.left.height > x.right.height + 1 then
if x.left.left.height < x.left.right.height
then LR-Rotation(x)
else R-Rotation(x)
CS 3343 Analysis of Algorithms Chapter 6: Slide – 14
else if x.left.height + 1 < x.right.height then
if x.right.left.height > x.right.right.height
then RL-Rotation(x)
LR-Rotation else L-Rotation(x)
x ← x.parent
An LR-rotation is used when the left child is too high and the left-right grandchild
is higher than the left-left one. CS 3343 Analysis of Algorithms Chapter 6: Slide – 16
AVL Illustration
9 10
Analysis of AVL Trees Horner’s Algorithm
The height h of an AVL tree with n nodes satisfies log2(n/2) < h < 1.5 log2 n
A binary tree of height h has ≤ 2h+1 −1 nodes. algorithm Horner(P [0..n], x)
// Evaluates a polynomial at x
n < 2h+1 → n/2 < 2h → log2(n/2) < h // Input: A value x and an array P of coefficients
An AVL tree with height h at worst has subtrees of height h − 1 and h − 2. // Output: The value of the polynomial at x
Assuming heights h − 1 and h − 2 have at least 1.6h−1 and 1.6h−2 nodes, v ← P [n]
respectively: for i ← n − 1 downto 0 do
v ← x ∗ v + P [i]
n > 1.6h−1 + 1.6h−2 > 1.6h →
return v
1.6h < n → h < 1.5 log2 n
CS 3343 Analysis of Algorithms Chapter 6: Slide – 18 CS 3343 Analysis of Algorithms Chapter 6: Slide – 20
Fast Exponentiation
Horner’s Rule and Fast Exponentiation 19
We can evaluate xn quickly based on the binary expansion of the exponent. For
example,
Horner’s Rule
210 can be evaluated by 210 = 210102 = 28 ∗ 22 .
Horner’s rule is an efficient algorithm for evaluating a polynomial. For example, 213 can be evaluated by 213 = 211012 = 28 ∗ 24 ∗ 2.
5x2 − 3x + 8 can be evaluated by (5x + 3)x + 8. Algorithm on next page corresponds to book’s right to left algorithm.
7x3 + x2 − 9x − 2 can be evaluated by ((7x + 1)x − 9)x − 2. CS 3343 Analysis of Algorithms Chapter 6: Slide – 21
11 12
Fast Exponentiation Algorithm
algorithm FastExpt(x, n)
// Evaluates xn
// Input: A value x and an integer n ≥ 0
// Output: The value of xn
term ← x
product ← 1
while n > 0 do
if n mod 2 = 1 then
product ← product ∗ term
term ← term ∗ term
n ← ⌊n/2⌋
return product
13