Você está na página 1de 27

One algorithm from The Book:

A tribute to Ira Pohl


Alexander Stepanov
A9.com
http://www.stepanovpapers.com/IraPohlFest.pdf
1

The highest compliment [Erds] could pay


to a colleague's work was to say, That's
straight from The Book.
Encyclopedia Britannica

CS needs its Book


The Book contains algorithms that are:
Beautiful
Optimal
Useful

elements, the median or, as will be discus


minimum and maximum elements.
Algorithms which take unordered
ordered) sets and produce the desired
fundamental to computing, making this ar
most intensely studied both practically a
cally. In the theoretical attacks, the prin
Programming
R. Morris
work has been the comparison. An algor
Techniques
Editor
pared to some norm on the number of
The two norms of most interest are the m
the average. The maximum norm (M-n
greatest number of comparisons the alg
over all possible inputs ( n ! orderings are p
average norm (E-norm) is the average
comparisons the algorithm takes with re
uniform distribution on the possible orderin
algorithm for a given norm is the one th
Copyright (~) 1972, Association for Computing Machinery, Inc.
Pohl
General permission to republish, but not for profit, allI rora part
that norm.
of this material is granted, provided that reference is madeU to
this
niversity of C
a l i f ogoals
r n i a * of this paper are: (1) to de
The
publication, to its date of issue, and to the fact that reprinting
technique for proving algorithms optimal
privileges were granted by permission of the Association for Computing Machinery.
mum norm; and (2) using the technique d
*
Information
and
Computer
Science
Department,
and
A technique for proving min-max norms of sorting
algorithm
for afinding
bo
A sortingexhibit
problema isnew
a special
case of P2
finding
set
Stevenson
College,
University
of
California,
Santa
Cruz,
CA
a l g o r i t h m s is given. O n e new a l g o r i t h m for finding the
partition. The
elements
identified by
rank
in
mum
and are
maximum
of their
a set,
which
is
95060. This paper is a revision of a privately distributed communiminimum
and
maximum
e
l
e
m
e
n
t
s
of
a
set
with
fewest
a
linear
order.
For
a
given
finite
set
cation, March 1970.
optimal in the M-norm.

A Sorting
Problem and
Its Complexity

comparisons is proved o p t i m a l with this technique.


K e y W o r d s a n d P h r a s e s : s o r t i n g , computational
462
complexity, computational combinatorics
C R C a t e g o r i e s : 5.29, 5.31

X = {xl, x 2 , . . . ,

x,}

Communications
There is some
permutation r*, such that
of
XTr*~l) < Xlr*~2)
< . . . < XTr*~n~ .
the ACM

June 1972
Volume 15
Number 6

(Note: we will ignore throughout the case of XTr*<~) 4


=
Other partitions which do not completely sort the

Finding both min and max


To find minimum (or maximum) of n elements
we need n 1 comparisons
Dont we need 2n 2 (or 3?) comparisons to
find both?
3
2
Ira showed that we need at most d 2 ne
comparisons
And he showed that his algorithm is optimal

maybe min or maybe max

not max

not min

not min and not max


6

Strict Weak Ordering


Weak trichotomy

y_y

x_xy

Transitivity

(x

y^y

z) ) x

Irreflexivity, or strictness

(x

x)
7

template <StrictWeakOrdering R>!


struct min_max!
{!
R r;!
!
template <Regular T> // T == Domain<R>!
const T& min(const T& x, !
const T& y) const {!
if (r(y, x)) return y;!
else
return x;!
}!

Weak Commutativity
Is min commutative?
Not for StrictWeakOrdering
Weak Commutativity!

a bb a

Set with min defined is


semigroup
(weak Abelian) semigroup

Weak theories
equivalence axioms (instead of equational)

template <Regular T>


const T& max(const T&
const T&
if (r(y, x)) return
else
return
}!
!

// T == Domain<R>!
x, !
y) const {!
x;!
y;!

10

// the idiot who designed STL wrote:!


template <Regular T> // T == Domain<R>!
const T& max(const T& x, !
const T& y) const {!
if (r(x, y)) return y;!
else
return x;!
}!
!
// why is it wrong?!
!

11

template <Regular T> // T ==


pair<T, T> construct(const T&
const T&
if (r(y, x)) return {y, x};
else
return {x, y};!
}!
!

Domain<R>!
x, !
y) const {!
!

12

template <Regular T> // T == Domain<R>!


pair<T, T> !
combine(const pair<T, T>& x, !
"
"const pair<T, T>& y) const {!
return { min(x.first, y.first), !
max(x.second, y.second) }; !
}!
};!

13

Iterators

Input
Forward
Bidirectional
RandomAccess

14

template <StrictWeakOrdering R>!


struct compare_dereference!
{!
R r;!
!
template <InputIterator I>!
// Domain<R> == ValueType<I>!
bool operator()(const I& i, !
const I& j) const {!
return r(*i, *j);!
}!
};!
!
15

template <ForwardIterator I, !
StrictWeakOrdering R>!
pair<I, I> !
min_max_element_even_length(I first, !
I last, !
R r) {!
// assert(distance(first, last) % 2 == 0)!
min_max<compare_dereference<R>> op{r};!
if (first == last) return {last, last};!
!

16

I prev = first;!
pair<I, I> result = !
op.construct(prev, ++first);!
while (++first != last) {!
prev = first;!
result = op.combine(!
result,
!
op.construct(prev, ++first));!
}!
return result;!
}!

17

template <ForwardIterator I, !
StrictWeakOrdering R>!
pair<I, I> !
min_max_element(I first, I last, R r) {!
min_max<compare_dereference<R>> op{r};!
I prev = first;!
if (first == last || ++first == last) !
"return {prev, prev};!

18

pair<I, I> result = !


op.construct(prev, first);!
while (++first != last) {!
prev = first;!
if (++first == last) !
return op.combine(result, !
{prev, prev});!
result = op.combine(!
result, !
"
op.construct(prev, first));!
}!
return result;!
}!
19

Type Functions
template <InputIterator I> !
using ValueType = typename
!
std::iterator_traits<I>::value_type;!

20

template <InputIterator I, !
StrictWeakOrdering R>!
pair<ValueType<I>, ValueType<I>>
min_max_value_nonempty(I first, !
I last, !
R r) {!
typedef ValueType<I> T; !
min_max<R> op{r};!
T val = *first;!
if (++first == last) return {val, val};!
!

21

pair<T, T> result = !


op.construct(val, *first);!
while (++first != last) {!
val = *first;!
if (++first == last) !
" return op.combine(result, !
{val, val});!
result = op.combine(!
result, !
"
op.construct(val, *first));!
}!
return result;!
}!
22

template <InputIterator I, !
StrictWeakOrdering R>!
pair<ValueType<I>, ValueType<I>>
min_max_value(I first, I last, R r) {!
typedef ValueType<I> T;!
if (first == last) !
return {supremum(r), infimum(r)}!
return min_max_value_nonempty(first, !
last,!
r);!
}!

23

I have been teaching this algorithm every


2 3 years for the last 30 years
When I teach it, I implement it anew
Writing the code and teaching it gives me
joy every time

24

THANK YOU, IRA!

25

Getting rid of an extra compare


// Add to min_max:!
template <Regular T> // T == Domain<R>!
pair<T, T> combine(const pair<T, T>& x,!
const T& val) const {!
if (r(val, x.first)) return { val, x.second};!
if (r(val, x.second)) return x;!
return {x.first, val};!
}!

26

Getting rid of an extra compare (2)


// In min_max_element and!
// min_max_value_nonempty, replace:!
if (++first == last) !
return op.combine(result, {val, val});!
!
// with!
if (++first == last)!
return op.combine(result, val);!

27

Você também pode gostar