CS Lunch Mary Allen Wilkes Wednesday 12:15 Kendade 307 2 Divide - - PowerPoint PPT Presentation
CS Lunch Mary Allen Wilkes Wednesday 12:15 Kendade 307 2 Divide - - PowerPoint PPT Presentation
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.
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?
4
Recursion Tree
T(n) T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) T(n/4) T(2) T(2)
Solving recursively Solving base cases
5
T(n) T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) T(n/4) T(2) T(2)
1∙cn 2∙cn/2 4∙cn/ 4 n/2∙c # of problems at this level cost of a subproblem, excluding cost due to recursion
6 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Recursion Tree
T(n) T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) T(n/4) T(2) T(2)
1∙cn 2∙cn/2 4∙cn/ 4 Cost at each recursion level n/2∙2c How many recursion levels?
7-1
Recursion Tree
T(n) T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) T(n/4) T(2) T(2)
1∙cn 2∙cn/2 4∙cn/ 4 Cost at each recursion level n/2∙2c = cn = cn = cn = cn How many recursion levels?
7-2
Recursion Tree
T(n) T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) T(n/4) T(2) T(2)
1∙cn 2∙cn/2 4∙cn/ 4 Cost at each recursion level n/2∙2c = cn = cn = cn = cn How many recursion levels? log2n
7-3 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Recursion Tree
T(n) T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) T(n/4) T(2) T(2)
1∙cn 2∙cn/2 4∙cn/ 4 Cost at each recursion level n/2∙2c = cn = cn = cn = cn How many recursion levels? log2n Total = cn log2n
7-4
Generalizing the Recurrences
If recurrence involves dividing a problem into 2 pieces that are half the original size, and has linear cost
- utside 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
8 Divide + Merge Subproblem size Num subproblems Total cost 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(nlog q) O(n2) n/2 2 O(n2) 9 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Recommender Systems
Netflix tries to match your movie preferences with
- thers.
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!!
10
Counting Inversions
Similarity metric: number of inversions between two rankings. My rank: 1, 2, …, n. Your rank: a1, a2, …, an. Movies i and j are inverted if i < j, but ai > aj.
You Me 1 4 3 2 5 1 3 2 4 5 A B C D E Movies Inversions 3-2, 4-2
What is the brute force algorithm?
11
Divide and Conquer
4 8 10 2 1 5 12 11 3 7 6 9
Count inversions relative to a sorted list
12-1 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Divide and Conquer
4 8 10 2 1 5 12 11 3 7 6 9
Count inversions relative to a sorted list
4 8 10 2 1 5 12 11 3 7 6 9
Divide into 2 sublists of equal size
12-2
Divide and Conquer
4 8 10 2 1 5 12 11 3 7 6 9
Count inversions relative to a sorted list
4 8 10 2 1 5 12 11 3 7 6 9
Divide into 2 sublists of equal size
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
Recursively count the inversions
12-3
Divide and Conquer
4 8 10 2 1 5 12 11 3 7 6 9
Count inversions relative to a sorted list
4 8 10 2 1 5 12 11 3 7 6 9
Divide into 2 sublists of equal size
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
Recursively count the inversions
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. Combine by adding recursive counts and inversions across halves
12-4 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Divide and Conquer
4 8 10 2 1 5 12 11 3 7 6 9
Count inversions relative to a sorted list
4 8 10 2 1 5 12 11 3 7 6 9
Divide into 2 sublists of equal size
5 blue-blue inversions 8 green-green inversions
Recursively count the inversions
9 blue-green inversions Total = 5 + 8 + 9 = 22.
Combine by adding recursive counts and inversions across halves
Cost
13-1
Divide and Conquer
4 8 10 2 1 5 12 11 3 7 6 9
Count inversions relative to a sorted list
4 8 10 2 1 5 12 11 3 7 6 9
Divide into 2 sublists of equal size
5 blue-blue inversions 8 green-green inversions
Recursively count the inversions
9 blue-green inversions Total = 5 + 8 + 9 = 22.
Combine by adding recursive counts and inversions across halves
Cost O(1)
13-2
Divide and Conquer
4 8 10 2 1 5 12 11 3 7 6 9
Count inversions relative to a sorted list
4 8 10 2 1 5 12 11 3 7 6 9
Divide into 2 sublists of equal size
5 blue-blue inversions 8 green-green inversions
Recursively count the inversions
9 blue-green inversions Total = 5 + 8 + 9 = 22.
Combine by adding recursive counts and inversions across halves
Cost O(1) 2*T(n/2)
13-3 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Divide and Conquer
4 8 10 2 1 5 12 11 3 7 6 9
Count inversions relative to a sorted list
4 8 10 2 1 5 12 11 3 7 6 9
Divide into 2 sublists of equal size
5 blue-blue inversions 8 green-green inversions
Recursively count the inversions
9 blue-green inversions Total = 5 + 8 + 9 = 22.
Combine by adding recursive counts and inversions across halves
Cost O(1) 2*T(n/2) ???
13-4
Finding Inversions
Combine: count blue-green inversions Assume each half is sorted. Count inversions where ai and aj are in different halves. Merge two sorted halves into sorted whole.
Variation of mergesort
14
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
numLeft = 6
Total:
15 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
2 numLeft = 6
Total: 6
16
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
2 3 numLeft = 5
Total: 6
17
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 2 3 numLeft = 4
Total: 6
18 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 2 3 numLeft = 3
Total: 6
19
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 11 2 3 numLeft = 3
Total: 6 + 3
20
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 11 14 2 3 numLeft = 2
Total: 6 + 3
21 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 11 14 2 3 16 numLeft = 2
Total: 6 + 3 + 2
22
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 11 14 2 3 16 17 numLeft = 2
Total: 6 + 3 + 2 + 2
23
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 11 14 2 3 18 16 17 numLeft = 1
Total: 6 + 3 + 2 + 2
24 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 11 14 2 3 18 19 16 17 numLeft = 0
Total: 6 + 3 + 2 + 2
25
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 11 14 2 3 18 19 23 16 17 numLeft = 0
Total: 6 + 3 + 2 + 2
26
10 14 18 19 3 7 16 17 23 25 2 11
Merge and Count
Merge and count step. Given two sorted halves, count number of inversions where ai and aj are in different halves. Combine two sorted halves into sorted whole.
7 10 11 14 2 3 18 19 23 25 16 17 numLeft = 0
Total: 6 + 3 + 2 + 2 = 13
27 Slides13 - Recurrences, CountingInversions.key - March 18, 2019
Counting Inversions: Implementation
Sort-and-Count(L) { if list L has one element return (0, L) Divide the list into two halves A and B (rA, A) ← Sort-and-Count(A) (rB, B) ← Sort-and-Count(B) (rC, L) ← Merge-and-Count(A, B) r = rA + rB + rC return (r, L) }
28 Counting Inversions: Implementation
Merge-and-Count (A, B) { curA = 0; curB = 0; count = 0; mergedList = empty list while (not at end of A && not at end of B) { a = A[curA]; b = B[curB]; if (a < b) { append a to mergedList; curA++; else { append b to mergedList; curB++; count = count + num elements left in A } } if (at end of A) append rest of B to mergedList; else append rest of A to mergedList; return (count, mergedList); }