Example of usage of Prefix Sum Compacting an Array 0 0 0 e 1 0 0 e 2 0 0 0 e 3 A 0 0 0 e 1 0 0 e 2 0 0 0 e 3 B ● Given an array A with many zeroes, compact it – Output an array B such that all the values in A not zero are at the beginning of B Any idea on the solution (first in sequential)? Any idea on the solution (first in sequential)? Example of usage of Prexix Sum Compacting an Array 0 0 0 e 1 0 0 e 2 0 0 0 e 3 A 0 0 0 e 1 0 0 e 2 0 0 0 e 3 B ● Given an array A with many zeroes, compact it – Output an array B such that all the values in A not zero are at the beginning of B ● Hint: use an extra (binary) array S such that – S[i] == 0 if A[i] == 0 – S[i] == 1 if A[i] != 0 How can we use S? How can we use S? 1 2 3 S Example of usage of Prexix Sum Compacting an Array 0 0 0 e 1 0 0 e 2 0 0 0 e 3 A 0 0 0 e 1 0 0 e 2 0 0 0 e 3 B ● Given an array A with many zeroes, compact it – Output an array B such that all the values in A not zero are at the beginning of B ● Hint2: compute the prexif sum of S – S = [0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 3] And now? And now? 1 2 3 S Example of usage of Prexix Sum Compacting an Array 0 0 0 e 1 0 0 e 2 0 0 0 e 3 A 0 0 0 e 1 0 0 e 2 0 0 0 e 3 B ● Given an array A with many zeroes, compact it – Output an array B such that all the values in A not zero are at the beginning of B ● Initialize B with zeroes ● If A[i] != 0 then B[????] = ???? 1 2 3 S Example of usage of Prexix Sum Compacting an Array 0 0 0 e 1 0 0 e 2 0 0 0 e 3 A 0 0 0 e 1 0 0 e 2 0 0 0 e 3 B ● Given an array A with many zeroes, compact it – Output an array B such that all the values in A not zero are at the beginning of B ● Initialize B with zeroes ● If A[i] != 0 then B[S[i]] = A[i] How would you do it in parallel? How would you do it in parallel? 1 2 3 S Example of usage of Prefix Sum Compacting an Array Algorithm COMPACT 1. assign value 1 to e i and value 0 to the others 2. compute the PREFIX SUMS of these values, and store the results on S 3. begin time of Step 3???? time of Step 3???? for 1 ≤ i ≤ n pardo begin B(i): = 0; if A(i) ≠0 then B( S(i) ):=A(i) end end 0 0 0 e 1 0 0 e 2 0 0 0 e 3 1 2 3 A S 0 0 0 e 1 0 0 e 2 0 0 0 e 3 B
7
Embed
Example of usage of Prefix Sum Example of usage of Prexix Sumsbrinz.di.unipi.it/peppe/FilesPaginaWeb/1-PrefixSum-Web.pdf · 2018. 10. 3. · Example of usage of Prefix Sum Compacting
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Example of usage of Prefix SumCompacting an Array
0 0 0 e1 0 0 e2 0 0 0 e3A 0 00e1 0 0e2 0 0 0e3B
● Given an array A with many zeroes, compact it– Output an array B such that all the values in A not
zero are at the beginning of B
Any idea on the solution (first in sequential)?Any idea on the solution (first in sequential)?
Example of usage of Prexix SumCompacting an Array
0 0 0 e1 0 0 e2 0 0 0 e3A 0 00e1 0 0e2 0 0 0e3B
● Given an array A with many zeroes, compact it– Output an array B such that all the values in A not
zero are at the beginning of B● Hint: use an extra (binary) array S such that
– S[i] == 0 if A[i] == 0– S[i] == 1 if A[i] != 0
How can we use S? How can we use S?
1 2 3S
Example of usage of Prexix SumCompacting an Array
0 0 0 e1 0 0 e2 0 0 0 e3A 0 00e1 0 0e2 0 0 0e3B
● Given an array A with many zeroes, compact it– Output an array B such that all the values in A not
zero are at the beginning of B● Hint2: compute the prexif sum of S
– S = [0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 3]
And now?And now?
1 2 3S
Example of usage of Prexix SumCompacting an Array
0 0 0 e1 0 0 e2 0 0 0 e3A 0 00e1 0 0e2 0 0 0e3B
● Given an array A with many zeroes, compact it– Output an array B such that all the values in A not
zero are at the beginning of B
● Initialize B with zeroes● If A[i] != 0 then B[????] = ????
1 2 3S
Example of usage of Prexix SumCompacting an Array
0 0 0 e1 0 0 e2 0 0 0 e3A 0 00e1 0 0e2 0 0 0e3B
● Given an array A with many zeroes, compact it– Output an array B such that all the values in A not
zero are at the beginning of B
● Initialize B with zeroes● If A[i] != 0 then B[S[i]] = A[i]
How would you do it in parallel?How would you do it in parallel?
1 2 3S
Example of usage of Prefix SumCompacting an Array
Algorithm COMPACT1. assign value 1 to e
i and value 0 to the others
2. compute the PREFIX SUMS of these values, and store the results on S
3. begin time of Step 3????time of Step 3????for 1 ≤ i ≤ n pardo
begin B(i): = 0; if A(i) ≠0 then B( S(i) ):=A(i)end
end
0 0 0 e1 0 0 e2 0 0 0 e31 2 3
AS
0 00e1 0 0e2 0 0 0e3B
Example of usage of Prefix SumCompacting an Array
Algorithm COMPACT1. assign value 1 to e
i and value 0 to the others
2. compute the PREFIX SUMS of these values, and store the results on S
3. begin constant time!constant time!for 1 ≤ i ≤ n pardo
begin B(i): = 0; if A(i) ≠0 then B( S(i) ):=A(i)end
end
0 0 0 e1 0 0 e2 0 0 0 e31 2 3
AS
0 00e1 0 0e2 0 0 0e3B
Prefix Sums
● To understand the parallel solution, let us start from describing it sequentially
● We will first present a recursive solutionrecursive solution
Ideas?Ideas?
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]
How can we use Y?How can we use Y?
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]● Hint: Let us assume to have the prefix sum of Y
– Z = [3, 13, 24, 30]
Can we compute the prexif sums of X from Z?Can we compute the prexif sums of X from Z?
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]● Hint: Let us assume to have the prefix sum of Y
– Z = [3, 13, 24, 30]● PrefixX =
[2, Z[?], ....]
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]● Hint: Let us assume to have the prefix sum of Y
– Z = [3, 13, 24, 30]● PrefixX =
[2, Z[1], ....]
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]● Hint: Let us assume to have the prefix sum of Y
– Z = [3, 13, 24, 30]● PrefixX =
[2, Z[1], ?]
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]● Hint: Let us assume to have the prefix sum of Y
– Z = [3, 13, 24, 30]● PrefixX =
[2, Z[1], Z[1]+X[3], ?]
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]● Hint: Let us assume to have the prefix sum of Y
– Z = [3, 13, 24, 30]● PrefixX =
[2, Z[1], Z[1]+X[3], Z[2], ?]
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]● Hint: Let us assume to have the prefix sum of Y
– Z = [3, 13, 24, 30]● PrefixX =
[2, Z[1], Z[1]+X[3], Z[2], Z[2]+X[5], ....]
Prefix Sums
● X = [2, 1, 4, 6, 9, 2, 1, 5]● Let us reduce the problem to a n/2 array Y, where
each element of Y is the sum in pairs of the elements in X
– Y = [3, 10, 11, 6]● Hint: Let us assume to have the prefix sum of Y
● At the second step, each X[i] is added to X[i+2]– X2 = [4, 13, 18, 20, 26, 23, 30, 36]
● Doubling Time:– At step k, X[i] is added to X[i+2k-1]
Prefix Sums by Doubling* Operation supressed
How many steps do we need to finish?How many steps do we need to finish?
Prefix Sums by Doubling* Operation supressed # contains final sum
Tp = O(????)p = ????
Prefix Sums by Doubling* Operation supressed # contains final sum
Tp = O(log n)p = n-1
Prefix Sums by Doubling
● What about the Work (total number of operations)?– At the first step: ???? operations– At the second step: ???? operations– At the third step: ???? operations– At the kth step: ???? operations
● Total: ???? operations
Prefix Sums by Doubling
● What about the Work (total number of operations)?– At the first step: n-1 operations– At the second step: ???? operations– At the third step: ???? operations– At the kth step: ???? operations
● Total: ???? operations
Prefix Sums by Doubling
● What about the Work (total number of operations)?– At the first step: n-1 operations– At the second step: n-2 operations– At the third step: ???? operations– At the kth step: ???? operations
● Total: ???? operations
Prefix Sums by Doubling
● What about the Work (total number of operations)?– At the first step: n-1 operations– At the second step: n-2 operations– At the third step: n-4 operations– At the kth step: ???? operations
● Total: ???? operations
Prefix Sums by Doubling
● What about the Work (total number of operations)?– At the first step: n-1 operations– At the second step: n-2 operations– At the third step: n-4 operations– At the kth step: n-2k-1 operations
● Total: ???? operations
Prefix Sums by Doubling
● What about the Work (total number of operations)?– At the first step: n-1 operations– At the second step: n-2 operations– At the third step: n-4 operations– At the kth step: n-2k-1 operations
● Total: Σ k=0..log n-1
(n-2k) =
Prefix Sums by Doubling
● What about the Work (total number of operations)?– At the first step: n-1 operations– At the second step: n-2 operations– At the third step: n-4 operations– At the kth step: n-2k-1 operations
● Total: Σ k=0..log n-1
(n-2k) =
(n-1) + (n-2) + .... + (n-2log n - 1) =
Prefix Sums by Doubling
● What about the Work (total number of operations)?– At the first step: n-1 operations– At the second step: n-2 operations– At the third step: n-4 operations– At the kth step: n-2k-1 operations
● What about the Work (total number of operations)?– At the first step: n-1 operations– At the second step: n-2 operations– At the third step: n-4 operations– At the kth step: n-2k-1 operations