ch06 n.ppt
ch06 n.ppt
Transform-and-
Conquer
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-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.
● 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 inA. Levitin
many “Introduction togeometric algorithms.
the Design & Analysis of Algorithms,”
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. nd
2 ed., Ch. 6 6-2
How fast can we sort ?
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-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?
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-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)
6-5
Another algorithm? Hashing, which works well on
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
●
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6
Instance simplification – Gaussian
Elimination
Given: A system of n linear equations in n unknowns with an
arbitrary coefficient matrix.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-6
Gaussian Elimination (cont.)
for i ←1 to n-1 do
replace each of the subsequent rows (i.e., rows
i+1, …, n) by
the 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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-7
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
x1 = (6 – 6 + 4*1)/2 = 2
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-8
Pseudocode and Efficiency of Gaussian
Elimination
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-9
Searching Problem
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-10
Taxonomy of Searching
Algorithms
● List searching (good for static data)
• sequential search
• binary search
• interpolation search
< > 5
K K 1
3
0
1
1 7
Example: 5, 3, 1, 10, 12, 7, 2
9 9
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-12
Dictionary Operations on Binary Search
Trees
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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-16
General case: Single R-rotation
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-17
General case: Double LR-
rotation
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-18
AVL tree construction - an
example
Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-19
AVL tree construction - an example
(cont.)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-20
Analysis of AVL trees
● h ≤ 1.4404 log2 (n + 2) - 1.3277 N(h-1) + N(h-2) ≤
average height: 1.01 log2n + 0.1 N(h)
for large n (found
empirically)
● Disadvantages:
• frequent rotations
• complexity
[k1, ≥ kn-
< k1
k2 ) 1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-24
Analysis of 2-3 trees
● log3 (n + 1) - 1 ≤ h ≤ log2 (n + 1) - 1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-25
Heaps and Heapsort
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-26
Illustration of the heap’s
definition
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-30
Example of Heap Construction
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-31
Pseudopodia of bottom-up heap
construction
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-32
Heapsort
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-33
Example of Sorting by Heapsort
Sort the list 2, 9, 7, 6, 5, 8 by heapsort
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-34
Analysis of Heapsort
Stage 1: Build heap for a given list of n keys
worst-case h-1
C(n) = Σ 2(h-i) 2i = 2 ( n – log2(n + 1)) ∈ Θ(n)
i=0
# nodes
at level i
Stage 2: Repeat operation of root removal n-1 times
(fix heap)
worst-case n-1 2log2 i ∈
C(n) = Σ Θ(nlogn) i=1
Both worst-case and average-case efficiency:
Θ(nlogn)
In-place: yes
Stability: no (e.g., 1 A.1)
Levitin “Introduction to the Design & Analysis of Algorithms,” 2 ed., Ch. 6
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. nd 6-35
Priority Queue
Efficiency: O(log n)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-37
Horner’s Rule For Polynomial
Evaluation
Given a polynomial of degree n
p(x) = anxn + an-1xn-1 + … + a1x + a0
and a specific value of x, find the value of p at that
point.
1 1 0 1
a8 a2
a4 a : a2 i terms
a8 * a4 * a :
product (computed right-to-left)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-43
Examples of Solving Problems by
Reduction
● linear programming
[ ] [ ]
1 0
0
101 100
2 4 1
0 01 0
2 01
A= A^2 = 0
0
1 0 0 000
3 0
0
000 000
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd ed., Ch. 6 6-45