SlideShare a Scribd company logo
MERGING IN PARALLEL
ALGORITHM
Submitted To: Submitted By:
Asst. Professor Sneha Garg
Dalapat Songra 14MT-CS15
Merging
• It is defined as follows:
• Let A = (a1, a2, . . .,ar) and B = (b1, b2, . . . , bs) two
sequences of numbers sorted in nondecreasing order
• it is required to merge A and B, that is, to form a
third sequence C = (c1, c2, . . . , cm,), where m=r+s
• C is also in nondecreasing order in which all elements
belongs to either A or B.
A NETWORK FOR MERGING
• Merging will be accomplished by a collection of very
simple processors communicating through a special-
purpose network.
• This special-purpose parallel architecture is known as
an (r, s)-merging network. All the processors to be
used are identical and are called comparators.
two assumptions:
1. the two input sequences are of the same size, that is,
r = s = n >, 1, and
2. n is a power of 2.
• When n = 1, a single comparator produces output in
sorted order.
• When n = 2, the two sequences A = {a1, a2} and B =
{b1, b2} are correctly merged by the network in Fig.
3.2.
• Processor P1 compares the smallest element of A to
the smallest element of B. Its top output must be the
smallest element in C, that is, c1.
• Similarly, the bottom output of P2 , must be c4.
• One additional comparison is performed by P3 to
produce the two middle elements of C.
parallel Merging
ODD EVEN MERGING
• an (n, n)-merging network is obtained by recursive
construction.
• First, the odd-numbered elements of A and B {a1, a3,
a5, . . . , an-1,) and {b1, b3, b5, . . . , bn-1,), are merged
using an (n/2, n/2)-merging network to produce a
sequence {d1, d2, d3, . . . , dn).
• Simultaneously, the even numbered elements of the
two sequences, {a2, a4, a6, . . . , an} and {b2, b4, b6, . . .
, bn}, are also merged using an (n/2,n/2)-merging
network to produce :a sequence {el, e2,e3,. . .,en}.
• The final sequence {c1, c2, . . . , c2n} is now obtained
from C1 = d1, c2n, = en, c2i = min(di + l, ei), and c2i+1
= max(di+1 , ei) for i = I,2 ,..., n- 1.
• The final comparisons are accomplished by a rank of
n - 1 comparators
• Each of the (n/2, n/2)-merging networks is
constructed recursively by using two (n/4, n/4)-
merging networks followed by a rank of (n/2) - 1
comparators.
parallel Merging
A = {3,5,7,9} and B = {2,4,6,8}.
MERGING ON THE CREW MODEL
• Sequential Merging
Parallel Merging
• A CREW SM SIMD computer consists of N processors
PI, P2, . . . , PN.
• Two sequences A and B as input and produces the
sequence C as output,we assume that r <= s.
BINARY SEARCH
procedure CREW MERGE (A, B, C)
parallel Merging
parallel Merging
parallel Merging
parallel Merging
parallel Merging
parallel Merging
parallel Merging
MERGING ON THE EREW MODEL
• an adaptive and cost-optimal parallel algorithm for
merging on the EREW SM SIMD model of
computation.
• The algorithm merges two sorted sequences A = (a1,
a2, . . . , ar) and B = (b1, b2, . . . , bs) into a single
sequence C = {c1,c2,. . . , cm,).
• It uses N processors P1, P2, . . . , PN,
where 1 < N < r + s
Finding the Median of Two Sorted
Sequences
• Given two sorted sequences A = {a1, a2, . . . ,ar} and
B = {b1, b2, . . . , bs), where r, s >= 1
procedure TWO-SEQUENCE MEDIAN (A, B, x, y)
Step 1:
(1.1) lowA = lowB = 1
(1.2) highA = nA = r
(1.3) highB = nB = s
Step 2:
while nA > 1 and nB > 1 do
(2.1) u = lowA + CEILING[(highA - lowA – 1)/2]
(2.2) v = lowB + CEILING[(highB - lowB - 1)/2]
(2.3) w = min(FLOOR(nA/2),FLOOR(nB/2))
(2.4) nA = nA - w
(2.5) nB = nB - w
(2.6) if au >= bv then
(i) highA = highA - w
(ii) lowB = lowB + w
else
(i) lowB = lowB + w
(ii) highB = highB - w
end if
end while.
Step 3:
Return as x and y the indices of the pair from A and B considering lowA to highA ,lowB to highB and
take nA ,nB elements respectively for A and B.
Example
parallel Merging
procedure EREW MERGE (A, B, C)
Step 1:
(1.1) Processor P1 obtains the quadruple (1, r, 1, s)
(1.2) for j = 1 to log N do
for i = 1 to 2^(j-1) do in parallel
Processor Pi having received the quadruple (e, f, g, h)
(1.2.1) {Finds the median pair of two sequences)
TWO-SEQUENCE MEDIAN (A[e, f], B[g, h], x, y)
(1.2.2) {Computes four pointers p1, p2, q1 , and q2, as follows:}
if ax is the median then
(i) p1 = x
(ii) q1 = x + 1
(iii) if by < ax then
(a) p2 = y
(b) q2 = Y + 1
else
(a) p2 = y - 1
(b) q2 = Y
end if
else
(i) P2 = Y
(ii) q2 = y + 1
(iii) if ax < by then
(a) p1 = x
(b) q1 = x + 1
else
(a) p1 = x - 1
(b) q1 = x
end if
end if
(1.2.3) Communicates the quadruple (e, p,, g, p,) to P,, -
(1.2.4) Communicates the quadruple (q,, f, q,, h) to P2,i
end for
end for.
parallel Merging
parallel Merging
parallel Merging
parallel Merging
parallel Merging
parallel Merging
Step 2:
for i = 1 to N do in parallel
Processor Pi having received the quadruple (a, b, c, d)
(2.1) w = 1 + ((i – 1)(r + s))/N
(2.2) z = min{i(r + s)/N, (r + s)}
(2.3) SEQUENTIAL MERGE (A[a, b], B[c, d], C[w, z])
end for.

More Related Content

PPTX
Parallel sorting algorithm
PDF
Matrix chain multiplication
PPT
Time complexity
PPTX
Merge Sort
PPTX
Back face detection
PDF
Recursive algorithms
PPT
Backtracking
PPT
Fundamentals of the Analysis of Algorithm Efficiency
Parallel sorting algorithm
Matrix chain multiplication
Time complexity
Merge Sort
Back face detection
Recursive algorithms
Backtracking
Fundamentals of the Analysis of Algorithm Efficiency

What's hot (20)

PPTX
Recursive Descent Parsing
PPT
Backtracking Algorithm.ppt
PPTX
Regular expression to NFA (Nondeterministic Finite Automata)
PPT
Selection sort
PDF
Code generation in Compiler Design
PDF
Image restoration
PPT
Selection sort
PPTX
Dynamic programming - fundamentals review
PPTX
All pair shortest path
PPTX
Daa:Dynamic Programing
PPT
Graph coloring problem
PPT
Divide and Conquer
PPTX
Row major and column major in 2 d
PPTX
Bruteforce algorithm
PPT
Dinive conquer algorithm
PPTX
1.10. pumping lemma for regular sets
PPTX
Matrix chain multiplication
PPTX
Merge sort algorithm
PDF
Introduction to Algorithms Complexity Analysis
PDF
Binary Search - Design & Analysis of Algorithms
Recursive Descent Parsing
Backtracking Algorithm.ppt
Regular expression to NFA (Nondeterministic Finite Automata)
Selection sort
Code generation in Compiler Design
Image restoration
Selection sort
Dynamic programming - fundamentals review
All pair shortest path
Daa:Dynamic Programing
Graph coloring problem
Divide and Conquer
Row major and column major in 2 d
Bruteforce algorithm
Dinive conquer algorithm
1.10. pumping lemma for regular sets
Matrix chain multiplication
Merge sort algorithm
Introduction to Algorithms Complexity Analysis
Binary Search - Design & Analysis of Algorithms
Ad

Similar to parallel Merging (20)

PDF
Parallel Algorithms
PPS
PRAM algorithms from deepika
PDF
Improved Computing Performance for Listing Combinatorial Algorithms Using Mul...
PDF
IMPROVED COMPUTING PERFORMANCE FOR LISTING COMBINATORIAL ALGORITHMS USING MUL...
PDF
IMPROVED COMPUTING PERFORMANCE FOR LISTING COMBINATORIAL ALGORITHMS USING MUL...
PDF
Parallel Algorithms
PPTX
Unit 2 - 2.2 (Basic Algorithms).pptxeeee
PPTX
PPTX
In-class slides with activities
PPTX
Merge sort
PDF
An adaptive framework towards analyzing the parallel merge sort
PDF
Gk3611601162
PPTX
Data structure 8.pptx
PPT
Sorting algorithms
PPTX
Merge sort algorithm power point presentation
PPT
part-1.ppt high performance computing...
PPT
Tri Merge Sorting Algorithm
PPTX
ch16.pptx
PPTX
ch16 (1).pptx
Parallel Algorithms
PRAM algorithms from deepika
Improved Computing Performance for Listing Combinatorial Algorithms Using Mul...
IMPROVED COMPUTING PERFORMANCE FOR LISTING COMBINATORIAL ALGORITHMS USING MUL...
IMPROVED COMPUTING PERFORMANCE FOR LISTING COMBINATORIAL ALGORITHMS USING MUL...
Parallel Algorithms
Unit 2 - 2.2 (Basic Algorithms).pptxeeee
In-class slides with activities
Merge sort
An adaptive framework towards analyzing the parallel merge sort
Gk3611601162
Data structure 8.pptx
Sorting algorithms
Merge sort algorithm power point presentation
part-1.ppt high performance computing...
Tri Merge Sorting Algorithm
ch16.pptx
ch16 (1).pptx
Ad

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
DOCX
573137875-Attendance-Management-System-original
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Welding lecture in detail for understanding
PPTX
UNIT 4 Total Quality Management .pptx
PDF
PPT on Performance Review to get promotions
PPTX
additive manufacturing of ss316l using mig welding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT
Mechanical Engineering MATERIALS Selection
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
573137875-Attendance-Management-System-original
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Foundation to blockchain - A guide to Blockchain Tech
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Lecture Notes Electrical Wiring System Components
Model Code of Practice - Construction Work - 21102022 .pdf
Internet of Things (IOT) - A guide to understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Welding lecture in detail for understanding
UNIT 4 Total Quality Management .pptx
PPT on Performance Review to get promotions
additive manufacturing of ss316l using mig welding
Embodied AI: Ushering in the Next Era of Intelligent Systems
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mechanical Engineering MATERIALS Selection

parallel Merging

  • 1. MERGING IN PARALLEL ALGORITHM Submitted To: Submitted By: Asst. Professor Sneha Garg Dalapat Songra 14MT-CS15
  • 2. Merging • It is defined as follows: • Let A = (a1, a2, . . .,ar) and B = (b1, b2, . . . , bs) two sequences of numbers sorted in nondecreasing order • it is required to merge A and B, that is, to form a third sequence C = (c1, c2, . . . , cm,), where m=r+s • C is also in nondecreasing order in which all elements belongs to either A or B.
  • 3. A NETWORK FOR MERGING • Merging will be accomplished by a collection of very simple processors communicating through a special- purpose network. • This special-purpose parallel architecture is known as an (r, s)-merging network. All the processors to be used are identical and are called comparators.
  • 4. two assumptions: 1. the two input sequences are of the same size, that is, r = s = n >, 1, and 2. n is a power of 2.
  • 5. • When n = 1, a single comparator produces output in sorted order. • When n = 2, the two sequences A = {a1, a2} and B = {b1, b2} are correctly merged by the network in Fig. 3.2. • Processor P1 compares the smallest element of A to the smallest element of B. Its top output must be the smallest element in C, that is, c1. • Similarly, the bottom output of P2 , must be c4. • One additional comparison is performed by P3 to produce the two middle elements of C.
  • 7. ODD EVEN MERGING • an (n, n)-merging network is obtained by recursive construction. • First, the odd-numbered elements of A and B {a1, a3, a5, . . . , an-1,) and {b1, b3, b5, . . . , bn-1,), are merged using an (n/2, n/2)-merging network to produce a sequence {d1, d2, d3, . . . , dn). • Simultaneously, the even numbered elements of the two sequences, {a2, a4, a6, . . . , an} and {b2, b4, b6, . . . , bn}, are also merged using an (n/2,n/2)-merging network to produce :a sequence {el, e2,e3,. . .,en}.
  • 8. • The final sequence {c1, c2, . . . , c2n} is now obtained from C1 = d1, c2n, = en, c2i = min(di + l, ei), and c2i+1 = max(di+1 , ei) for i = I,2 ,..., n- 1. • The final comparisons are accomplished by a rank of n - 1 comparators • Each of the (n/2, n/2)-merging networks is constructed recursively by using two (n/4, n/4)- merging networks followed by a rank of (n/2) - 1 comparators.
  • 10. A = {3,5,7,9} and B = {2,4,6,8}.
  • 11. MERGING ON THE CREW MODEL • Sequential Merging
  • 12. Parallel Merging • A CREW SM SIMD computer consists of N processors PI, P2, . . . , PN. • Two sequences A and B as input and produces the sequence C as output,we assume that r <= s.
  • 14. procedure CREW MERGE (A, B, C)
  • 22. MERGING ON THE EREW MODEL • an adaptive and cost-optimal parallel algorithm for merging on the EREW SM SIMD model of computation. • The algorithm merges two sorted sequences A = (a1, a2, . . . , ar) and B = (b1, b2, . . . , bs) into a single sequence C = {c1,c2,. . . , cm,). • It uses N processors P1, P2, . . . , PN, where 1 < N < r + s
  • 23. Finding the Median of Two Sorted Sequences • Given two sorted sequences A = {a1, a2, . . . ,ar} and B = {b1, b2, . . . , bs), where r, s >= 1
  • 24. procedure TWO-SEQUENCE MEDIAN (A, B, x, y) Step 1: (1.1) lowA = lowB = 1 (1.2) highA = nA = r (1.3) highB = nB = s Step 2: while nA > 1 and nB > 1 do (2.1) u = lowA + CEILING[(highA - lowA – 1)/2] (2.2) v = lowB + CEILING[(highB - lowB - 1)/2] (2.3) w = min(FLOOR(nA/2),FLOOR(nB/2)) (2.4) nA = nA - w (2.5) nB = nB - w (2.6) if au >= bv then (i) highA = highA - w (ii) lowB = lowB + w else (i) lowB = lowB + w (ii) highB = highB - w end if end while. Step 3: Return as x and y the indices of the pair from A and B considering lowA to highA ,lowB to highB and take nA ,nB elements respectively for A and B.
  • 27. procedure EREW MERGE (A, B, C) Step 1: (1.1) Processor P1 obtains the quadruple (1, r, 1, s) (1.2) for j = 1 to log N do for i = 1 to 2^(j-1) do in parallel Processor Pi having received the quadruple (e, f, g, h) (1.2.1) {Finds the median pair of two sequences) TWO-SEQUENCE MEDIAN (A[e, f], B[g, h], x, y) (1.2.2) {Computes four pointers p1, p2, q1 , and q2, as follows:} if ax is the median then (i) p1 = x (ii) q1 = x + 1 (iii) if by < ax then (a) p2 = y (b) q2 = Y + 1 else (a) p2 = y - 1 (b) q2 = Y end if
  • 28. else (i) P2 = Y (ii) q2 = y + 1 (iii) if ax < by then (a) p1 = x (b) q1 = x + 1 else (a) p1 = x - 1 (b) q1 = x end if end if (1.2.3) Communicates the quadruple (e, p,, g, p,) to P,, - (1.2.4) Communicates the quadruple (q,, f, q,, h) to P2,i end for end for.
  • 35. Step 2: for i = 1 to N do in parallel Processor Pi having received the quadruple (a, b, c, d) (2.1) w = 1 + ((i – 1)(r + s))/N (2.2) z = min{i(r + s)/N, (r + s)} (2.3) SEQUENTIAL MERGE (A[a, b], B[c, d], C[w, z]) end for.