http://wrap.warwick.ac.uk/
Original citation:Schönhage, A., Paterson, Michael S. and Pippenger, N. (1975) Finding the median. Coventry, UK: Department of Computer Science. (Theory of Computation Report). CS-RR-006
Permanent WRAP url:
http://wrap.warwick.ac.uk/59399 Copyright and reuse:
The Warwick Research Archive Portal (WRAP) makes this work by researchers of the University of Warwick available open access under the following conditions. Copyright © and all moral rights to the version of the paper presented here belong to the individual author(s) and/or other copyright owners. To the extent reasonable and practicable the material made available in WRAP has been checked for eligibility before being made available.
Copies of full items can be used for personal research or study, educational, or not-for-profit purposes without prior permission or charge. Provided that the authors, title and full bibliographic details are credited, a hyperlink and/or URL is given for the original metadata page and the content is not changed in any way.
A note on versions:
The version presented in WRAP is the published version or, version of record, and may be cited as it appears here.
The
tl
UNIVETS
ty
of
Warwick
A
M.
N.
rl
TF{ECRY
OF
COMPUTATICN
RFPORT
N0. 6
FINDING THE MEDIAN
S chtinh age
Paterson
P'ippenger
Department
of
University
of COVINTRY CV4 ENGLAND.Computer Science
I,'larwi ck
FINDING TI{E MEDIANT
by
ls fi:l :!:l:t
A.Schbnhage,
M.Paterson
and N.Pippenger.,t
Departmentof
Mathematics,University
of
TUbingen, Germany.:'r:'r
pststlment
of
Computen Science,
University
of
Warwick,CoventnY, U.K.
:!:tfs }l3lhgmatical Sciences Depantmentr IBM Research Center,
Yonktor^rn Heights
,
N.Y.'
U. S.A.t
This
resear.ch was supportedin
pant bythe
NationalScience Foundation unde:: nesearch
grant
GJ-34671to
MIT Pro'iect MAC.
Abstnact
An algo::ithmis
descr"ibed which deter,ninesthe
medianof
nelements using
in
the worst
case a numbenof
comparisonsIntroduction
For many p:roblems concerned
with
order"ing elements theneis
onlv
ananrow gap between rowe:: bounds on
the
numbe::of
pai::-wise comDanisonsnequired
in
the
worst
case and upper bounds pnovidedby
simple algor"ithms.For
the
problemof
sorting
n
totally
ordered erements,for
example,rlnnar an'l 'l^r.ron lgsnds as}rrnptotic to nJog.n ane easil-y obtained
soJ.rrl/LvLf
(see
[3],
section
5.3.1). In
contrast
with
thiso
the
pr"oblemof
finding
the
median(or,
whenn is
even,eithen
of
the
tworrmediansil) has p::ovedmuch more
challenging,
andonly
recently
has any upper boundthat is
O(n)been
obtained.
At
presentthe
best
lower bound knor^rnis
l-.75n +o(n)
([2],
I t+]),
whil-ethe
best
upper bound. p:reviousry nepor-tedis
5.43n+
o(n)([r], 5'43...
=
39L/72).
rn
this
paper we shar-r impnovethis
upper boundby
giving
an algonithm usingonly
3n +o(n)
comparisons.We may always assume
that
na rr+ @r? or ?r- -rl to the elements
odd,
for. if n is
even we can adioincompute
either of the
two Itmediansil+^
2,
Theoretical- fi:amewonkWe suppose
that
weare
Bivena
totally
orderedset
T.
Theorder is
not
knowninitialry
and can onry be detenmined by penfonminga
sequenc..of
pair-wise
comparisons between elementsof T.
After
any such sequence,the
curnentinformation
aboutthe
orde:rof
T
is
expressi.ble asa
partial-order
Q onthe
el_ementsof
T.Let
P bea partially
orderedset.
A sequenceof
comparisons betweenelements
of T is said to
produee Pin
T whenthe partial order
Qobtainedrrcontains, P, that is,
when theneis
an onden-preserving embeddingof
Pinto Q. If ltl
=n ) lf l,
wedefine B(prn) to
bethe
minimum numberof
comparisonsrequired in the
wonst case by anoptimal
algonithmto
pnoduceThe
nantial
onde:: Skot k
+ m +m
the
eentnc, whichis
less
than eacheach
of
the
m
r"emaining elements.thein
Hasse diagnams,-m
thus SI
byI
elements has oneparticular
element,of k
othe:: elements andqreater
thanPantial
ou-de::swill
be depicted byThe problem
of finding
thev,
si
in
T,
whenlrl
=
2k+
l-1-^,,-r c^- -r qk tluullu f v! 5\ek /.
median
of
n
elementsis
=n. In this
paper wethe
pr:oblemof
pnoducingshall
denive an upperl?. Yanrs hvnothesis
It is
cleanthat
g(Prn)
is
aalways
ignore
anyextra
elements.shown when P
is
decreasing
functiol, .'f
n,That g(Prn) can
stri'ri
l-ysince one can
decrease
is
-2-tnr thAn oIP I| = \- 7 ' '
clP Al
= I QTn^^
5\r tO,, - / t Dltlue
can be shown
be embedded
8
(asP can
by an erlumenation of cases ) but in thp na*1-ial o?der
r^rhr'nh i< nnndtt.ad ttc.ino 7 qrrmmptnr'n nnmn:niqonq
' eJ
"""-Yao conjectuned
in[5] that
g(Sk,r)
-m--m
=g(sl) forafl.n >k +m+
l-v
n in
r.rnndqthat extra
elements donot help in
producingS,-.
We knowlll
of no
eounter-example, andthis
hypothesis hasa
::emarkabl-e consequencethat stimulated the
developmentof our
algorithm.Theorem
3.1
(Provedin [5] for a
boundof
3n.)Unden Yaots hypothesis, ther.e
is a
median algor.ithmfor n
el-ementsthat
usesat
most2'5n
+o(n)
comparisons.Proof.
Take 4k+ 2
elements and perform 2kf 1
comparisonsto
obtain2k
+ L disjoint pairs.
Applyto the
lowen 2k+ I
el_ements an optimal_='l onrjt-hm J-h:J-
-rodu.es
SI.
Thenesult
containsthe p;rrtial
ord.er shor^rno!6v! r L11"' Lrrq L yr vuquso uk.
in
figune
1 o which contains t3u*t.v
Thus
by
Yaors hyPothesissimilarly
take
6k*
4
el-ements and perform 3k +2
companisonsto
obtain
3k+
2
disjoint pains.
Apply an optimar are;1i.lhmthat
pnoducestin*t
to
the
upper 3k+
2
elements.The::esult
containssjj.*j.
Thus by Yaorshypothes
is
,
^2k+L\
/^2k+t
,, \g(tZt
*I
,, =g("2kn]r
oKt
+J.<3k+2+c(s;k+I)
Combining these
results
we obtain. 2k+1
.^k,s(s;;
-)
..
sk+
3+ s(s;)
n
)k.
)v+1since
c(s;) -
o
ands(s;;)
<
e(t;;;;),
aniteration of this
inequalitv
yields
c(sl)
.< 5k +o(roe
k)Setting
n
=
2k+
I
gives
the
theonem
nThe remainder
of this
papenwil-I
be devotedto
proving (without
usingyaof
s
hypothesis)that
there
is
a median algonithmfor
n
e-Lements tha't:uses
at
most 3n+
o(n)
compar^isons.+.
Factory PnoductionTn ceneral f o npndll.'F m 6] jginr'n+ n^nr'aq n"F : n=nf i:1 '\r'1er. P, wbiCh
-- Lv yt uuuus rrr \rroJvrrrL vvl/r!e
we denote by m
x
P,
mayrequire
fewer than m timesthe
numberof
comparisons'l
tn
nr.odrrr'ea single P.
For example,g(Sirk) = 6 fon k > 5,
butE(2
x Sl) -. ff.
A systematicexploitation of this sort of
ecr:nomyis
achieved
by trfactoniestt.
Weshalt sustain the
metaphonin
which we negandthe
devetopingpartial
orden asa
gl:aph whichis
being rrassembled'r fnomcomponent pants by making new comparisons.
Dtz+.I ,u*rr;+r<,
f 2)
s(s-"'-)
=c(s;
.< 2k +
r
+ e(sl:).-4-A
faetory for a partial- order
Pis a
comparison algorithm withcontinual input
andoutput of
el-ements.Th. input, in the
simplest kindof factory, consists of singleton elements. at intenvals a
newdisjoint
copy
of
Pis output.
Thevital character.istics
o.1,r factory are
thenumber
of
comparisons neededto set
upthe factony, the initial
eost I,
the
nunrbenof
compar^isons then neededto
produce each copyof P,
theun'f
t eost U,
andthildly the
pr.oduetion yesidu.eR,
whichis the
maximumtirlmber
of
elementsthat
can r"emainin the
factony whentack of
input stopspnoduction.
Fonal-l
m >0, I *
m.U gives an upper bound on thenumber
of
comparisonsrequired to
pnoduce mx
P.An unexpected featur:e
of our
median algorithmsis that they
involvev
the factory
productionof
many copiesof Sl for
somekr
wherek
=o(n).
The
following
theor"emis
eventual-l-y provedin section 9, It is
statedhere
to
providemotivation fon the next
section.Theonem 4.1
Fo:: any
k,
there
is
a
factony
'KKF,- for"Sl
with
characteristics
r Il P :c ,r^fined above SuCh that
-k' "k' 'k *"
*"
u.
K-K-K-
5k,
r.
=o(k2)"
R
= o(t 2)fjeveraf
stronger
resu.l-tswitl-
be givenin l-ater sections. At this
stagewe
shall explain
how suchfactories
can beexploited to
provideeconomical-median algorithms.
-5-5.
Basic median algonithmTheonem
5.l-Given
factonies
FUsatisfying
UU-
Akfor
some Ar
OoIk
= O(t<2)and
\
: o(k'),
theneis
a median algonithmwhich,
for
n
elements(n
od,1),uses
at
most An +o(n)
companisons.Proof-
Givenn let k
t +eL r\ - Lrr J . = Lr,$I .
The rrrE qr6vI'rLllu algonithmfinst
-LII.!'L seLU up rngsets
upthe
facto'y
- F,k
using
I,
K-comparisons. The subsequent openationsinvolve
foun interconnected p L"CCeSSeS .(i)
whenevensufficiently
manyinput
erements ane suppliedto F.
a newJ(
v
copy --rJ
of
S,' -k --is
producedat
cost
. "k.U,(ii)
The centresof atr
tire
s|
which a::e pnoduced aneto
beordened.
Aseach new
sf
:-s completed,its
cent:reis
insented using binany insention(see I S]
) into
an ond.ered chainof
pnevious centnes.(
iii )
V{hen nofu
rthen
Sl
-k can be produced,i.e. (i)
hashalted,
considerthe
two extneme ^k'Slrs
of
the
ondenedchain
(seefigune
2J.€.'- .
r rl5. z.
$r
eliminated
eliminated
t;
-6-Suppose
the
chainis of
length
t
andthe
tc-rtal numberof
elements remainingin
the factory
is r.
The centreof
the
upperSf
is
above eachother
centre and above each erementin
the
bottomharf
or
anySf.
Prouid.ed't-l
>r,
this
centre
andall- the
elementsin
the
upperiralf of
the
uPperSf
K an"above more than
half of the total
numbenof
efeniel'Its'
Therefore these k+lelements
are
abovethe
mediano andsimilarly, the
centre and l-owenhalf
ofthe lower Sl
a::e belowthe
med.ian.All-
these2(k+I)
elements can beN
eliminated
fromthe algorithm,
leaving
the
problemof
detenmining thernedian
of
the
remainingelements.
The lowerhalf of
the
uppers]l
J< and the^k
upper
half of
the
lowerSi
are returrledto
the
factor:y as 2k singletonel-ements.
(iv)
When noneof (i), (ii) or (iii)
can proceed we must haven <
\
(fr:om
(i))
andt
-<r+1
(fnom(iii)).
Thusthe
total-
nuinber mof
remainingelements must
satisfY
m =
t(zt+r)
+r
.<(Rk+l)
(zt+r)
n
\
=o(k")
by
the
conditions
of
the
theonem. The medianof
these m elements, whichisthemed.ianoftheor.iginalset'canbefoundbyanylinearmedian
algorithm
or,
as weprefer
hesefon
simplicity'
by
sorting
them usingo(k".1og
k)
comparisons.The
total-
numberof
SIrs
p::oducedin
Fnis
pnecisely
(n-m)7(k+:-)+
t'
where
mrt
are the
nwrbens usedin (iv).
Fon each,the
binanyinsertion
in (ii)
requires
at
mostftoe,
(n/(2k+f))
I
comparisons' The numberof
comparisons used
by
the
camplete algolrithmis
ther:eforeat
moste
rk *
((n-m)/(t+r)
+t)
(uu+
fLoz'
(n/(2k+r))-)+
o(t"'tog
lc)-<
(n/k)ut
n
O((n 1ogn)/k)
+o(k3
rog k)-An
n l
Using Theonem
4.1
we immediately haveCo::ollary 5.2
There
is
a
median algonithmfon
n
elements which nequinesat
most5n
+ o(n)
comparisons.6.
HypenpairsThe
proof
of
Theonem3.1
red usto
the
partiar
o:rders we definehene
as
rfhypenpair.srr, which ane an impontant productof
the
factories
we
shall
descnibe.A hyperpair
P*,
whe::e wis
a
binanystring, is
a
finite partial
onden
with
a distinguished
element,the
eentre rdefined reeu::sivelyby
:(i)
Pr (f is
the
emptystning)
is
a single
el-ement, andA
(ii) .w1-..w0*-vruJvf,^rLvvy4.P--, or P is ntrtar'nod fr:i-.m two disjoint nnn'ieq nf p by
companing
the
centnes andtaking the
highen on lower^of
theserespectively
asthe
new centne.Examples
of
hypenpains,with the
centnes, ane :',,0=
\,P'=
\
,
Porr=
d
-B-Theonem 6.
I
Suppose
l"l
= m and P-- hascentre
c'l'i
\ p
hae ?m elementsrandexactly 2*-l-
.ornparisons ar^e requiredto
\!/
produce
it.
(ii) If
w hash
ones andm-h
zeroes thenc
togethenwith
those efementsh
m-hgreater
than
(l-essthan)c
in
the partial-
ordenare
2"
el-ementsQ""'
e_l-ements) forming
.
Pnh (Prm-n)with
centre
c.
The elements gneaterthan
c
forma
aisjoint
set
of
hyperpair"
P)., PO, POO'...POh-l, and theelements
less
than
c
fonma
disjoint
set
of
hyperpairsP^,
PIr trrr...Prm-h-l
I r r 1 |
IT-:nrr k
has
h
zeroesh
\<
and
h
ones thenP*
containsfigure
4, containedwith
centre
c for
which w =
0IIOIO, h = 3,
andthe
hypenpair ane drawn boldlY.k
D,
K
Pr:oof. (i)
and(ii)
may be provedby induction
onm. (iii)
followsrr'om (1t-l r-J
This
theonemis illustrated
k
=
7.
The slsmentsof
ther r5. T .
1n
1n
by ^7
::
-1
-nr"tntn
I
.
Accounting princip.lesWe have been ::epnesenting
pa::tial
orders bytheir:
Hasse diagrams.It
happensthat
the
pantial- onders which we needto
descr"ibe arenepresentable
by
acyclic
diag::ams.Each compar:ison performed combines twcracyclic
componentsinto one.
For easeof
description
we sometimes ignoreparts
of
the
partial
onder by removing edges fnomthe
diagram.To assess
the
total
numbenof
comparisons usedby
an algonithm wehave found
it
convenientto
countthe
number.of
edgesrfr
r:emovedin
ttr,isway. Initially
theneare
no edges, andfinally, if
we have preservedacyclicityrthere
will
ben-I,
thus the
numberof
companisons made must.be
n-I+n.
Thesuitabitity of this
convention depends r:nthe
special natureof
our
aleorithms.Pnuning
In
Theonem 6.1In
order
to
separ:atefr'nm fhe npst some
15 edges need
to
be(iii)
we showedthat
suitable
hyperpajrs
containthe
subgnaph containing -jusl.; tirese 2k+I elementsof
the
edges must bebroken. In
figure
4 wefjnd
that
nemoved. Ttris operationis
call-eel ptwning.ST
Let
pr" (w) nthe
tpuard-pmtning eostfor
P--, bethe
n'-rmberof
eCF. -,' l- - lj'
that
must be b::okento
nemoveall
el-ements exceptthe
eerrtr:e and thosegreater
thanthe centne.
The doumuardnruning eost J'r,7 P.r, pou(w)js
defined
similarly. In
figur:e 4
we can count pr"(0110J"0,r:10
andpr^(011010)
=
11.The
special
hyperpairs H-for r )
C ane Cefinedby
:n-r.
o
H-p
"l
-
'0
H-p
"2
- '0r
and H^.
zr
- D +-fot(ro)
"H = P t-l
"2t+l-
0r(r0)' -r
for t
>, 1r,. |
^r
Thus I
H"l -
2'.
Notethe
curious
initial
inregularity
of
the
binarvsequences
used.
Theeffect of this will
beto
equalize upward- anddownward-pnuning costs .
Lemma
l
(i)
pr- (olt)
=
2 ,
Dr- (01)
= 2'IO
(ii)
Forall w, pr,
(wOI) = 2pnr(w)+
f
and
Dr
(w]0) =
2p::o(w)t
1(iii)
For:al-l
t
>or Dr.
-_L(Orr(Or)t)
=
e.zt-t
and pr (or(ro)t)
-o
=
3.21-rProof. (i)
By inspection(ii) In
figur:e
5 we showa
PwOI pansedinto
fou:: connected cop.iesof P*. It is
evident
that to
upwa:rd-prune P*0, we mustupward-prune two
of
these copies and renil ,.e onefurther
edge.The
nesult
for
downwand-pnuningof
Purl_Ois
by a
symmetnicargument.
__-"!Y:d:tTlu_*p
upward-prune
nemove edge
€.'a q llnt,r:nJ-nnrtn "-a
n€ P
r 16. J. \rPwor u yr urlrtrtS ". .,n] . wvI
Some useful
Iater
referenceTheorem
8.l-(i) If
any edgethe
centre
is
properties
AS:
of
the
H
nstructures
are coller.:tedfor
nf an H-^
is
l:en',')-r-'dothe
component no'L {:onta.irii:rgr
H
for" somes
< l?.S
(li)
For:h
>-I,
H2h can be pansedinto itn
len'i:re C:.nda.lisjoint
set {H^, Hr
H2,,,.,H2h_l_} wherethe
ceritr"esof
v Jt
Ho, H3o H5,
HTt...'il2h-j_ are
above C andthe
eer:rtresol-Hl' H2'
Iq n H6 , . . .'H2h-2 ane ]relcv"' C "
(iii)
WhenH,- is
upwand-prune<1fon
:.
: 0, 3,
S,
'7r..,
r
lr/) |
or
downward-prunedfor r = f., 2, 4, 6r...
the::e remain2''-'-'
-
t-t.
Jelements, and
the
numberlf
edgesbr:.t^n
;s at n'c''
t/, .2rL r zProof
.
( i.)
and (ii ) are
prcvt'.lby
i.ndrrc*-:i':n "Lemma
I (iii)
and Theorem6"i (ii)
If(
i-ii )
f r:-l.l ows f r'nm{l ,. ,r*;3I.r i: ,2
h
If k.<
2"--l,
Hrn can be prunedto
a:subgraphwith
2l<+J- elemeli-s.
^lccontaining S;.
The pnuningcost is at
mcrst3l +
2h andthe
cc-npon{'nt. rd*'t.lr:hed
are o.f the
form H, s <
2h.s
r;,13i)r1
.
{Jsingthe
Cecompcsiti"onof (ii) of lhe
iheor"ern, we uprrlar'-}-p:'r:r,a
sui"Lable subl.;i-';of rhe
upper hype::pairslo yielc a
tc"t;:t,L qrfk
gf,::li',.:'.-:ancl ,r,:tach
the
:"enaining upperhyperpairs.
Asimilar
operationis
,
,.'*rrmedon
Lhelover hypenpains. It
fc:Ilcws fpom(iii)
cf ii:e
!l'r"1 : ' "'i::rat
this is possible
andthat the cost is
boundedas claimed I
9.
Simplefactories for sl
We desc:ribe
a
factonY FU which producespartiat
orderswith
2k+1el-ernents containing
SI.
Any suchpartial-
ondenwiII
be denoted by-K
-r. h-l h
ail.
Let
h satisfl'r 2"
-.s
k
-<2"-1.
Fr- use:"the following
pair
of
J<' - K
r nf er"r'e I t v+\r ated uvg nrocesses r- v .
Hyperpairing.
Wheneverthere
is
a
pair of
H"'s
in
the
fac.tory i-o:t,srome
r
<
2h-their
centresare
compar"edto
prodricea
new!{'^,r.
l{hent "s',"' n*l
:n H io nnnrirrned it iS Sent tO the pnuning prOCeSS.
or. ,,2h ro }/! vuuL
-L
pruning,
Each Hrnis
pnunedto
an Sn as dei;cnibedin
Cor':l-lary 8.2" The detaeherl hvnerDairs ...tE,. E _H
with s <
2h a::e::eturnedto
the
hyperpair':LngS -v
process.
The S]'is
output
fromthe
factory.
-f
Vlleen
F.
comesto a halt
thene can beat
mos+^''^
^^^'lr 'r€u
fr.-'r,a ,*o LL' d ll.dl-L Lllelc
uqll us q L lllvo L vrrc --yJ "- t'I'
.Jach
r <
2hin the factorY
and so )h2R =2'"-L
<4k--k
-k
To produce m ?utputs
of Sir.rt
most (31:+ 2ir):n
edges are broken ti:L."i.rrf;-v
'),,',,iiingo
2km edgesare
outpr-rtin
tlLeSi's
andat
most 4l'-- edgesft:'' "'
companisons
is at
most(5k+2h)m+I+k2
and
so
Uk: 5k
andThis
specification
of
a
.< 4k
proves Theonem 4.I as prrrTl'-sed.
I
kF
-k
1n Improvements
The
first,
and most obvLous, impnovementto
the
l:asic
algorithn
c..,f
section
5 anises fnomthe
obsenvationthat
the
elements returned fromthe top
on bottom halvesof
the
-Sl-<ts efiminatedat
the
endsof
the
chaink
can be broken
into
(k-1)/2 pairs
and onesingleton.
Whenevera
pair'
instead
of
twosingletons,
is
input
to
the
hypenpair"ing pnocessof
FU,one comparison
is
saved.
Thetotal
numbenof
comparisonsthus
savediri
ti:e
basic
algor"ithmis ]n
+
o(n),
whichyields
a median algor:ithm witironly
4.5n +o(n)
comParisons.A second more fundamental improvemerrt comes by
the
new pllocessof
t'graftingtt.
When an Hrn has been completed bythe
hyperpairing pr"ocess'instead
of
p::uningit
immediately, we compal?e newsingteton
elements withits
centre,
at
the
cost
of
only
oneper
element,until k of
the
newelements
are
alf
above o::all
belowthe centne.
The new elements mav beused
in
the fina]-
5f
urro*ing
moreof
the
hvpenpairs attachedto
ttre:centne
to
be simply detached, breakingjust
one edge, insteadof
beingupwa::d-
or
downwand-pruned which bneaks manyedges.
Fo::'simplicity
we candr.op
the
first
improvement descnibed above sothat
the iactory
inputconsists
just of singletons.
The numberof
edges bnokenin
theconstnuction
of
eacnsf
usingg::afting
is at
nosty'zk + 2h (see TheoremB'l)
instead
of
approximately3k.
This
saving a}l.owsa
median algonithm rrsingat
most 3.5n*
o(n)
companisons. thoughin this
easethe
moresignifir:ant
nesult
is
the following
:-14-Theorem
10.I
For any
k,
there
is
a
factony -KKKF,
fon
Sf
with
U,--
3'5k.This
is
o11rbest
upper bound onthe
unit
cost
when pr"oducing lange numbersv
of Sl's. It
coincides remankablywith the
Louet bound pnovedin
[ 3]fon
the
productionof
a
single
S'f,.Our"
final
task
is to
combinethe
ideasof
the
two impnovementsoe:scnibed above and
to
give
the nesulting
algor:ithmin
gneatendetail.
lI.
Thefinal-
algorithmWe
find it
natur^alto
descnibethe
algo:rithmin
termsof
interconnecting
processeswith
ttpipelines"
conveyingpantially
orderedsets
fromthe
output
of
one processto
the input
of
anothen.
There arealso
two :resenvoirs, one containingsingletons
andthe
othen containingpair"s.
A flow-diagramil-Iustnating
the
intenconnectionsis
givenin
figune
7.Initia[y all n
elements anein
the
singleton
t-eser"voin.(i)
Pairing.
Singletons ane
input
fromthe
singleton reservoi::
wheneverpossible,
comparedin disjoint
pairs
andoutput
to
the
pair reser' -"'
(i-i)
HAperpair+ng.If
noother
processis active, this
process c':t€mptsto
produ,:eone new
H^,
'zn as descnibedin
section
9. If
necessarya
pair
may beinnur
from rrv...--.vr---J.-2nthe
pain
reservoir.
Any new H.Lis
output
to
the grafting
t'i-i.'\ Cpnffa'nn
\ rrf /
T?ris nroness is annl icd
errnn'lrr nf nainc -- r**- r'nnttt fnnm tha
snefi r'no nf en H the nenJ-r'p
-- s- ! r^ro v- .- --2h
pairs
as shownin
figune
6.to
eachll "
fr^om(ii).
ZN
n= i n noq r. rrnr'Yt Al- :nrr
ysfr
has edges
tc a
numbenof
It
uses a^€ !]. ^
D LdEe Vr Llrs
singletons
and
p-r p-r5.
ihaf 1 t I l\t +rrf L4s!!J t
a
newpair
If o-
4D
othenwise
The
resul-t
of=D- =D -0.
Asis
compared with2 max
tor,
no) .<k
-
2nc
in
the following
way :hen
S
?n
J"
t
long
ass
+fhe aanfr o
(A)
comparec with
a, andif c
>a
compal?ec with b'
/r
\b
Pr:
:Pr+l-(B)
comparec
withthese companisons
is
h' =nd if n ( h a.tmnaYle n uriil' a
!t qrrs A+
-o
-oQ)
(3)b
a
+1
s : =s+l_
-16-In
case(2),
the
edge ab has been bnoken and sIn
case(ef)
the
edgecb,
andin
case(as)
tneThe nunrben
of
edges broken upto
a
given stages
+min{pl,
poi
+min{pl
+
l,
po}.has been increased b;" one
"
edge ca o has been bnoken "
is at
mostWhen
s
+
2 max{pl, Po} =k
onk-l-r
the nesulting
partial
onder"is
passed
to
the
p::uning process.(iv)
Pruning.When an
input
is
necej,ved fnom(iii) it is to
be prunedto.trr
-Lc" 5k .i
ln
-- ^.. ^Lsucha
way
that
as many aspossible
of
the
hypenpains attachecj t.othe
centne ane detachedintact.
Thusk -
(s+2pr) elements needto
hepnoduced
by
upward-pr:uning fromthe
uppen har-fof
the
Hrn,
andk -
(s+2po)by
dor"rnwar"d-pruning fromthe
r-owen harf.
Fnom Theor.emB,r (ii)
anri{i;
i jt^re see
that,
by pnuning an appropniate subsetof
the
sub_hlperpairsof
which
nrh
i"
composed, any such numbersof
el.ements may be obtaj.neC r\nanr'earrr Rrr Tlggpgm g.l_ (iii) thp toi_al nf rrnvt suroErJ ' uJ r rreorelll tt. l- ( llf J LrrE Lv Lar ur upward- and downwand -prrrrr ine
costs
is
bounded aboveby % x (total
numbenof
elementsproduced).
Inaddition
at
most 2h edges ane brokento
detacli complete sub-hyperpaips"The nurnben
of
edges bnoken duninggr.afting
and pr"uningvaries
vrith -vdifferent Sit".
The accountingis
madeeasier
if
we a;-so assessat
thi.spoint trr"
,,.,nlur
of
edges which may be b::oken when andif
such anij.
i sultimately
destroyedat
one endof
the
chain,
Eithenthe
upperor
lrr^rerhal-f must be decomposed
into
pains andsingletons fon
recycling
to
t-fr.rresenvoins.
The nurnber"of
edges bnokenconsists
of
onc-:to
detach eachpain
and oneto
detach eachsingfeton,
and.this is
bounde<l abov6 5,7The
total-
of
edges broken duninggraftingr
pruning andthe
final-destruction
of
tfre
Sl is at
mostK
s+2 min{pl_,
po}
+
f +
3/z .(2k-2s-2Pt-2po)+
2h+
}(k+s+t)-(
11"k+
2h+ t/, - Tr,(s
+
2 max{pr,Po})
-< 2k+
2h+
3since
s +
2 max{p-, p }
--L -O >. k-1.(v)
Inserting.
This
process receivesthe
new par"tial- order"sf foot (iv)
andinserts
its
centneinto
the
totally
ordened chainof
centresof
previouspar"tial
ordens, usinga
binary
insention
algorithm.
The numbenof
edgesbr^oken
during
insertion
is
onefess
thanthe
nunberof
compar:isons madeand so
is at
mostlogr(n/k)
since
the::eare
nevelr more thann/(2k+f)
centnesin
the
chain.( vr
.1
It LLmLnat1-ng.This
processis
invokedonly
when noneof
the
pnevious pnocessescan
continue, therefore
we can assunethat
these processesare
netainingat
mostthe following
elements :in (i)
a
singletonin (ii)
one eachof
HIr
H2'...rH2h-l_in (iii)
one Hrn and 2k-4other
eLenentsin (iv)
none,and
both
reservoirs
are
empty'
This
makesa
totaf of at
most,>hrl ? h-]
2zILiL
- r
+
2k-4 -<8k'+
2k-
5 since
2"'-<
k.Suppose
the
chainof
centres maintained by process(v) is of
length
t
at this time.
Then, as arguedin
the
proof
of
Theorem5'I' if
):k
t-I
>
gkz+
2k-
5the
centneof
the highest
Si in
the
chain must be abovethe
median.
Ther:efo::ethis
centre
andall
el-ementsin
the
upperhalf
of
-t-8-:k
this
Sf
may beeliminated
fromthe
algon:'.titm" Similari"vthe
eent::e an,lIower ha.l-f
of
the
l-owest5f in
the
chain may beel-iminated.
The etip.^iconnecting
the
eliminatea
"f"*"nts
to
the
main componentwiII
becounted later".
The lowe:: hal-f
of
the highest
SX anclthe
uppe:rhalf of
the
fcwestif'
ar^e broken down
into
pairs
andsingletons
andrecycled
to the
restrrel:ti.vereservoins.
The edees bnokenin
the
courseof this
have been counte(l'i-l.readv
in
( iv ) .(vii)
Coda.This finishing
pnocessis
invoked when noother
process cancontinue,
soat
most 8k2+
2k-
5 elementsare
netained by pnocesses(i)-(iv),
andthe
chain maintainedby
(v)
and(vi) is of
lengtha
t.< 1+ 8k'+
2k- 5.
Thetotal
numbenof
el-ements nemainingis
thenefr-rr:p0(k").
The medianof
the
oniginal net
is
the
medianof this
remairiinse
set,
which may be for.rnd usingO(k")
comparisons.l'2.
Final- accountThe numbe::
of
edges bnoken dur.ingthe
constructi.on,-lnsertiorrltd
destruction
of o"" Sf,
plus
the
numberof
edges connectingthe
k+.1elements which
are eliminated
is at
most2k+2b+3+logrb/k)+k+t
which
gives a
nurnbenof
3+
0((1ogn)/k)
per
element eii.minated,
Sin,'o?1
n-O(k")
elementsare eliminated
in this
way andthe
e.:oCa usesO(k
)further
companisonsthe
total
fo:: the
whole atgonithmis at
mostChoosing we reach
,l - \!
K
- tn log^
n,z
or.:r
final-
uppen bound of3
3n + O((n
log n)q) -
:n.aa a a Q
aa a a a a singletons
(i)
painingpa]-rs
IIIII
hyperpairing
(
iii )
gr"afting(iv)
pruning(rr) r'n<ani-r'ns
+.rvv- L4lro
vi
)
elirninating(vii)
codaf rE;. t.
-20-Acknowledgements
We were encouraged and suppor:ted
in this
workby
useful-conversations
with
Michael Rabin and severa-l membersof
theTheory
of
Computation Groupat
Pr"oject MAC.Referelrces
t
fl
BIumrM.,FloydrR., PrattrV., RivestrR.
and TarjanrR.,"Time bounds
for selectionr',
JCSS, Z
(I973)
448-461.i21 KirkpatrickrD.,
trTopicsin the
complexityof
combinatorialalgorithms'rn
Tech. Report No. 74 (Dec. 1974), Dept.of
Comp.
Sci.,
LJniv.of
Toronto.t
3l KnuthrD.,
rrTheArt of
Computer Programming", Vol.3,(Addison-Wes1ey, 1973 ) .
t
4] PrattrV.,
andYaorF.o
"On lowen boundsfor
computing ther-th largest
element",Proc.
l-4th Ann. IEEE Symp.onSwitching and Automata Theory, (fgZS) ZC-gf.
t