ch06
ch06
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1
Instance simplification - Presorting
Solve a problem’s instance by transforming it into
another simpler/easier instance of the same problem
Presorting
Many problems involving lists are easier when list is sorted, e.g.
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.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2
How fast can we sort ?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3
Searching with presorting
Presorting-based algorithm:
Stage 1 Sort the array by an efficient sorting algorithm
Stage 2 Apply binary search
Good or bad?
Why do we have our dictionaries, telephone directories, etc.
sorted?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4
Element Uniqueness with presorting
Presorting-based algorithm
Stage 1: sort by efficient sorting algorithm (e.g. mergesort)
Stage 2: scan array to check pairs of adjacent elements
Efficiency: O(n2)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
a x + a x + Education,
… +Inc. a Upper
x Saddle
= b River, NJ. All Rights Reserved. a x =b
Gaussian Elimination (cont.)
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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Example of Gaussian Elimination
Solve 2x1 - 4x2 + x3 = 6
3x1 - x2 + x3 = 11
x1 + x2 - x3 = -3
Gaussian elimination
2 -4 1 6 2 -4 1 6
3 -1 1 11 row2 – (3/2)*row1 0 5 -1/2 2
1 1 -1 -3 row3 – (1/2)*row1 0 3 -3/2 -6 row3–(3/5)*row2
2 -4 1 6
0 5 -1/2 2
0 0 -6/5 -36/5
Backward substitution
x3 = (-36/5) / (-6/5) = 6
x2 = (2+(1/2)*6) / 5 = 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. AllxRights
= (6 – 6 + 4*1)/2 = 2
Reserved.
Pseudocode and Efficiency of Gaussian Elimination
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10
Taxonomy of Searching Algorithms
List searching
• sequential search
• binary search
• interpolation search
Tree searching
• binary search tree
• binary balanced trees: AVL trees, red-black trees
• multiway balanced trees: 2-3 trees, 2-3-4 trees, B trees
Hashing
• open hashing (separate chaining)
• closed hashing (open addressing)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11
Binary Search Tree
<K >K
Searching – straightforward
Insertion – search for key, insert at leaf where search terminated
Deletion – 3 cases:
deleting key at a leaf
deleting key at node with single child
deleting key at node with two children
BonusA.: Levitin
inorder traversal
“Introduction to the Design & produces sorted
Analysis of Algorithms,” list
3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13
Balanced Search Trees
Attractiveness of binary search tree is marred by the bad (linear)
worst-case efficiency. Two ideas to overcome it are:
10 10
0 1 0 0
5 20 5 20
1 -1 0 1 -1
4 7 12 4 7
0 0 0 0
2 8 2 8
(a) (b)
1 0 0 -1 0 0
R LR
2 > 1 3 1 > 1 3
0 0
1 2
(a) (c)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16
General case: Single R-rotation
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17
General case: Double LR-rotation
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18
AVL tree construction - an example
0
8
1 2 1
6 6 6
1 0 2 0 R (5) 0 0
5 8 5 8 > 3 8
0 1 0 0
3 3 2 5
0
2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19
AVL tree construction - an example (cont.)
2 0
6 5
-1 0 LR (6) 0 -1
3 8 > 3 6
0 1 0 0 0
2 5 2 4 8
0
4
-1 0
5 5
0 -2 0 0
3 6 3 7
RL (6)
0 0 1 > 0 0 0 0
2 4 8 2 4 6 8
0
7
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20
Analysis of AVL trees
h 1.4404 log2 (n + 2) - 1.3277
average height: 1.01 log2n + 0.1 for large n (found empirically)
Disadvantages:
• frequent rotations
• complexity
2-node 3-node
K K1, K 2
8 3, 8 3, 8
>
2, 3, 5 9 2 5 9 2 4, 5 9
3, 8 > 3, 5, 8 >
3 8
2 4, 5, 7 9 2 4 7 9 2 4 7 9
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24
Analysis of 2-3 trees
log3 (n + 1) - 1 h log2 (n + 1) - 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25
Heaps and Heapsort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26
Illustration of the heap’s definition
10 10 10
5 7 5 7 5 7
4 2 1 2 1 6 2 1
Note: Heap’s elements are ordered top down (along any path
down from its root), but they are not ordered left to right
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27
Some Important Properties of a Heap
Given n, there exists a unique binary tree with n nodes that
is essentially complete, with h = log2 n
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28
Heap’s Array Representation
Store heap’s elements in an array (whose elements indexed,
for convenience, 1 to n) in top-down left-to-right order
Example:
9
1 2 3 4 5 6
5 3 9 5 3 1 4 2
1 4 2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30
Example of Heap Construction
9 7 > 9 8 9 8
6 5 8 6 5 7 6 5 7
2 9 9
9 8 > 2 8 > 6 8
6 5 7 6 5 7 2 5 7
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 31
Pseudopodia of bottom-up heap construction
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32
Heapsort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 33
Example of Sorting by Heapsort
Sort the list 2, 9, 7, 6, 5, 8 by heapsort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37
Insertion of a New Element into a Heap
Insert the new element at last position in heap.
Compare it with its parent and, if it violates heap condition,
exchange them
Continue comparing the new element with nodes up the tree
until the heap condition is satisfied
6 8 > 6 10 > 6 9
2 5 7 10 2 5 7 8 2 5 7 8
Efficiency: O(log n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 38
Horner’s Rule For Polynomial Evaluation
coefficients 2 -1 3 1 -5
x=3
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 40
Horner’s Rule pseudocode
1 1 0 1
a8 a4 a2 a : a2 i terms
a8 * a4 * a : product
(computed right-to-left)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 44
Examples of Solving Problems by Reduction
linear programming
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 45