Fall 2008Array Manipulation Algorithms1. Fall 2008Array Manipulation Algorithms2 Searching Let A =...

25
Fall 2008 Array Manipulation Algori thms 1 Array Manipulation Algorithms
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    0

Transcript of Fall 2008Array Manipulation Algorithms1. Fall 2008Array Manipulation Algorithms2 Searching Let A =...

Fall 2008 Array Manipulation Algorithms 1

Array Manipulation Algorithms

Fall 2008 Array Manipulation Algorithms 2

Searching

Let A = (a1, a2, …, an) be a sorted array of data such that a1< a2< …< an.

Given an element x, we are interested in finding out the index k, such that ak x < ak+1.

It works in O(n) time sequentially.

The binary search method solves the search problem in O(log n) time.

Fall 2008 Array Manipulation Algorithms 3

Binary SearchAlgorithm Binary_Search Input: Array of data a1< a2< …< an and an element x. Output: Index k such that ak x < ak+1.BEGIN a0 = - ; an+1 = +; left = 1; right = n; While (left right) mid = (left+right)/2; Case: x < amid : right = mid – 1; x = amid : Return(mid); x > amid : left = mid + 1; End-case End-while Return(right);END.

Fall 2008 Array Manipulation Algorithms 4

Example of Binary Search

Given array A = (20, 24, 25, 29, 32, 35, 39, 85) and x = 22.Initially: left = 1, right = 8, and mid = 4.

x = 22 < a4 = 29, set right = mid – 1 = 3.

Iteration 1: left = 1, right = 3, and mid = 2.

x = 22 < a2 = 24, set right = mid – 1 = 1.

Iteration 2: left = 1, right = 1, and mid = 1.

x = 22 > a1 = 20, set left = mid + 1 = 2.

Now, left > right, return the value of right = 1 (= k).

Fall 2008 Array Manipulation Algorithms 5

Parallel Search

Algorithm Parallel_Search Input: Array of data a1< a2< …< an and an element x.

Output: Index k such that ak x < ak+1.

BEGIN a0 = - ; an+1 = +;

For k = 0 to n do in parallel If ak x and x < ak+1 then

RESULT = k End-if End-parallelEND. O(1) time, O(n) PEs in CREW PRAM model

Fall 2008 Array Manipulation Algorithms 6

Parallel Search with More Data

When the length of the array is more than the number of processors the algorithm may not be implemented in O(1) time.

Let p be the number of processors and n = pm.

Array can be divided into p segments, and each segment will have r (= n/p) entries.

Fall 2008 Array Manipulation Algorithms 7

Set Ri = 0 if x = air

= -1 if air < x = 1 if x < air

If Ri = 0, then k = ir.

If Ri 0, then choose the segment j where Rj-1 = - 1 and Rj = 1. The value x falls in the segment j.

Repeat the process in segment j until k is found.

a1, a2, …, ar ar+1, ar+2, …, a2r … a(p-1)r+1, …, apr

Segment 1 Segment 2 … Segment p

Fall 2008 Array Manipulation Algorithms 8

Algorithm for Search More Data

Algorithm Parallel SearchMoreData Input: Array of data a1< a2< …< an and an element x, n > p. Output: Index k such that ak x < ak+1.BEGIN If x < a1 then Return (0); Else if x > an then Return(n); End-if r = n / p; For i = 1 to p do in parallel Case: air = x : Ri = 0; air < x : Ri = - 1; air > x : Ri = 1; End-case End-parallel

Fall 2008 Array Manipulation Algorithms 9

For i = 1 to p do in parallel If Ri = 0 then Return(ir); End-if End-parallel R0 = -1; For j = 1 to p do in parallel If Rj = 1 and Rj-1 = - 1 then If ajr-1 = a(j-1)r then Return(jr-1) End-if Return((j-1)r + SearchMoreData(Sub-array in Segment j, x)); End-if End-parallelEND.

O(logpn) time, O(p) PEs in CREW model

Fall 2008 Array Manipulation Algorithms 10

Searching in Unsorted Array

Let A = (a1, a2, …, an) be an array of unsorted entries and x be an given value.

We want to find the index k such that ak = x. If no such k exists, then the value returned is 0.

Fall 2008 Array Manipulation Algorithms 11

Algorithm Parallel_Search Input: Array A = (a1, a2, …, an) and an element x. Output: Index k such that ak = x, if not found, k = 0. (Assuming that there is only one k found.)BEGIN k = 0; For i = 1 to n do in parallel if ai = x then k = i; End-if End-parallelEND.

O(1) time, O(n) PEs in EREW PRAM model

Fall 2008 Array Manipulation Algorithms 12

Merging By Ranking Let A = (a1, a2, …, am) and B = (b1, b2, …, bn) be

two sorted arrays. Merging A and B means forming a new sorted array with the (m+n) elements of A and B.

For example, if A = (2, 4, 11, 12, 14, 35, 95, 99) and B = (6, 7, 9, 25, 26, 31, 42, 85, 87, 102, 105), then the array got by merging A and B is C = (2, 4, 6, 7, 9, 11, 12, 14, 25, 26, 31, 35, 42, 85, 87, 95, 99, 102, 105).

The sequential algorithm works in O(m+n) computing time.

Fall 2008 Array Manipulation Algorithms 13

Sequential MergingAlgorithm Sequential Merge Input: Sorted arrays A and B of size m and n, respectively. Output: Merged sorted array C = (c1, c2, …, cm+n).BEGIN am+1 = bn+1 = +; i = j = k = 1; While k m+n do If ai < bj then ck = ai; i = i + 1; Else ck = bj; j = j + 1; End-if k = k+ 1; End-whileEND;

Fall 2008 Array Manipulation Algorithms 14

Paralleling The Merging Let rank(x:A) being the number of entries in A whi

ch are less than or equal to x. For example: if A = (2, 4, 11, 12, 14, 35, 95, 99) a

nd B = (6, 7, 9, 25, 26, 31, 42, 85, 87, 102, 105), then rank(6:A) = 2, rank(6:B) = 1, rank(9:A) = 2, rank(9:B) = 3, rank(25:A) = 5, rank(25:B) = 4.

We further define that rank(B:A) is an array (r1, r2, …, rm) where ri = rank(bi:A).

rank(B:A) = (2, 2, 2, 5, 5, 5, 6, 6, 6, 8, 8) rank(B:B) = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) rank(A:B) = (0, 0, 3, 3, 3, 6, 9, 9) rank(A:A) = (1, 2, 3, 4, 5, 6, 7, 8)

Fall 2008 Array Manipulation Algorithms 15

rank(x, AB) = rank(x:A) + rank(x:B), when A and B are disjoint.rank(A:AB) = (1, 2, 3, 4, 5, 6, ,7 ,8) + (0, 0, 3, 3, 3, 6, 9, 9) = (1, 2, 6, 7, 8, 12, 16, 17)

rank(B:AB) = rank(B:A) + rank(B:B) = (2, 2, 2, 5, 5, 5, 6, 6, 6, 8, 8) + (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) = (3, 4, 5, 9, 10, 11, 13, 14, 15, 18, 19)

Let RA = rank(A, AB) and RB = rank(B, AB), then C(RAi) = Ai and C(RBi) = Bi . For example:

C(1) = a1= 2, C(6) = a3= 11, C(8) = a5= 14, C(17) = a8= 99,

C(3) = b1= 6, C(9) = b4= 25, C(14) = b8= 85, C(19) = b11=105.

Fall 2008 Array Manipulation Algorithms 16

Parallel MergingAlgorithm Parallel Merging and Ranking Input: Sorted arrays A and B of size m and n, respectively. Output: Merged sorted array C = (c1, c2, …, cm+n).BEGIN For i {1, 2, …, m} and j {1, 2, …, n} do in parallel find rank(ai:A), rank(ai:B), rank(bj:A), rank(bj:B); End-parellel RA = rank(A:A) + rank(A:B); RB = rank(B:A) + rank(B:B); For i = 1 to m do in parallel C(RAi) = Ai; End-parallel For i = 1 to n do in parallel C(RBi) = Bi; End-parallelEND

Do in Parall

el

O(log n) time, O(m+n) PEs in CREW model

n m

Fall 2008 Array Manipulation Algorithms 17

Sorting Algorithms

Computer manufacturers estimate that over 25 percent of the running time of computer is spent on sorting.

Searching a particular entry is made easy only when the file is in a sorted form.

Several sequential sorting algorithms can take O(nlog n) computing time.

Fall 2008 Array Manipulation Algorithms 18

Bubble Sort There is an unsorted array A = (8, 5, 7, 4, 3, 1, 2, 0). Sequential bubble sort:

x1 x2 x3 x4 x5 x6 x7 x8

Original 8 5 7 4 3 1 2 0

Step 1 5 8 7 4 3 1 2 0

Step 2 5 7 8 4 3 1 2 0

Step 7 5 7 4 3 1 2 0 8

Setp 8 5 7 4 3 1 2 0 8

Comparison made: (n-1) + (n-2) + … + 1 = O(n2)

Fall 2008 Array Manipulation Algorithms 19

Parallel Bubble sort Let us employ four processors and assign the two

numbers to each processor.

For odd steps, processor Pi: (x2i-1, x2i), if x2i-1 > x2i, then interchange x2i-1 and x2i, 1 i 4.

For even steps, processor Pi: (x2i, x2i+1), if x2i > x2i+1, then interchange x2i and x2i+1, 1 i 3.

Fall 2008 Array Manipulation Algorithms 20

Example: Parallel Bubble Sort

x1 x2 x3 x4 x5 x6 x7 x8

Original 8 5 7 4 3 1 2 0

Step 1 5 8 4 7 1 3 0 2

Step 2 5 4 8 1 7 0 3 2

Step 3 4 5 1 8 0 7 2 3

Step 4 4 1 5 0 8 2 7 3

Step 5 1 4 0 5 2 8 3 7

Step 6 1 0 4 2 5 3 8 7

Step 7 0 1 2 4 3 5 7 8

Step 8 0 1 2 3 4 5 7 8

Fall 2008 Array Manipulation Algorithms 21

Parallel AlgorithmAlgorithm Parallel Bubble-Sort Input: Unsorted array A(1:n), n is even. Output: Sorted array A(1:n).BEGIN For k = 1 to n If k is odd then For i = 1 to i = n/2 do in parallel If A2i-1 > A2i then interchange them End-if End-parallel Else For i = 1 to i = (n-1)/2 do in parallel If A2i > A2i+1 then interchange them End-if End-parallel End-if End-forEND.

O(n) time, O(n) PEs in EREW PRAM model

Fall 2008 Array Manipulation Algorithms 22

Parallel Merge Sort There is an array x(1:n). If we consider xi alone as an

array, it is sorted already.

We can merge x1 and x2 to get the sorted array (a1, a2).

Similarly, if (x1, x2) and (x3, x4) are sorted arrays, we can merge them to get the sorted array (a1, a2, a3, a4).

x1 x2 x3 x4 x5 x6 x7 x8

Original 8 5 7 4 3 1 2 0

Step 1 5 8 4 7 1 3 0 2

Step 2 4 5 7 8 0 1 2 3

Step 3 0 1 2 3 4 5 7 8

Fall 2008 Array Manipulation Algorithms 23

Parallel AlgorithmAlgorithm Parallel Merge-Sort Input: Unsorted array A(1:n), n = 2k. Output: Sorted array A(1:n).BEGIN For i = 1 to n do in parallel Ti = Ai; End-parallel p = n / 2; Do For j = 1 to p do in parallel Tj = Parallel_Merging_Ranking(T2j-1, T2j); End-parallel While (p = p / 2) > 0; A = T1;END.

O(log2n) time, O(n) PEs in CREW model

Fall 2008 Array Manipulation Algorithms 24

Sorting Networks

It is possible to use a special purpose hardware to sort a collection of data.

There are two preliminary sorting hardware which are capable of sorting two elements.

x

y

Max(x, y)

Min(x, y)

Upward Sorting Network

x

y

Min(x, y)

Max(x, y)

Downward Sorting Network

Fall 2008 Array Manipulation Algorithms 25

Sorting Four Elements

x1

x2

x3

x4

y1

y2

y3

y4