07 - Transform and Conquer
07 - Transform and Conquer
07 - Transform and Conquer
Main idea:
• Many problems involving lists are easier when list is sorted:
searching
computing the median (selection problem)
checking if all elements are distinct (element uniqueness)
• Also
Topological sorting helps solving some problems for dags.
Presorting is used in many geometric algorithms.
Efficiency of sorting
• Efficiency of algorithms with pre-sorts depends on efficiency of sorting
• Theorem (see Sec. 11.2): ⌈log2 n!⌉ n log2 n comparisons are necessary in
the worst case to sort a list of size n by any comparison-based algorithm.
• Note: About n log2 n comparisons are also sufficient to sort array of size n (by
mergesort, quicksort)
CPS 616 TRANSFORM-AND-CONQUER 7-2
Generalization
• Given: A system of n linear equations in n unknowns with an arbitrary
coefficient matrix.
a11x1 + a12x2 + … + a1nxn = b1
a21x1 + a22x2 + … + a2nxn = b2
...
an1x1 + an2x2 + … + annxn = bn
• Transform to: An equivalent system of n linear equations in n unknowns with
an upper triangular coefficient matrix.
c11x1+ c12x2 + … + c1nxn = d1
c22x2 + … + c2nxn = d2
...
cnnxn = dn
• Solve the latter by substitutions starting with the last equation and moving up
to the first one.
• The transformation is accomplished by a sequence of elementary operations
on the system’s coefficient matrix (which don’t change the system’s
solution):
for i ←1 to n-1 do
replace each of the subsequent rows (i.e., rows i+1, …, n) by
a difference between that row and an appropriate multiple
of the i-th row to make the new coefficient in the i-th column
of that row 0
Pseudocode
• Stage 1: Reduction to an upper-triangular matrix
for i ← 1 to n-1 do // remove multiple of row i from others below
for j ← i+1 to n do // rows j from which row i is removed
factor ← A[j, i] / A[i, i]
for k ← i to n+1 do // columns of row j
A[j, k] ← A[j, k] - A[i, k] * factor
• Stage 2: Back substitutions
for j ← n downto 1 do //go from bottom row j to top
t←0
for k ← j +1 to n do //substitute solved x’s into equation
t ← t + A[j, k] * x[k]
x[j] ← (A[j, n+1] - t) / A[j, j] // solve equation for row
CPS 616 TRANSFORM-AND-CONQUER 7-5
Efficiency
• Θ(n3) + Θ(n2) = Θ(n3)
Example
• Solve 2x1 - 4x2 + x3 = 6 2 -4 1 6
3x1 - x2 + x3 = 11 3 -1 1 11
x1 + x2 - x3 = -3 1 1 -1 -3
• Gaussian Elimination
2 -4 1 6
row2 – (3/2)*row1: 0 5 -1/2 2
row3 – (1/2)*row1: 0 3 -3/2 -6
2 -4 1 6
0 5 -1/2 2
row3–(3/5)*row2 : 0 0 -6/5 -36/5
• Which represents:
2x1 - 4x2 + x3 = 6
5x2 -1/2 x3 = 2
-6/5x3 = -36/5
• Backward substitution
x3 = (-36/5) / (-6/5) = 6
x2 = (2+(1/2)*6) / 5 = 1
x1 = (6 – 6 + 4*1)/2 = 2
CPS 616 TRANSFORM-AND-CONQUER 7-6
Degenerate Trees
Degenerate Degenerate AVL Tree
BST (half of the levels are fully balanced)
1
100 100
1 1
45
50 140
30 1 1 1 1
40 60 120 150
18
1 1 1 0 1 0 0
0 0 0 0
10 18 33 41 52 105
0
10
• Analysis of Efficiency
height 1.4404 log2 (n + 2) - 1.3277 i.e. O(log n)
average height: 1.01 log2n + 0.1 for large n (found empirically)
Search and insertion are O(log n)
Deletion is more complicated but is also O(log n)
• Disadvantages:
frequent rotations (see later)
complexity
CPS 616 TRANSFORM-AND-CONQUER 7-9
• Rotations
If a key insertion violates the balance requirement at some node, the subtree
rooted at that node is transformed via one of the four rotations. (The rotation
is always performed for a subtree rooted at an “unbalanced” node closest to
the new leaf.)
CPS 616 TRANSFORM-AND-CONQUER 7 - 10
CPS 616 TRANSFORM-AND-CONQUER 7 - 11
2-3 Trees
• Definition A 2-3 tree is a search tree that
may have 2-nodes and 3-nodes
is height-balanced (all leaves are on the same level)
CPS 616 TRANSFORM-AND-CONQUER 7 - 13
50
ℎ𝑒𝑖𝑔ℎ𝑡
𝑛=∑ 2𝑖
𝑖=0
40 60
(2ℎ𝑒𝑖𝑔ℎ𝑡+1 − 1)
=
2−1
25 43 55 65
= (2ℎ𝑒𝑖𝑔ℎ𝑡+1 − 1)
18 33 41 47 52 58 63 70 So height = log2(n+1)-1
50 100
20 40 150 200
5 10 45 48 110 130 210 215
25 30 65 85 160 180
55 60 90 95
70 75
ℎ𝑒𝑖𝑔ℎ𝑡
𝑖
(3ℎ𝑒𝑖𝑔ℎ𝑡+1 − 1)
𝑛 = 2. ∑ 3 = 2. = (3ℎ𝑒𝑖𝑔ℎ𝑡+1 − 1)
𝑖=0 3−1
So height = log3(n+1)-1
• Efficiency
log3 (n + 1) - 1 height log2 (n + 1) - 1
Search, insertion, and deletion are in (log n)
CPS 616 TRANSFORM-AND-CONQUER 7 - 14
• Construction:
• The idea of 2-3 tree can be generalized by allowing more keys per node
2-3-4 trees
B-trees
• However, 2-3 tree implementation is complicated. These trees can instead be
implemented as red-black BSTs
CPS 616 TRANSFORM-AND-CONQUER 7 - 15
RED-BLACK TREES
Definition
2-3 Trees can be represented as special BSTs using rules:
• Nodes and edges are either red or black
• A node is the same colour as its upper edge
• On any path from the root to a leaf the number of black edges is the same
• A red node that is not a leaf has 2 black children
• A black node that is not a leaf has either
Two black children, or
One child of each colour, or
A single child which is a red leaf
Conversion:
A 3-node: is converted into one of the two red-black trees
50 100 50 100
100 50
or
Examples
A 2-3 Tree: Is converted into:
M S M
D F Q V F S
D H Q V
A E H N O R T X Z
A E O R T X
N Z
Properties
• The height of the red-black tree is at most twice the height of the
corresponding 2-3 tree.
• Therefore the logarithmic-time operations on 2-3 trees will be logarithmic
time on the red-black implementations of those trees provided that each
conversion operation (like splitting) can be done in constant time.
CPS 616 TRANSFORM-AND-CONQUER 7 - 16
REPRESENTATION CHANGE :
POLYNOMIAL EVALUATION USING HORNER'S RULE
Problem: (Handout 4)
Find the value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0
at a point x = x0
Horner's Rule
p(x) = anxn + an-1xn-1 +… + a1x1 + a0
= [(((anx + an-1)x + an-2)x + an-3)x + … + a1] x + a0
Algorithm:
p a[n]
for i n-1 downto 0 do
p p x + a[i]
return p
Efficiency
#multiplications = #additions = n
Example
p(x) = 2x4 - x3 + 3x2 + x - 5
= (2x3 - x2 + 3x + 1) x - 5
= ((2x2 - x + 3) x + 1) x - 5
= (((2x - 1) x + 3) x + 1) x – 5
Arrange coefficients in table:
2 -1 3 1 -5
x=3
2x - 1 = 5
(2x - 1) x + 3 = 18
((2x - 1) x + 3) x + 1) = 55
(((2x - 1) x + 3) x + 1) x – 5 = 160
CPS 616 TRANSFORM-AND-CONQUER 7 - 17
• Algorithm:
Calculate gcd(m,n) first using Euclidian Algorithm
Apply formula above
Solution:
• find a minimal path from Pwgc|| to ||Pwgc using BFS
CPS 616 TRANSFORM-AND-CONQUER 7 - 20