Chapter 1
Chapter 1
Chapter 1. Foundations
In this book (Chapters 2 and 3),
omputing expe
tations is always redu
ed to
omputing nite sums. Of
ourse, average
ase running time is never larger than
worst
ase running time and sometimes mu
h smaller. However, an average
ase
analysis always poses the following question: does the a
tual use of the algorithm
onform to the probability distribution on whi
h our analysis is based?
We
an now formulate one goal of this book. Determine T (n) for important
A
nately, this goal is beyond our rea
h for many algorithms at the moment. We have
Version: 18.10.99 Time: 17:53 {1{
2
to
onne ourselves to determine upper and lower bounds for T (n), i.e., to asymp-
A
toti
analysis. A typi
al
laim will be: T (n) is bounded above by some quadrati
fun
tion. We write T (n) = O(n ) whi
h means that T (n)
n for
onstants
2 2
> 0 and n and all n n . Or we
laim that T (n) grows at least as fast as n log n.
0 0
We write T (n) =
(n log n) whi
h means that there are
onstants
> 0 and n 0
su
h that T (n)
n log n for all n n (log denotes log to base two throughout
0
this book). We
ome ba
k to this notation in Se
tion 1.6.
We
an also
ompare two algorithms A and A for the same problem. We say
1 2
that A is faster than A if T (n) T (n) for all n and that A is asymptot-
1 2 A1 A2 1
i
ally faster than A if lim !1 T (n)=T (n) = 0. Of
ourse, if A is asymp-
2 n A1 A2 1
toti
ally faster than A then A may still be more eÆ
ient than A on instan
es of
2 2 1
small size. This trivial observation is worth being exemplied.
Let us assume that we have 4 algorithms A; B; C; D for solving problem P with
running times T (n) = 1000n, T (n) = 200n log n, T (n) = 10n and T (n) = 2
A B C
2
D
n
a quadrati
fun
tion. Lower bounds are more diÆ
ult to obtain. A problem P has
omplexity
(n ) if every algorithm for P has running time at least
(n ). Lower
2 2
bound proofs require the dis
ussion of an entire
lass of algorithms and are usually
very diÆ
ult to obtain. Lower bounds are only available in very rare
ir
umstan
es
(
f. Se
tions 2.1.6, 2.3, 3.4 and 5.7).
We will next dene running time and storage spa
e in pre
ise terms. To do so
we have to introdu
e a ma
hine model. We want this ma
hine model to abstra
t
the most important features of existing
omputers, so as to make our analysis
meaningful for every-day
omputing, and to make it simple enough, to make analysis
possible.
a
umulator 0
1 index register 1 1
2 index register 2 2
3 index register 3 3
.. ..
. .
Figure 1. A RAM
The instru
tion set of a RAM
onsists of the following list of one address
instru
tions. We use reg to denote an arbitrary register ,
,
,
, i to denote a
1 2 3
non-negative integer, op to denote an operand of the form i, (i) or reg, and mop
to denote a modied operand of the form (i +
). In applied position operand
j
RAMs. We dis
uss other possibilities at the end of this se
tion. The instru
tion set
onsists of four groups:
Load and store instru
tions:
reg op , e.g.,
1 (2),
mop , e.g., (
2 + 3),
op reg , e.g., (3) ,
mop , e.g., (
2 + 3) .
Jump instru
tions:
goto k , k 2 N0 ,
if reg 0 then goto k , k 2 N0 ,
where 2 f=,6=,<,,>,g is a
omparison operator.
Arithmeti
instru
tions:
op, mop,
where 2 f+, ,,div,modg is an arithmeti
operator.
Version: 18.10.99 Time: 17:53 {3{
4
Indexregister instru
tions:
j
i j , 1j3 , i2N :
0
0:
1 (0) 1
1: 1 1
2: if
1 = 0 then goto 6 n+1
3: 2 n
4:
1
1 1 n
5: goto 2 n
6: (1) 1
Program 1
The
ost of a
onditional jump if reg 0 then goto k is independent of the
ontent
of reg be
ause all
omparison operators require only that one
he
ks some few bits
of the binary representation of reg.
Under the unit
ost measure the
ost of an instru
tion is 1 + # of storage a
-
esses (we use the symbol # with the meaning of \number"), under the logarithmi
ost measure it is 1+ sum of the lengths of the addresses and numbers involved.
Thus the exe
ution time of an instru
tion is independent of the data in the unit
ost measure, but it depends on the data in the logarithmi
ost measure.
Example (
ontinued): The instru
tions of program 1 have the following
osts:
Instru
tion Unit Cost Logarithmi
Cost
0 2 L(0) + 1 + L((0))
1 1 1 + L(1)
2 1 1 + L(6)
3 1 1 + L() + L(2)
4 1 1 + L(
) + L(1) 1
5 1 1 + L(2)
6 2 L(1) + 1 + L()
We thus have total
ost 4n + 6 under the unit
ost measure and (n ) under the 2
logarithmi
ost measure (
ompare Se
tion 1.6 for a denition of ). Note that the
ost of line 3 is P (1+ L(2 )+ L(2)) = (n ). Note also, that one
an redu
e the
n
i=0
1 i 2
ost of
omputing 2 to (log n) under the unit
ost measure and to (n) under
n
Hen
e spa
e
omplexity is (L(2 )) = (n) under the logarithmi
ost measure.
n
Address modi
ation by index registers is the only means of address modi
ation
in RAMs. Most realisti
omputers allow two other te
hniques for address mod-
i
ation: general address substitution and dire
t address
al
ulation. General
address substitution allows us to use any lo
ation as an index register, i.e., mod-
ied operands of the form (i + (j ))
an be used also. The
ost of fet
hing su
h
an operand is 2 in the unit
ost and L(i) + L(j ) + L((j )) in the logarithmi
ost
measure. It is not too hard to simulate the enlarged instru
tion set by our original
instru
tion set with only a
onstant in
rease in
ost. Let us for example
onsider
the instru
tion (i + (j )). It is simulated by
1 (j )
(i +
) 1
Simulating Program
(0)
1 8 6 + L(i) + L(j ) + 2L((j ))+
(j )
1 L((i + (j ))) + 2L(
) 1
(i +
) 1
(0)
1
The
ost of the simulating program is only larger by a
onstant fa
tor in the unit
ost measure. We thus have
Version: 18.10.99 Time: 17:53 {6{
1.1. Ma
hine Models: RAM and RASP 7
Lemma 1. General address substitution redu
es the
ost of RAM programs by
only a
onstant fa
tor in the unit
ost measure.
The situation is slightly more
ompli
ated in the logarithmi
ost measure. Fa
tor
L(
)
annot be estimated in a simple way. We therefore
hange the simulation
1
method and arrive at a simple
onne
tion between the
ost of the original program
and the
ost of the simulating program. We want lo
ation 0 to always
ontain the
ontent of
. We a
hieve this goal by inserting (0)
after every instru
tion
1 1
whi
h modies
and by inserting
(0) before every instru
tion whi
h uses
.
1 1 1
For example, we repla
e (i +
) by
(0); (i +
) and
(i) by
1 1 1 1
1 (i); (0)
. This modi
ation in
reases the
ost only by a
onstant fa
tor
1
under both measures. Finally, we repla
e the instru
tions using general address
substitution as des
ribed above, i.e., we repla
e, e.g., (i + (j )) by
1
(j ); (i +
). Note that we do not have to in
lude this pie
e of
ode into
1
bra
kets (0)
and
1 (0) as before be
ause we took
are of saving
1 1
elsewhere. We thus have (details are left to Exer
ise 2)
Theorem 1. General address substitution
an redu
e the time
omplexity of RAM
programs by at most a
onstant fa
tor in both
ost measures.
Next we dis
uss dire
t address
al
ulation. We extend the RAM model by a
program store PS and
all the extended model RASP (Random A
ess Stored
Program Ma
hine). The program store
onsists of innitely many lo
ations num-
bered 0; 1; 2; : : : . Ea
h lo
ation has two parts. The rst part
ontains the name of
the instru
tion (the op
ode), the se
ond part
ontains the operand, i.e., either an
address or the number of an instru
tion.
Example (
ontinued): The RASP-version of our example program is shown in
Figure 2. We use the RAM-instru
tion as the op
ode, data addresses are repla
ed
by symbol a and instru
tion addresses are repla
ed by k.
Op
ode Address
0
(a) 1 0
1 a 1
2 if
= 0 then goto k
1 6
3 a 2
4
a 1 1 1
5 goto k 2
6 (a) 1
Figure 2. A RASP program for
omputing 2 n
The number of op
odes is nite be
ause there are only four registers and only
a nite number of instru
tions. For the sequel, we assume a xed bije
tion between
op
odes and some initial segment of the natural numbers. We use Num to denote
that bije
tion.
Version: 18.10.99 Time: 17:53 {7{
8
In addition to the RAM instru
tion set, the RASP instru
tion set
ontains so-
alled
-instru
tions. -instru
tions operate on the program store. They are
9
(i) >
> i 2 N0 ;
h
>
(i +
) >
=
h 2 f1; 2g;
h j
(i) >
>
> j 2 f1; 2; 3g:
h
>
;
(i +
)
h j
Instru
tion (i) loads the h-th
omponent of lo
ation i of PS into the a
u-
h
Theorem 2 follows immediately from the observation, that a RAM program uses
only a xed number of storage lo
ations of the program store and that therefore the
additive fa
tor L(k) (k being the
ontent of the program
ounter)
an be bounded
by a
onstant whi
h is independent of the parti
ular input. Thus the \RASP
ost"
of a RAM instru
tion is at most
times the \RAM
ost" where
= 1 + L(length
of RAM program to be exe
uted on a RASP).
Theorem 3 is more diÆ
ult to prove. One has to write a RAM program whi
h
interprets RASP programs. Data store, program store and registers of the RASP
are stored in the data store of the RAM, more pre
isely, we use lo
ation 1 for the
a
umulator, lo
ations 2, 3 and 4 for the index registers, lo
ations 5, 8, 11, 14, : : :
for the data store, and lo
ations 6, 7, 9, 10, 12, 13, 15, 16, : : : for the program
store. Two adja
ent
ells are used to hold the two
omponents of a lo
ation of the
program store of the RASP. Lo
ation 0 is used as an instru
tion
ounter; it always
ontains the number of the RASP instru
tion to be exe
uted next. The interpreter
has the following stru
ture:
Version: 18.10.99 Time: 17:53 {8{
1.2. Randomized Computations 9
(1) loop: load the op
ode of the RASP-instru
tion to be exe
uted into the
a
umulator;
(2) de
ode the op
ode and transfer
ontrol to a modul whi
h simulates
the instru
tion;
(3) simulate the instru
tion and
hange the instru
tion
ounter.
(4) goto loop.
We leave the details to the reader (Exer
ise 3). A
ording to Theorems 2 and 3,
time
omplexities on RAMs and RASPs dier only by a
onstant fa
tor. Sin
e
we will negle
t
onstant fa
tors anyway in most of what follows, the
hoi
e of the
ma
hine model is not
ru
ial. We prefer the RAM model be
ause of its simpli
ity.
So far, RAMs (and RASPs) have no ability to intera
t with their environment,
i.e., there are no I/O-fa
ilities. The details of the I/O-fa
ilities are not important
ex
ept for Chapter 6 and we therefore always assume that the input (and output) is
stored in the memory in some natural way. For Chapter VI on NP-
ompleteness we
have to be more
areful. We equip our ma
hines with two semi-innite tapes, a read
only input tape and a write only output tape. The input tape
ontains a sequen
e
of integers. There is one head on the input tape whi
h is positioned initially on
the rst element of the input sequen
e. Exe
ution of the instru
tion Input
transfers the integer under the input head into the a
umulator and advan
es the
input head by one position. The
ost of instru
tion Input is 1 in the unit
ost measure and 1 + L(n) in the logarithmi
ost measure where n is the integer
to be read in. Similarly, the statement Output transfers the
ontent of
onto the output tape. Whenever a RAM attempts to read from the input tape and
there is left no element on the input tape, the
omputation blo
ks. We will then
say that the output is undened and that the time
omplexity of that parti
ular
omputation is the number of time units
onsumed until blo
king o
urred.
There are two important extensions of RAMs whi
h we have to dis
uss: randomized
RAMs and nondeterministi
RAMs. We dis
uss randomized RAMs now and put
o the dis
ussion of nondeterministi
RAMs to Chapter 6.
A randomized RAM (RRAM) has the ability to toss a perfe
t
oin and to
make further
omputation dependent on the out
ome of the
oin toss, i.e., there is
an additional instru
tion
random
whi
h assigns to either 0 or 1 with probability 1=2 ea
h. The
ost of this in-
stru
tion is 1 in both measures. We illustrate this new
on
ept by a very simple
example, an RRAM whi
h
omputes
onstant 0.
1: random
2: if 6= 0 then goto 1
Version: 18.10.99 Time: 17:53 {9{
10
Apparently, the
ontent of is 0 when the program stops. However, the running
time of the algorithm depends on the out
ome of the
oin tosses. More pre
isely,
if the random
hoi
e
omes out 0 at the k-th toss for the rst time, k 1, then
the running time is 2k in the unit
ost measure. Sin
e the
oin is assumed to be
fair,
P the probability of this
ase is 2 and therefore the average running time is
k
The notion of RRAM is most easily made pre
ise by redu
ing it to ordinary
RAMs with two input fa
ilities. The rst input fa
ility re
ords the a
tual input
p for the randomized
omputation (as above, we leave the exa
t nature of that
input fa
ility unspe
ied), the se
ond input fa
ility is a read only input tape whi
h
ontains a sequen
e of 0's and 1's. Exe
ution of random reads the next element
(if there is one) from the input tape and transfers it into the a
umulator .
Let A be a RAM program. For s a sequen
e of 0's and 1's it thus makes sense
to talk about A(p; s), the output of A on input p and sequen
e s of
oin tosses, and
T (p; s), the running time of A on input p and sequen
e s of
oin tosses. Again,
A
we leave it unspe
ied, whether the output is written into the memory or onto an
output tape. The expe
ted running time of randomized algorithm A on input p is
then dened by X
T (p) = lim 2 T (p; s): k
!1 A
k
s 2f0 1gk
;
A
s 2f0 1gk
; 2f0 1gk+1
t ;
and sequen
e s of
oin tosses stops regularly, i.e., is not blo
ked be
ause sequen
e s
is exhausted, then T (p; s) = T (p; t). If it is not blo
ked but never halts then
A A
We
an now dene T (n) and T (n) as des
ribed above. Of
ourse T (n) is only
A
av av
!1k 2 k
An argument similar to the one used in Lemma 1 shows that the limit in the
denition above always exists. Of
ourse, only the
ase "(n) < 1=2 is interesting.
Then A gives the desired output with probability larger than 1=2. A randomized
algorithm A is
alled Las Vegas algorithm for fun
tion f if it
omputes f with
error probability 0 of error, i.e., "(n) = 0 in the above denition. In parti
ular,
whenever A(p; s) stops and is dened then A(p; s) = f (p). Las Vegas algorithms
are a parti
ularly suitable
lass of randomized algorithms be
ause the output is
ompletely reliable. We will see examples of Las Vegas algorithms in Se
tions 2.1.3
and 3.1.2.
Of
ourse, we want the error probability as small as possible. Suppose, that we
have a randomized algorithm A whi
h
omputes f : P 7! Y with error probability
at most "(n). If "(n) is too large we might just run A several times on the same
input and then determine the output by a majority vote. This should strengthen
our
onden
e in the output.
time O(T ) then there is a randomized algorithm B whi
h
omputes f with error
A
time units.
to T time units ea
h. It then outputs whatever the majority of the simulated runs of
A outputs. Apparently B runs for at most O((1+ m) T ) = O(m ( ) T (n)) 1 1
A
time units. Moreover, f (p) 6= B (p; s ; : : : ; s ) i A(p; s ) 6= f (p) for at least m=2
2
1 m i
2 T
lim jfs 2 f0; 1g ; f (p) 6= A(p; s)gj + jfs 2 f0; 1g ; T (p; s) > T gj
k T
A
!1
k 2 k
2 T
+ ( )=21
2
= 1
2
( );
1
2
1
2
sin
e A
omputes f with error at most (and therefore the rst term is bounded
Version: 18.10.99 Time: 17:53 {11{
12
by ) and sin
e for p 2 P with g(p) = n we have
T (n) T (p)
A A
fs 2 f0; 1g ; T (p; s) T g
T
>T ; A
2 T
2 T m
X
m
m
i
i
(1
) m i
i=m=2
X
m
m
i
m=2
(1
) m=2
(sin
e
< 1=2)
i=m=2
2
(1
)
m m=2 m=2
= (4 (1 )) m=2
Æ: (by denition of m)
It is worth illustrating Lemma 2 by an example. Assume that = 0:49 and Æ = 0:03.
Then m = 2d(log Æ=) log(1 ( ) )e = 2dlog 0:03= log 0:9999e = 70128. Thus we
1 2
have to repeat a
omputation whi
h gives the
orr
t answer with probability 0.51
2
about 70000 times in order to raise the level of
onden
e to 0.97 . If we start with a
more reliable ma
hine, say = 0:25, then m redu
es to 2dlog 0:03= log 0:9375e = 110.
By this example we see that bringing the error down from 0.49 to 0.25 is the diÆ
ult
part, in
reasing the level of
onden
e further is easy.
Randomized algorithms have a fair
oin available and deterministi
algorithms
have not. It is therefore important to know how well a randomized algorithm
an be
simulated by a deterministi
algorithm. We approa
h this problem from two sides.
First we show that for xed problem size one
an always repla
e the fair
oin by a
xed sequen
e of 0's and 1's of reasonable length (Theorem 1), and then we show
how to use good pseudo-random number generators in randomized
omputations.
Theorem 1. Let n 2 N, N = jfp 2 P ; g(p) ngj, Æ = 1=(N + 1) and let A,
B , and f be dened as in Lemma 2. Then there is a sequen
e s 2 f0; 1g , 0
T m
fs 2 f0; 1g ; B (p; s) 6= f (p)g Æ 2
T m
= 2 =(N + 1); T m T m
and therefore X X
if B (p; s) 6= f (p) then 1 else 0
s 2f0 1gT m
; p 2 n
P
X X
= if B (p; s) 6= f (p) then 1 else 0
p2Pn s2f0;1gT m
N 2 =(N + 1) T m
< 2 : T m
X
if B (p; s0 ) 6= f (p) then 1 else 0 < 2 =2 = 1: T m T m
p2 n
P
a randomized ma
hine A with = 1=4 and running time T (n) = n for some k. A
k
Taking Æ = 1=(2 +1), Lemma 2 yields a ma
hine B with worst
ase running time
n+1
T (n) = O(( log Æ) T (n)) = O(n ) and error probability at most Æ. Moreover,
B A
k +1
su
h that there is no \obvious"
onne
tion between argument x and value T (x).
The most popular
hoi
e of fun
tion T is
T (x) = (a x +
) mod 2 m
the numbers a and
are of the same range. The result is nally trun
ated to
the last m bits. A user of a pseudo-random number generator provides a \seed"
Version: 18.10.99 Time: 17:53 {13{
14
x0 2 f0; 1g and uses the transformation T to generate a sequen
e x ; x ; : : : ; x
m
1 2 k
of algorithm A when using sequen
e s of
oin tosses does not ex
eed its expe
ted
running time by more than a fa
tor of, say, two. Then most random sequen
es of
any xed length are good, i.e., h is a statisti
al test and has polynomial running
A
time if A has (we restri
t our attention to polynomial time bounded algorithms
be
ause we saw in the beginning of this
hapter that algorithms with exponential
running time are hopelessly ineÆ
ient).
We say that a pseudo-random number generator passes test h if \many" (a
pre
ise denition is given below) sequen
es in the range of are good.
Suppose now that passes all statisti
al tests of polynomial time
omplexity.
Then passes also test h if A is a polynomial time bounded algorithm and hen
e
A
Remark: If passes test h well then a random element in the range of satises h
with probability ex
eeding 1=8 while a true random element of f0; 1g satises h
E ( m)
with probability ex
eeding 1=2. The
hoi
e of
ut-points a = 1=2 and a = 1=8 is
1 2
arbitrary; however 0 < a a is essential.
2 1
statisti
al test h, whi
h either misses
ompletely or does not hit with suÆ
iently
high probability. In other words, the properties of a random element in range() are
Version: 18.10.99 Time: 17:53 {15{
16
diÆ
ult to predi
t, and hen
e the elements produ
ed by are rightly
alled pseudo-
random sequen
es. Note that \being random" is the same as \being diÆ
ult to
predi
t".
It is not known whether (very) good random number generators in the sense
of this denition exist. However, it
an be shown that very good random number
generators with E (m) = m for any k
omputable in polynomial time exist if any
k
one of the following number theoreti
problems is hard: the dis
rete logarithm
problem or the problem of fa
toring integers. We have to refer the reader to the
literature for a dis
ussion of these results (
f. A.C. Yao: \Theory and Appli
ations
of Trapdoor Fun
tions", IEEE FOCS 1982, 80{91).
We pro
eed on the assumption that a (very) good polynomial time
omputable
pseudo-random number generator exists with, say, E (m) = m . We show that 2
n
1 if T (p; s) 2t n ;
h (s) = A
t
0 otherwise
p
is
omputable in time O(t n ) and we have h (s) = 1 for at least fty per
ent of
t
p
the bit strings of length 2t n . This follows from the fa
t that the running time of
t
T on p and s
an ex
eed twi
e thep expe
ted value for at most half of the sequen
es
A
fs 2 f0; 1g E (m)
; s 2 range( ) and h (s) = 1g 6= ;
p
or
fs 2 f0; 1g ; s 2 range () und T (p; s) 2t n g 6= ;:
E (m)
A
t
nomial bound on the time needed for
omputing . Thus the existen
e of good
pseudo-random number generators leads to more eÆ
ient simulations of probabilis-
ti
ma
hinest by deterministi
ma
hines. Note that the naive simulation has running
time O(2 t n ).
t n t
do s (x);
run A on p and s for up to 2t n steps; t
2t n having probability at least 1=8 only 8 iterations of the loop are required on
t
the average. pHen
e the algorithm above simulates A in time O(T (n)) = O(n ) and A
t
uses only O( n ) random bits. Thus if true random
hoi
es are possible but
ostly,
t
this is a very signi
ant improvement. This
ompletes our dis
ussion on the use of
(very) good pseudo-random number generators in randomized
omputations.
We end this se
tion with a remark on the relation between the expe
ted time
omplexity of deterministi
algorithms and the running time of probabilisti
algo-
rithms.
Let A be a Las Vegas algorithm whi
h
omputes fun
tion f : P 7! Y . Let us
assume for simpli
ity that A makes at most a(n)
oin tosses for every n 2 N on any
input p 2 P with g(p) = n. Then
X
T (p)
A = T (p; s)=2
A
a(n)
:
s 2f0 1ga(n)
;
Suppose also that we are given a probability distribution on the set P of problem
instan
es. Let B be a deterministi
algorithm for f , whose expe
ted running time
Version: 18.10.99 Time: 17:53 {17{
18
on inputs of size n is minimal (B is
ertain to exist if P , the set of problem instan
es n
where expe
tations are
al
ulated with respe
t to probability distribution . For ev-
ery xed s 2 f0; 1g algorithm A with sequen
e s of
oin tosses is a deterministi
a(n)
p2 P n 2
p P n
p 2 P n s 2f0 1ga(n) 2
; p P n
X X
(p) T (p; s)=2
A
a(n)
p 2 P n s 2f0 1ga(n)
;
X
(p) T (p): A
p 2 P n
Thus the expe
ted running time of Las Vegas algorithm A on inputs of size n
an
not be better than the expe
ted running time of the best deterministi
algorithm.
We will use this fa
t to derive lower bounds on the randomized
omplexity of some
sorting problems in Chapter 2. For sorting and some related problems we will
derive
(n log n) lower bounds on the expe
ted running time of a large
lass of
deterministi
algorithms in Chapter 2. The inequality derived above immediately
extends that lower bound to the
orresponding
lass of Las Vegas algorithms.
Only a few algorithms in this book are formulated in RAM
ode, most algorithms
are formulated in a high level ALGOL-like programming language. We feel free
to introdu
e additional statements into the language, whenever the need arises
and whenever translation into RAM
ode is obvious. Also we make frequent use
of
omplex data stru
tures su
h as lists, sta
ks, queues, trees and graphs. We
hoose this very high level des
ription for many algorithms be
ause it allows us
to emphasize the prin
iples more
learly. Most statements of our programming
language are known from ALGOL-like languages. In parti
ular, we use
Version: 18.10.99 Time: 17:53 {18{
1.3. A High Level Programming Language 19
the
onditional statement:
if h
onditioni then hstatementi else hstatementi fi;
the iterative statement:
while h
onditioni do hstatementi od;
the for -loop:
for i from hexpressioni step hexpressioni to hexpressioni
do hstatementi od
If the step size is unspe
ied then it is 1 by default. We also use a se
ond form of
the for -loop:
for i 2 hseti do hstatementi od
with the following semanti
s. The statement is exe
uted jhsetij-times; i runs
through the members of the set in some unspe
ied order. Assignments are written
in the form hvariablei hexpressioni. Translation of all statements above into
RAM
ode is simple. We des
ribe the translation in the
ase of a while-statement
while B do S od:
Let P be a RAM program for B , i.e., P evaluates expression B and leaves a 0 (1)
1 1
in the a
umulator, if B evaluates to false (true). Let P be a RAM program for S .
2
Then the RAM
ode of Program 4 realizes the while-loop.
P1
if = 0 then goto exit;
P2
goto rst instru
tion of P1 ;
exit:
Program 4
Program 4 also denes the
omplexity of the while-loop; it is the sum of the
time units spent on the repeated testing of the
ondition and the exe
ution of the
body.
Variables in our programs
ontain unstru
tered elementary values or stru
tured
values. The elementary data types are integer, real, boolean, pointer and
an additional unspe
ied data type. We use this additional data type in sorting
algorithms; the data to be sorted will be of the unspe
ied type. The operations
dened on this additional data type are given on a
ase by
ase basis. Stru
tured
data types are strings over some alphabet, re
ords, arrays, lists, sta
ks, queues,
trees and graphs. Strings are treated in 2.2, graphs in 4.1, all other stru
tured types
are treated in 1.4. The type of a variable will be obvious from the
ontext in most
ases; in this
ase we will not de
lare variables expli
itely.
Version: 18.10.99 Time: 17:53 {19{
20
Pro
edures play a major role in our programs. Parameters are restri
ted to
elementary types. Then parameter passing takes
onstant time (
f. Se
tion I.5).
Non-re
ursive pro
edures are easily redu
ed to RAM
ode; one only has to substi-
tute the pro
edure body at the pla
e of the
all. The situation is more
ompli
ated
for re
ursive pro
edures. We treat re
ursive pro
edures in 1.5.
Comments are bra
keted by
o and o
.
Re
ords and arrays are the most simple ways of stru
turing data.
An array A of n elements
onsists of n variables A[1℄; : : : ; A[n℄ of the same
type. An array is stored in n
onse
utive storage lo
ations, e.g., in lo
ations BA +1,
BA + 2; : : :,BA + n. Here BA stands for base address. If x is a variable stored in
lo
ation i then a
essing array element A[x℄ is realized by means of index registers.
The following pie
e of
ode
1 (i);
(BA +
) 1
loads A[x℄ into the a
umulator for a
ost of 4 in the unit
ost measure and 2+L(i)+
L(BA) + 2L(x) + L(A[x℄) in the logarithmi
ost measure. Again the logarithmi
ost measure is proportional to the length of the numbers involved.
Re
ords are xed size
olle
tions of variables of dierent type, e.g.,
re
ord age : integer; in
ome : real end:
A variable x of this re
ord type is easily simulated by two simple variables, a variable
x:age of type integer and a variable x:in
ome of type real.
Queues, sta
ks, lists and trees are treated in the se
tions below. They are all
redu
ed to arrays.
Queues and sta
ks are used to represent sequen
es of elements whi
h
an be modied
by insertions and deletions. In the
ase of queues insertions are restri
ted to the
end of the sequen
e and deletions are restri
ted to the front of the sequen
e. A
typi
al example is a waiting line in a student
afeteria. Queues are also known
under the name FIFO store (rst in - rst out). In the
ase of sta
ks, insertions
and deletions are restri
ted to the end of the sequen
e: LIFO store (last in - rst
out). Very often, the names Push and Pop are used instead of insertion into and
deletion from a sta
k.
A sta
k K is most easily realized by an innite array K [1℄, K [2℄, : : : and an
index Top of type integer. The sta
k
onsists of elements K [1℄,: : :,K [Top ℄; K [Top ℄
is the top element of the sta
k. The following pie
e of
ode realizes operation
Push (K; a)
1.4.2. Lists
Pro
edure Insert takes two parameters, a pointer to the element after whi
h we
want to insert and the
ontent of the new element.
pro
edure Insertr (p :"element, a:real); pro
edure Inserta (p :integer; a:real);
var q : "element; var q : integer;
Newr (q); Newa (q);
q":
ont a; Content [q℄ a;
q":next p":next; next [q℄ next [p℄;
p ":next q next [p℄ q
end. end.
This pro
edure fails for empty lists. (The following pro
edure Delete fails already
for lists
onsisting of only one element.) A real implementation must take this into
a
ount and add tests for spe
ial
ases. In general, the des
riptions of our algorithms
skip sometimes su
h spe
ial
ases to enhan
e the readability of the book. Figure 6
illustrates one
all of Insertr (p; a).
Pro
edure Delete takes one parameter, a pointer to the element whi
h pre
edes
the element we want to delete.
pro
edure Deleter (p :"element); pro
edure Deletea (p : integer);
var q :"element; var q : integer;
q p":next; q next [p℄;
p":next q":next; next [p℄ next [q℄;
Disposer (q) Disposea (q)
end. end.
It remains to dis
uss the supply of unused nodes in more detail. Supply is again a
linear list with head Free . We will only des
ribe the array version of pro
edures New
and Dispose be
ause these pro
edures are usually built-in fun
tions in programming
languages whi
h
ontain re
ords. Internally, re
ords are always realized by arrays
and therefore Newr and Disposer are identi
al to Newa and Disposea . A supply of
n elements is
reated by
Version: 18.10.99 Time: 17:53 {23{
24
before the
all
p:
after exe
uting (1)
p: q:
after exe
uting (2) and (3)
p: q: a
after exe
uting (4)
p: q: a
One often uses linear lists to realize sta
ks and queues. In parti
ular, several sta
ks
and queues may share the same supply of unused nodes. This will guarantee high
storage utilization if we have knowlegde of the total length of all sta
ks and queues
but no knowlegde of individual length. Typi
al examples
an be found in Chapter 6.
We store a graph by listing the set of its su
essors for ea
h node. In a graph of n
nodes and m edges these lists have total length m, but nothing is known in advan
e
about the length of individual lists.
1.4.3. Trees
tree. Node v is the root of the tree, (v) = m is its degree and T is the i-th
i
subtree of T .
In the graph-theoreti
literature trees as dened above are usually
alled ordered
rooted trees. We always draw trees with the root at the top and the leaves at the
Version: 18.10.99 Time: 17:53 {25{
26
bottom. As shown in the example tree of Figure 7 nodes are drawn as
ir
les and
leaves are drawn as re
tangles.
v1
v2 v3
b1 v4 b2 b3
b4 b5
Figure 7. Tree T with nodes and leaves
Ex
We use the following terms when we talk about trees. Let T be a tree with
root v and subtrees T ; 1 i m. Let w = root(T ). Then w is the i-th son of v
i i i i
and v is the father of w . Des
endant (an
estor) denotes the re
exive, transitive
i
Denition: Tree T is a binary tree if all nodes of T have degree exa
tly 2.
Our example tree T is a binary tree. A binary tree with n nodes has n +1 leaves.
Ex
We have asso
iated rows with nodes and leaves in some arbitrary way. If
information is only stored in the nodes and not in the leaves then leaves do not
have to be stored expli
itly. All rows
orresponding to leaves
an be deleted and
pointers pointing to leaves are set to 0. A 0-pointer then represents a subtree
Version: 18.10.99 Time: 17:53 {26{
1.4.3. Trees 27
root = 5 Content Lson Rson
1
Content of v 8 4 4
2
Content of v 3 2 1
3
Content of b 0 1 0
4
Content of b 0 5 0
5
Content of v 2 1 7
6
Content of b 0 2 0
7
Content of v 6 3 9
8
Content of b 0 4 0
9
Content of b 0 3 0
Figure 8. Realization of T by arrays Ex
onsisting of a single leaf. In the diagrams we will not draw leaves in this
ase (
f.
Figures 9 and 10).
v1
v2 v3
v4
Figure 9. T Ex without leaves
1.5. Re ursion
Re
ursive pro
edure Symord traverses a binary tree in symmetri
al order. Before
we
an estimate time and spa
e
omplexity of Symord we need to take a look at
the implementation of re
ursive pro
edures in RAM or RASP
ode. Re
ursive pro-
edures are realized by means of a sta
k. We asso
iate with ea
h
all (in
arnation,
a
tivation) of a pro
edure an element of the sta
k,
alled a
tivation re
ord. The
a
tivation re
ord
ontains
omplete information about the
all, i.e.,
a) the values of the a
tual parameters,
b) the return address,
) the lo
al variables.
If a pro
edure has n parameters and m lo
al variables then the a
tivation re
ord
onsists of n + 1 + m storage lo
ations. The i-th
ell, 1 i n,
ontains the value
of the i-th a
tual parameter, the (n +1)-st
ell
ontains the return address, i.e., the
address of the instru
tion whi
h follows the pro
edure
all in the
alling program,
and the (n + 1 + j )-th
ell
ontains the j -th lo
al variable. Parameters and lo
al
variables are addressed indire
tly via the a
tivation re
ord. More pre
isely, if Top is
the address of the storage lo
ation immediately pre
eding the a
tivation re
ord then
lo
ation Top + i
ontains the i-th a
tual parameter and
ell Top + n +1+ j
ontains
the j -th lo
al variable. Top is best stored in an index register. After
ompletion of
the pro
edure
all
ontrol is transfered to the address stored in lo
ation Top + n +1.
Also Top is redu
ed after
ompletion, i.e., the a
tivation re
ord is deleted from the
sta
k. Parameters and return address are
omputed by the
alling program, i.e.,
the rst n + 1
ells of an a
tivation re
ord are initialized by the
alling program.
We are now ready to give the non-re
ursive version of Symord by Program 6. Array
K [1 : : 1℄ is used as the sta
k.
Version: 18.10.99 Time: 17:53 {28{
1.5. Re
ursion 29
Call Symord (Lson [v℄) (line (3) of the re
ursive program) is simulated by lines
(11'){(15') of the non-re
ursive program. In (11') storage spa
e for the a
tivation
re
ord is reserved. The a
tivation re
ord is initialized at lines (12') and (13');
at (12') the value of the a
tual parameter and at (13') the return address \M1"
is stored. At line (14')
ontrol is transfered to Symord and the re
ursive
all is
started. Upon
ompletion of the
all
ontrol is transfered to label M1 (line (15')).
The spa
e for the a
tivation re
ord is released and exe
ution of Symord is resumed.
Analogously, one
an asso
iate the other instru
tions of the non-re
ursive program
with the instru
tions of the re
ursive program.
Program 6 is pra
ti
ally a RASP program. Line (23') uses a -instru
tion. It
an be turned into a RAM program either by the method des
ribed in the proof of
Version: 18.10.99 Time: 17:53 {29{
30
Theorem 3 of Se
tion 1 or more simply by repla
ing line (23') by Program 7.
if K [Top + 2℄ = \M1"
then goto M1;
if K [Top + 2℄ = \M2"
then goto M2;
goto HP
Program 7
We have des
ribed a simplied method for translating re
ursive programs into
non-re
ursive ones. The method suÆ
es if global variables
an only
ome out of
the main program. This will be the
ase throughout this book. In the general
ase
a single register Top does not suÆ
e; one has to repla
e it by a set of registers, one
for ea
h a
tivation re
ord whi
h
an
ontain global variables. We refer the reader
to a book on
ompiler
onstru
tion for details, e.g., Gries (71)).
We are now ready to analyze the time
omplexity of re
ursive pro
edures in
the unit
ost measure. A
onstant number of instru
tions is required to set up a
re
ursive
all, namely to in
rease Top , to store the n a
tual parameters and the
return address. Note that n is xed and in dependent of the input; n
an be
infered from the program text. Also, we allow only elementary types in parameter
position and hen
e a single parameter
an be passed in
onstant time. Upon return
from a re
ursive pro
edure Top has to be de
reased. Altogether, the administrative
overhead for a pro
edure
all is bounded by a
onstant.
The following method for
omputing the time
omplexity of re
ursive pro
e-
dures is often useful. We asso
iate with every pro
edure
all the
ost of exe
uting
the pro
edure body, in
luding the administrative
ost for initiating further re
ur-
sive
alls but ex
luding the time spent inside re
ursive
alls. Then we sum over all
alls and obtain the total
ost in this way. We illustrate this method on pro
edure
Symord . Ea
h line in the body of Symord takes
onstant time. Re
all that we
only
ount the time required to set up the re
ursive
alls at lines (3) and (5) of
Program 5 but that we do not
ount the time needed to exe
ute the re
ursive
alls.
Symord is
alled on
e for ea
h leaf and node of a binary tree. Thus the total
ost
of Symord is O(n) where n is the number of leaves and nodes of the binary tree.
The summation over all
alls of a pro
edure
an be done more formally by
means of re
ursion equations. Let T (v) be the running time of
all Symord (v).
Then
if v is a leaf;
T (v) =
+ T (Lson [v℄) + T (Rson [v℄) otherwise
1
2
for suitable
onstants
and
. We
an now show by indu
tion on the height of v
1 2
(the tree with root v) that T (v) =
# nodes in the tree with root v +
# leaves
2 1
in the tree with root v. Although the indu
tion proof is very simple we in
lude it
for dida
ti
purposes.
Version: 18.10.99 Time: 17:53 {30{
1.6. Order of Growth 31
Height(v) = 0: Then v is a leaf and we have T (v) =
. 1
Height(v) > 0: Then v is a node and we have
T (v) =
+ T (Lson [v℄) + T (Rson [v℄)
2
=
# nodes(v) +
# leaves(v):
2 1
In most
ases we will not be able to derive exa
t expressions for the running time
of algorithms; rather we have to be
ontent with order of growth analysis. We use
the following notation.
Denition: Let f : N 7! N be a fun
tion. Then O(f ),
(f ) and (f ) denote
0 0
the following sets of fun
tions
O(f ) = fg : N ! N ; 9
> 0 9n : g(n)
f (n)
0 0 0
for all n n g 0
(f ) = fg : N ! N ; 9
> 0 9n : g(n)
f (n)
0 0 0
for all n n g 0
for all n n g 0
More pre isely, if Æ is an n-ary operation on fun tions and A ; : : : ; A are sets of 1 n
fun
tions, then Æ(A ; : : : ; A ) denotes the natural extension to sets of fun
tions, e.g.,
1 n
represent larger and larger sets of fun
tions; the bounds be
ome
oarser and
oarser
from left to right.
Version: 18.10.99 Time: 17:53 {31{
32
1.7. Se
ondary Storage
lo
ations. It takes 5000 time units to transport a page from one memory to the
other. This assumption approximates the behavior of modern disk memory.
1) Develop a RAM program for
omputing 2 whi
h runs in O(log n) time units
n
in the unitP
ost measure and O(n) time units in the logarithm-
ost measure. [Hint:
Let n = k
a 2 , a 2 f0; 1g, be the binary representation of n. Note that
i
i
i
2 = ( ((2 k ) 2 k ) 2 ) 2 .℄
i=0
n a 2 a 1 2 a1 2 a0
2) Give a detailed proof of Theorem 1 of Se
tion 1.1. In parti
ular, dis
uss the
assumption that a RAM program does not use
ell 0. Show that one
an add 1 to
all addresses dynami
ally and hen
e free
ell 0.
3) Give a detailed proof of Theorem 3 of Se
tion 1.1.
4) Show how to translate
onditional and iterative statements into RAM
ode.
5) In doubly linked lists every list element points to its su
essor and its prede
es-
sor. Do Se
tion 1.4.2. for doubly linked lists.
6) A tree is k-ary if every node has exa
tly k sons. Show that a k-ary tree with n
nodes has exa
tly k + (k 1)(n 1) leaves.
7) Prove
f (n) = O(f (n))
O(f (n)) + O(f (n)) = O(f (n))
O(f (n)) = O(f (n))
O(f (n)) O(g(n)) = O(f (n) g(n))