CS4102 Algorithms Fall 2020 Warm up π 2 π What is ? π=0
π π π Finite Geometric Series π=0 If π > 1 β β = The series The next term The series The first term multiplied by π in the series 1 + π + π 2 + β― + π π π 1 + π + π 2 + β― + π π 1 π π+1 1 2
π π π Finite Geometric Series π=0 If π < 1 β β = The series The next term The series The first term multiplied by π in the series 1 + π + π 2 + β― + π π π 1 + π + π 2 + β― + π π 1 π π+1 1 3 Solve for the series
CS4102 Algorithms Fall 2020 Another Warm up 1) Rewrite log 10 π so the log on π has base 2 2) Rewrite π log π π so that π is not in the exponent
1) Rewrite log 10 π so the log on π has base 2 Log Rules Rewriting 1. log π π¦ + log π π§ = log π π¦ β π§ 1. log π π 2. y β log π π¦ = log π π¦ π§ 2. log π a log π π (rule 3) 3. π log π π¦ = π¦ 3. log π π β log π π (rule 2) 4. log π π = 1 5. log π 1 = 0 log π π is a constant! (doesnβt depend on π ) log π π = Ξ(log π π)
2) Rewrite π log π π so that π is not in the exponent Log Rules Rewriting 1. π log π π 1. log π π¦ + log π π§ = log π π¦ β π§ 2. y β log π π¦ = log π π¦ π§ 2. π log π π logπ π (rule 3) 3. π log π π¦ = π¦ 3. π (log π n)β (log π π) (rule 2) 4. log π π = 1 π log π π (log π π) 4. (exponentiation rule) 5. log π 1 = 0 5. π log π π (rule 3)
Divide and Conquer β’ Divide: β Break the problem into multiple subproblems, each smaller instances of the original β’ Conquer: β If the suproblems are βlargeβ: β’ Solve each subproblem recursively β If the subproblems are βsmallβ: β’ Solve them directly (base case) β’ Combine: β Merge together solutions to subproblems
Analyzing Divide and Conquer 1. Break into smaller subproblems 2. Use recurrence relation to express recursive running time 3. Use asymptotic notation to simplify β’ Divide: πΈ(π) time, β’ Conquer: recurse on small problems, size π‘ β’ Combine: C(π) time β’ Recurrence: β π π = πΈ π + π(π‘) + π·(π)
Recurrence Solving Techniques Tree ? Guess/Check βCookbookβ 9
Analyzing Merge Sort 1. Break into smaller subproblems 2. Use recurrence relation to express recursive running time 3. Use asymptotic notation to simplify β’ Divide: 0 comparisons β’ Conquer: recursively solve 2 small subproblems, size π 2 β’ Combine: π comparisons β’ Recurrence: π β π π = 2 π 2 + π 10
Recurrence Solving Techniques 1. Draw the recursive structure 2. Label work done within each recursive call Tree 3. Sum work done per recursive depth 4. Find pattern ? 5. Sum work done total Guess/Check β Cookbook β 11
Tree method π π = 2π π Size of the list 2 + π βnon - recursiveβ work done π A call to π mergesort οο π total / level Recursive Calls π π π 2 π 2 2 2 log 2 π levels π π π π π 4 π 4 π 4 π 4 4 4 4 4 of recursion β¦ β¦ β¦ β¦ β¦ 1 1 1 1 1 1 1 1 1 1 1 1 log 2 π π π = π = π log 2 π Base Cases! π=1 12
Multiplication β’ Want to multiply large numbers together 4 1 0 2 π -digit numbers Γ 1 8 1 9 β’ What makes a βgoodβ algorithm? β’ How do we measure input size? β’ What do we βcountβ for run time? 13
βSchoolbookβ Method How many total 4 1 0 2 multiplications? π -digit numbers Γ 1 8 1 9 3 6 9 1 8 π mults 4 1 0 2 π mults π levels 3 2 8 1 6 π mults β Ξ(π 2 ) + 4 1 0 2 π mults 7 4 6 1 5 3 8 14
Divide and Conquer method 1. Break into smaller subproblems π = 10 4 1 0 2 2 + a b a b π = 10 Γ 1 8 1 9 2 c d + c d 10 π Γ ( ) + a c π 10 Γ Γ 2 + ( ) + a d b c Γ ( ) b d 15
D&C Multiplication Pseudocode def dc_mult(x, y): Whatβs missing?! n = length of larger of x,y a = first n/2 digits of x b = last n/2 digits of x c = first n/2 digits of y d = last n/2 digits of y ac = dc_mult(a, c) ad = dc_mult(a, d) bc = dc_mult(b, c) bd = dc_mult(b, d) return ac*10^n + (ad + bc)*10^(n/2) + bd
D&C Multiplication Pseudocode def dc_mult(x, y): n = length of larger of x,y if n == 1: return x*y a = first n/2 digits of x b = last n/2 digits of x Divide c = first n/2 digits of y d = last n/2 digits of y ac = dc_mult(a, c) ad = dc_mult(a, d) Conquer bc = dc_mult(b, c) bd = dc_mult(b, d) return ac*10^n + (ad + bc)*10^(n/2) + bd Combine
Divide and Conquer Multiplication β’ Divide: β Break π -digit numbers into four numbers of π 2 digits each (call them π , π , π , π ) β’ Conquer: β If π > 1 : β’ Recursively compute ππ , ππ , ππ , ππ β If π = 1 : (i.e. one digit each) β’ Compute ππ , ππ , ππ , ππ directly (base case) β’ Combine: π 10 π ππ + 10 2 ππ + ππ + ππ 18
Divide and Conquer method How much total arithmetic? 2. Use recurrence relation to express recursive running time π 10 π ππ + 10 2 ππ + ππ + ππ Recursively solve π π = 4π π 2 + 5π 19
Divide and Conquer method log 2 π π π = 5π 2 π 3. Use asymptotic notation to simplify π π = 4π π 2 + 5π π=0 5π 5π π π π π π 4 5π 5π 5π 5π 2 β 5π 2 2 2 2 2 2 2 2 π π π π π π π π 16 β¦ 5π 5π 5π 5π 5π 5π 5π 5π 4 β 5π 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ β¦ 2 log 2 π β 5π β¦ 5 5 5 5 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 20
Divide and Conquer method 3. Use asymptotic notation to simplify π π = 4π π 2 + 5π log 2 π π π = 5π 2 π π=0 π π = 5π 2 log 2 π+1 β 1 2 β 1 π π = 5π(2π β 1) = Ξ(π 2 ) 21
Karatsuba 1. Break into smaller subproblems π = 10 4 1 0 2 2 + a b a b π = 10 Γ 1 8 1 9 2 c d + c d 10 π Γ ( ) + a c π 10 Γ Γ 2 + ( ) + a d b c Γ ( ) b d 22
a b Karatsuba Γ c d π 10 π ππ + 10 2 ππ + ππ + ππ This can be Canβt avoid these simplified π + π π + π = ππ + ππ + ππ + ππ ππ + ππ = π + π π + π β ππ β ππ Two One multiplication multiplications 23
Karatsuba Pseudocode def dc_mult(x, y): n = length of larger of x,y if n == 1: return x*y a = first n/2 digits of x b = last n/2 digits of x Divide c = first n/2 digits of y d = last n/2 digits of y ac = dc_mult(a, c) bd = dc_mult(b, d) Conquer adbc = dc_mult(a+b, c+d) β ac β bd return ac*10^n + (adbc)*10^(n/2) + bd Combine
a b How much total Karatsuba Γ c d arithmetic? 2. Use recurrence relation to express recursive running time π 10 π ππ + 10 π + π π + π β ππ β ππ + ππ 2 Recursively solve π π = 3π π 2 + 8π 25
Karatsuba β’ Divide: β Break π -digit numbers into four numbers of π 2 digits each (call them π , π , π , π ) β’ Conquer: β If π > 1 : β’ Recursively compute ππ , ππ , π + π π + π β If π = 1 : β’ Compute ππ , ππ , π + π π + π directly (base case) β’ Combine: π β 10 π ππ + 10 π + π π + π β ππ β ππ + ππ 2 26
a b Karatsuba Algorithm Γ c d 1.Recursively compute: ππ , ππ , (π + π)(π + π) 2. ππ + ππ = π + π π + π β ππ β ππ π 3. Return 10 π ππ + 10 2 ππ + ππ + ππ π π = 3π π 2 + 8π Pseudocode 1. π¦ β Karatsuba(π, π) 2. π§ β Karatsuba(π, π) 3. π¨ β Karatsuba(π + π, π + π) β π¦ β π§ 4. Return 10 π π¦ + 10 π 2 π¨ + π§ 27
Karatsuba log 2 π π π = 8π (3 2 ) π 3. Use asymptotic notation to simplify π π = 3π π 2 + 8π π=0 8π π 8π β 1 π π π 8π β 3 8π 8π 8π 2 2 2 2 2 2 2 π π π π π π 8π β 9 β¦ 8π 8π 8π 8π 8π 8π 4 4 4 4 4 4 4 4 4 4 4 4 4 β¦ β¦ β¦ β¦ β¦ β¦ β¦ 8π β 3 log 2 π β¦ 8 8 8 8 8 8 8 8 8 8 1 1 1 1 1 1 2 log 2 π 1 1 1 1 28
Karatsuba 3. Use asymptotic notation to simplify π π = 3π π 2 + 8π log 2 π π π = 8π (3 2 ) π π=0 (3 2 ) log 2 π+1 β1 π π = 8π 3 2 β 1 Lots of arithmetic (see pdf) π π = 24 π log 2 3 β 16π = Ξ(π log 2 3 ) β Ξ(π 1.585 ) 29
π 2 π 1.585 30
Recurrence Solving Techniques Tree ? Guess/Check (induction) 1. Use Tree method to make a guess at the asymptotic running time β Cookbook β 2. Select a specific function with that asymptotic running time 3. Use induction to show the specific function is an upper/lower bound on the actual running time 31
Recommend
More recommend