CS4102 Algorithms Fall 2020 Warm up Simplify: 1 + 2 + 3 + β― + (π β 1) + π = 1
π π + 1 1 + 2 + 3 + β― + (π β 1) + π = 2 π π + 1 2
Matrix Multiplication π 1 2 3 2 4 6 π Γ 4 5 6 8 10 12 7 8 9 14 16 18 2 + 16 + 42 4 + 20 + 48 6 + 24 + 54 = β β β β β β 60 72 84 = 132 162 192 204 252 300 Run time? π(π 3 ) 3
Matrix Multiplication D&C Multiply π Γ π matrices ( π΅ and πΆ ) Divide: π 1 π 2 π 3 π 4 π 1 π 2 π 3 π 4 π 5 π 6 π 7 π 8 π 5 π 6 π 7 π 8 π΅ = πΆ = π 9 π 10 π 11 π 12 π 9 π 10 π 11 π 12 π 13 π 14 π 15 π 16 π 13 π 14 π 15 π 16 4
Matrix Multiplication D&C Multiply π Γ π matrices ( π΅ and πΆ ) π 1 π 2 π 3 π 4 π 1 π 2 π 3 π 4 π΅ 1,1 π΅ 1,2 πΆ 1,1 πΆ 1,2 π 5 π 6 π 7 π 8 π 5 π 6 π 7 π 8 π΅ = πΆ = π 9 π 10 π 11 π 12 π 9 π 10 π 11 π 12 π΅ 2,1 π΅ 2,2 πΆ 2,1 πΆ 2,2 π 13 π 14 π 15 π 16 π 13 π 14 π 15 π 16 Combine: π΅πΆ = π΅ 1,1 πΆ 1,1 + π΅ 1,2 πΆ 2,1 π΅ 1,1 πΆ 1,2 + π΅ 1,2 πΆ 2,2 π΅ 2,1 πΆ 1,2 + π΅ 2,2 πΆ 2,2 π΅ 2,1 πΆ 1,1 + π΅ 2,2 πΆ 2,1 π π = 8π π 2 + 4 π 2 Cost of Run time? 2 additions 5
Matrix Multiplication D&C π π = 8π π 2 + 4 π 2 2 π π = 8π π 2 + Ξ(π 2 ) π = 8, π = 2, π π = π 2 Case 1! π log π π = π log 2 8 = π 3 π π = Ξ(π 3 ) We can do betterβ¦ 6
Matrix Multiplication D&C Multiply π Γ π matrices ( π΅ and πΆ ) π 1 π 2 π 3 π 4 π 1 π 2 π 3 π 4 π΅ 1,1 π΅ 1,2 πΆ 1,1 πΆ 1,2 π 5 π 6 π 7 π 8 π 5 π 6 π 7 π 8 π΅ = πΆ = π 9 π 10 π 11 π 12 π 9 π 10 π 11 π 12 π΅ 2,1 π΅ 2,2 πΆ 2,1 πΆ 2,2 π 13 π 14 π 15 π 16 π 13 π 14 π 15 π 16 π΅πΆ = π΅ 1,1 πΆ 1,1 + π΅ 1,2 πΆ 2,1 π΅ 1,1 πΆ 1,2 + π΅ 1,2 πΆ 2,2 π΅ 2,1 πΆ 1,2 + π΅ 2,2 πΆ 2,2 π΅ 2,1 πΆ 1,1 + π΅ 2,2 πΆ 2,1 Idea: Use a Karatsuba-like technique on this 7
Strassenβs Algorithm Multiply π Γ π matrices ( π΅ and πΆ ) π 1 π 2 π 3 π 4 π 1 π 2 π 3 π 4 π΅ 1,1 π΅ 1,2 πΆ 1,1 πΆ 1,2 π 5 π 6 π 7 π 8 π 5 π 6 π 7 π 8 π΅ = πΆ = π 9 π 10 π 11 π 12 π 9 π 10 π 11 π 12 π΅ 2,1 π΅ 2,2 πΆ 2,1 πΆ 2,2 π 13 π 14 π 15 π 16 π 13 π 14 π 15 π 16 Find π΅πΆ : Calculate: π 1 + π 4 β π 5 + π 7 π 3 + π 5 π 1 = π΅ 1,1 + π΅ 2,2 (πΆ 1,1 + πΆ 2,2 ) π 1 β π 2 + π 3 + π 6 π 2 + π 4 π 2 = π΅ 2,1 + π΅ 2,2 πΆ 1,1 π 3 = π΅ 1,1 (πΆ 1,2 β πΆ 2,2 ) π΅ 1,1 πΆ 1,1 + π΅ 1,2 πΆ 2,1 π΅ 1,1 πΆ 1,2 + π΅ 1,2 πΆ 2,2 π 4 = π΅ 2,2 (πΆ 2,1 β πΆ 1,1 ) π΅ 2,1 πΆ 1,2 + π΅ 2,2 πΆ 2,2 π΅ 2,1 πΆ 1,1 + π΅ 2,2 πΆ 2,1 π 5 = π΅ 1,1 + π΅ 1,2 πΆ 2,2 π 6 = π΅ 2,1 β π΅ 1,1 (πΆ 1,1 + πΆ 1,2 ) Number Mults.: 7 Number Adds.: 18 π π = 7π π 2 + 9 π 7 = π΅ 1,2 β π΅ 2,2 (πΆ 2,1 + πΆ 2,2 ) 2 π 2 8
Strassenβs Algorithm π π = 7π π 2 + 9 2 π 2 π = 7, π = 2, π π = 9 2 π 2 Case 1! π log π π = π log 2 7 β π 2.807 π π = Ξ π log 2 7 β Ξ(π 2.807 ) 9
Strassenβs Algorithm π 3 π log 2 7 10
Is this the fastest? Best possible is unknown May not even exist! 11
Recurrence Solving Techniques Tree ? Guess/Check β Cookbook β Substitution 12
Observation β’ Divide: πΈ(π) time, β’ Conquer: recurse on small problems, size π‘ β’ Combine: C(π) time β’ Recurrence: π π = πΈ π + π(π‘) + π·(π) β’ Many D&C recurrences are of form: π π = ππ π π + π(π) 13
Master Theorem π π = ππ π π + π(π) β’ Case 1: if π π = π(π log π π βπ ) for some constant π > 0 , then π π = Ξ(π log π π ) β’ Case 2: if π π = Ξ(π log π π ) , then π π = Ξ(π log π π log π) β’ Case 3: if π π = Ξ©(π log π π+π ) for some constant π > 0 , and if π ππ β€ ππ(π) for some constant π < 1 and all sufficiently large π π , then π π = Ξ(π π ) 14
Substitution Method β’ Idea: take a βdifficultβ recurrence, re -express it such that one of our other methods applies. β’ Example: π π = 2π π + log 2 π 15
Tree method log 2 π 1/2 = 1 2 log 2 π π π = 2π( π) + log 2 π log 2 π log 2 π π 1 1 log 2 π π π + 2 log 2 π 2 log 2 π 1 log 2 log 2 π 1 1 1 4 log 2 π 4 log 2 π 4 log 2 π 4 log 2 π log 2 π + + + π π π π β¦ β¦ β¦ β¦ 2 β¦ 1 1 1 1 1 1 log 2 π 2 2 2 2 2 + + + + + π π = π(log 2 π β log 2 log 2 π) 16
Substitution Method π π = 2π π + log 2 π I donβt like the Β½ in π π = 2π π 1/2 + log 2 π the exponent Let π = 2 π , i.e. π = log 2 π Now the variable is in the exponent on both sides! π π 2 π = 2π 2 + π Rewrite in terms of exponent! 2 π Let π π = 2π + π S will operate exactly as T, just Case 2! 2 redefined in terms of the exponent Let π π = Ξ(π log π) Substitute Back π π = π(2 π ) Let T π = Ξ(log π log log π) 17
Tree method π π 2 π = 2π 2 π π = 2π( π) + log 2 π π = 2 π + π 2 log 2 π log 2 π π 2 π 1 1 log 2 π π π + 2 log 2 π 2 π/2 2 log 2 π 2 π/2 1 1 1 1 4 log 2 π 4 log 2 π 4 log 2 π log 2 log 2 π 4 log 2 π log 2 π + + + 2 π/4 2 π/4 2 π/4 2 π/4 π π π π β¦ β¦ β¦ β¦ 2 β¦ 1 1 1 1 1 1 log 2 π 2 2 2 2 2 + + + + + 18
Tree method π = 2 π π 2 π = 2π(2 π/2 ) + π π π 2 π π π 2 π/2 2 π/2 π + 2 2 log 2 π π π π π π + + 2 π/4 4 2 π/4 4 2 π/4 4 2 π/4 4 + β¦ β¦ β¦ β¦ 2 1 β¦ 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 π + + + + + 19
Tree method π π = 2π π π = 2 π + π π 2 π = π(π) 2 π π π π π π π/2 π/2 + 2 2 log 2 π π π π π π + + 4 4 4 4 + π/4 π/4 π/4 π/4 β¦ β¦ β¦ β¦ 1 β¦ 1 1 1 1 1 1 π 1 1 1 1 1 + + + + + π π = π(π β log 2 π) = π(log 2 π β log 2 log 2 π) 20
Recommend
More recommend