7
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

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

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

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

Page 2: 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

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], ....]

Page 3: 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

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

– Z = [3, 13, 24, 30]● PrefixX =

[2, Z[1], Z[1]+X[3], Z[2], Z[2]+X[5], Z[3], Z[3]+X[7], ....]

Prexif Sums (parallel version)

1. if n = 1 then s(1) := x(1) return2. for 1≤i≤n/2 pardo y(i) := x(2i - 1) * x(2i)3. recursively compute the prefix sums of y(1), ...,

y(n/2) and store them in z(1), ..., z(n/2)4. for 1≤i≤n pardo

i. if i even then s(i) :=z (i/2)ii.if i =1then s(1):= x(1)iii.if i odd then s(i) := z(i-1/2)*x(i)

end

Page 4: 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

T(n) = ????

X 3 5 2 3 4 6 3 1 3 8 10 13 17 23 26 27 S

Y 8 5 10 4 8 13 23 27 Z

Y 13 14 13 27 Z

Y 27 27 Z

T(n) = T(n/2) + O(1) T(n) = O(log n)

W(n) = ????

X 3 5 2 3 4 6 3 1 3 8 10 13 17 23 26 27 S

Y 8 5 10 4 8 13 23 27 Z

Y 13 14 13 27 Z

Y 27 27 Z

T(n) = T(n/2) + O(1) T(n) = O(log n)

work-optimal!!

W(n) = W(n/2) + O(n) W(n) = O(n)

X 3 5 2 3 4 6 3 1 3 8 10 13 17 23 26 27 S

Y 8 5 10 4 8 13 23 27 Z

Y 13 14 13 27 Z

Y 27 27 Z

Prefix Sums via Doubling

● Another interesting technique that can be used to solve the Prexif Sums is the Doubling

– Iterative– A processing technique in which accesses or actions

are governed by increasing powers or 2– That is, processing proceeds by 1, 2, 4, 8, 16, etc.,

doubling on each iteration

Prefix Sums

● At the first step, each X[i] is added to X[i+1]– X = [4, 9, 5, 2, 10, 6, 12, 8]– X1 = [4, 13, 14, 7, 12, 16, 18, 20]

How would you continue?How would you continue?

Prefix Sums

● At the first step, each X[i] is added to X[i+1]– At any time if an index exceeds n, the operation is

supressed– X = [4, 9, 5, 2, 10, 6, 12, 8]– X1 = [4, 13, 14, 7, 12, 16, 18, 20]

● At the second step, each X[i] is added to X[i+2]– X2 = [4, 13, 18, 20, 26, 23, 30, 36]

Next step?Next step?

Page 5: 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

Prefix Sums

● At the first step, each X[i] is added to X[i+1]– At any time if an index exceeds n, the operation is

supressed– X = [4, 9, 5, 2, 10, 6, 12, 8]– X1 = [4, 13, 14, 7, 12, 16, 18, 20]

● 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

Page 6: 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

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

● Total: Σ k=0..log n-1

(n-2k) =

(n-1) + (n-2) + .... + (n-2log n - 1) =(n log n) – (1 + 2 + 4 + 2log n - 1) =

Page 7: 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

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) =(n log n) – (1 + 2 + 4 + 2log n - 1) =(n log n) – (2 log n – 1)

Prefix Sums

AlgorithmAlgorithm WorkWork TimeTime

Sequential N N

Recursive N Log N

Doubling N log N – (2 log N – 1) Log N

List Ranking

● Given a linked list, stored in an array, compute the distance of each element from the end (either end) of the list

– Problem is similar to prefix sums, using all 1’s to sum

● Called Pointer Jumping (not doubling) when using pointers

● Don’t destroy original list!

INPUTINPUT

CONTENTSUCCESSOR

CS

n1 2 3 n

e1 e21

e3ø

OUTPUT OUTPUT

Array R such that R(i) is equal to the distance (rank) of item C(i) from the end of the list.

List Ranking

e1 e2 e3 e4 e5 e6 e7 e8

Idea

● At the beginning we initialize an array R (rank), that will contain the rank of each element

– That is, the distance of each element from the end of the list

R: 1 1 1 1 1 1 1 NIL

e1 e2 e3 e4 e5 e6 e7 e8

Idea

● At the beginning, there are two elements with the correct rank....which oneswhich ones????

R: 1 1 1 1 1 1 1 NIL