Thanks to Miles Jones, Russell Impagliazzo, and Sanjoy Dasgupta at UCSD for these slides.
COL351: Slides for Lecture Component 18
COL351: Slides for Lecture Component 18 Thanks to Miles Jones, - - PowerPoint PPT Presentation
COL351: Slides for Lecture Component 18 Thanks to Miles Jones, Russell Impagliazzo, and Sanjoy Dasgupta at UCSD for these slides. Master Theorem How do you solve a recurrence of the form = + ! We will
Thanks to Miles Jones, Russell Impagliazzo, and Sanjoy Dasgupta at UCSD for these slides.
COL351: Slides for Lecture Component 18
Consider the summation !
!"# $
π ! It behaves differently for different values of π .
Consider the summation !
!"# $
π ! It behaves differently for different values of π . If π < 1 then this sum converges. This means that the sum is bounded above by some constant π. Therefore ππ π < 1, π’βππ !
!"# $
π ! < π πππ πππ π π‘π !
!"# $
π ! Ο΅ π(1)
Consider the summation !
!"# $
π ! It behaves differently for different values of π . If π = 1 then this sum is just summing 1 over and over n times. Therefore ππ π = 1, π’βππ !
!"# $
π ! = !
!"# $
1 = π + 1 Ο΅ π(π)
Consider the summation !
!"# $
π ! It behaves differently for different values of π . If π > 1 then this sum is exponential with base π . ππ π > 1, π’βππ !
!"# $
π ! < ππ $ πππ πππ π, π‘π !
!"# $
π ! Ο΅ π π $ π > π π β 1
Consider the summation !
!"# $
π ! It behaves differently for different values of π . !
!"# $
π ! Ο΅ 9 π 1 ππ π < 1 π π ππ π = 1 π π $ ππ π > 1
Size π Size π/π Size π/π% Size 1 Depth log& π 1 subproblem π subproblems π% subproblems π'()! $ subproblems
After π levels, there are π! subproblems, each
So, during the πth level of recursion, the time complexity is π
$ &" *
π! = π π!
$ &" *
= π π* π π*
!
After π levels, there are π! subproblems, each of size π/π!. So, during the πth level, the time complexity is π
$ &" *
π! = π π!
$ &" *
= π π* π π*
!
After log& π levels, the subproblem size is reduced to 1, which usually is the size
So the entire algorithm is a sum of each level. π π = π π* !
!"# '()! $
π π*
!
π π = π π" &
#$% &'(! )
π π"
#
Case 1: π < π" Then we have that
* +" < 1 and the series converges to a constant so
π π = π π"
π π = π π" &
#$% &'(! )
π π"
#
Case 2: π = π" Then we have that
* +" = 1 and so each term is equal to 1
π π = π π" log+ π
π π = π π" &
#$% &'(! )
π π"
#
Case 2: π > π" Then the summation is exponential and grows proportional to its last term
* +" &'(! )
so π π = π π" π π"
&'(! )
= π π&'(! *
Top-heavy Steady-state Bottom-heavy
π π Ο΅ π π* ππ π < π* π π* log π ππ π = π* π π'()! + ππ π > π*
π π Ο΅ π π* ππ π < π* π π* log π ππ π = π* π π'()! + ππ π > π*
3)
xL xM yL yM xR yR
π π Ο΅ π π* ππ π < π* π π* log π ππ π = π* π π'()! + ππ π > π*
234(,167) 234 1
!"# $%&' !"# %
Russell Impagliazzo Sanjoy Dasgupta Ragesh Jaiswal (Thanks for slides: Miles Jones)
Week-06 Lecture 24: Divide and Conquer (Tree and Computational Geometry)
Def LCA(r): Lsubtree = explore(r.lc) Rsubtree = explore(r.rc) for all vertices π£ in Lsubtree: πππ π£, π = π for all vertices π€ in Rsubtree: πππ π , π€ = π for all vertices π£ in Lsubtree: for all vertices π€ in Rsubtree: πππ π£, π€ = π LCA(r.lc) LCA(r.rc)
Def LCA(r): Lsubtree = explore(r.lc) Rsubtree = explore(r.rc) for all vertices π£ in Lsubtree: πππ π£, π = π for all vertices π€ in Rsubtree: πππ π , π€ = π for all vertices π£ in Lsubtree: for all vertices π€ in Rsubtree: πππ π£, π€ = π LCA(r.lc) LCA(r.rc) If the binary tree is balanced, then each recursive call is of size $,-
%
How long does the non-recursive part take?
Def LCA(r): Lsubtree = explore(r.lc) Rsubtree = explore(r.rc) for all vertices π£ in Lsubtree: πππ π£, π = π for all vertices π€ in Rsubtree: πππ π , π€ = π for all vertices π£ in Lsubtree: for all vertices π€ in Rsubtree: πππ π£, π€ = π LCA(r.lc) LCA(r.rc) If the binary tree is balanced, then each recursive call is of size $,-
%
How long does the non-recursive part take? π π = 2π π β 1 2 + O n% Using the master theorem with a=2, b=2, d=2, π π = π π%
Def LCA(r): Lsubtree = explore(r.lc) Rsubtree = explore(r.rc) for all vertices π£ in Lsubtree: πππ π£, π = π for all vertices π€ in Rsubtree: πππ π , π€ = π for all vertices π£ in Lsubtree: for all vertices π€ in Rsubtree: πππ π£, π€ = π LCA(r.lc) LCA(r.rc) If the binary tree is uneven then the runtime recurrence is π π = π π + π π + π ππ Where π is the size of the left subrtree and π is the size of the right subtree. What do you think the total runtime will be? Take a guess and we can check it!!!
3 and a left-over
' $
π§ π¦ π¦#
π§ π¦ π¦#
π§ π¦ π¦#
π
;
!, letβs look in a 2πΓπ rectangle with that point
! " Γ ! " squares then there
can never be more than one point per square.
# we only have to compare it with at
most a constant c points lower than it (smaller y)
# by π§ values.
# we have to sort the vertices in π(πlog π) time and make at
most cπ comparisons in π(π) time for a total combine step of π π log π .
# and so worst case, the combine step
takes π(π log π) time.
# and so worst case, the combine step
takes π(π log π) time.
π π = 2π π 2 + π(π log π) This is T(n) = O(n (log n)^2) Pre-processing : Sort by both x and y, keep pointers between sorted lists Maintain sorting in recursive calls reduces to T(n) =2 T(n/2) +O(n), so T(n) is O(n log n)