Session-4 & 5

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

PRAM ALGORITHMS

PRAM ALGORITHMS

Introduction:
• So far we have discussed algorithms for single processor
• In this session, we introduce parallel machines
• Example: weather forecasting
• If you start today to forecast tomorrow's weather using single processor you may
get result after few days (since its a Compute-intensive problem)
PRAM MODEL

• So we need multiple machines/processors to solve such type of problems.


• Parallel machines offer the potential of decreasing the solution time enormously.
• The idea of parallel computing is very similar.
• Given a problem to solve, we partition the problem into many
subproblems; let each processor work on a subproblem; and when all
the processors are done, the partial solutions are combined to arrive at
the final answer.
• Any algorithm designed for a single-processor machine as a sequential
algorithm
• Any designed for a multiprocessor machine as a parallel algorithm.
MERGING

• Given two sorted sequences, the problem of merging is to combine two sorted sequences
into a single sorted sequence.
ODD-EVEN MERGE


PRAM(Parallel Random Access Machines):
MERGING:
ODD-EVEN Merge algorithm:

Step 0: if m=1, merge the sequence with one


comparison.

Step 1: Partition X1 nad X2 into their odd and even


parts. That is, partition X1 into X1odd = k1,k3….km-1 and
X1even = k2,k4,….km. Similarly, partition X2 into X2odd and
X2even.
Step 2: Recursively merge X1odd with X2odd using m
processors. Let L1 = l1,l2,l3,….lm be the result. Note that
X1odd, X1even,X2odd, and X2even are in sorted order. At the
same time merge X1even with X2even using the other m
processors to get L2 = lm+1, lm+2,…..l2m.
Step 3: Shuffle L1 and L2; that is form the sequence L =
l1, lm+1,l2, lm+2, … lm,l2m. Compare every pair (lm+i,li+1)
interchange them if they are out of order. That is,
compare lm+1 with l2 and interchange them if need be,
compare lm+2 with l3 and interchange them if need be, and
so on. Output the result sequence.
• Sort the following two lists using odd-even merge algorithm
X1={ 2,8,11,13,14,18,21,29} and X2={ 3,6,11,15,16,20,23,31}
SORTING

• Given a sequence of n keys, the problem of sorting is to rearrange the given sequence
either in ascending or descending order.
PRAM SORTING:

ODD-EVEN Sort Algorithm:

Step 0: If n ≤ 1, return X.

Step 1: Let X = k1,k2,k3,… kn be the input. Partition the


input into two:
X1’ = k1,k2,…..k n/2 and X2’ = kn/2+1 , kn/2+2,…..
Kn.
PRAM SORTING:

ODD-EVEN Sort Algorithm:

Step 2: Allocate n/2 processors to sort X1’ recursively.


Let X1 be the result.
At the same time employ the other n/2 processors to
sort X2’ recursively.Let X2 be the same.

Step 3: Merge X1 and X2 using PRAM MERGE


Algorithm and n = 2m processors.
EXAMPLE
• X= 10,7,15,12,5,9,16,20,25,42,39,37,21,14,8,15

• X1’= 10,7,15,12,5,9,16,20 X2’=25,42,39,37,21,14,8,15

Sort Sort
5,7,9,10,12,15,16,20 8,14,15,21,25,3739,42

Odd Even Odd Even


5,9,12,16 7,10,15,20 8,15,25,39 14,21,37,42

Odd merge Even merge


L1: 5, 8, 9, 12,15,16,25,39 L2 : 7, 10,14,15,20,21,37,42

Shuffle L : 5, 7,8,10,9,14,12,15,15,20,16,21,25,37,39,42
Compare
5, 7,8,9,10,12,14,15,15,16, 20,21,25,37,39,42
SAMPLE QUESTIONS

• Differentiate single processor and multiprocessor


• Discuss the purpose of PRAM Algorithms
• Sort the following sequence using odd-even sort algorithm
X={ 4,6,3,17,14,11,2,34,7,5,12,45,21,19,11,32}
• Sort the following two lists using odd-even merge algorithm
X1={ 1,12,18,23,44,48,51,69} and X2={ 13,26,31,35,46,50,71,73}

You might also like