1 / 86
Recap: Prefix Sums
- Given A: set of n integers
- Find B: prefix sums
3 1 1 7 2 5 9 2 4 3 3 3 4 5 12 14 19 28 30 34 37 40
Recap: Prefix Sums Given A : set of n integers Find B : prefix sums - - PowerPoint PPT Presentation
Recap: Prefix Sums Given A : set of n integers Find B : prefix sums A: 3 1 1 7 2 5 9 2 4 3 3 B: 3 4 5 12 14 19 28 30 34 37 40 1 / 86 Recap: Parallel Prefix Sums Recursive algorithm Recursively computes sums Use
1 / 86
3 1 1 7 2 5 9 2 4 3 3 3 4 5 12 14 19 28 30 34 37 40
2 / 86
– Recursively computes sums – Use partial sums to get prefix sums
3 / 86
4 / 86
3 1 1 7 2 5 9 2 3 1 1 7 2 5 9 2
5 / 86
3 1 1 7 2 5 9 2
6 / 86
3 1 1 7 2 5 9 2
7 / 86
3 1 1 7 2 5 9 2
8 / 86
3 1 1 7 2 5 9 2
4 8 7 11
9 / 86
3 1 1 7 2 5 9 2
4 8 7 11 12 18
10 / 86
3 1 1 7 2 5 9 2
4 8 7 11 12 18 30
11 / 86
12 / 86
3 1 1 7 2 5 9 2
4 8 7 11 12 18
13 / 86
3 1 1 7 2 5 9 2
4 8 7 11 12 18
14 / 86
3 1 1 7 2 5 9 2
4 8 7 11 12 18
15 / 86
3 1 1 7 2 5 9 2
4 8 7 11 12
16 / 86
3 1 1 7 2 5 9 2
4 8 7 11 12
17 / 86
3 1 1 7 2 5 9 2
4 12 19 12
18 / 86
3 1 1 7 2 5 9 2
4 12 19 12
19 / 86
3 4 5 12 17 19 28
4 12 19 12
20 / 86
3 4 5 12 17 19 28
3 1 1 7 2 5 9 2
21 / 86
3 4 5 12 17 19 28
3 4 5 12 14 22 28 30
22 / 86
– Create an array of 1s and 0s – Prefix sums gives # of 1s up to each point – Used to separate an array into 2
– separate array into upper-case and lower-case
– separate array into numbers >x and <x
23 / 86
P r e R E c F I
U l M a
S
24 / 86
P r e R E c F I
U l M a
S
25 / 86
1 1 1 1 1 1 1 1
P r e R E c F I
U l M a
S 1 1
26 / 86
1 1 1 1 1 1 1 1
P r e R E c F I
U l M a
S 1 1
27 / 86
1 1 1 1 1 1 1 1
P r e R E c F I
U l M a
S 1 1
28 / 86
1 1 1 2 3 5 5 6 6 7 8 8 4 9
P r e R E c F I
U l M a
S 10 3
29 / 86
– The number of capital letters with index ≤ i
1 1 1 2 3 5 5 6 6 7 8 8 4 9
P r e R E c F I
U l M a
S 10 3
30 / 86
1 1 1 2 3 5 5 6 6 7 8 8 4 9
P r e R E c F I
U l M a
S 10 3
31 / 86
– B[i] is the index of each capital in C!
1 1 1 2 3 5 5 6 6 7 8 8 4 9
P r e R E c F I
U l M a
S 10 3
32 / 86
– B[i] is the index of each capital in C!
2 3 4 5 6 7 1 8 9 10 11 12 13 14 15 16
1 1 1 2 3 5 5 6 6 7 8 8 4 9
P r e R E c F I
U l M a
S 10 3 17 P F I X U S R S M E
33 / 86
2 3 4 5 6 7 1 8 9 10 11 12 13 14 15 16
1 1 1 1 1 1 1
P r e R E c F I
U l M a
S 17 P E F I X U S R S M
34 / 86
2 3 4 5 6 7 1 8 9 10 11 12 13 14 15 16
1 2 3 3 4 5 4 5 6 6 7 6 4 7 1
P r e R E c F I
U l M a
S 7 3 17 P E F I X U S R S M
35 / 86
2 3 4 5 6 7 1 8 9 10 11 12 13 14 15 16
1 2 3 3 4 5 4 5 6 6 7 6 4 7 1
P r e R E c F I
U l M a
S 7 3 17 P E F I X U S R S M
36 / 86
– where j = B[n] + B‘[i] = 10 + B‘[i]
2 3 4 5 6 7 1 8 9 10 11 12 13 14 15 16
1 2 3 3 4 5 4 5 6 6 7 6 4 7 1
P r e R E c F I
U l M a
S 7 3 17 P E F I X U S R S M 2 3 4 5 6 1 7 a r e c
37 / 86
38 / 86
39 / 86
40 / 86
– Works in-place – O(n2) worst-case – BUT O(n log n) expected
– Find pivot – Partition around pivot
41 / 86
42 / 86
3 9 1 7 4 5 8 2
43 / 86
4 7 3 5 8 2
9 1
44 / 86
4 7 3 5 8 2
9 1
45 / 86
4 7 3 5 8 2
9 1
46 / 86
4 7 3 5 8 2
9 1
47 / 86
4 7 3 5 8 2
9 1
48 / 86
4 7 3 5 8 2
9 1
49 / 86
4 7 3 5 8 2
1 9
50 / 86
4 7 3 5 8 2
1 9
51 / 86
4 7 3 5 8 2
1 9
52 / 86
4 7 9 5 8 2
1 3
53 / 86
4 7 9 5 8 2
1 3
54 / 86
4 7 9 5 8 2
1 3
55 / 86
4 7 9 5 8 2
1 3
56 / 86
4 2 9 5 8 7
1 3
57 / 86
4 2 9 5 8 7
1 3
58 / 86
1 4 5 7 8 9
2 3
59 / 86
60 / 86
4 7 3 5 8 2
9 1
61 / 86
4 7 3 5 8 2
9 1
– Prefix sums!
62 / 86
4 7 3 5 8 2
9 1
– 1 if A[i] ≤ A[0] – 0 otherwise
1 1 1
1
63 / 86
4 7 3 5 8 2
9 1
1 2 3 3 3 4
1 2
64 / 86
4 7 3 5 8 2
9 1
– C[B[i]] = A[i]
1 2 3 3 3 4
1 2 4 2
1 3
65 / 86
4 7 3 5 8 2
9 1
– B‘[i] = 1 if A[i] > A[0] – B‘[i] = 0 otherwise
1 1 1
1 4 2
1 3
66 / 86
4 7 3 5 8 2
9 1
2 2 3 4 4
1 1 4 2
1 3
67 / 86
4 7 3 5 8 2
9 1
– C[B[n-1] + B‘[i]] = A[i]
2 2 3 4 4
1 1 4 2
1 3 9 8 7 5
68 / 86
69 / 86
70 / 86
– O(n) extra space needed
– Difficult to make iterative – Perform many small prefix-sums
71 / 86
– One iteration for each level
3 5 2 7 6 9 12 16 19 17 14 22 25 20 23 1
72 / 86
3 5 2 7 6 9 12 16 19 17 14 22 25 20 23 1
73 / 86
– Move pivots to front
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
74 / 86
– Move pivots to front
– Compare each to the pivot in its partition
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
1 1 1 1 1 1 1 1 1 1 1
75 / 86
– Each partition is a separate segment
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
1 1 1 1 1 1 1 1 1 1 1
76 / 86
– Each partition is a separate segment – Can combine into 1 operation...
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
2 2 3 1 2 3 1 2 2 2 3 1 1 2 2 1
77 / 86
– 1 if start of new segment – 0 otherwise
1 4 1 5 2 1 3 4 2 6 1 3 4 3
1 1 1
1
78 / 86
– 1 if start of new segment – 0 otherwise
1 4 1 5 2 1 3 4 2 6 1 3 4 3
1 1 1
1 4 8 1 6 8 9 12 16 0 2 6 1 1 4 8 3
79 / 86
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
1 1 1 1 1 1 1 1 1 1 1
1 1 1
80 / 86
2 2 3 1 2 3 1 2 2 2 3 1 1 2 2 1
1 1 1
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
81 / 86
2 2 3 1 2 3 1 2 2 2 3 1 1 2 2 1
1 2 9 6 7 16 12 14 22 20 3
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
82 / 86
– Build B‘
1 1 1 1 1
1 2 9 6 7 16 12 14 22 20 3
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
83 / 86
– Segmented prefix sums on B‘
1 1 1 2 2 1 1 2
1 2 9 6 7 16 12 14 22 20 3
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
84 / 86
– Copy remaining A values into C
1 2 9 6 7 16 12 14 22 20 3
5 19 17 25 23 1 1 1 2 2 1 1 2
1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
85 / 86
1 1 1 2 2 1 1 2
1 2 9 6 7 16 12 14 22 20 3
5 19 17 25 23 1 5 2 9 6 7 16 12 19 17 14 22 25 20 23 3
86 / 86
– Unroll loops, etc.