Example of usage of Prefix um A 0 0 0 e 1 0 0 0 0 0 B e 1 Example of usage of Prexix um A 0 0 0 e 1 0 0 0 0 0 B e 1 Initialize B with zeroes Any idea on the solution (first in sequential)? If A[i]!= 0 then B[????] =???? Example of usage of Prexix um A 0 0 0 e 1 0 0 0 0 0 B e 1 0 0 0 0 0 0 0 0 Hint: use an extra (binary) array such that [i] == 0 if A[i] == 0 [i] == 1 if A[i]!= 0 Example of usage of Prexix um A 0 0 0 e 1 0 0 0 0 0 B e 1 0 0 0 0 0 0 0 0 Initialize B with zeroes If A[i]!= 0 then B[[i]] = A[i] How can we use? How would you do it in parallel? Example of usage of Prexix um A 0 0 0 e 1 0 0 0 0 0 B e 1 Hint2: compute the prexif sum of = [0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 3] And now? A Example of usage of Prefix um 0 0 0 e 1 0 0 0 0 0 Algorithm COMPACT 1. assign value 1 to e i and value 0 to the others B e 1 2. compute the PREFIX UM of these values, and store the results on 3. begin time of tep 3???? for 1 i n pardo begin B(i): = 0; if A(i) 0 then B( (i) ):=A(i)
A Example of usage of Prefix um 0 0 0 e 1 0 0 0 0 0 Algorithm COMPACT 1. assign value 1 to e i and value 0 to the others 2. compute the PREFIX UM of these values, and store the results on 3. begin constant time! for 1 i n pardo begin B(i): = 0; if A(i) 0 then B( (i) ):=A(i) B e 1 Can we compute the prexif sums of X from Z? To understand the parallel solution, let us start from describing it sequentially We will first present a recursive solution Ideas? [2, Z[?],...] How can we use Y? [2, Z[1],...]
[2, Z[1],?] [2, Z[1], Z[1]+X[3], Z[2], Z[2]+X[5],...] [2, Z[1], Z[1]+X[3],?] [2, Z[1], Z[1]+X[3], Z[2], Z[2]+X[5], Z[3], Z[3]+X[7],...] [2, Z[1], Z[1]+X[3], Z[2],?] Prexif ums (parallel version) 1.if n = 1 then s(1) := x(1) return 2.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)
via Doubling X 3 5 2 3 4 6 3 1 3 8 10 13 17 23 26 27 Y 8 5 10 4 8 13 23 27 Z Y 13 14 13 27 Z Y 27 27 Z T(n) =???? Another interesting technique that can be used to solve the Prexif ums 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 X 3 5 2 3 4 6 3 1 3 8 10 13 17 23 26 27 Y 8 5 10 4 8 13 23 27 Z Y 13 14 13 27 Z Y 27 27 Z 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] T(n) = T(n/2) + O(1) T(n) = O(log n) How would you continue? W(n) =???? X 3 5 2 3 4 6 3 1 3 8 10 13 17 23 26 27 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!! 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] W(n) = W(n/2) + O(n) W(n) = O(n) Next step?
At the first step, each X[i] is added to X[i+1] by Doubling * Operation supressed # contains final sum 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+2 k-1 ] p = n-1 Tp = O(log n) by Doubling by Doubling * Operation supressed At the first step:???? operations At the second step:???? operations At the third step:???? operations How many steps do we need to finish? by Doubling * Operation supressed # contains final sum by Doubling At the second step:???? operations At the third step:???? operations p =???? Tp = O(????)
by Doubling At the third step:???? operations by Doubling by Doubling by Doubling (n-1) + (n-2) +... + (n-2 log n - 1 ) = by Doubling by Doubling (n-1) + (n-2) +... + (n-2 log n - 1 ) = (n log n) (1 + 2 + 4 + 2 log n - 1 ) =
by Doubling List Ranking INPUT C e 1 1 n ø n CONTENT UCCEOR (n-1) + (n-2) +... + (n-2 log n - 1 ) = (n log n) (1 + 2 + 4 + 2 log n - 1 ) = (n log n) (2 log n 1) OUTPUT Array R such that R(i) is equal to the distance (rank) of item C(i) from the of the list. Idea e 1 e 4 e 5 e 6 e 7 e 8 Algorithm Work Time R: 1 1 1 1 1 1 1 NIL equential N N Recursive N Log N Doubling N log N (2 log N 1) Log N 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 of the list List Ranking Given a linked list, stored in an array, compute the distance of each element from the (either ) 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! Idea e 1 e 4 e 5 e 6 e 7 e 8 R: 1 1 1 1 1 1 1 NIL At the beginning, there are two elements with the correct rank...which ones????