Recurrence Relations: Change of Variable

advertisement
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
CS 312: Algorithm Analysis
Lecture #9: Solving Divide and
Conquer Recurrence Relations
with Change of Variable
Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick
Announcements
 HW #6 Due Today
 Questions about Non-homogeneous RR?
 Project #2
 Questions?
 Early Day: Wednesday
 Due: Friday
Objectives
 Understand how to analyze recurrence
relations that are typical of divide and
conquer algorithms
 Learn to use the “change of variable”
technique to solve such recurrences
 Review Merge sort (quickly!)
 Theoretical analysis using RRs
 Time permitting, prove the Master theorem
Divide and Conquer
Analysis Approach: Reduction
Divide and Conquer Recurrence Relation
Change
of
Variable
Non-Homogeneous Recurrence Relation with
Geometric Forcing Function
LTI Homogeneous Recurrence Relation
Analysis of Binary Search
Analysis Approach: Reduction
Divide and Conquer Recurrence Relation
Change
of
Variable
Non-Homogeneous Recurrence Relation with
Geometric Forcing Function
LTI Homogeneous Recurrence Relation
“Unwind
the Stack”
Analysis of Binary Search (cont.)
Review: Merge sort




Split the array in 1/2
Sort each 1/2
Merge the sorted halves
Adhoc(): Use insertion sort when sub-arrays are
small
3 1 4 1 5 9 2 6 5 3 5 8 9
3 1 4 1 5 9
2 6 5 3 5 8 9
1 1 3 4 5 9
2 3 5 5 6 8 9
1 1 2 3 3 4 5 5 5 6 8 9 9
Analysis of merge()
procedure merge (U[1..p+1], V[1..q+1], A[1..n])
i,j  1
U[p+1],V[q+1]  sentinel
How long to merge?
for k  1 to p+q do
if U[i] < V[ j]
then A[k]  U[i]; i  i + 1
else A[k]  V[j]; j  j + 1
3 1 4 1 5 9 2 6 5 3 5 8 9
U
A
3 1 4 1 5 9
2 6 5 3 5 8 9
1 1 3 4 5 9
2 3 5 5 6 8 9
1 1 2 3 3 4 5 5 5 6 8 9 9
V
Merge sort
procedure mergesort (A[1..n])
if n is small enough then insertsort (A)
else
array U[1..1+floor(n/2)], V[1..1+ceil(n/2)]
U[1..floor(n/2)]  A[1..floor(n/2)]
V[1..ceil(n/2)]  A[1+floor(n/2)..n]
mergesort (U)
mergesort (V)
merge (U,V,A)
What is the efficiency of Mergesort?
Merge sort
Using the Master Theorem
a = number of sub-instances that must be solved
n = original instance size (variable)
n/b = size of subinstances
d = polynomial order of g(n),
where g(n) = cost of dividing and recombining
t  n   a  t  n / b     nd 
 ( n d )

d
t (n)  (n log n)
 (nlogb a )

if a  b
d
if a  b
d
if a  b
d
Efficiency of Mergesort
 𝑡 𝑛 = 2 ⋅ 𝑡(𝑛/2) + 𝑛
 𝑎 = 2, 𝑏 = 2, 𝑑 = 1
 ( n d )

d
t (n)  (n log n)
 (nlogb a )

if a  b
d
if a  b
d
if a  b
d
Another Useful Logarithm Identity
𝑎log𝑏 𝑛 = 𝑛log𝑏 𝑎
Proof:
Statement


𝑎log𝑏 𝑛
Reason
=
log𝑏 𝑛
log
𝑎
𝑏
𝑏
log𝑏 𝑛
log
𝑎
𝑏
𝑏
= 𝑏 log𝑏 𝑎⋅log𝑏 𝑛
𝑥 𝑦 and log 𝑥 𝑦 are inverses
𝑥𝑦
𝑧
= 𝑥 𝑦⋅𝑧
 𝑏 log𝑏 𝑎⋅log𝑏 𝑛 = 𝑏 log𝑏 𝑛⋅log𝑏 𝑎
commutativity of ⋅
 𝑏 log𝑏 𝑛⋅log𝑏 𝑎 = 𝑛log𝑏 𝑎
𝑥 𝑦 and log 𝑥 𝑦 are inverses
(Sketch of the)
Proof of the Master Theorem
How to proceed?
Proof of the Master Theorem
Proof of the Master Theorem
Proof: case 1
Proof: case 2
Proof: case 2
Proof: Conclusion
Proof: Conclusion
Assignment
 Read: Section 2.3 in the textbook
 HW #7:
 Part III Exercises (Section 3.2)
 Analyze 3-part mergesort using recurrence
relations techniques
 Problem 2.4 in the textbook (using the master
theorem where possible)
Download