Divide and Conquer Summary • Divide – Identify one or more subproblems • Conquer – Solve some or all of those subproblems • Combine – Use the subproblems’ solutions to find large solution
Generic Divide and Conquer Solution def myDCalgo (problem): if baseCase(problem): solution = solve(problem) #brute force if necessary return solution subproblems = Divide(problem) for sub in subproblems: subsolutions.append( myDCalgo (sub)) solution = Combine(subsolutions) return solution 2
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 3
a b Karatsuba × c d 𝑜 10 𝑜 𝑏𝑑 + 10 2 𝑏𝑒 + 𝑐𝑑 + 𝑐𝑒 This can be Can’t avoid these simplified 𝑏 + 𝑐 𝑑 + 𝑒 = 𝑏𝑑 + 𝑏𝑒 + 𝑐𝑑 + 𝑐𝑒 𝑏𝑒 + 𝑐𝑑 = 𝑏 + 𝑐 𝑑 + 𝑒 − 𝑏𝑑 − 𝑐𝑒 Two One multiplication multiplications 4
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
Divide and Conquer Summary • Divide – Identify one or more subproblems: • Conquer – Solve some or all of those subproblems: • Combine – Use the subproblems’ solutions to find large solution:
Generic Divide and Conquer Solution def myDCalgo (problem): if baseCase(problem): solution = solve(problem) #brute force if necessary return solution subproblems = Divide(problem) for sub in subproblems: subsolutions.append( myDCalgo (sub)) solution = Combine(subsolutions) return solution 7
Karatsuba Pseudocode def dc_mult(x, y): 𝑈 𝑜 = 3𝑈 𝑜 2 + 𝑃(𝑜) 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
Maximum Sum Continuous Subarray Problem The maximum-sum subarray of a given array of integers 𝐵 is the interval [𝑏, 𝑐] such that the sum of all values in the array between 𝑏 and 𝑐 inclusive is maximal. Given an array of 𝑜 integers (may include both positive and negative values), give a 𝑃(𝑜 log 𝑜) algorithm for finding the maximum-sum subarray. 9
Naïve Solution 5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 10
What does a 𝑜 log 𝑜 recurrence look like? • 𝑈 𝑜 =
Tree method 𝑈 𝑜 = 2𝑈(𝑜 2 ) + 𝑜 𝑜 𝑜 𝑜 total / level 𝑜 𝑜 𝑜 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 𝑜 𝑗=1 12
Divide and Conquer Θ(𝑜 log 𝑜) 5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Recursively Divide in half Solve on Right Solve on Left 13
Divide and Conquer Θ(𝑜 log 𝑜) Largest sum Largest sum that ends here + that starts here 6 8 1 -7 -3 -6 -13 2 -12 5 13 -37 -42 -20 5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Recursively Divide in half Solve on Right Solve on Left 25 19 Find Largest sum that spans the cut 14
Divide and Conquer Θ(𝑜 log 𝑜) Return the Max of Left, Right, Center 6 8 1 -7 -3 -6 -13 2 -12 5 13 -37 -42 -20 5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Recursively Divide in half Solve on Right Solve on Left 25 19 Find Largest sum that spans 𝑈 𝑜 = 2𝑈 𝑜 the cut 2 + 𝑜 19 15
Divide and Conquer Summary Typically multiple subproblems. • Divide Typically all roughly the same size. – Break the list in half • Conquer – Find the best subarrays on the left and right • Combine – Find the best subarray that “spans the divide” – I.e. the best subarray that ends at the divide concatenated with the best that starts at the divide
Generic Divide and Conquer Solution def myDCalgo (problem): if baseCase(problem): solution = solve(problem) #brute force if necessary return solution subproblems = Divide(problem) for sub in subproblems: subsolutions.append( myDCalgo (sub)) solution = Combine(subsolutions) return solution 17
MSCS Divide and Conquer Θ(𝑜 log 𝑜) def MSCS (list): if list.length < 2: return list[0] #list of size 1 the sum is maximal {listL, listR} = Divide (list) for list in {listL, listR}: subSolutions.append( MSCS (list)) solution = max(solnL, solnR, span(listL, listR)) return solution 18
Recommend
More recommend