You are on page 1of 22

UMass Lowell Computer Science 91.

503

Analysis of Algorithms
Prof. Karen Daniels
Fall, 2001

Lecture 9
Tuesday, 11/20/01

Parallel Algorithms
Chapters 28, 30

Overview

Sorting Networks

Comparison Networks
0-1 Principle
Bitonic Sorting Network
Merging Network
Sorting Network

Sorting Networks
Chapter 28
Comparison Networks
0-1 Principle
Bitonic Sorting Network
Merging Network
Sorting Network

Comparison Networks:
Definition
Comparison Network only performs comparisons.
Comparisons may occur in parallel.
Comparison Network contains only comparators & wires.
2-input comparator:
input wires

output wires

source: 91.503 textbook Cormen et al.

Comparison Networks:
Definition (continued)
Running Time:
Comparator
uses (1) time.
Define time using
wire depth.
Input wire has
depth = 0.
Comparator
with input wire
depths dx, dy
has output
wire depths =
max(d x , d y ) 1
Depth of
comparison
network = max
depth of a
comparator.

Graph of
interconnections
must be acyclic.

source: 91.503 textbook Cormen et al.

Sorting Network:
Definition
Sorting Network:
Comparison Network for which output

sequence is monotonically increasing

Example:

source: 91.503 textbook Cormen et al.

Sorting Network:
Structure
Families of
Comparison
Networks

COMPARATORs

HALF-CLEANERs
Recursive
Structure
Parallel
MergeSort
Strategy

Sort n values in
O( lg2n ) time

Bitonic
Sorting
Sorting

Merging

Networks

Networks

Networks

BITONIC-SORTERs

MERGERs

SORTERs

0-1 Principle
If sorting network works correctly for {0,1} inputs,
it works correctly on arbitrary input numbers.
allows us to limit attention to {0,1} inputs

Proof relies on function monotonicity:

source: 91.503 textbook Cormen et al.

0-1 Principle (continued)


Proof of Lemma 28.1:
f monotonically increasing
comparator with
inputs f(x), f(y) produces
outputs f(min(x,y)), f(max(x,y))

Induction on wire depth

source: 91.503 textbook Cormen et al.

0-1 Principle (continued)


Example applying Lemma 28.1:

x
f ( x) f
2

source: 91.503 textbook Cormen et al.

0-1 Principle (continued)


If sorting network works correctly for {0,1} inputs,
it works correctly on arbitrary input numbers.

allows us
to limit
attention to
{0,1} inputs

source: 91.503 textbook Cormen et al.

Sorting Network:
Structure
Families of
Comparison
Networks

COMPARATORs

HALF-CLEANERs
Recursive
Structure
Parallel
MergeSort
Strategy

Sort n values in
O( lg2n ) time

Bitonic
Sorting
Sorting

Merging

Networks

Networks

Networks

BITONIC-SORTERs

MERGERs

SORTERs

Bitonic Sorting Network


Bitonic Sequence
monotonically increases then monotonically decreases
or

can be circularly shifted to conform to this

Example:

< 1, 4, 6, 8, 3, 2 >
{0,1} bitonic sequence has structure:
0i

1j 0 k

or

1i 0j 1k

i, j , k 0

Bitonic Sorter:
comparison network that sorts bitonic {0,1} sequences
will be used to construct Sorting Network
source: 91.503 textbook Cormen et al.

Bitonic Sorting Network


Bitonic Sorter uses HALF-CLEANERs
HALF-CLEANER: - comparison network of depth 1
- input line i compared with line i + n/2 for i = 1,2,,n/2

Sample
inputs &
outputs:

source: 91.503 textbook Cormen et al.

Bitonic Sorting Network

source: 91.503 textbook Cormen et al.

Bitonic Sorting Network

source: 91.503 textbook Cormen et al.

Bitonic Sorting Network


BITONICSORTER[n/2]
HALF-CLEANER[n]
BITONICSORTER[n/2]

source: 91.503 textbook Cormen et al.

Recurrence for depth of


BITONIC-SORTER[n]

Sorting Network:
Structure
Families of
Comparison
Networks

COMPARATORs

HALF-CLEANERs
Recursive
Structure
Parallel
MergeSort
Strategy

Sort n values in
O( lg2n ) time

Bitonic
Sorting
Sorting

Merging

Networks

Networks

Networks

BITONIC-SORTERs

MERGERs

SORTERs

Merging Network
Merge 2 sorted input sequences into 1 sorted output sequence.
use modification of BITONIC-SORTER
KEY IDEA: For sorted input sequences X, Y: XYR is bitonic
can merge X, Y, using BITONIC-SORTER(XYR)
challenge: perform reversal implicitly

source: 91.503 textbook Cormen et al.

Merging Network

BITONICSORTER[n/2]

BITONICSORTER[n/2]

source: 91.503 textbook Cormen et al.

Sorting Network:
Structure
Families of
Comparison
Networks

COMPARATORs

HALF-CLEANERs
Recursive
Structure
Parallel
MergeSort
Strategy

Sort n values in
O( lg2n ) time

Bitonic
Sorting
Sorting

Merging

Networks

Networks

Networks

BITONIC-SORTERs

MERGERs

SORTERs

Sorting Network
MERGER[2]

SORTER[n/2]

MERGER[4]
MERGER[2]

MERGER[n]
SORTER[n/2]

MERGER[8]
MERGER[2]
MERGER[4]
MERGER[2]

Recurrence for depth of


SORTER[n]
0
if n 1

D ( n)

k
D
(
n
/
2
)

lg
n
if
n

2
and
k

source: 91.503 textbook Cormen et al.