1 CS Lunch Mary Allen Wilkes Wednesday 12:15 Kendade 307 2 Divide and Conquer Divide-and-conquer. Break up problem into several parts. Solve each part recursively. Combine solutions to sub-problems into overall solution. Most common usage. Break up problem of size n into two equal parts of size n/2. Solve two parts recursively. Combine two solutions into overall solution in linear time. Mergesort 3 mergesort(m, low, high) { if high == low { return m[low] Complexity? } else if (high == low + 1) { Base case - O(1) return sort m[low] and m[high]; Divide - O(1) } Recursive cases ?? else { Merge - O(n) middle = (low + high) / 2 left = mergesort(m, low, middle-1) right = mergesort(m, middle, high) return merge(left, right) } } Slides13 - Recurrences, CountingInversions.key - March 18, 2019
4 mergesort Recurrence Relation Note that if n > 2, merge sort of size n requires 2 merge sorts each of size n/2 followed by a merge of all n items: T(n) ≤ 2 ∙ T(n/2) + cn when n > 2 T(2) is O(1) Problem: How do we solve T(n) for a O() value? 5 Recursion Tree T(n) T(n/2) T(n/2) Solving recursively T(n/4) T(n/4) T(n/4) T(n/4) T(2) T(2) Solving base cases 6 cost of a subproblem, excluding cost due to recursion # of problems at this level 1 ∙ cn T(n) 2 ∙ cn/2 T(n/2) T(n/2) 4 ∙ cn/ 4 T(n/4) T(n/4) T(n/4) T(n/4) n/2 ∙ c T(2) T(2) Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Recursion Tree 7-1 Cost at each recursion level 1 ∙ cn T(n) 2 ∙ cn/2 T(n/2) T(n/2) 4 ∙ cn/ 4 T(n/4) T(n/4) T(n/4) T(n/4) n/2 ∙ 2c T(2) T(2) How many recursion levels? Recursion Tree 7-2 Cost at each recursion level 1 ∙ cn = cn T(n) 2 ∙ cn/2 = cn T(n/2) T(n/2) 4 ∙ cn/ 4 = cn T(n/4) T(n/4) T(n/4) T(n/4) n/2 ∙ 2c = cn T(2) T(2) How many recursion levels? Recursion Tree 7-3 Cost at each recursion level 1 ∙ cn = cn T(n) = cn 2 ∙ cn/2 T(n/2) T(n/2) 4 ∙ cn/ 4 = cn T(n/4) T(n/4) T(n/4) T(n/4) n/2 ∙ 2c = cn T(2) T(2) How many recursion levels? log 2 n Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Recursion Tree 7-4 Cost at each recursion level 1 ∙ cn = cn T(n) = cn 2 ∙ cn/2 T(n/2) T(n/2) 4 ∙ cn/ 4 = cn T(n/4) T(n/4) T(n/4) T(n/4) n/2 ∙ 2c = cn T(2) T(2) How many recursion levels? Total = cn log 2 n log 2 n 8 Generalizing the Recurrences If recurrence involves dividing a problem into 2 pieces that are half the original size, and has linear cost outside of the recurrence => O(n log n) Mergesort is just one example If recurrence involves solving 1 subproblem of half the size of the original, and has constant cost outside of the recurrence => O(log n) Binary search is just one example 9 Divide + Subproblem Num subproblems Total cost Merge size O(1) n/2 1 O(log n) O(n) n/2 1 O(n) O(n) n/2 2 O(n log n) O(n) n/2 q > 2 O(n log q ) O(n 2 ) n/2 2 O(n 2 ) Slides13 - Recurrences, CountingInversions.key - March 18, 2019
10 Recommender Systems Netflix tries to match your movie preferences with others. You rank n movies. Netflix consults database to find people with similar tastes. Netflix can recommend to you movies that they liked. Doing this well was worth $1,000,000 to Netflix!! 11 Counting Inversions Similarity metric: number of inversions between two rankings. My rank: 1, 2, …, n. Your rank: a 1 , a 2 , …, a n . Movies i and j are inverted if i < j, but a i > a j . Movies A B C D E Inversions Me 1 2 3 4 5 3-2, 4-2 You 1 3 4 2 5 What is the brute force algorithm? 12-1 Divide and Conquer Count inversions relative to a sorted list 1 5 4 8 10 2 6 9 12 11 3 7 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
12-2 Divide and Conquer Count inversions relative to a sorted list 1 5 4 8 10 2 6 9 12 11 3 7 Divide into 2 sublists of equal size 1 5 4 8 10 2 6 9 12 11 3 7 12-3 Divide and Conquer Count inversions relative to a sorted list 1 5 4 8 10 2 6 9 12 11 3 7 Divide into 2 sublists of equal size 1 5 4 8 10 2 6 9 12 11 3 7 Recursively count the inversions 5 blue-blue inversions 8 green-green inversions 5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-7 12-4 Divide and Conquer Count inversions relative to a sorted list 1 5 4 8 10 2 6 9 12 11 3 7 Divide into 2 sublists of equal size 1 5 4 8 10 2 6 9 12 11 3 7 Recursively count the inversions 5 blue-blue inversions 8 green-green inversions 5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-7 Combine by adding recursive counts and inversions across halves 9 blue-green inversions 5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7 Total = 5 + 8 + 9 = 22. Slides13 - Recurrences, CountingInversions.key - March 18, 2019
13-1 Divide and Conquer Count inversions relative to a sorted list Cost 1 5 4 8 10 2 6 9 12 11 3 7 Divide into 2 sublists of equal size 1 5 4 8 10 2 6 9 12 11 3 7 Recursively count the inversions 5 blue-blue inversions 8 green-green inversions Combine by adding recursive counts and inversions across halves 9 blue-green inversions Total = 5 + 8 + 9 = 22. 13-2 Divide and Conquer Count inversions relative to a sorted list Cost 1 5 4 8 10 2 6 9 12 11 3 7 Divide into 2 sublists of equal size O(1) 1 5 4 8 10 2 6 9 12 11 3 7 Recursively count the inversions 5 blue-blue inversions 8 green-green inversions Combine by adding recursive counts and inversions across halves 9 blue-green inversions Total = 5 + 8 + 9 = 22. 13-3 Divide and Conquer Count inversions relative to a sorted list Cost 1 5 4 8 10 2 6 9 12 11 3 7 Divide into 2 sublists of equal size O(1) 1 5 4 8 10 2 6 9 12 11 3 7 2*T(n/2) Recursively count the inversions 5 blue-blue inversions 8 green-green inversions Combine by adding recursive counts and inversions across halves 9 blue-green inversions Total = 5 + 8 + 9 = 22. Slides13 - Recurrences, CountingInversions.key - March 18, 2019
13-4 Divide and Conquer Count inversions relative to a sorted list Cost 1 5 4 8 10 2 6 9 12 11 3 7 Divide into 2 sublists of equal size O(1) 1 5 4 8 10 2 6 9 12 11 3 7 2*T(n/2) Recursively count the inversions 5 blue-blue inversions 8 green-green inversions Combine by adding recursive counts and ??? inversions across halves 9 blue-green inversions Total = 5 + 8 + 9 = 22. 14 Finding Inversions Variation of mergesort Combine: count blue-green inversions Assume each half is sorted. Count inversions where a i and a j are in different halves. Merge two sorted halves into sorted whole. 15 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 6 3 7 10 14 18 19 2 11 16 17 23 25 Total: Slides13 - Recurrences, CountingInversions.key - March 18, 2019
16 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 6 3 7 10 14 18 19 2 11 16 17 23 25 2 Total: 6 17 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 5 3 7 10 14 18 19 2 11 16 17 23 25 2 3 Total: 6 18 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 4 3 7 10 14 18 19 2 11 16 17 23 25 2 3 7 Total: 6 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
19 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 3 3 7 10 14 18 19 2 11 16 17 23 25 2 3 7 10 Total: 6 20 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 3 3 7 10 14 18 19 2 11 16 17 23 25 2 3 7 10 11 Total: 6 + 3 21 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 2 3 7 10 14 18 19 2 11 16 17 23 25 2 3 7 10 11 14 Total: 6 + 3 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
22 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 2 3 7 10 14 18 19 2 11 16 17 23 25 2 3 7 10 11 14 16 Total: 6 + 3 + 2 23 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 2 3 7 10 14 18 19 2 11 16 17 23 25 2 3 7 10 11 14 16 17 Total: 6 + 3 + 2 + 2 24 Merge and Count Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. numLeft = 1 3 7 10 14 18 19 2 11 16 17 23 25 2 3 7 10 11 14 16 17 18 Total: 6 + 3 + 2 + 2 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Recommend
More recommend