0% found this document useful (0 votes)
44 views

Pram Algorithms: Merging and Graph Coloring

This document summarizes an optimal parallel algorithm for merging two sorted arrays A and B, each of length n. The algorithm partitions A and B into subsequences of length O(log n) such that each element of a subsequence of A is smaller than elements in the next subsequence of B. This partitioning takes O(n) time. Each resulting merging subproblem can then be solved optimally in O(log n) time, resulting in an overall optimal O(n) time and O(n) processor algorithm for merging A and B in parallel.

Uploaded by

Arnab Sanyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Pram Algorithms: Merging and Graph Coloring

This document summarizes an optimal parallel algorithm for merging two sorted arrays A and B, each of length n. The algorithm partitions A and B into subsequences of length O(log n) such that each element of a subsequence of A is smaller than elements in the next subsequence of B. This partitioning takes O(n) time. Each resulting merging subproblem can then be solved optimally in O(log n) time, resulting in an overall optimal O(n) time and O(n) processor algorithm for merging A and B in parallel.

Uploaded by

Arnab Sanyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

20082015

PARALLEL AND DISTRIBUTED ALGORITHMS


BY
DEBDEEP MUKHOPADHYAY
AND
ABHISHEK SOMANI

http://cse.iitkgp.ac.in/~debdeep/courses_iitkgp/PAlgo/index.htm

PRAM ALGORITHMS:
MERGING AND GRAPH COLORING
2

20082015

AN OPTIMAL MERGING
We have seen a parallel merging in the last class which takes O(log n)
time with n processors.
What would be a cost-optimal algorithm to perform the merging?
The technique is based on a general strategy which is called
partitioning.
As opposed to Divide and Conquer, the crux lies here in suitably partitioning the
problem which helps combining to get the result easy.

OPTIMAL MERGE-PARTITIONING
b1blogm

blogm+1b2logm

a1aj(1)

aj(1)+1aj(2)

bilogm+1b(i+1)logm

aj(i)+1aj(i+1)

Spawn k(m)=m/log m processors.


Each processor divides the array B into subarrays, Bi, where |Bi|=log m.
It finds rank of bilogm in A using the binary search algorithm, and let
j(i)=rank(bilogm:A).
The configurations of Ai and Bi shown above.

20082015

EXAMPLE
Let A=(4,6,7,10,12,15,18,20), B=(3,9,16,21)
m=4, k(m)=4/log4=2.
B=((3,9),(16,21))
rank(9,A)=3.
Thus A=((4,6,7),(10,12,15,18,20))
Note each element of A1 and B1 is larger than each element in A0 or
B0.
Hence, we can merge A and B by merging separately the pairs (A0,B0)
and (A1,B1).
5

PROOF
Let rank(bilogm:A)=j(i)
Thus we have: aj(i)<bilogm<aj(i)+1.
This result implies that:
bilogm+1>bilogm>aj(i) (thus each element of Bi is larger than each
element of Ai-1)
Likewise, aj(i)+1>bilogm (thus each element of Ai is larger than each
element of Bi-1)

20082015

TIMING ANALYSIS
The finding of j(i)s takes O(n) time, since the binary search is applied
to all the elements of A in parallel.
Thus the total number of operations required to execute this step (of
doing the binary search and partitioning) is
O((logn)xm/log(m))=O(m+n)
Consider the merging of two arrays each of length n.
After the partitioning step we end up with an independent set of
merging sub-problems.
This outcome is the essence of partitioning.
We would like to handle each merging subproblem in O(logn) time, st the algorithm
stays cost optimal.

THE MERGING SUBPROBLEMS


Consider the merging subproblem corresponding to (Ai,Bi). Recall
|Bi|=logn, for all indices i.
If |Ai|=O(log n), we can merge the pair (Ai,Bi) using an optimal
sequential algorithm in O(log n) time.
Otherwise, we apply the previous algorithm to partition Ai into blocks
each of which is of size O(log n) (in this case Ai plays the role of B,
and Bi plays the role of A!)
This step will take O(log log n) time using O(|Ai|) operations.m
Thus we can make each of the subsequences to be of length O(log n)
Then we apply the best sequential algorithm to merge the subsequences in O(logn)
using number of processors
/
, the number of resources also does
not increase asymptotically.

You might also like