IVERSON - A Programming Language PDF
IVERSON - A Programming Language PDF
I \T E R SON
A Programming
Language
1.2 Programs, 2
Conventions, 7
Arithmetic operations, 11
Logical operations, 11
Elementary operations, 13
Matrices, 14
Index systems, 14
1.6 Rotation, 14
1. 7 Special vectors, 15
1.8 Reduction, 16
1.9 Selection, 17
Compression, 17
1.12 Transpositions, 25
xi
Contents xii
1.16 Ranking, 31
Reordering operations, 32
Permutations, 33
Function mapping, 34
Ordering vector, 36
1.18 Maximization, 36
1.21 Subroutines, 39
1.22 Files, 40
Directed graphs, 45
Ordered trees, 46
Well formation, 52
Homogeneous trees, 58
References, 62
Exercises, 62
Chapter 2 MICROPROGRAMMING 71
Additive indexing, 73
Indirect addressing, 74
Dynamic relocation, 74
Branch instructions, 80
Contents xiii
Logical instructions, 83
Arithmetic instructions, 84
Shift instructions, 85
Convert instructions, 87
Input-output instructions, 88
References, 103
Exercises, 103
Partitions, 115
Pools, 116
Sumnlary, 118
References, 128
Exercises, 129
Contents xiv
References, 155
Exercises, 156
notation, 169
notation, 173
References, 174
Exercises, 175
Contents xv
References, 240
Exercises, 241
References, 262
Exercises, 263
INDEX 277
ILLUSTRATIONS
Table 1.8
Typographic conventions for classes of operands
9
Program 1.9
Selection on ban k ledger L (Example I. I) 19
Program 1. 10
I nterfiling program 20
Table 1. I I
The Cartesian prod uct A == Xl @ x 2 @ x 3 31
Program 1. 12
Mapping defined by a permutation vector j 35
Progranl 1. I3
Modes of subroutine reference 40
Program I. 14
PrograITI for Exanlple 1.3 42
Progranl I. I5
Program for Example 1.4 44
Figure I. I 6
A general triply rooted tree with A(T) == 16, v(T) ==
Il(T) == 26 45
(n, U) 47
Program 1.2 I
Determination of the index matrix I associated with a
bet) 56
Figure 1.24 Set selection and maximunl prefix and suffix operations
59
xvii
xviii Illustrations
Figure 1.25 Homogeneous tree H and dispersion and 1110nlent
vectors 60
107
of x 112
segments 114
representation of A 121
Illustrations xix
Figure 4.6 Uniform tree and mapping vector for the set k = (200.
Figure 4.7 Contracted tree and associated leaf list matrix M for
168
notation 170
xx Illustrations
Table 6.1 Types of file operations 176
207
Progran1 6.24 Forward and backward bubble sort with string indicator
219
tree 227
Illustrations xxi
Table 6.37 Characteristics of internal sorting nlethods (for random
distribution of distinct keys) 238
1.1 INTRODUCTION
z +- 1
v +- x x 3.1416 2 z~yxz
2 v~vxx 3 z+-2xz
Thus Program 1.1 is a program of two statements defining the result l' as
the (approximate) area of a circle of radius :r~ whereas Program 1.2 is an
infinite program in which the quantity z is specified as (2y)n on the nth
execution of the two-statement loop. Statements will be numbered on the
left for reference.
A number of similar programs may be subsumed under a single more
general progranl as follows. At certain branch points in the program a
finite number of alternative statements are specified as possible successors.
One of these successors is chosen according to criteria determined in the
statelnent or statements preceding the branch point. These criteria are
usually stated as a comparison or test of a specified relation between a
specified pair of quantities. A branch is denoted by a set of arrows leading
to each of the alternative successors~ with each arrow labeled by the
1.2 Progranls 3
comparison condition under which the corresponding successor is chosen.
The quantities compared are separated by a colon in the statenlent at the
branch point, and a labeled branch is followed if and only if the relation
indicated by the label holds when substituted for the colon. The conditions
on the branches of a properly defined program must be disjoint and
exhaustive.
Program 1.3 illustrates the use of a branch point. Statement ~5 is a
comparison which determines the branch to statements /1 I, () I, or }' I,
according as z > n, z == n, or z < n. The program represents a crude but
effective process for determining x == n'2:\ for any positive cube n.
, - - - - - - - - - - ~3
x +- y x y
~4 z +- y x x
> <
f31 k +-- k -7 2 ~ ~5 z : n --)'1 k+-kx2
(51
y ~O
2 k ~ 1
3 x ~y x y
4 z~yxx
> z . n
5 ~
6 k~kx2
7 y~y+k
8 k~k+2
9 y~y-k
i +- peA)
2 j +- v(B)
3
k +- v(A)
4
C/ ~O
5
C/ ~ Cji + Aki X Bjk
6
k~k-l
> k 0
7
8
j ~j - 1
>
9 j:O
10 i ~ i-I
> ~
11 i 0
Program 1.5. Steps 1-3 initialize the indices, and the loop 5-7 continues to
add successive products to the partial sum until k reaches zero. When this
occurs, the process continues through step 8 to decrement j and to repeat the
entire sunlnlation for the new value ofj, providing that it is not zero. I fj is zero,
the branch to step 10 decrements i and the entire process overj and k is repeated
fronl j = v(B), providing that i is not zero. If i is zero, the process is cOl1lplete,
as indicated by the exit arrow.
i -(- fleA) +1
2 i-(-i-l
3 .i ~- vCR) + 1
4 .i ~-.i - 1
5 k ~-- v(A) + 1
6 C/ -(- 0
7 k-(-k-l
8 C/ ~- C/ + Aki x B/.;
statenlent s. Program 1.7 shows the use of these conventions in a refa fill u
lation of Prograrn 1.6. More generalIy~ two or rnore otherwise independent
progranls may interact through a statenlent in one progranl specifying a
branch in a second. The statenlent nunlber occurring in the branch rllust
then be augrnented by the name of the progranl in which the branch is
effected. Thus the statenlent (0) -~ Program 2.24 executed in Progranl I
causes a branch to step 24 to occur in Program 2.
~ i +-p(A) +1
2 i +- i - I , (=ft, =) -* (3, 9)
3 j +- v( B) +1
4 j +-j - 1, (~f~~ =) -* (5, 2)
5 k +- v(A) + 1
7 k +- k - 1, (/, =) -* (8, 4)
Conventions
The Sunlrnary of Notation at the end of the book sunlnlarizes the nota
tion developed in this chapter. Although intended prinlarily for reference,
it supplements the text in several ways. It freq uently provides a nl0rc
concise alternative definition of an operation discussed in the text, and it
8 The language 1.3
also contains inlportant but easily grasped extensions not treated explicitly
in the text. By grouping the operations into related classes it displays
their family relationships.
A concise progralnming language Inust incorporate falnilies of opera
tions whose nleInbers are related in a systenlatic manner. Each fanlily will
be denoted by a specific operation sYInbol~ and the particular nlenlber of the
family will be designated by an associated controlling paranleter (scalar~
vector. Inatrix~ or tree) which inlnlediately precedes the nlain operation
symbol. The operand is placed illlmediately after the nlain operation
synlbol. For exanl ple~ the operation k t x (left rotation of x by k places)
Inay be viewed as the kth melnber of the set of rotation operators denoted
by the symbol t.
Operations involving a single operand and no controlling paranleter
(such as l.r]' or rrl) will be denoted by a pair of operation syInbols which
enclose the operand. Operations involving two operands and a controlling
paralneter (such as the mask operation la~ U, bl) will be denoted by a pair
of operation symbols enclosing the entire set of variables~ and the con
trolling parameter will appear between the two operands. In these cases
the operation symbols themselves serve as grouping synlbols.
In interpreting a compound operation such as k t (j ~ x) it is inlportant to
recognize that the operation symbol and its associated controlling paraln
eter together represent an indivisible operation and must not be separated.
]t would~ for example, be incorrect to assunle that j t (k ~ x) were
equivalent to k t (j ~ x), although it can be shown that the conlplete opera
t
tions j ~ and k t do commute, that is, k (j ~ x) === j ~ (k x). t
The need for parentheses will be reduced by assunling that conlpound
statenlents are, except for intervening parentheses~ executed fronl right to
t t
left. Thus k t j ~ x is equivalent to k (j ~ x), not to (k j) ~ x.
Structured operands such as vectors and nlatrices, together with a
systenlatic component-by-conlponent generalization of elenlentary opera
tions, provide an important subordination of detail in the description of
algorithms. The use of structured operands will be facilitated by selection
operations for extracting a specified portion of an operand, reduction
operations for extending an operation (such as logical or arithInetic
multiplication) over all components~ and permutation operations for
reordering components. Operations defined on vectors are extended to
nlatrices: the extended operation is called a rOlf operation if the under
lying vector operation is applied to each row of the nlatrix and a colurnn
operation if it is applied to each column. A colunln operation is denoted
by doubling the symbol employed for the corresponding row (and vector)
operation.
A distinct typeface will be used for each class of operand as detailed in
1.3 Structure (~ll he language 9
Table 1.8. Special quantities (such as the prefix vectors a/ defined in Sec.
1. 7) will be denoted by Greek letters in the appropriate typeface. For
mneIllonic reasons, an operation closely related to such a special quantity
I Rcpresentation
Typc of
I
Operand
Printed Typed
Literal
Alphabetic Ronlan, u.c. and I.c. Circled u.c. and I.c. rOlllall.
Nunleric Standard nunleral Standard nunleral
Variable I
will be denoted by the )ame Greek letter. For exalnple, 'Y.ju denotes the
maxiIllum prefix (Sec. 1.10) of the logical vector u. Where a Greek letter
is indistinguishable from a Roman, sanserif characters will be used, e.g"
E and I for the capitals epsilon and iota.
Arithmetic operations
The ordinary arithmetic operations will be denoted by the ordinary
symbols +, -, x, and --:-- and defined as usual except that the domain and
range of multiplication will be extended slightly as follows. If one of the
factors is a logical variable (0 or I), the second may be arbitrary and the
product then assumes the value of the second factor or zero according as
the value of the first factor (the logical variable) is I or O. Thus if the
arbitrary factor is the literal "q~" then
Oxq=qxO=O
and 1 x q = q X 1 = q.
According to the usual custom in ordinary algebra, the multiplication
symbol may be elided.
Logical operations
The elementary logical operations and, or, and not will be denoted by A ,
V , and an overbar and are defined in the usual way as follows:
The quantity I' is called the j-residue o.l n n10dulo b and is denoted by b Ij n.
For example, 3109 == 0, 3119 == 3, and 310 10 == I. Moreover, if n 2 0,
then b 10 n is the remainder obtained in dividing n by band q is the integral
part of the quotient. A number n is said to be of ecen parity ifits O-residue
modulo 2 is zero and of odd parity if 210 n == I.
If two numbers n and In have the same j-residue Inodulo b, they differ
by an integral multiple of b and therefore have the saIne k-residue Inodulo
b for any k. If b /j n == b /j m, then In and n are said to be congruent rnod b.
Congruency is transitive and reflexive and is denoted by
In == n (nlod b).
I. the.fioor oj' or (or integral part of .r), denoted by l.rJ and defined as the
largest integer not exceeding .r,
2. the ceiling (~l,r, denoted by rorl and defined as the snlallest integer not
exceeded by .r.
Thus
f3. 141==4, l3.14J==3, l-3.14J == -4,
f3.001 == 3, l3.00 J == 3, l-3.00J == -3.
1
Clearly for == -l-rJ and lorJ .r frl Moreover, n == bln -:- bJ + b 10 n
for all integers n. Hence the integral quotient In -:-
bJ is equivalent to the
quantity q occurring in the defInition of the j-residue for the case j == O.
1.5 Structured operands 13
Elementary operations
Any operation defined on a single operand can be generalized to apply
to each member of an array of related operands. Similarly, any binary
operation (defined on two operands) can be generalized to apply to pairs
of corresponding elenlents of two arrays. Since algorithrTIs conlnlonly
incorporate processes which are repeated on each member of an array of
operands, such generalization permits effective subordination of detail in
their description. For example, the accounting process defined on the
data of an individual bank account treats a number of distinct operands
within the account, such as account number, name, and balance. More
over, the over-all process is defined on a large number of sirTIilar accounts,
all represented in a conlmon format. Such structured arrays of variables
will be called structured operands, and extensive use will be made of three
types, called rector, rnatrix, and tree. As indicated in Sec. S.l of the
SunllTIary of Notation, a structured operand is further classified as logical,
integral, nUlnerical, or arbitrary, according to the type of elenlents it
contains.
A rector x is the ordered array of elements (Xl' X 2 , X 3 ' ,x,'(x). The
variable Xi is called the ith component of the vector x, and the number of
components, denoted by vex) (or simply v when the detennining vector is
clear from context), is called the dinlension of x. Vectors and their conl
ponents will be represented in lower case boldface italics. A nUlnerical
vector X may be rnultiplied by a numerical quantity k to produce the
scalar n1ldtiple k x X (or kx) defined as the vector z such that Zi == k X Xi.
All elementary operations defined on individual variables are extended
consistently to vectors as component-by-corTIponent operations. For
exarTIple,
Matrices
A matrix III is the ordered two-dimensional array of variables
MIl, M 2 l, MJ~(M)
MI~(M)
2 2
M 1 , M 2 ,
Mf1(M) M/I(M)
1 , JI(M)
The vector (M/, M 2 i, . . . , M/) is called the ith rOH' rector of M and is
denoted by Mi. Its dimension v(M) is called the rOH' dimension of the
matrix. The vector (M/, M j 2, . . . , M/l) is called the jth colunul rector
of M and is denoted by M j Its dimension f-1(M) is called the colulnn
dinlension of the matrix.
The variable M/ is called the (i,j)th component or element of the matrix.
A matrix and its elements will be represented by upper case bol.dface
italics. Operations defined on each element of a matrix are generalized
component by component to the entire matrix. Thus, if 0 is any binary
operator,
P = M 0 N<:=>P/ = M/ 0 N/.
Index systems
A ~j t B~Ai ==jl t Bi
C ~ k 11 B<=>-C j == k j tB j
The first operation is an extension of the basic vector rotation to each row
of the matrix and is therefore called rOlt' rotation. The second operation is
the corresponding column operation and is ,therefore denoted by the
doubled operation synlbol 11. For example, if
k==(0,1,2),
and
a b C)
B = d e .~'
(
g h I
then
~) and k Ii B == (~: ~
:).
h g b .(
Right rotation is extended analogously.
1.8 REDUCTION
-~ / II = ((((I :1'= 0) I) * 1) 0)
=(((1 1)~ 1)=1=0)
= ((0 ::/= I) 0)
=(1 0)=1,
and 2 10 (+ /u) = 2 10 3 = ]. Similarly, = /u = 2 10 (+ Iii), and as a con
sequence,
=I)U = =/ii,
a useful companion to De Morgan's law.
To complete the system it is essential to define the value of cJ/e(O), the
reduction of the null vector of dimension zero, as the identity elelnent of
the operator or relation . Thus + /e(O) = V /e(O) = 0, and x /e(O) =
A /e(O) = 1.
A reduction operation is extended to matrices in two ways. A rOH'
reduction of a matrix X by an operator () is denoted by
Y~ ,:)/X
and specifies a vector y of dimension fleX) such that Yi = c) Xi. A
COIUJ11n reduction of X is denoted by Z ~ all X and specifies a vector z of
dimension veX) such that Zj = c)/Xj
For example, if
u= (~ ~ ~) I
then +/U = (2,2,3), +//U = (2, L 3, I), A//U = (0,0, 1,0), /)U ==
(0,0,1), =//U = (0, I, I, I), and +/(=//U) = 3.
1.9 SELECTION
Compression
The effective use of structured operands depends not only on generalized
operations but also on the ability to specify and select certain elenlents or
groups of elements. The selection of single elements can be indicated by
indices, as in the expressions Vi' Mi, M j , and M/. Since selection is a
binary operation (i.e., to select or not to select), nlore general selection is
18 The language 1.9
3
3
3
2 3
A 23 A 43
(A 41
1
A )
and ulvll A === vllul A ===
A:3 A
4
3
1. the name, account number, and balance for each account with a balance
less than two dollars.
2. the nan1e, account nun1ber, and balance for each account with a negative
balance exceeding one hundred dollars.
3. the nan1e and account nun1ber of each account with a balance exceeding
one thousand dollars.
L'I/
with colunln vectors L 1 , L'2' and L 3 representing nanles, account nUIllbers, and
balances, respectively, and with row vectors LI, L'2, ... , LI/I representing
individual accounts. An unassigned account nunlber is identified by the word
"'none" in the nanle position. The four output lists will be denoted by the
Illatrices P, Q, R, and S, respectively. They can be produced by PrograIll 1.9.
Legend
Program 1.9 Selection on bank ledger L (Exanlple 1.1)
i +-
2 j+-O 2 C ~ \a,u,b\
3 k ~O
(b)
4 k v(a) +v(b)
5 k~k+l
6 3/ 1k 1 a, b Given vectors.
c c = (al,bl,b2,a2,b3,b4,a3, ...).
7 j ~j + 1
Index of a.
8 ck ~ bj
j Index of b.
9 i ~ i + 1 k Index of c.
10 ck -(- (Ii u u = (0,1,1,0,1,1,0, ...).
(a)
Legend
Program 1.10 Interfiling progran1
(which describes the merging of the vectors a and b, with the first and every
third component thereafter chosen from a) can be described alternatively
as shown in Program 1.1017. Since II == (I, 2, 3, 4, 5, 6, ... ), then
(3) 10 II == (1,2,0, 1,2,0, ... ), and consequently the vector u specified by
step 1 is of the form u == (0, 1, I, 0, 1. 1, 0, ... ).
Mesh operations on matrices are defined analogously, row mesh and
column mesh being denoted by single and double reverse virgules, respec
tively.
The catenation of vectors x, y, ... , z is denoted by x y z
and is defined by the relation
x == u\\ul X + u\ul X,
and X == u\\uIIX + u\\uIIX.
The nlask operation is defined formally as follows:
Analogous relations hold for the row mask and row Inesh and for the
colulnn mask and colulnn rnesh.
Certain selection operations are controlled by logical nlatrices rather
than by logical vectors. The rOll' cOfnpressiol1 VIA selects clernents of A
corresponding to the nonzero elenlents of V. Since the nonzero elenlcnts
of V nlay occur in an arbitrary pattern, the result nl ust be construed as a
vector rather than a nlatrix. More precisely, VI A denotes the catenation
of the vectors V) Ai obtained by row-by-row compression of A by V.
22 The language 1.9
u == (~ ~ ~ ~)
then VIA ==
(A 2\ A 4 1, A.")l, A 12, A 22, A 2a, A:3:3),
This fonn ulation enlphasizes the fact that nla trix nl ultipl ication incorpor
ates two elCIllentary operations ( +, x) and suggests that they be displayed
explicitly. The ordinary Inatrix product will therefore be written as
X Y.
24 The language 1.11
(~
3 2
0 3
A= 1 0
0 0 !) and B=
0
2
2
0
u V = U: v.
In matrix algebra, the notion of partitioning a matrix into subnlatrices of
contiguous rows and columns can be generalized to an arbitrary parti
tioning specified by a logical vector u. The following easily verifiable
identities are typical of the useful relations which result:
ul/(X ~ Y) = (u/IX) ~ Y.
:~) ::)
Y2' Yl'
~ Y ==
(Yl'
Yl' Y2' ; Y ~ ==
(Yl'
Y2' Y2' ;
1.12 TRANSPOSITIONS
C~B C/ == B/
C/ == B~::;~; i == 1,2, ,11(B)
---+
-.. I
For a vector x, either x or x will denote reversal of the order of the com
ponents. For ordinary matrix transposition (that is, B), the commonly
used notation B will also be employed.
Since transpositions can effect anyone or more of three independent
alternatives (i.e., interchange of row and column indices or reversal of
order of row or of column indices), repeated transposition can produce
eight distinct configurations. There are therefore seven distinct transfornla
tions possible ~ all can be generated by any pair of transpositions having
nonperpendicular axes. *
Certain of the special logical vectors introduced in Sec. 1.7 have useful
analogs in logical matrices. Dimensions will again be indicated in paren
theses (with the column dimension first) and may be elided whenever the
dilllension is determined by context. If not otherwise specified, a matrix is
assumed to be sq uare.
Cases of obvious interest are the ju!1 matrix fern x n), defined by
fern x n) == 0, and the identity matrix I(m x n), defined by 1/ == (i == j).
More generally, superdiagonal matrices kl(m X n) are defined such that
kl/(m X n) == (j == i + k), for k :2: 0. Clearly 1 == I. Moreover, for
sq uare matrices hi 1"1 == (hr-k)/.
Four triangular matrices will be defined, the geometrical Sylllbols
employed for each indicating the (right-angled isosceles) triangular area of
* These transpositions generate the rotation group of the square [cf. Birkhoff and
MacLane (1941) Chap. VI]. A pair of transpositions commute if and only if their axes
are perpendicular. Hence the pair +-- and t may be written unambiguously as-<t- .
Moreover, ~t- = X. The remaining two transformations can be denoted by 4 and~,
with the convention that the operator nearest the operand (i.e., the horizontal) is
executed first.
1.14 Po~vn()fl1ials and positional nUlnher ,\ysteI11S 27
the 111 X n rectangular matrix which is occupied by ones. Thus
The use of the matrices E and I will be illustrated briefly. The relation
u ;, v == 2 10 (u v) can be extended to logical matrices as follovvs:
U / V == (2 E) 10 ( u ~ V) ~
mixed-radix system in which the successive radices (from high to low order)
are the successive components of a radix rector y.
The base y calue oj' x is a scalar denoted by y J__ x and defincd as the
scalar product y ~ x == w x, where w == n/
y is the \veighting vector.
For example, if y == (7, 24, 60, 60) is the radix vector for the con1n10n
temporal system of units, and if x == (0, 2, 1, 18) represents elapsed tinlC in
days, hours, minutes, and seconds, then
y J_ X == (D 7y) : X
exhibits the fact that the operation is of the double operator type. Its
use in the generalized matrix product therefore requires no secondary scan
operator. This will be indicated by a null placed over the syrnbol _l. Thus
The forward selection alb and the backward selection Tlb defined in
Sec. l.10 can both be used to reduce any vector b to a similar set, that is,
(alb)lb == (Tlb)lb == b.
Moreover, if f = (alx)lx, g = (aly)ly, and h = (alz)lz, then x = y n z
implies that f = g n h, and x = y u z implies that f = g u h.
The unit vector Ei(n) will be recognized as a special case of the charac
teristic vector E/ in which x consists of the single component j, and
y = l"(n), where h is the index origin in use. In fact, the notation can be E:"
used to make explicit the index origin h assumed for E i .
If z is any vector of dimension two such that Zl E x and Z2 E y, then z is
said to belong to the Cartesian product of x and y. Thus if x = (a, b, c)
and y = (0, I), the rows of the matrix
a 0
a 1
b 0
A=
b 1
c 0
c 1
are a complete list of the vectors z belonging to the product set of x and
y. The matrix A will be called the Cartesian product of x andy and will be
denoted by x @ y.
The foregoing definition by example will be formalized in a more
general way that admits the Cartesian product of several vectors (that is,
u ('9 v 09 ... 09 y) which need not be sets, and which specifies a unique
ordering of the rows of the resulting matrix. Consider a family of vectors
Xl, x 2 , .. , x' of dimensions d l , d 2 , . . . , do' Then
A +- X
I?
x-
0) (9 ... (9 x'"<=>.
A 1 :, d (k -- )
= (1 0
Xk" Xh" . . . , xl" '
, )
for all vectors k such that I ::s:: k i ---: d i . Clearly v(A) = s, and ,u(A) =
x Id. As illustrated by Table 1.11, the rows of the Cartesian product A are
not distinct if anyone of the vectors Xi is not a set.
If the vectors Xi are all of the same dimension, they may be considered
as the columns of a matrix X, that is, Xi = Xi. The product Xl (>9 x 2
':'J' . . x' = Xl @ X 2 09 ... 09 x, may then be denoted by (il X, or
alternatively by (2,IIY, where Y is the transpose of X. For example, if
x2 = (#, *) A b # 1
x:3 = (0,1) b *
0
d = (3, 2,2) b *
1
a # 0
a # 1
a * 0
a * 1
Table 1.11 The Cartesian product A = Xl x2 X3
1.16 RANKING
K +- B II G'f<=;>K j == Bj I C j .
Use of the ranking operator in a lnatrix prod uct reg uires no secondary
scan and is therefore indicated by a superior null synlbol. Moreover,
since the result lnust be linlited to a two-dinlensional array (nlatrix), either
the pre- or post-operand is required to be a vector. Hence
J ~- B~) C<:-~->Ji ~ Bi I C,
K +- b{ C<~>Kj == b I Cj .
)= (v(b)e) -L (b to x).
Reordering operations
The selection operations employed thus far do not permit convenient
reorderings of the components. This is provided by the mapping operation
defined as follows: *
For example, if a = (a, b, ... , z) and k = (6, 5,4), then C = (f, e, d).
The foregoing definition is meaningful only if the components of k each
lie in the range of the indices of a, and it will be extended by defining a j as
the null element if) does not belong to the index set of a. Formally,
am. if m i E ll(v(a))
C +-- am<=>c i = {
l
if m i ll(v(a)).
The ability to specify an arbitrary index origin for the vector a being
mapped is provided by the following alternative notation for mapping:
am. if m i E li(v(a))
c+-mS;a<=?c;=,o
{ '
if m i li(v(a)),
where i-origin indexing is assumed for the vector a. For example, if a
is the alphabet and m = (5,0,0,4,27,0,3), then C = m Jo a = (f, 0, 0, e,
0,0, d), and (c *oe)/c = (f, e, d). Moreover, mJ2a = (d, 0,0, c, Z, 0, b).
Elision of j is permitted.
If a ~ b, and m = b lj a, then clearly m.L b = a. If a $ b, then
m Jj b contains (in addition to certain nulls) those components common to
b and a, arranged in the order in which they occur in a. In other words,
(m * oe)/(m.Lb) = a n b.
* For the purposes of describing algorithms, this notation is superior to the classical
"disjoint cycles" notation for permutations [cf. Birkhoff and MacLane, (1941)] because
(1) the direction of the transformation (from a to c) is unequivocally indicated, and (2)
the notation directly indicates a straightforward and efficient method for actual execu
tion, namely, indirect addressing.
1.17 MapjJing and jJermutations 33
Consequently, if p, q, ... , t are vectors, each contained in b, then each
can be represented jointly by the vector b and a mapping vector. If, for
example, b is a glossary and p, q, etc., are texts, the total storage required
for b and the mapping vectors might be considerably less than for the
entire set of texts.
Ji
Mapping may be shown to be associative, that is, m l (m 2.L a) =
(ml.L m 2)Jj a. Mapping is not, in general, commutative.
Mapping is extended to Inatrices as follows:
m J A, and M Ja.
The alternative notation (that is, c = an}), which does not incorporate
specification of the index origin, is particularly convenient for matrices and
is extended as follows:
A ~ B,n <=> Ai = Bn1,.,
A ~ B n1 <=:> Ai = B,ni'
Permutations
A vector k of dimension n is called a j-origin per/nutation rector if
k == lj(n). A permutation vector used to map any set of the same dinlen
sion produces a reordering of the set without either repetition or suppres
sion of elements, that is, k.L a == a for any set a of dimension v(k). For
example, if a = (f, 4, *, 6, z), and k = (4,2, 5, 1,3), then k Xl a = (6,4,
Z, f, *).
If p is an h-origin permutation vector and q is any j-origin permutation
vector of the same dimension, then q .L p is an h-origin permutation vector.
Since
the interval vector lj(n) will also be called the j-origin identity pernultation
vector. If p and q are two j-origin permutation vectors of the same
dimension n and if q .L p = lien), then p .L q = lien) also and p and q are
said to be inrerse permutations. If p is any j-origin permutation vector,
then q = p lj li is inverse to p.
t
The rotation operation k x is a special case of permutation.
34 The language 1.17
Function mapping
A function f which defines for each element b i of a set b a unique
correspondent ale in a set a is called a mapping fronl b to a. If f(b i ) = ale,
the element b i is said to map into the element ale. If the elements f(b i )
exhaust the set a, the functionfis said to map b onto a. If b maps onto a
and the elements j'(b i ) are all distinct, the mapping is said to be one-to-one
or biunique. In this case, v(a) = v(b), and there exists an inverse mapping
from a to b with the same correspondences.
A program for performing the mapping f from b to a must therefore
determine for any given element b E b, the correspondent a E a, such that
a = feb). Because of the convenience of operating upon integers (e.g.,
upon register addresses or other numeric synlbols) in the automatic
execution of programs, the mapping is frequently performed in three
successive phases, determining in turn the following quantities:
1. the index i = b l h,
2. the index k such that ale = f(bJ,
3. the element ale.
The three phases are shown in detail in Program l.I2a. The ranking is
performed (steps 1-3) by scanning the set b in order and comparing each
element with the argument b. The second phase is a permutation of the
integers 1,2, ... , v(b), which may be described by a permutation vector
j, such that Ii = k. The selection of ji (step 4) then defines k, which, in
turn, determines the selection of ale on step 5.
Example 1.2. If
b = (apple, booty, dust, eye, night),
a = (Apfel, Auge, Beute, Nacht, Staub)
are, respectively, a set of English words and a set of German correspondents (both
in alphabetical order), and if the function required is the n1apping of a given
English word b into its German equivalent a according to the dictionary corre
spondences:
English: apple booty dust eye night
German: Apfel Beute Staub Auge Nacht
thenj = (1,3,5,2,4). If b = "night," then i = 5,ji = 4, and a = a4 = Nacht.
2 i~-i+l 2 i +- i + 1
3 b bi 3 a aj
4 k +-ii 4 k -<-ki
5 a +- a7,~ 5 b +- b 1.:
i+-blb
i -<-0
k -<-ii
2 i +- i + 1
a -<- ak
3 a ai
5 k+-k+l
~
6 i : il;
k +- (b = b)/i
7 h +-- bk
a +-a k
a Set of correspondents in
Programs (a, d, e) and set
of arguments in Programs
(b, c).
b Set of arguments in Pro
grams (a, d, e) and set
of correspondents in Pro
grams (b, c).
j, k Mutually inverse
permutation vectors.
Legend
Program 1.12 Mapping defined by a permutation vector j
35
36 The language 1.17
Ordering vector
If x is a numeric vector and k is a j-origin permutation vector such that
the components of Y = k.L x are in ascending order, then k is said to order
x. The vector k can be determined by an ordering operation defined as
follows:
k +- 8j jx
implies that k is a j-origin permutation vector, and that if Y = k Jj x, then
either Yi < Yi+1 or Yi = Yi+1 and k i < k i +1. The resulting vector k is
unique and preserves the original relative order among equal components.
For example, if x = (7, 3, 5, 3), then ()ljX = (2,4, 3, 1).
The ordering operation is extended to arbitrary vectors by treating all
nonnumeric quantities as equal and as greater than any numeric quantity.
For example, if a = (7,0,3, ,0 5,3), then ()lja = (3,6,5, 1,2,4), and if
b is any vector with no numerical components, then ()jjb = li(v(b)).
Ordering of a vector a with respect to a vector b is achieved by ordering
the b-index of a. For example, if a = (e, a, s, t, 4,7, t, h), and b is the
alphabet, then m = b II a = (5, 1, 19,20,0,0,20,8) and ()ljm = (2, 1,8,
3, 4, 7, 5, 6).
The ordering operation is extended to matrices by the usual convention.
If K = ())/A, then each column of the matrixB = KJ.LAisinascending
order.
1.18 MAXIMIZATION
(4,13) 1_ (B i o H) = (25,4,37,23,28,40,29,24,6,26,9,33,13),
and (e[((4, 13) ~ (B i O H)))/H = (s, 3)
is the highest ranking card in the hand.
specifies the variable x as the vector whose base two value is the number z.
Certain ambiguities remain in the foregoing statement. First, the
dimension of x is not specified. For example, if z = 12, x = (1, 1, 0, 0) is
an admissible solution, but so are (0, 1, 1, 0, 0) and (0, 0, 0, 1, 1, 0, 0).
This could be clarified by compatibility with a specified dimension of E.
Thus the statement
(2(5)) x+-z
vex) +- 5
(2) 1- x +- z; vex).
The second ambiguity concerns the permissible range of the individual
components of x. For example, the base two value of x = (5, 2) is also
twelve. For certain functions it is therefore necessary to adopt some
obvious conventions concerning the range of the result. The assulnption
implicit in the preceding paragraph is that each component of x is limited
to the range of the residues modulo the corresponding radix. This con
vention will be adopted. Hence the pair of statements
b ~ \ulb, u, a\.
Similarly,
ulb ~ ula
is equivalent to
b ~ Ib, u, al.
Vectors and matrices are arrays which exhibit one level and two levels of
structure, respectively. Although in certain fields, such as tensor analysis,
it is convenient to define more general arrays whose rank specifies the
number of levels of structure (i.e., zero for a scalar, one for a vector of
scalars, two for a vector of vectors (matrix), three for a vector of matrices,
etc.), the notation will here be lilnited to the two levels provided by the
matrix. * The present section will, however, indicate methods for ren10ving
this limitation.
The only essential particularization to two levels occurs in the provision
of single and double symbols (e.g., "I" and "II", "~" and "Jl") for row
and column operations, respectively, and in the use of superscripts and
subscripts for denoting rows and columns, respectively. In applications
requiring multiple levels, the former can be generalized by adjoining to the
single symbol an index which specifies the coordinate (e.g., "'II" and "/2'"
for row and for column compression, and, in general, "//'.) The latter can
be generalized by using a vector index subscript possessing one con1ponent
index for each coordinate.
The generalized notation can be made conlpatible with the present
notation for vectors and matrices by adopting the nam.e tensor and a
symbol class (such as capital italics) for the general array of arbitrary rank.
1.21 SUBROUTINES
X+-P
y +-q
Cos (p, q)
r +- Z
--1 r - Cos (p , q) ~
Cos
r +- Z
1.22 FILES
Many devices used for the storage of information impose certain restric
tions upon its insertion or withdra\val. The items recorded on a magnetic
tape, for example, may be read from the tape much more quickly in the
order in which they appear physically on the tape than in some other
prescribed order.
Certain storage devices are also self-indexing in the sense that the item
selected in the next read from the device will be determined by the current
state or position of the device. The next itenl read from a magnetic tape,
for example, is determined by the position in which the tape was left by the
last preceding read operation.
To allow the convenient description of algorithlTIs constrained by the
characteristics of storage devices, the following notation will be adopted.
A file is a representation of a vector x arranged as follows:
The null elements denote the "unused" portion of the file not employed in
1.22 Files 41
representing x. Each partition Pj determines a position (position j) in the
file. If a file (1) is in position j, then a forward read, denoted by
x, p +- 0(1),
U ~ e(m)
<I> .1
1, Input files for i E "l(m).
2 k ~O
Each has terminal
3 2
7T(<I>1 ) ~ 1 partition A2
<1>2
1 Output file.
4 i +- m
u File <I> l is exhausted if
5 n{<I>l) +- 1 and only if u i = 1.
i=
6 i+-i-l b Itern to be recorded.
7 u:e
Legend
8 k +- ml 1(k + 1)
9 Uk : 1
1
10 b,p +-<I>k
11 Uk ~(p = A2)
12 u:e
2
13 <1>1 +- b, Al
14 <D 12 +- b, A 2
item read. However, when the last file becomes exhausted, step 14 is executed
instead to record the last item, together with the final partition A2
Steps 1-6 initialize the parameters u and k and rewind all files. After the last
item is recorded by step 14, the file rewinds are repeated before the final termina
tion on step 7.
It is sometimes convenient to suppress explicit reference to the partition
symbol read from a file by using a statement of the form
.Al I fh I A2 .
~x+-o'-V~,
where the indicated branches depend on the value of the partItIon Pj+l
which terminates the read. Thus the left or the right branch is taken
according to whether Pj+I = Al or Pj+l = A2 Certain files (such as the
IBM 7090 tape files) permit only such "immediate" branching and do not
permit the partition symbol to be stored for use in later operations, as was
done in Program 1.14.
I n recording, the lowest level partition Al may be elided. Thus statement
13 of Program 1.14 may be written as
<D I 2 +- b.
1.22 43
A file n1ay be read or recorded backward as well as forward. A backward
read is denoted by
x, P +-- I(I>,
and if <I) is initially in position j + I, then x == Xj' P == Pj' and the final
position becomes j. Backward recording is defined analogously. The
zero prescript may be omitted frOITI the syn1bol 0(1) for both fOf'Nard
reading and recording.
The conventions used for matrices can be applied in an obvious way to
an array of files (1)/. For example, the statement
7TDI) +--
denotes the rewinding of the rOlV of Jiles (D/, j E ll(V(V)): the staten1cnt
denotes the rewinding of the colunlll of Jiles <:1)/, i E llCUD)); and the
statement
ul(Di +-- ulx, ulp
denotes the recording of the vector con1ponent x j on file (D/ together \vith
partition Pj for all j such that u j == 1.
As for vectors and matrices, j-origin indexing D1ay be used and \vill
apply to the indexing of the file positions and the partition vector A as well
as to the array indices. I-Iowever, the prescripts (denoting direction of
read and record) are independent of index origin. O-origin indexing is
used in the following example.
Example 1.4. Files <Doo and <1\0 contain the vectors x andy, respectively, each
of dinlension n. In the first phase, the conlponents are to be nlerged in the order
x o, Yo, Xl' Y1' ... 'XlI-I' Yjl-l' and the first n conlponents of the resulting vector
are to be recorded on file (1)0 1 , and the last n on file <1\1. In other words, the
vectors Xl = anlz, andy 1 = wnlz are to be recorded on (1)0 1 and (1)/, respectively,
where z = \x, U, y\, and U = (0, 1, 0, 1, ... , 0, 1). In the next phase, the roles
of input and output files are reversed, and the sanle process is perfornled on Xl
and yl, that is, x 2 = an lexl, u, yl J, and y2 = w n lexl, U, yl J are recorded on
files <1)0 0 and <1)1, respectively. The process is to be continued through IJl phases.
Program 1.15. The program for Exanlple 1.4 begins with the rewind of the
entire 2 x 2 array of flIes. To obviate further rewinding, the second (and each
subsequent even-nunlbered) execution is perfornled by reading and recording all
files in the backward direction. Step 6 perfornls the essential read and record
operation under control of the logical vector u, whose conlponents u o, u l ' u 2
detennine, respectively, the subscript of the file to be read, the subscript of the
file to be recorded, and the direction of read and record. The 11le superscripts
(determining which classes serve as input and output in the current repetition) are
also determined by u 2 ' the input being u 2 and the output u 2 . The loop 6-8 copies
44 The language 1.22
O-origin indexing
Legend
Program 1.15 Program for Example 1.4
n iten1s, alternating the input files through the negation of U o on step 7. When
the loop terminates, u 1 is negated to interchange the outputs, and the loop is
repeated unless u 1 = u 2 - Equality occurs and causes a branch to step 3 if and
only if all 2n items of the current phase have already been copied.
Step 3 decrements 111 and is followed by the negation of U on step 4. The con1
ponent u 2 must, of course, be negated to reverse direction, but the need to negate
U o and u 1 is not so evident. It arises because the copying order was prescribed for
the forward direction, beginning always with the operation
Not all computer files have the very general capabilities indicated by the
present notation. Some files, for example, can be read and recorded in the
forward direction only and, except for rewind, cannot be positioned
directly. Positioning to an arbitrary position k must then be performed by
a rewind and a succession of (k - 1) subsequent reads. In some files, re
cording can be performed in the forward direction only, and the positions
are defined only by the recorded data. Consequently, recording in posi
tion k makes unreliable the data in all subsequent positions, and recording
must always proceed through all successive positions until terminated.
1.23 Ordered trees 45
Directed graphs
114
1311 n16
2 2221 n21
223 n22
22231 n25
Figure 1.16 A general triply rooted tree with ).(T) = 16, 'J(T) = (3, 3, 4, 3, 2),
veT) = 5, ~(T) = (3, 7, 8, 5, 3), and {leT) = 26
46 The language 1.23
general structure (such as Fig. 1.17) in which a unidirectional association
may be specified between any pair of its components.
A directed graph comprises a vector n and an arbitrary set of unilateral
associations specified between pairs of its components. The vector n is
called a node rector and its components are also called nodes. The associa
tions are conveniently specified by a (logical) connection matrix U of
dimension v(n) X v(n) with the following convention: there is an associa
tion, called a branch, fronl node i to node j if and only if Uj ; = 1.
A directed graph admits of a simple graphical interpretation, as
illustrated by Fig. 1.17. The nodes might, for example, represent places,
and the lines, connecting streets. A two-way street is then represented by
a pair of oppositely directed lines, as shown between nodes 3 and 4.
If k is any mapping vector such that
then the vector p = kJn is called a path rector of the graph (n, U). The
dimension of a path vector is also called its length. Nodes k 1 and k v are
called the initial and final nodes, respectively; both are also called
tenninal nodes. If j is any infix of k, then q = jJ n is also a path. It is
called a subpath of p and is said to be contained in p. If v(q) < v(p), then
q is a proper subpath of p. If k 1 = k" and p = kJn is a path of a length
exceeding one, p is called a circuit. For example, if k = (6, 1,7,7,2, 6,
1, 5), then p = (n 6, n 1, n 7, n 7, n 2, nfl' n 1, n 5) is a path vector of the graph
of Fig. 1.17, which contains the proper subpaths (n 7, n 2, n 6), (n 1, n 7,
n 7, n 2, n 6, n 1), and (n 7, n 7), the last two of which are circuits. Node j
is said to be reachable from node i if there exists a path from node i to
node j.
Ordered trees
A graph (such as Fig. 1.16) which contains no circuits and which has
at most one branch entering each node is called a tree. Since each node
is entered by at most one branch, a path existing between any two nodes
in a tree is unique, and the length of path is also unique. Moreover,
if any two paths have the same final node, one is a subpath of the
other.
Since a tree contains no circuits, the length of path in a finite tree is
bounded. There therefore exist maximal paths which are proper subpaths
of no longer paths. The initial and final nodes of a maximal path are
called a root and leaf of the tree, respectively. A root is said to lie on the
first leeel of the tree, and, in general, a node which lies at the end of a path
of length j from a root, lies in the jth level of the tree.
1.23 Ordered trees 47
n2
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0
n5
u= 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0
n4
(ex"(X) jv(T))
is a nlini111U111.
Program 1.18. The index vector i specifies the path currently under test. Its
last conlponent is incremented repeatedly by step 7 until the loop 6-8 is terminated.
If the path T i agrees with the corresponding prefix of the argument x, ternlination
occurs through the branch to step 9, which tests for conlpletion before step 10
augnlcnts i by a final zero component. Step 5 then respecifies d as the degree of
the penultilnate node of the set of d paths next to be tested by the loop. Termina
tion by a branch from step 6 to step 2 occurs if all d possible paths are exhausted
without finding agreement on step 8. I n this event, retraction by one level occurs
on step 2, and d is again respecified. Jf 1'( i) = I, the paths to be searched conl
prise the roots of the tree and d nlust therefore be specified as the nU111ber of
roots. This is achieved by executing step 3 and skipping step 5. Retraction to a
vector i of dinlension zero occurs only jf all roots have been exhausted, and final
ternlination from step 4 indicates that the tree possesses no path equal to the
argunlent x.
2 i +- wi/i
T Given tree.
be determined.
5 d +- (5(;)1/i, T)
fJ.l(T) Number of roots of T.
i
6 w /; : d
(5(i, T) Degree of node i.
7 ; +- i + wI
8 =I- T i al'(i)/x Legend
9 v(i) : vex)
10 i +- wI\i
Moreover, if n is itself the alphabet (that is, n = (a, b, c, ... , z)), then
the vector n' of Table 1.19a is a permutation of n, and d' is the associated
degree vector. Table 1.19b shows another such pair, nil and d".
The degree vector provides certain useful infornlation lTIOSt directly.
For example, since each leaf is of degree zero, }.(T) = +/(d = 0). More
over, the number of roots is equal to the number of nodes less the total of
the degrees, that is, flol(T) = v(d) - + /d, and the 11laxinlunl degree
occurring in T is given by beT) = ((f d)/d)l. Finally, the degree vector
and the node vector together can, in certain permutations (those of Table
1.19), provide a complete and compact descri ption of the tree.
* In the I-origin indexing system used here it would be possible to use the nUI11eric
zero to represent the null. In O-origin indexing, however, zeros occur as con1ponents of
index vectors and must be distinguishable from the nulls used.
50 The language 1.23
I~
]11
n' ]' d ll nIl
3 a 1 0 0 0 0 1 3 a 0 0 0 0 1
4 c 1 1 0 0 0 2 2 b 0 0 0 0 2
0 f 1 1 1 0 0 3 2 g 0 0 0 0 3
0 d 1 1 2 0 0 4 4 c 0 0 0 1 1
0 r 1 1 3 0 0 5 0 z 0 0 0 1 2
0 e 1 1 4 0 0 6 1 11 0 0 0 1 3
0 z 1 2 0 0 0 7 0 k 0 0 0 2 1
1 11 1 3 0 0 0 8 3 h 0 0 0 2 2
2 i 1 3 1 0 0 9 0 j 0 0 0 3 1
0 P 1 3 1 1 0 10 0 1 0 0 0 3 2
0 q 1 3 1 2 0 11 0 f 0 0 1 1 1
2 b 2 0 0 0 0 12 0 d 0 0 1 1 2
0 k 2 1 c 0 0 13 0 r 0 0 1 1 3
3 h 2 2 0 0 0 14 0 e 0 0 1 1 4
0 0 2 2 1 0 0 15 2 i 0 0 1 3 1
3 111 2 2 2 0 0 16 0 0 0 0 2 2 1
0 u 2 2 2 1 0 17 3 111 0 0 2 2 2
2 s 2 2 2 2 0 18 0 v 0 0 2 2 3
0 t 2 2 2 2 1 19 0 P 0 1 3 1 1
0 w 2 2 2 2 2 20 0 q 0 1 3 1 2
1 x 2 2 2 3 0 21 0 u 0 2 2 :2 1
0 y 2 2 2 3 1 22 2 s 0 2 2 :2 :2
0 v 2 2 3 0 0 23 1 x 0 2 2 2 3
2 g 3 0 0 0 0 24 0 t 2 2 2 2 1
0 j 3 1 0 0 0 25 0 w 2 2 2 :2 :2
0 1 3 2 0 0 0 26 0 Y 2 2 2 3 1
I
Full left list 111atrix [T Full right list 111atrix ]T
(a) (b)
Table 1.19 Full list 111atrices of the tree of Fig. 1.16
dimension /leT) X veT), which, together with the associated node vector,
completely describes the tree, If, for example, the node vector n is the
alphabet, the tree of Fig. 1.16 is described by the node vector n' and index
matrix I' of Table l.l9a or, alternatively, by n" and I" of Table 1.19b.
Because of the utility of the degree vector, it 'will be annexed to the array
of node vector and index matrix, as shown in Table 1.19a~ to form a full
list lnatrix of the tree. The degree vector and node vector together will be
called a list lnatrix. As remarked, the list Inatrix can~ in certain pernluta
tions, alone describe the tree.
Formally, the full list matrix M of a tree T is defined as follows: a2/ M
is an index matrix of the tree, M 1 is the associated degree vector, and M;!.
1.23 Ordered trees 51
is the associated node vector. Thus for each k E llCU(T)), M/;: = b(i, T),
and M/~ = (Ti).l'(i)' where i is the nonnull portion of -a,2/M\ that is, i =
((fi2/ Mk) *0)/fi 2/ Mk). The corresponding list matrix is a 2/ M.
Since a full list matrix provides a complete description of a tree regard
less of the order in which the nodes occur in the list, any column permu
tation MP (that is, any reordering among the rows) is also a full list matrix.
Two particular arrangements of the full list nlatrix are of prime interest
because each possesses the following properties: (1) the nodes are grouped
in useful ways, and (2) the list matrix (i.e., the degree vector and node
vector) alone describes the tree without reference to the associated index
matrix. They are called the full left list matrix and full r(r;lzt list matrix and
are denoted by [T and ]T, respectively. Table 1.19 shows the fuHleft and
full right lists of the tree of Fig. 1.16.
The left list index matrix I is left justified, * that is, the null elements are
appended at the right of each index. The rows Ij are arranged in increasing
order on their values as decimal (or rather (b(T) + 1)-ary) fractions with
the radix point at the left and the nulls replaced by zeros. More precisely,
the rows are arranged in increasing order on the function (1'(o))
(a to Ij), where a = (0, 1, 2, ... , ()(T)). t
The right list Inatrix is right justified and is ordered on the sanle function,
namely (v(a)) ~ (a Lo Ij). The rows are therefore ordered on their values
as integers, i.e., with the decimal point at the right. Fronl the exalnple of
Table I.I9b it is clear that the right list groups the nodes by levels, i.e.,
level j is represented by the infix (i J a l,)/ /(]T), where k = fJ-lT), and
i = + /aj-1/fJ-(T). In Table 1.19b, for exanlple, fJ-(T) = (3, 7, 8, 5, 3), and
if j = 3, then k = 8, i = 10, and level j is represented by rows i + 1 = II
to i + k = 18. The right list is therefore useful in executing processes
(such as the pth degree selection sort) \vhich require a scan of successive
levels of the tree.
The left list groups the nodes by subtrees, i.e., any node i is followed
inl111ediately by the remaining nodes of its subtree T i . Fornlally, if I =
cx 2/[T, and if i = (II,; :F o)/II,', then the tree T i is represented by the infix
((k - I) J a/I(Ti ))/ /[T. In Fig. 1.19a, for exa111ple, if k = 16, then i =
(2,2,2), {l(T;) = 7, and T i is represented by rows 16 to 22 of [T. The left
list is therefore useful in processes (such as the construction of a H uffnlan
code and the evaluation of a cOlnpound statenlent) which require a
treatnlent of successive subtrees.
The row index of a node in a right (left) list nlatrix is a graph index of
the node and will be called the right (left) list index.
* The term left list and the notation
[T are both intended to suggest left justification.
These statements hold only for I-origin indexing. ]n O-origin indexing, a =
-j-
(~, 0,1, ... ,()(T) -1).
52 The language 1.23
Well fornlation
A two-colulnn matrix which forms the right list of son1e tree is said to
be a It'ell fornled right list. Since the ordering of the nodes in a right list of
a given tree is unique, the right list of a given tree is unique. Conversely,
any well fonned right list specifies a unique tree according to the algorithm
of Progran1 1.20.
Identical relnarks apply to the left list, except that Progran1 1.20 is
replaced by PrograIn 1.21. Moreover, the necessary and sufficient
conditions for the well formation of a left list are identical with those for a
right list and are derived by virtually identical arguments. The case will
be stated for the right list only.
If R is a well fornled right list representing a tree T, then the dispersion
(i.e., the nUlnber of roots) vl(T) = v(R I ) - (+ IR I ) must be strictly
positive. Moreover, if S = CiJ II R is any suffix of R, then S is a right list of
the tree obtained by deleting from T the first j nodes of the original list.
For, such level-by-Ievel deletion always leaves a legitin1ate tree with the
degrees of the remaining nodes unchanged. Consequently, the nun1ber of
roots deternlined by every suffix of R l must also be strictly positive. In
other words, all components of the suffix dispersion rector s defined by
and left lists, a nlatrix may be characterized sinlply as well or ill fonned.
1.23 Ordered trees 53
The purpose served by the degree vector d in the description of a tree is
sometimes served instead [e.g., Burks et a1. (1954)] by the vector g ==
- d. It is sonlewhat 1110re convenient in the analysis of well fOflnation,
since the expression for the suffix dispersion vector then sinl.plifies to
or s == (I + 0);-1 g.
The index matrix as a function of the degree vector
The complete determination of the tree corresponding to a given list
111atrix Al is best described as the determination of the associated index
matrix I. For both left and right lists this can be achieved by a single
forward scan of the rows of M and of I.
For a right list R it is first necessary to deternline r, the nUlnber of roots.
The first r components of R are then the roots of the tree in order, the
next R 1l components of R are the second-level nodes reachable fronl the
first root, and so forth. Programs 1.20 and 1.21 describe the processes for a
right list and a left list, respectively.
Program 1.20. In each execution of the nlain loop 13--16, the ith row of the
right list R is exanlined to deternline the index vector of each node on the succeed
ing level which is directly reachable fronl it. The nunlber of such nodes is con
trolled by the paranleter d, initialized to the degree of the ith node by step 12.
The (right list) index of the nodes reachable fronl node i is deternlined byj, \vhich
is incremented on step 14 as the index vector of each node is detern1ined. The
index vectors of the successive nodes reachable fronl node i have the final con1
ponents 1, 2, 3, ... , and each nlust be prefixed by the index vcctor of node i.
This assignnlent is effected by the vector v, which is initialized by thc indcx vector
of node i rotated left by one (step 11), and which is incren1cnted by step 15 before
each assignment occurring on step 16. At the outset, v is set to zero and d is sct
to the nunlber of roots as deternlined by step 4.
Sincej is, at step 10, equal to the current nunlber of roots r augnlented by the
cumulative degrees of the first i - I nodes, then r = j - i + 1 and the exit on
step 10 therefore occurs always and only in the event of ill forn1ation. ;-\Itcrna
tively, the test can be viewed as an assurance that each row of the ll1atrix 1 is
specified before it is itself used in specification.
When step 5 is first reached, the index n1atrix I is cOlllplete but is cxprcssed in
I-origin indexing with zeros representing the null elen1cnts. Steps 5-7 translate
the matrix to the origin and nlask in the nccessary null elen1cnts.
Program 1.21. The index vectors lj are detennined in order under control of
the paranletcrj. The loop 5-18 traces a continuous path through the tree, deter
mining the index of each successive node of the path by rotating the index of the
preceding node (step 17) and adding one to the last con1ponent (step 13), and
maintaining in the connection vector c a record C i +1 of the indexj of the successor
of node i in the path traced. The path is interrupted by the occurrellce of a leaf
(that is, L1j = 0 on step 18), and the degree vector L 1 is then scanned by the loop
54 The language 1.23
2 j+-O
R Right list of T.
4 d +- ~{Rl) (+ IR]) T.
i Index of row of R cur
5 TJ +- (I = 0)
ren tl y examined.
6 I +- I + (> - I)E j Right list index of node
7 I ~-II, U,oEI reachable from node i.
v Current index vector.
8 i : peR)
> Origin with respect to
9 i +- i + 1
which I is finally ex
>
10 i :j pressed.
11 v +- t Ii
Legend
12 d -(-R1i
13 d+-d-l <
14 j +-j + 1
15 v -(- v + WI
16 Ii +- v
(19-20) to deternline the index i of the last preceding node whose branches renlain
inconlpleted. Steps 22-23 then respecify v as the index vector of the node follow
ing node i in the path last traced, and step 21 decren1ents the conlponent L1i of
the degree vector. The branch fron1 step 19 to step 22 occurs at the con1pletion
of each rooted subtree. The test for well fonnation (step 12) is the sanlC as that
applied to the right list in Program 1.20, except that the notation for the relevant
paranleters differs. The concluding operations (6-9) include left justification on
step 7.
P +-- U/T
2
L Left list of T.
3 V~O
1 Left index matrix of T.
4 r ~ veL}) - (+IL 1 ) j Index of row of 1 being
determined.
5 j /1(L)
i Left list index of path
6 U ~ (1 = 0) node preceding node j
1 ~ ( + 1lJ.(U) t 1 in current path (Step
7
16), or index of last
8 1 ~1 + (1) - I)E previous node whose
1 11, U, E1
branches remain un
9 +-- 0
17 Legend
18
19 i -(- i - I
22 k ~- cHI
23 V ~- II.'
Fig. 1.16 with n as the alphabet, and U is the tree of Fig. 1.22a, then P
is the tree of Fig. 1.22b. The new indices are shown to the left of each node
of P. The set of nodes 221, 222, ... ,226, are all on the sanle level of P
although they have been shown with branches of different lengths to
permit easy identification with the nodes of the original tree T.
56 The language 1.23
111 f
131
21 k
2
b
U U/T
(a) (b)
The compress operation is best executed on the left list because of the
grouping by subtrees. Program 1.23 gives a suitable algorithlTI which also
serves as a formal definition of the compress operation.
Program 1.23. The vector u is specified as the node vector of the left list of the
controlling logical tree U and controls the subsequent process. Step 4 deternlines
j as the index of the first zero conlponent of u. Steps 6 and 7 then delete the corre
sponding nodes of u and of the left list of T, but only after step 5 has deternlined d
as the change in degree which this deletion will occasion to the root of the sIllallest
subtree containing the deleted node. Steps 9-1 I perfornl a back\\'ard scan of the
degree vector to deternline j as the index of the root of the subtree, and step 12
effects the requisite change in its degree. The exit on step 9 occurs only if the node
deleted is a root of the original tree, in which event no change is produced in the
degree of any other node.
Two further compress operations controlled by logical vectors are
defined as follows. Path cornpressiol1 is denoted by
P +- u/T.
1.23 Ordered trees 57
12 Legend
P *- ulIT,
and P is obtained from T by deleting each rooted subtree T i for which
U i == O.
Path compression by a unit vector j produces a tree of hcight onc.
Such a tree is, in effect, a vector and will be treated as one.
Two related special logical trees are defined: the path tree UE such that
u!UE == 0 and u!UE is the full tree E whose nodes are all unity, and the
level tree uE such that ulluE == 0, and ulluE == E.
implies that node i of Z is the product of node i of X and node i of y for all
i. Similarly,
58 The language 1.23
specifies M as a tree (of the saIne structure as T) such that node i of M is
the j-origin b-index of node i of T.
The lnapping operation is extended to trees so as to pennute the rooted
subtrees of a tree. Forn1aIly
P ~ rn fJ T
implies that fLl(P) == vern), that Pi is a single null character if rn i ti(fLl(T)),
and otherwise Pi == T m.' where j-origin indexing is used for T.
Permutation of the s'ubtrees rooted in node i of T can be effected as
follows:
The notation ciiT will denote the application of the binary operator or
relation c to the nodes of T in right list order (i.e., dOl1'n successive levels)
and G /T will denote the san1e application in left list order (i.e., across
paths). If the operator is sYlnn1etric (i.e., its operands con1mute), then
oilT == OfT.
Maximization (UrT) and minin1ization (UlT) are extended to trees in
the obvious \vay.
The operations 'l./u, (!)/u, a/a, and T/a are each extended in two ways:
across paths and down levels. Examples of each appear in Fig. 1.24.
Operations extending down levels are denoted by double virgules and
represent an application of the corresponding vector operation to each
level of the tree considered as a vector. For example, the staten1ent
V~(JIIA
implies that each level of V is the forward set selection of the corresponding
level of A, that is, j /V == a/j/A. Operations extending across paths are
denoted by single virgules and are defined in tern1S of subtrees. Thus
V~'l./U
implies that V is obtained fron1 the logical tree U by setting to zero all
nodes of any subtree rooted in a zero node, and
V ~ (/)/U
Homogeneous trees
If, for all j, every node on level j of a tree T is either of degree zero or of
degree 'V j ;l(T), then the tree T is said to be unij()rn7. If all leaves of a
1.23 Ordered trees 59
uniform tree T lie in the same level (necessarily the top), then the tree is
said to be hOIJ1ogeneous. The structure of a homogeneous tree is completely
characterized by its dispersion vector veT). All maxin1al paths in a homo
geneous tree are clearly of the same length, nan1ely veT) == v(v(T)). Figure
1.25 shows a homogeneous tree and its associated dispersion vector.
I----~O
a/U a/IV
1 o
~-_....>@ ~I---->"'@
w/u 1 1 w/IV
~I-----~""CD ~I----"~CD
o
(J/U o o O-//V
~I---_->~@ ~f----""'>@
--~1
r/U 1 o r//U
~--->-CD ~I---->~CD
Figure 1.24 Set selection and 111axin1unl prefix and suffix operations
60 The language 1.23
A tree T for which veT) = 111 is called an I1z-waJ' tree, and a tree for
which vI(T) = 1 and (iI/v(T) = m is called a singular I1z-lray tree.
The jth component of the mOlnent vector of
a homogeneous tree is clearly equal to the prod
uct of the first j components of the dispersion
n
vector, that is, fL(T) = (0 + I) / veT). The dis
persion vector is, in turn, uniquely deternlined
by the moment vector. The total nunlber of
nodes is given by [leT) = + /fL(T), and it can also
be shown that fl(T) = Y J_ y, where y is the
dispersion vector in reverse order.
Tree compression of a honl0geneous tree H
g (that is, lJ/H) does not generally produce a
homogeneous tree, and, in fact, any tree P of
arbitrary structure can be represented by a pair
of homogeneous trees U and H such that P =
U/H. On the other hand, both path and level
q
compression of homogeneous trees produce
r homogeneous trees. Moreover, ifP = u/H, then
v(P) = u/v(H), and if P = u//H, then v(P) =
v(H) - (+ /u)a].
Since the structure of a homogeneous tree is
completely specified by its dispersion vector k,
h the structure of the special logical trees can be
specified in the forms E(k), UE(k), and uE(k).
In a homogeneous tree, the right list or left
list index of a node can be detennined as an
explicit function of its index vector. Conversely,
the index vector i can be determined directly
from the corresponding left list index, to be
denoted by I(i), or from the right list index rei).
U In developing the relations between indices it'
II(H) = (2,3,2) will be convenient to use O-origin indexing
M(H) = (2,6,12) throughout.
Figure 1.25 Homogen The right list index is given by
eous tree H and disper
rei) = f(i) + g(i),
sion and moment vectors
where f(i) = + /aJ/(i) -1 /fL(T)
is the number of nodes in the first 1'(i) - 1 levels, and
g(i) = 1
(a '(i)/v(T)) 1- i
is the rank of node i in the v(i)th level. For exampIe, if i = (I, 0, 1) in the
1.23 Ordered trees 61
tree of Fig. 1.25, then fJ-(H) = (2, 6, 12), j(i) = +/(2, 6) = 8, and
g(i) = (2,3,2) (1,0,1) = 7.
Since j(i) depends only on v(i), the index i may be detennined fron1 r by
first determining v(i) as the largest value for which j'(i) :s: r, and then
determining i such that
(cx'J(i)jv(l')) _L i = r - f(i).
In tracing a path through a tree, the kth node of the set reachable fron1
node i is the node j = i .2:) (k). It is therefore useful to express r(j) as a
function of rei). Clearly
f(j) = f(i) + (fJ-(T))I'(i)--l,
g(j) = g(i) X (v(T))IJ(i) + jl'-l.
In the special case of a singular hon10geneous In-way tree,
f(i) = 1 + 1n + n1 2 + ... + 1111'(i)-::' = (111e) l- e(v(i) - 1)
m 1!(i)-1 _ 1
111 - 1
Hencej(j) = 1 + m X j(i), and g(j) = nl X g(i) + 11'-1' Recursion can
therefore be performed simply upon the single function rei) as follows:
r( j) = 111 X r( i) + 1 + jl' - 1.
The left list index lei) is most conveniently expressed as a function of
v(i) and of the vector z(i) (zero extension of i), where z = cx '(i>Cv(T))\i. 1
Clearly v(z) = veT) and z is the index of the "earliest" leaf reachable froIn
node i. In Fig. 1.25, for example, z((I, 2)) = (1,2,0).
The zero extension has the obvious property that every node above the
path TZ(i) precedes node i in the left list, and every node belo\v the path
follows it. The number of nodes in the path which precede node i is
v(i) - 1.
The number of leaves above the path TZ(i) is veT) ~ z(i), and n10re
generally, the number of (j - 1)th level nodes above it is given by
(cxj/v(T)) ~ (cxj/z(i)). Consequently,
l'(T)
REFERENCES
Birkhoff, G., and S. MacLane (I941), A Survey of Modern A (fjebra, Macmillan, New
York.
Burks, A. W., D. W. Warren, and J. B. Wright (1954), "An Analysis of a Logical
Machine Using Parenthesis-free Notation," Mathel11atical Tables and Other Aids to
COl1zputation, vol. VIII, pp. 53-57.
Dickson, L. E. (1939), New First Course in the Theory of Equations, Wiley, New York.
Garner, Harvey L. (1959), "The Residue Number System," IRE Transactions, vol. EC-8,
pp. 140-147.
Goldstine, H. H., and J. von Neumann (1947), "'Planning and Coding of Problems for
an Electronic Computing Instrument," Report on the MathelJlatical and Logical
Aspects ofan Electronic COlllpUtil{[[ InstrUl11ent, Part II, vol. 1, Institute for Advanced
Study, Princeton.
Iverson, K. E. (1954), "Machine Solutions of Linear Differential Equations," Doctoral
Thesis, Harvard University.
Jacobson, N. (1951), Lectures in Ahstract A~fjebra, vol. 1, Van Nostrand, New York.
Kunz, K. S. (1957), Nurnerical Analysis, McGraw-Hill, New York.
Margenau, H., and G. M. Murphy (1943), The Mathel11atics of Physics and Chel11istry,
Van Nostrand, New York.
Phister, M. (1958), Logical Des({n of D{[{ital C0l11puters, Wiley, New York.
Richards, R. K. (1955), Arithl11etic Operations in Digital Computers, Van Nostrand,
New York.
Riordan, J. (1958), An Introduction to C0l11hinatorial Analysis, Wiley, New York.
Rutishauscr, H. (1959), "Zur Matrizeninversion nach Gauss-Jordan," Zeitschrift fiir
A/~{e~vandte Mathenzatik und Physik, vol. X, pp. 281-291.
Wright, H. N. (1939), First Course in Theory of Numbers, Wiley, New York.
EXERCISES
Organize each of the programs according to the method of leading decisions.
Except where otherwise indicated, use I-origin indexing. The conventions of
Sec. S.I of the Summary of Notation will be used in the statement of each
of the exercises.
1.1 Let d = (a, 2,3,4,5,6,7,8,9, 10,j, q, k), S = (c, d, h, s), u = (1,0, 1,0,1),
v = (0, I, I, 1, 0), x = (16, 8, 4, 2, I), and y = (2, 3, 4, 5, 6). Determine
(a) the dimensions v(d), v(s), and vex).
(b) the vectors x + y, x - y, x x y, x -:- y, and u + v.
(c) the logical vectors u /\ v, u V v, (u =/-= v), and (u = v).
(d) the reductions + lx, x IY, /\ lu, and V Iv.
(e) the base two values of u and of v, that is, +/(x x u), and +/(x x v).
(f) the rotated vectors 2 t d, 4 i s, and iy.
(g) the unit vector 1(5) in a I-origin system, and 3(5) in a O-origin system.
(h) the infixes (a 5 (7) /\ w 5 (7)) and 2 t a 3 (7).
Exercises 63
1.2 Show that
(a) x /L l(n) = 11! (Include the case n = 0.)
(b) + /Lj(n) = n(n + 2j - 1) -:- 2.
(c) xj(k i x) = x/x.
(d) (k i x) + (k i y) = k i (x + y).
1.3 Write detailed (i.e., conlponent-by-conlponent) progran1s for the following
operations. Include tests for conlpatibility of the operands.
(a) w ~- U 1\ v. (g) u ~- a)(k).
(b) W ~- V V V. (h) u -(-- i 1 a)(k).
(c) b ~ - u/a. (i) c ~- \a, u, b "
(d) B +-- u/A. (j) c -(- la, u, b/.
(e) B ~- u/vj I A. (k) c ~- u,a.
(f) x +-- (x > O)/x.
1.4 Establish the identities
(a) la, u, b/ = \u/a, u, u/b;.
(b) \a, u, b, = /u\a, U, u'\b/.
1.5 The classic "rings-o-seven" puzzle can be posed as follows: an ordered
collection of n rings is to be placed on (renl0ved fronl) a bar under the following
constraints:
(i) ring n nlay be placed on or renloved at \vill.
(ii) ring k may be placed on or removed only if ring (k + 1) is on and all
succeeding rings are off.
The state of the rings can be described by a logical vector u, with Uk = 1 if ring k
is on. Write programs on U which describe the removal of the rings beginning
with
(a) U = [The successive values of u represent a r~flected Gray code; see
Phister (1958).]
(b) u arbitrary.
1.6 The ordered array of variables used to represent a variable x in S0111e coding
system 111ay be considered as a vector representation of ::r, denoted by p(~r). In the
8421 code for decimal digits, for exanlple, p(O) = (0,0,0,0), pel) = (0,0,0, 1),
and, in general, p(x) is defined by the relation + /[w x p(x)] = x, where w = (8,4,
2, 1). For each of the following coding systems, (see Richards, pp. 183-184 for
definitions), write a concise expression for p(::c):
(a) the excess-three code for decimal digits.
(b) any chosen two-out-of-five code.
(c) any chosen biquinary code.
(d) the semaphore code for the alphabet (see any good dictionary). Denote
each code by a two-component vector p(x) S; lO(8). Use a l:t', where
a = (a, b, c, ... , z).
1.7 Let X be a square sparse matrix represented by the logical nlatrix V =
(X *0) and either or both of the vectors r = V/X, and c = VIIX. Write pro
grams to determine the product Y = X ~- X, using the argunlents
(a) r, c, and U.
(b) rand V.
(c) c and U.
64 The language
1.8 Prove that
(a) rrl = -l-.TJ.
(b) lla --:- bJ --:- cJ = la --:- bcJ for all positive integers a, b, and c.
1.9 Let r = E/A, and c = EllA be the row list and colunln list, respectively, of
the nlatrix A, and let r ll , A/, and C k be corresponding elenlents of the three
representations of A.Deternline:
(a) h as a function of k, v(A), and fleA).
(b) k as a function of h, v(A), and fleA).
(c) the pernlutation vector h such that c = h r. J
1.10 Show that
(a) /u = /ii (Use De Morgan's law for two variables and induction.)
(b) ;/ /u = 210 + /u (Use induction.)
(c) =/u = 210 +/u.
(d) /u = =/ii.
(e) U'~ v = (2e) 10 (U ~- v).
(f) U c~\ V = U v V.
(g) (t Xu) !\ (v Xw) = (t Aw) /\ (v 7\ u).
1.11 (a) Show that +/x = +/(u/x) + +/(u/x). (Include the case u = 0.)
(b) What properties are required of an operator 0 that it satisfy the relation
established for + in part (a)?
1.12 Show that
(a) X x Y = (ii/X) (iiIIY) + (u/X) ~ (uIIY).
(b) u/(X Y) = X ;, (u/Y).
(c) ull(X Y) = (uIIX) ~ Y.
(d) (u v)/a = (u/v)/(u/a).
1.13 Use the result of Exercise 1.Il(b) to extend the results of Exercise I. 12(a-c)
to logical operators.
1.14 Write progranls to determine:
(a) the value of the polynomial x at the point a, that is, to evaluate (I/e) __ L x
for.ll = a. Use no nlore than vex) multiplications.
(b) the derivative of the polynomial x, that is, the vector z such that
d
(IJe) ~ Z =- ((I/e) x), and l'(Z) = lJ(X).
dy
(c) the integral z of the polynonlial x satisfying the boundary condition
(ae) J_ Z = b.
(d) the quotient q and remainder r obtained in dividing the polynonlial n by
the polynonlial d, for l'(d) ~ v(n}.
(e) the value of the polynonlial n at the point a by using part (d) with d =
(I, -a).
d
(f) the value of - ((ye) _L n) at the point a by t\\'O applications of part (e).
dy
(g) an approxinlate real root of the equation (ye) 1_ x = 0, using parts (e) and
(f) and the Newton-Raphson formula [Kunz (1957)].
Exercises 65
1.15 Let the conlponents of the vector r be the real roots of a polynolllial x.
Write a progralll to
(a) deterllline the synlnletric functions of r. [Dickson (1939), Ch. X.]
(b) deternline x as a function of r.
1.16 Write a progralll to deternline the polynolllial x consisting of the fIrst 11
terms of the exponential series 1 +,1/ + y2/2! -+- ....
1.17 Write a progranl to deternline the nloduli of all roots of the polynonlial x,
using the Graeffe method [Kunz (1957)]. Assunle that operations for the logar
ithm and exponential functions are available as subroutines.
1.18 List all the I-origin perlllutation vectors of dinlension four which are self
lnverse.
1.19 Using I-origin indexing, write progranls to derive
(a) the pernlutation k which is inverse to the pernlutation j.
(b) a pernlutation j which transfornls a given logical vector u to a prefix vector.
1.20 A square logical lllatrix U such that +/U = +// U = is sonletinlCs called
a per/11utation nlatrix, since prenlultiplication of a nUlnerical vector x deterlllines
a pernlutation of x. Write programs to deternline
(a) the permutation matrix U corresponding to the I-origin pernlutation vector
k, that is, deternline U such that U J
x = k 1 x.
(b) the pernlutation k corresponding to a given pernlutation 11latrix U.
(c) the pernlutation V which is inverse to the pernlutation U.
1.21 Let p be the vector representation of a pernlutation and let c be the
standard representation in ternlS of disjoint cycles, including all cycles of one
[Jacobson (1951), p. 34.] Each cycle of c is enclosed in square brackets, each
half-bracket being considered as a component of c. For exanlple, if c = (L I, 3,
5,], L 2,4,], L 6,]), then p = (3,4,5,2, 1,6), v(c) = 12, and v(P) = 6, and, in
general, v( c) = v(P) + 2k where k is the nunlber of disjoint cycles in p. The
usual elision of cycles of one would give c = (L 1, 3, 5, ], L 2,4, ]), but this deter
Dlines a unique correspondent p only if the dimension of p is otherwise specified,
and inclusion of all cycles of one will therefore be aSSUDled. If each infix of
numerical cOlllponents in c is preceded by a left bracket and followed by a right
bracket, and if c determines a legitimate pernlutation vector p, then c is said to be
well./()rnlcd.
(a) Write a progranl to deternline p as a function of a well fornled pernlutation
c. Include determination of the dimension of p.
(b) Modify the program of part (a) to incorporate checks on the well fornlation
of c. If c is ill formed, the vector p is to be defined as the literal "ill
formed."
(c) Modify part (b) to process a sequence of vectors cl, c 2 , ,each pair being
separated by a single null element, and the end of the sequence being
indicated by a pair of successive null elenlents, i.e., to process z =
c1 (0) c2 C,. (0,0). Include checks on the well fornlation
of each pernlutation.
(d) Write a progranl to deternline the parity [Jacobson (1951), p. 36] of a
pernlutation vector p.
66 The language
1.22 Write detailed progranls for the following processes:
(a) k -(- 01/x (i) nl-(--bl o a
(b) y -(~- nl JIx (j) M -(- B ~o a
(c) V +- u r x (k) u -(- b(l
o
(d) V-(ur X (I) c -(- b n a
(e) v -(- 'l..lu (nl) c -(-- b L\, a
(f) V-(wjjU (n) c -(- b u a
(g) v -( alb (0) C -(- a (:) b
(h) V -(- TjjB
1.23 (a) Copy onto file (1)1 2 successive groups of itenls fronl the row of flies (1)1
in cyclic order, onlitting any exhausted files. The end of each group is
denlarked by a partition A2 , and the end of each file by a partition Aa .
(b) A file which is always recorded in the forward direction and read in the
backward direction functions as a stack. Using flle (1)2 2 as a stack,
nlodify the progranl of part (a) so as to reverse (in the output tile (1\2)
the order of the itenls within each group.
1.24 The acconlpanying node vector n and connection nlatrix C together specify
a directed graph (C/ = 1 indicates a branch fronl node i to node j) which is, in
fact, a tree.
n = (a, h, C, d, c, ,f, g)
0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0
C= 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0
(a) Draw one of the possible ordered trees represented by nand C.
(b) For the tree T of part (a) show the full left list [T.
(c) Show the full right list ]T.
1.25 Write progranls which include tests on conlpatibility and which deternline
(a) L = [T fronl R = ]T
(b) S = ](u/T) fronl fJ.(T), ]T, and u
(c) M = [(ujjT) fronl L = [T and u
(d) M = [(k II T) fronl L = [T and k
1.26 (a) Give an exalnple of a well fonned right list which denl0nstrates that a
prefix of a right list is not, in general, well fornled.
(b) Show clearly where the argunlent used in establishing the well fornla
tion of any suffix of a well fornled list breaks down when applied to a
prefix.
1.27 Give fornlal proofs for the facts that
(a) a left list groups nodes by subtrees.
(b) a right list groups nodes by levels.
(c) ~l(T) = lo(ll) - + Ill, where II is the degree vector of T.
1.28 Write progranls to deternline ~(T) as a function of
(a) the left list degree vector of T.
(b) the right list degree vector of T.
1.29 Trace Progranls 1.20 and 1.21 for the tree of Exercise 1.24.
1.30 Show that for a honlogeneous tree H, ,1f(H) = Y Y, where y= v(H).
1.31 If H is honlogeneous, v(H) = (3,2,3,4), and i = (1,0,2), deternline, in a
O-origin systenl
(a) the left list index I(i).
(b) the right list index rei).
(c) the index j of the nodc whose left list index is 27.
1.32 (a) If K = LO(Il) 1 ((11) ~ LO(I1)), show that K + K = n(E - n.
(b) If Y is any pernlutation of x and l'(x) = 11, show that x K /', x
Y K;~~ y.
1.33 Using the Euclidean algorithm, write progranls to deternline:
(a) d as the greatest conlnlon divisor of positive integers ,I' and /f.
(b) II as the g.c.d. of x and y where ct, x, and y represent polynonlials in ,:
(e.g., (: ) x).
1.34 To assure uniqueness, the nunlber of different digits (synlbols) used in a
base h nunlber systenl nlust not exceed h. The linlitation to the particular range
o ( l i ' h is, however, not essential. For exanlple, a base three systcnl can be
constructed using digits -1, 0, and 1, for which it is convenient to adopt the
synlbols -,0, and +, respectively. The positive nunlbers beginning at zero are
then represented by the sequence 0, +, + -, +0, + +, + - -, + -0, + - +,
+0 -, +00, etc. The negative nunlbers beginning at zero are 0, -, - +, -0,
- -, - + +, - +0, - + -, -0+, -00, etc.
(a) Construct addition and nlultiplication tables for this nunlber systenl and
calculate the sunl and the product of the nunlbers 0 - and - -. Usc the
decinlal systenl to check all results.
(b) Negative nunlbers are represented in this systenl without the attachnlcnt
of a special sign position. Spccial rules regarding sign are therefore
banished except that it is nece~sary to fornlulate a rule for changing the
sign of a nunlber, i.e., to nlultiply by nlinus one. Fornlulate such a
rule.
1.35 For any integer 11, let.('~ = 21n n, 1'3 = 31011'.('5 = sin n, and 1'7 = 71n 11.
As shown by Garner (1959), the ordered array(.('~, 1':3' .1'5' 1'7) provides a representa
tion of the integer n in a so-called residue nunlber systenl.
(a) Write the residue representations of the flrst ten nonnegative integers.
(b) For integers 11 in the range 0 n <'(2 x 3 x 5 x 7) show:
(1) that the representation is unique.
(2) that an addition algorithnl nlay be defined which treats the several
68 The language
colunlns independently, i.e., there are no carries. (The sunlS must
also lie within the specified range.)
(c) Discuss the choice of moduli for extending the range of the representa
tion.
(d) Show that the algorithm derived in part (b) is valid for all positive and
negative integers in the range -a12 :::;: n < al2 for a = 2 x 3 x 5 x 7.
(e) Derive an algorithm for obtaining -n fronl n.
(f) Derive an algorithm for multiplication.
(g) The sign of the number (i.e., its relation to zero) is not displayed
directly by this representation. Convince yourself that its deternli
nation is nontrivial.
1.36 Let x, y, and z be the positional representations of the nunlbers .r, .'I, and z
respectively. Using the floor and residue operations, write progranls to deter
mine z as a function of x andy, where z = J~ + y and the representation in use is
(a) base b.
(b) mixed base b.
(c) the +, -,0 base three system (of Exercise 1.34).
(d) the residue nunlber system (of Exercise 1.35).
1.37 Write programs for the multiplication z = x X y for each of the cases of
Exercise 1.36.
1.38 Write programs to convert in each direction between the following pairs of
number systems:
(a) base b l and base b 2
(b) base b l and base b 2
(c) base three and the +. -, 0 base three of Exercise 1.34.
(d) residue and base b (Exercise 1.35).
1.39 t
(a) Show that the superdiagonal matrices satisfy j/ 7,'/ = U ! 0/.
(b) A matrix of the form J = (xl + II) is called a Jordan box. Write the
expansion of the nth power of J.
(c) Show that X t y = Xl ~ y l + X 2 ~ y2 + ... + XI'(X) ~ yl'(X).
ei
(b) The sequence of operations which reduce the ith colunln of A to is callec
the ith step of the process, and the ith diagonal elenlent at the beginning of
the ith step is called the ith pivot elelnent. Modify the progranl of part (a)
so that each step is preceded by a colunln pernlutation which yields the
largest (in absolute value) pivot element possible. This nlodification tends
to reduce the accunlulation of round-off errors.
(c) In the Jordan elinlination of part (a), it is unnecessary to store the identity
matrix explicitly, and, since the ith colunln is first affected at the ith step,
only one new colunln need be brought in at each step. Moreover, the ith
colunln of A may be discarded after its reduction to e i on the ith step, and
it is therefore necessary to store only a square nlatrix at all ti!nes. Show
that by shifting all columns to the left and by nloving the rows upward
cyclically, a very unifornl process results, with the pivot elenlent in the
leading position at every step [Iverson (1954) or Rutishauser (1959)].
Write a progranl for the process.
(d) Modify part (c) to allow the choice of pivot elenlents as in part (b). The
effects of the pernlutation on the not explicitly recorded identity cannot be
indicated directly, but the performance of the sanle set of pernlutations in
reverse order upon the rows of the resulting inverse produces the sanle
result. Verify this and prograrTI the process.
1.41 (a) Show that a group [Jacobson (1951)] can be represented by a square
matrix M such that each row and each colunln is a pernlutation vector.
(b) Show that M i = M; = II for sonle i.
(c) What are the necessary and sufficient conditions that the group repre
sented by M be Abelian?
(d) Write a program to deternline all cyclic SUbgroups of a group repre
sented by M.
1.42 If U is a logical nlatrix whose rows are each nonzero, nlutually disjoint, and
collectively exhaustive (that is, (+/U e) = e, and +//U = e), then U defines
an 111-way partition of n, where 111 = li(U), and n = v(U). The partition is nlore
commonly represented by the vector p = +/U [Riordan (1958), p. 107]. Clearly
+ /p = n. Write a program to generate
(a) all partitions U of a given integer n.
(b) all distinct partitions of n, where U and V are considered equivalent if
p = +/U is a permutation of q = +/V.
1.43 Let x be a .'pace vector (i.e., of dinlension three), and let R(x) be the square
nlatrix l j (e !\ x). Sho\v that
(a) +/R(x x y) = (x x y) X e
(b) e; (xx y) = x x y
(c) (+ / R ( x x y x (w x z) = (x ~- y) x (w ~- z)
(d) (x ~- y) x (x x y) = (x x y) x (x x y) + 2(lx x jy) ~ (lx x jy).
1.44 Let x . y = (j x x ly) - (lx x jy) be the vector product of x and y for
vectors of dinlension three. Show that
(a) this agrees with the usual definition [Margenau and Murphy (1943)].
(b) x' y = -(y' x)
70 The language
(c) x y is perpendicular to x, that is, x (x . y) t = o. (Use the fact that
t x = 21 x for a vector of dinlension three.)
--- x +y
1.45 Let [x] = \/ (x t x) be the length of. x, let x J! y = [ ] x [
x x y]
be the cosine of
the angle between x andy, and let x a y = '/1 - (x y y)2 be the sine of the angle.
Use the results of Exercises 1.43 and 1.44 to show that for space vectors
(a) [x y] = [x] x [y] x (x a y). Note that [x . y] is the area enclosed by the
parallelogram defined by x and y.
(b) (x . y) . z = (x t
z) x y - (y z) t x x
(c) (x y) ; z = x t (y . z).
chapter 2
MICROPROGRAMMING
Dimension
Memory M 2 15 x 36
I ndex accumulators I 3 x 15
Sequence vector s (instruction counter) 15
Command vector C 36
Upper accumulator u (s, q,p, 1,2, ... ,35) 38
Lower accumulator (Quotient register) I (s, 1, 2, ... , 35) 36
Upper accumulator overflow II
c +- M ls
Additive indexing
It is convenient to the programlTIer to be able to add (or subtract) an
jndex quantity i to (fronl) the address portion of an instruction in the
comnland register c before its execution. This quantity is represented in
base two by a logical vector a and is stored in a special index register. In
the 7090, the data address portion of c is the fifteen-bit suffix w 1:>/ c and the
indexing is su btractive:
_lw};>/c +-- 21;> I CLw 1:>/c - J_a).
The reduction lTIodulo 2 1:> again indicates cyclic treatnlent of addresses.
The 7090 contains three index registers or index accul11ulators which 11lay
be used independently or jointly. They will be denoted by the index
In:ltri I of dimension 3 x 15. One or nlore (or none) of the index regis
ters Ij are selected according to the value of the vector i == (18 t 0.:3)/ C, the
three-bit index tag portion of the conl11land, as follows:
~_ w 1.)/ c +-- 2 1;) I C~ _W I :> / C - j J(( I8 t 3
0. ) / c) I) ).
The address in the conlmand register is clearly decrelnented by the base
* Since nurnber bases other than two will be used but rarely in the present chapter,
the elided form -.L x will be used instead of (2e) _~ x.
74 M icroprogranll11ing 2.1
two value of the vector obtained by oring together the selected rows of [.
The oring of the index accumulators permits simple circuits for their
selection. It is, however, of questionable value to the programnler, and
the 7090 index registers are normally used individually.
Indirect addressing
It is often convenient (as in the execution of a pernlutation) for a
programmer to specify a data address indirectly, i.e., to specify the address
of the word containing the desired data address. Such indirect addressing
could proceed through several levels, but in the 7090 it is limited to one.
C 12 /\ C 13 : 0
wI8jc -(_ w I8 jM Jw 15 jc
Dynamic relocation
The correct execution of any computer program requires that each
instruction and each operand be stored in the register assigned in the
construction of the program. A program can, however, be relocated by an
integral amount n if each word originally assigned to address j is assigned
to address j + 11, and if each address in the program is also incremented by
11. The incrementation of program addresses can be performed explicitly
by an assembler or other metaprogram, or it can be performed dynanlically
by an additive index register containing the number 11. An index register
employed exclusively for this purpose is called a base address register.
More generally, the provision of a table of base addresses pernlits
independent dynamic relocation of different blocks of a program, where
each block is confined to a set of successive registers. This is equivalent to
one-level indirect addressing in which a certain portion of the address
(e.g., vj(W 15 j c)) selects from memory one of a table of base addresses to
respecify the same portion vj(wl.~jc) thus:
V;'(W15jC) +--- Vj(W15jM~Vj(w15jC)).
If, for exalnple, v = a 7 , and the format is otherwise as in the 7090, then
columns 21-27 of registers 0 to (2 7 - 1) provide the base addresses for
2.1 Instruction preparation 75
successive blocks of 2 8 registers each. The 7090 provides no dyncunic
relocation.
.l: 1
>
2 eE /\ T : 0
3 .i +- + jaj /\ j(eE /\ T)
4
M lO-+2j -<-- W 15(36)\s
. 5
(15 ~ (1.3)j M 10 --j2j ~- (eE /\ T)j
7 e - 0
8 c -<- Mll+2j
9 e -<-M~s
10 J.s -<-2151 (I + _ s)
]1 f' ~O
12 a -<- k 1( e) /\ e 12 /\ e 13
13 kO(e) : 1
>
14 ~w15je -<- 215 I (J_w15 je - L(i X I))
15 Jw 9 je -<- 29 (J w 9 jc -
1 J (i ::: w 9 jl)
16 a : 0
1R (/ -<-- 0
i = (1 8 t (1.3) Ic
Program 2.4 Complete instruction fetch
15
Load Q 11 LDQ I *- M. w jc
15
Store 11 STO M- w jc +-- ( ~ a,2)/U --+
Store instruction
15
location counter 11 STL W 15 /M! w jc +-- S
normal numeric part of u (that is, all but the overflow bits), whereas SLW
(store logical word) stores the p-bit instead of the sign.
The instructions which load and store the index accumulators (ProgralTI
2.6) are of four types, as indicated by the leading letter of each of the
mnemonic codes, L for load index from memory, S for store index in
memory, A for load index from the address of the command register~ and
P for place the index in the upper accumulator or the upper accumulator
in the index. The portion of nlenl0ry, command register~ or upper
accumulator involved in each of the ten instructions which specify the
index is shown in steps 1-10. The last fi ve of these differ from the corre
sponding nlembers of the first five only by complenlentation on 215~ as
shown in step 11. Since the subtraction occurring in indexing (step 14 of
Program 2.4) is reduced modulo 21.\ the effect of complenlentation is to
add rather than subtract the quantity used to load the index aCCUlTIU
lator.
Step 12 shows that the index accumulators specified are selected by the
three-bit tag vector i == (18 1a. 3 )jc and that each receives the same specifying
quantity. Since the tag vector is used to select the index registers to be
specified, it cannot also be used to specify indexing of the instruction
itself, and, consequently, none of the load and store index instructions are
indexable. Neither do they permit indirect addressing.
80 M icroprogral1l111 ing 2.2
Load complen1ent of
decrement in index 00 LOC 7
Address to index
complemented 00 AXC
Place address in index
complemented 00 PAC 9
Place decrement in
index complemented 00 POC X +- (5 ~ a 15)lu 10
_L x +- 215 I ( - _l x) 11
i III +- e( + Ii) ;, x 12
(i = (18 ~ a 3 )lc)
The last four steps show the storing of the index accun1ulators. The
quantity stored is the or function of the accumulators selected by the tag
(18 1 a:3)jc.
Branch instructions
The basic branch instructions are of two main types, the transfer
z.Jenoted by a leading Tin the mnenlonic code) and the skip. The behavior
of the skip instructions is shown in steps 1-10 of Progranl 2.7, and is
typified by the PST (p-bit test) of steps 1 and 10. If the p-bit of the upper
2.2 Instruction execution 81
accumulator is not zero, the sequence vector is increnlented so as to skip
the next instruction in the sequence; if the p-bit is zero, the instruction has
no effect. The various skip instructions differ in the particular tests nlade,
and the last two (CAS and LAS) differ also in providing three alternatives,
_Ls +- 2 1ii
l(s + J_S) 10
f~- 2 12
or equal
=1=
Transfer with index 00 TXI .If -(- I (i XI) + ~ (3 t o. ;)jc 1 7
incremented
_L ilII -(.- (2 :Jly)( + ji)
1 8
flow
overflow
b -(- b V I ~ 0.
1
) 1\ (u ~ e 20
t: ] 21
b : 0 22
S -(-W 15 jc 23
w 15 jMO -(- s 24
b : 0 25
-is +- ] 26
-=1= 12
o. jc : p(HTR) 27
r -(- 0 28
=1= 29
r : 0
Logical instructions
The logical operations (Progranl 2.9) concern the logical part of ll,
which differs from the numeric part by including the p-bit rather than the
sign, and hence comprises a,2/U. The first instruction of the family (ORS)
produces the logical or of the word selected fronl storage with the vector
a2/u and returns the result to the sanle location in storage. The instruction
ANS (and to storage) is similar. In the ORA (or to accumulator) the
15
result u is of dimension 38, and the second operand (that is, M w /C) is
expanded to this dimension by inserting two leading zeros before oring
it with u. The instruction ANA is similar. It is easily verified that ORA
leaves the extra bits unchanged and that ANA resets them. The ERA
(exclusive or to accumulator) is anomalous in that it resets the extra bits.
The ACL (add and carry logical) is normally used only in certain parity
check algorithms. It adds the selected word to the logical part of u,
treating both as 36-bit positive base two numbers, again adds any resulting
84 M icroprogral11111ing 2.2
15 w 15 /C
Or to storage 11 ORS M_Lw /C +- (a 2ju) V M
15
Or to accumulator II ORA u +- u V (a2 \MI w /C)
overflow to the low order end, and places the result (which will not exceed
2~~(j- 1) in the logical part of u. The behavior of the remaining logical
instructions is evident from Program 2.9. As shown by the class functions
k O and k\ five of them do not permit indirect addressing. *
Arithmetic instructions
The description of arithmetic instructions will be illustrated by the family
of fixed pointt add instructions shown in Program 2. 10. The CLA (clear
and add) transfers the selected memory register to u, resetting the two
* Since each of these five instructions involves the accumulator only, the normal
address portion W 15 Ie does not represent an address, and its last three components are
used in representing the operation code itself. The possibility of indirect addressing
would suggest to the programmer the nonexistent possibility of specifying an arbitrary
indirect address in w 15 je.
t The 7090 incorporates three arithmetic functions: addition, multiplication, and
division, each of which nlay be performed in either a fixed (radix) point or floating point
nlode.
2.2 Instruction execution 85
Clear and
add 11 CLA u +- ( f (i2)\M Jw 15 /c
Clear and
subtract 11 CLS u +-- ( f ( 2)\(al :f M _~w15/C) ----+ 2
15
Add 11 ADD d +- M-- w /c 3
15
Subtract 11 SUB d +- (a l ~~ M J w /C) 4
Add
15
magnitude 11 ADM d *- (il 1\ M: w /c 5
Subtract
magnitude 11 SBM 6
Round 11 RND 7
8
9
~al/u +-2 37 1(lzl)
10
II *- U V x :j= u 2) 1\ (U o = do))
11
U o +- (z < 0) V (u o 1\ (z = 0))
12
overflow positions. The CLS (clear and subtract) differs only in that the
sign is reversed in transfer.
The instructions ADD, SUB, ADM, and SBM each transfer the
selected word to d with an appropriate sign, add it to the nun1ber repre
sented by u (including the overflow positions), and place the sum reduced
modulo 237 in u. The sign of a zero result is (as indicated by step 12) the
sign of the number originally contained in u.
'The overflow indicator u is set only by a carry (but not a borrow) [ron1
u 3 to u 2 . This indicator controls, and is reset by, certain branch in
structions.
The RND (round) instruction is used to round up the n1agnitude of the
number represented jointly by the upper and lower accun1ulator by one
unit in the high order position of the lower accumulator. As shown in
Program 2.10, the content of the upper accumulator only is actually
affected.
Shift instructions
In describing computer instructions, the term left shift of a vector x by r
places refers either to the left rotation x +- r i x or to the left rotation
86 Microprogramming 2.2
combined with a reset to zero of the last r "vacated positions," that is,
x +- (r i x) 1\ wr
Both types of shift occur in the 7090 and concern various portions of the
entire accumulator u EB 1, as shown in Program 2.11. The portion affected
is determined by the mask vector m.
u +- U V (a r X(mj(u I))
Accumulator left
shift 10 ALS m +- (il(38) (f) (36)
Accumulator right
10 ARS m ~- (il(38) (36)
shift
Long right shift 10 LRS m ~- (il(38) c) (il(36)
Exchange accumu
lator and MQ 00 XCA ( ~ a2)/u ~-~ 1
( ~ (i2)/U -0(- 0
Exchange logical
accumulator and MQ 00- XCL
r = _lw 8 jc
The first three instructions are left shifts. Each sets the accumulator
overflow indicator if any nonzero bits are lost, i.e., if any of the first r
positions of the affected portion are nonzero. The next three are analogous
right shifts, which do not, however, set the overflow indicator. In the
2.2 Instruction execution 87
"long" shifts LLS and LRS, one sign position specifies the other, although
the sign positions are otherwise excluded from the shift by the mask m.
The LGR shifts all positions save the sign of u; RQL rotates MQ
without resetting any positions; and XCA, which "exchanges" the
accumulators, is effectively a rotation except that it resets the overflow
bits. The amount of shift r is in each case determined by the base two
value ofw 8 /c.
Convert instructions
Each convert instruction (Program2.12) selects a six-bit infix of one of the
accumulators, adds its base two value to a "base address" initially specified
by the address portion of the instruction, and selects the men10ry register
2
00 CAQ} 3
00 CRQ
d +- c 4
1+-6t 1 5
j +-j - 1
6
a +- 215 / (_,_W 15 III + _L a. 6 II)
7
a.lOlc : p(CAQ)
9
a. 6 II +- a. 6 Id
10
_La.! lu +- 2 37 /( _~Cil/u + : d)
11
00 CVR 12
d +- c 13
j +-j 1 <
14
a +- 2 l5 j( lW 15 1d + ~w6Iu) 15
d +-Ma 16
Cil/u +- (6 f (al/u)) /\ Ci6 17
6
a. j"a lu 2
+- (a. lCi lu) V a. 1d
6 2 6
18
Input-output instructions
Because the data transmission rates of input-output equipment serving
a computer are n1uch lower than those of the computer proper, computer
systems are normally designed to permit concurrent operation of the
computer and one or more input-output units. The units are therefore
more or less autonomous.
In the 7090, the autonomy of input-output equipment is carried further
by providing eight data channels which transmit data to, and are controlled
by, the computer proper, and which in turn control and accept data from
the individual input-output units. The entire input-output process there
fore con1prises three levels of microprograms: a semiautonomous input
output unit controlled by a semiautonomous data channel controlled by
the cOlnputer proper.
Attention will be restricted to the magnetic tape input-output units of
the 7090. Each unit is available to one specific data channel i (for i =
o - 7), and a particular unit can be characterized as the file $/. The unit
is completely autonomous only in certain simple operations, such as
reH'ind, write end offile, backspace, and continue to end of record. Except
for these special operations, a given data channel can control only one of
its files at a time. The eight data channels may, however, operate con
currently under control of the computer proper.
Each channel i behaves as a subcomputer with its own sequence vector
Si, command vector Ci, data register Di, and other miscellaneous operands,
as shown in Table 2.13. The instructions of the subcomputer (listed in the
matrix K) are called channel commands and differ from the instructions of
the computer proper in both format and function.
Tape Units. Each tape unit behaves as a file (Sec. 1.22); each recorded
component is an alphanumeric character represented in a seven-bit odd
parity error-detecting code, the lowest level partition Ao is represented by
the intercharacter space on the tape, the second level partition Al (called
an end of record gap) is a longer blank space on tape.
Each record gap is immediately preceded by a parity check character
which is appended to the normal data of the record to permit an even
parity "longitudinal" parity check on each of the seven-bit positions of the
Dimension
Channel data registers D 8 x 36
Channel sequence vectors S 8 x 15
Channel command vectors C 8 x 36
r Toi : End of file A2
Channel trap T ~I T
I
i : Parity check 8 x 3
I .
L T 2 'l : Channel command
Channel trap enabled E 8 x 3
Channel trap enabled e
Tape position limits L Li: (Beginning, End) 8 x 2
Limit position on tape v (Determined by reflective marker)
Busy indicator b 8
Write or read indicator w 8
Tape unit index 8
o : Normal read-\vrite
Functions f 1 : Backspace record or write end of
file 8
2 : Backspace to file mark
3 : Rewind
Load channel waiting r (reload) 8
Write record gap (AI) next g 8
Current character X Xi is the 7-bit representation 8 x 7
Current parity check y 8 x 7
Interlock vector X Xi = 1 if character Xi is loaded 8
Current character selector V Vi jDi is current character 8 x 36
End of file indicator Q Qi : (Counter, Potential error) 8 x 2
Input-output indicator h
c d IOCD
TCH
r p IORP
r t IORT
Channel commands K
c P IOCP
c t IOCT
s P IOSP
s t lOST
Table 2.13 Channel operands
89
90 M icroprogralnlning 2.2
preceding record. The check character is recorded automatically and when
read from the tape is used in the parity check but is not translnitted with
the data of the preceding record.
The third and highest level partition (called end offile) is represented by
a special recorded character A2 which has the seven-bit representation
p(A 2) = (0, 0, 0, ], ], ], 1). It is recorded together with the appropriate
check character (which, since the check is of even-parity is also A~) as a
separate record. The character A2 alone is not recognized as an end of file
partition; only the sequence AI' A2 , A2 , Al is so recognized. Tapes are
normally stopped only at a record gap so that, on restarting, the tape is
fully accelerated before the end of the record gap (and hence data) is
reached.
Dinlension
Character buffer Zl
Partition buffer PI
Logical association (connection) AI
Busy indicator B ~ 8 x # of units per channel
Write-read status wi 1
Function status F
End of file counter RIJ
AO intercharacter gap
{
A2 end-of-file symbol (000 1111)
Table 2.14 Input-cutput unit operands
The tape unit parameters are listed in Table 2.14, and the operation of
tape unit (1)/ is described by Program 2.] 5. The unit idles at step 5 until its
busy indicator B/ is turned on by data channel i. After a starting delay of
about 650 microseconds reg uired to accelerate the tape and reach the
beginning of the record, one of four functions (listed under f in Table 2.13)
is perfornled as determined by the function indicator F/.
If F/ = 0, a normal read or write is perfornled under direct control of
the data channel as detailed in steps 18-37. If F/ *
0, one of the several
completely autononlOUS operations is initiated. If F/ is not zero and
W/ (write indicator) is unity, the autonOITIOUS function write end oj'file
is performed by steps 1-3, after which the busy indicator is turned off and
the unit returns to idle status. The end linlit indicator Lli is set by step 3
if the tape position exceeds a limit v set by a reflective marker attached to
the tape a short way before its extreme end.
If W/ = 0 and if F/ = ], 2, or 3, the unit backspaces to the next earlier
record gap, to the next earlier end of file position, or to position zero,
respectively. The last is called relt'ind. If, in backspacing, the tape becomes
<V/ ~A2' AO gi ~ gi V .11/
18
2 <v j i ~ A2, Al W/ : 1
19
V (7T(<v j i ) > v) em
3 Lli ~ Lli Z/, P/ ~ <D/
20 N
N
4 Bji ~O - A/ : 0
21
5 B/ : 0 Pi ~P/
22
6 starting delay Xi ~p(Zji)
23
7 F/ : 0 =
1 Xi ~ 1 24
8 Wji : 1 ~ pji : AO
25
9 Rji ~3 record gap delay
26
10 7T(<D ji) : 0 =F =
Aji : 1
27
11 LOi ~ LOi V (Fji:::j= 3) p(Zji) ~ Xi
28
12 Z/, P/ ~1<V/ Xi ~ 1
29
13 Rji : 0 f- <v j i ~ Zji, AO
30
14 Rji ~ (Zji = A2) x (R/ - 1) ~ L1i ~ Lli V (7T(<V/) > v) 31
15 = pji : Ao ~ gi : 0 32 ?
16 > Fji : 2 < p(Zji) ~ yi ~
33
-d:::. ~
17 R/ : 1 <D/ ~ Z/, Al 34 ~.
;:::;:
gi ~O 35 ~
~
~
~
record gap delay 36 s::
~.
Aji : 1 37 ;:::;:
=ft'
Program 2.15 Tape unit (1) /
'C
~
\0
N
T 2 i +- T 2 i V (r i J\ (kl = t))
Di +-0 35
2 I, == I k l : P
Vi +- a:6 (36) 36 ~....:
3 I I ~ r i J\ (k l = t) : 1
--
(k o =1= r) J\ ((3 t a:15)jCi ~ e) : 1 37 ~"'"':
a
4 III I (b i , A:) +- 0 (k =F c) J\ (p i = AI) : 0 38
CT.3
~
5 II I B:i) :
I (b i V 1 = Pi +-A o 39 S
S
I (B: FI W: - Vq
6 III , , , Xi) +- (l,fi' Wi, Wi) Xi : 0 40
i i i
>
IJi : 0
Xi +-0
: III delay
yi +- (yi =I- Xi) I II :~
9 II~ri+-O Tli +- Tli V (=jXi) I II 43
=
10 I I I r i : 0
(=jXi) J\ Eli : 1 I II 44
11 I I I (Pi EB A: i EB Qi) +- (A o' 1, 3, 0)
Q oi : 0 1-111 45
12 I I I (g i EB r i EB yi) +- 0 Q oi +- (Xi ~ p(A 2 )) X (Qoi - 1)
I III 46
13 I ~ Ci +-M.lS i QI i
+-
i
Q I V (Qoi > 0)
I III 47
14 rl.-LS i
<- 2 15
10 + .-LSi) (Ql i = 1) J\ (p i = AI) : 0
r-i II 48
15 I eft a ;C i : p(TCH)
3
TOi +- 1 III 49
16 Si +- w 15 jCi T 1 i +-T1 i V (QliJ\(Qoi =0)) ~II 50
17 I ~ (~a:18/Ci =2 17) V(~a:18jCi = 6 x 215) 1
- = i
: Eli J\ Q1 J\ (Qoi = 0) : 1 II 51 C,()')
N
N
em
~
I C 18i 0 Pi : AO 52 N
18 I :
19
wI5jCi ~ WI5/M~w15/Ci
Qi ~ (3,0)
53
20 k ~ K-,-a. /C 3 i
0
(V j yi) 1\ E 1 i : 1
55
21 Wi:
22 (3 ~ ( 15 )jCi : 0
yi ~O 56
=I=
23
24
gi ~
gi : 1
(k o = r)
Q oi
VijDi
: 0
~ aIjXi ill ::
25 * Di ~Mlw15/Ci Vi ~6 ~ Vi II 59
27 Vi ~ a 6 (36)
(3 ~ a l5 )jCi : 0 61
28 Xi : 0
~(3 ~ a l5 )jCi ~ -1(3 ~ a l5 )jCi - 1 62
Xi ~O C 19i : 0 63 ~
29 ~
""":
I Xi ~ (:l-jVijDi) E8 VijDi M --,-w 15 /C i ~ Di
64 s;:::
30 I I"":l
~.
31 I I yi ~ (yi :f- Xi) ~wl5jCi ~ 215 1(1 + ~wI5/Ci)
65 ;::=
~
~
32 ~ Vi +-- 6.f Vi ~
I"":l
s;:::
33 V~ : a6 ~.
;::=
34 I -~(3 ~ a l5 )jCi ~ l-(3 ~ ( 15 )jCi - 1
I
~
~
Program 2.16 Channel i
94 Microprogramming 2.2
20 WEF k +-4
20 BSR k +-1 2
20 BSF 3
20 REW 4
i +- (-l(9 t w 4 )jc) - 1 5
bi : 1 6
Ii +- k - 3 (k = 4) 8
Wi +- (k = 4) 9
Step 6 also makes the selected unit busy (B/ == 1), and hence starts it in
operation.
The normal read-write functions of the tape unit are described by steps
18-37. They are initiated by a branch from step 7 in the event that the
function indicator is zero. Over-all control by the channel is exercised
primarily through the association indicator A/, which is equal to unity if
unit j is logically associated with channel i, that is, if data are pennitted to
flow between them. If writing is in progress (Wji == 1) and A/ beconles
zero, the unit stops; if reading is in progress, the channel is freed ilnmedi
ately but the tape continues to a record gap.
Step 20 performs the read from tape, and if A / == 1, the partition read
is transferred to Pi' the seven-bit representation of the character read is
transferred to Xi (both for use by the channel), and the channel-unit
interlock Xi is set to unity to initiate appropriate disposition of the
character by the channel. If P/ is not a record gap reading continues, the
intercharacter delay (not shown) permitting time for the channel to
dispose of the character before the next is actually read. If P/ is a record
gap, the corresponding delay elapses before A/ is tested. If A/ == 0,
the branch to step 4 stops the unit. The tape stops only at a record
gap although transmission of data may be discontinued earlier by step
21.
The writing process begins at step 37 and nlay be discontinued before
any writing occurs (although the current record gap will be lengthened by
a few inches of blank tape). The main writing is perfornled by the loop
28-32, employing the channel interlock Xi. Step 31 sets the tape end lilllit
indicator. The loop terminates (step 32) when the HTite record gap
indicator gi is set to unity by the channel. Steps 33-36 then write the
longitudinal parity check character Yi supplied by the channel, together
with the inter-record gap partition AI. The write loop is then re-entered
unless A/ == O.
~
5
e)
~
e)
20 WRS}
20 RDS ~ i ~ -L(9 t ( 4)jc - 1 1 11 LCH i ~ -L(1 ~ all)jc - (8e) .l (5, 4, 4) + Co 9 J.s
bi : 1 2 ri ~ 1 10 ~
t i ~ -Lw jc
8
3 ri /\ bi 11 ~
Wi ~ (a jc ~ p (WRS
3
4 bi : 1 ~ 12
Ii ~O 5 h +-1 ~ 113
.f~ 1 6 11 RCH i +- -L(1 t all)jc - (8e) ~ (5, 4, 0) + Co I 14
Ci ~O 7 h +- h V bi I I 15
bi ~ 1 8 bi : 0 ~ 16
o --+ Channel i, 9 I 17
Si ~ w 15 jc h---J 18
ri+-l ~ 19
em
N
N
2.2 Instruction execution 97
channel trap on the next instruction fetch. This prevents a trap frolll
intervening between the WRS and the following instruction (which is
nornlally an RCH). The RDS differs only in the setting of Wi on step 4.
If the channel is not busy (i.e., not selected), the RCH instruction
(Program 2.18) selects the channel specif1ed by a portion of the operation
code, sets the input-output indicator h, and copies the instruction address
to the channel sequence vector Si. If the channel is busy, the RCH
instruction sets the selected channel to its step 9, whereupon" the channel
waits on the interlock at step 10. Meanwhile, step 18 of the RCH sets
Si and step 19 sets the interlock 1'"i so that the channel Inay proceed.
Steps 13 and 14 of the channel operation load the channel conllnand
register Ci and increment the channel seq uence register Si. Ifthe comnland
is a TCH (Transfer in Channel), step 16 causes a branch to a new seq uence
of commands. If not, the ll'ord count, represented by (3 1 a.L/Ci, is
tested. Ifit is zero and if the current command is either an IOSP or IOCP,
the branch to step 13 immediately fetches the next COlnn1and in seq uence.
Otherwise, indirect addressing of the conlInand occurs (step 19) unless
C{K is zero.
Step 20 specifies k according to the class of the command being executed.
The cOlnnlands are listed in the nlatrix K of Table 2.13.
The first cOlnponent KOi assumes the value c, r, or s according as the
COl1lnland having code i is terminated by a word count test, a record gap,
or by either (signal). The second component K 1 i assunles the value d, p,
or t according as the channel discontinues operation, proceeds to the next
cOlnmand in sequence (as determined by SI), or transfers to an LCH
(Load Channel) instruction which may be awaiting execution by the
computer proper. Execution of the LCH (Progranl 2.18) is delayed at
step 1I and branches to step 18 (to respecify Si in the nlanner of the RCH)
only if the channel reaches step 3.
Channel operation continues on the right-hand segn1ent (steps 35-65) if
the operation is a read (w i == 0), and on the left (steps 22-34) if it is a
uTite. In the latter case, a zero word count causes in1nlediate ternlination
of the current command.
The norn1al tern1ination of a command in either read or write n10de
occasions a branch to step 1, where the tests for continuation begin. Step
1 sets the Channel Conllnand Trap indicator T./ if the current conlInand is
of the transfer type and an LCH (Load Channel) is not awaiting execution
in the con1puter proper. If the con1nland is of the proceed type, step 2
branches to step 13, where the next comn1and in sequence is fetched. If the
comnland is of the transfer type and an LCH is waiting (1'"i == 1), step 3
branches to step 9 to reset parameters and permit the channel to be reloaded.
1Il all other circulllstances step 4 is executed to disassociate the unit fro III the
98 M icroprografJun ing 2.2
channel and to return the channel to idle status. In read status, certain
abnornlal events-the occurrence of a parity error, or an end of file
partition-return the channel to idle status ilnmediately, regardless of the
type of command being executed.
The write operation (steps 22-34) is relatively simple. If the word
count in (3 1 0. 15 )/ Ci is zero, steps 23-24 ternlinate the current conlnland
but first initiate the writing of an end of record gap* if the comlnand is of
the "record" type (e.g., an IORP). If the word count is not zero, step 25
transfers to the channel data register Di the memory word selected by the
address portion of the command. The loop 28-33 transfers to the tape
unit successive six-bit infixes of Di and maintains the longitudinal parity
check Yi (originally reset on step 12). When all six have been transferred,
the branch to step 34 decrements the word count and, unless it beconles
zero, repeats the entire process from step 25.
The read operation (steps 35-65) begins by resetting D i to zero and the
infix selector Vi to a G Step 37 ternlinates the current conlnland if it is of
the count or signal type and the word count is zero. Steps 38-39 tenl1inate
the command if it is of the record or signal type and if the last file partition
read is a record gap. The partition indicator Pi is reset to Ao by step 39.
Thus a record gap present when termination is caused by a zero count is
still present on the first execution of the succeeding conlmand, whereas a
gap which itself causes termination is not present on the succeeding
command.
Steps 40-43 show the data interlock, the deternlination of the longi
tudinal parity check, and the setting of the parity error trap T 1 i in the
event of a parity error in the character. If the corresponding channel trap
is enabled, step 44 causes immediate ternlination in the event of a parity
error. Steps 45-48 detect an end of file configuration (using a counter,
Qoi in a manner similar to that used in Program 2.15), set the indicator
Q/ if a partition character A2 appears at the beginning of a word, and
cause termination (from step 49) \vith the end of file trap Toi set if an end
of file configuration occurs. If the character A2 occurring at the beginning
of a word is not part of an end of file configuration, step 50 sets the tape
error trap TIl, and step 51 causes termination if the corresponding channel
trap is enabled.
Steps 53-56 are executed only if Pi is a record gap. They reset the
counters Qi controlling the end of fIle test, test and reset the longitudinal
parity vector yi, and may cause termination in the event of an error. Step
57 causes the character transfer of step 58 to be ski pped if the character is
* Since the partition Al is represented by a gap, the writing of one gap inlnlcdiately
following anothcr, with no intervcning data, has the effect (whcn subsequently rcad) of a
single rccord gap.
2.2 Instruction execution 99
b *-(b i "*- k)
t :
b : 0
MO ~ w I5 (36)\s
b : 0
11 SCH i ~ 2 _1 (9 ~ a.:~) j C + Co
y ~ e(36)
bi : 0
y~Ci
(3 ~ a.,15)jy ~ Si
(Y18' Y20) ~ 0
M .lw 15 /C +-- y
11 ENB y ~ MLw 15 /c
Eo ~w8jy
E 1 ~ (10 ~ a 8)jy
E2 ~w8jy
-+
E~E
10 RCT e ~ 1 ~
y:o
~y
y ~ t (x 1\ z)
x +- (x ::/= z)
Address
decoder
r: E (36) , - - - - - r
M
Adder
c~d.
c ~ M~_a.
on the path between d and s of Fig. 2.22 indicates that transfers occur
between w 1 ;)/d and s. The symbols rand c denote the selection of a rnatrix
row and a nlatrix column, respectively, as illustrated by the path betwccn
M and d. Pernlutations may be rcpresented in the fornl pJ. Thus if the
Exercises 103
vector d were to be transposed (reversed in order) in the transfer to c, the
path would be labeled with the expression
pS : ,
where p = 35
Il- (36)1 = (35,34, ... , 1,0).
REFERENCES
FalkofT, A., (1962) '"Algorithms for Parallel-Search Menlories," l.A.C.M. (to appear).
IBM Reference Manual, 7090 Data Processil{f{ Systeln, (1960), Form # A-22-6528,
International Business Machines Corporation.
Phister, M., (1958), Logical Design of D{fJital Conzputers, Wiley, New York.
EXERCISES
2.1 Write 7090 progranls for each of the following operations:
(a) M8 +-- M8 V M9
(b) M8 +- M8
f~M6 +-- 236 1 (~M8 + ~M9)
(c) M7 +_ ((~M8 + ~M9)
Li 236 )
(d) M6 -(-IM7, a 6 , M 9 1
(e) M6 +--IM7, w 6 , M 9 1
(f) MG -(- IN1 7 , (8 1 a IO ), M 9 1
(g) MG +-IM7, M8, M 9 1 [Use lx, u, YI = (x 1\ u) V (y 1\ u)]
(h) MG -(- U1W 1
2.2 I n the nlagnetic core technology enlployed in the 7090, logical disjunction
(or) and negation are nluch easier to produce than conjunction (and). Linliting
the logical functions employed to disjunction and negation, write microprogranls
for the following 7090 instructions:
(a) ANS (Use De Morgan's law, Sec. 1.8)
(b) ERA
2.3 In the nlagnetic core technology used in the 7090, each transfer of a quantity
Y into a register x is actually an or with the present content of the register, i.e.,
x +- Y V x. A register lnay also be reset to zero. Subject to the foregoing
restriction, write microprograms for
(a) the operation I +-- Mi. (Use two steps.)
(b) the operations of Exercise 2.2(a).
2.4 Describe the nlain portion of the instruction fetch of the 7090 (steps 9-18
of Progranl 2.4) in an algorithnl which satisfies the data path constraints of Fig.
2.22.
2.5 Repeat Exercise 2.4 so as to satisfy the constraints of Exercises 2.2 and 2.3
as well.
2.6 A vector p which pernlits only the follo\ving types of operation:
(i) p -(- \p, wI, ,t\
.. {y -(- w1jp
REPRESENTATION OF
VARIABLES
1t 22 I 1t 23 I 1t 24 Physical vector
r 11 r 21 r 12 r 22 r 13 r ')
3 r 14 r 24 r 15 r 25 Representand
Physical ~
) 1t 40 1t 41 1t 42 1t 43 1t 44 1t 45 1t 46 1t 47 1t 48 1t 49 1t 50 1t 51 1t 52 1t 53 1t 54 1t 55 1t 56 1t 57 1t 58
fi
59 f vector
~
~
~
~
~
~
1 7 I 0 2 1 9 0 4 2 7 0 5 2 3 0 1 3 2 0 3 Actual ~.
I
~
value
~
(b) Linear representation of rex) with I = 34 + 4i + 2). ~
""':
:::::
~
~
~
~
~
Xl X2 Xl X2 ~
I 1 2 2 t::i
~
c::;-
l-'
Figure 3.2 Linear representation of a nlatrix X o
.....,J
108 Representation of l'ariahles 3.2
Any structured operand can first be reduced to an equivalent vector, and
the grid 111atrix therefore suffices for describing the representation of any
construct, providing only that the representation of each of its elements is
solid. Thus a matrix X 111ay be represented by either the row-by-row list
r == E/ X or the column-by-column list c == Ell X, and a tree T 111ay be
represented by the left list matrix [T or the right list matrix ]T, either of
which may be represented, in turn, by a vector.
If a process involves only a sn1all number of variables, it is practical to
n1ake their allocation implicit in the algorithn1, i.e., to incorporate in the
algorithm the selection operations on the vector 1t necessary to extract the
appropriate variables. This is the procedure usually en1ployed, for
example, in simple con1puter progran1s. In processes involving numerous
variables, implicit allocation lTIay become too cumberSOlTIe and confusing,
and more systelTIatic procedures are needed.
Linear representations
The representation of a structured operand is said to be lincar if each
component is represented by an infix of the form (/1 a d )/1t, where I is a
linear function of the indices of the component. For exalTIple, the
representation of the matrix X indicated by Fig. 3.2 is linear, with d == 2
and I == - 11 + 5i + 8).
A linear representation is solid and can clearly be characterized by a
small number of paran1eters-the dilTIension d of each component and the
coefficients in the linear expression /. The representation of a vector x is
linear if and only if r 2 (x) == de and the difference () == r/(x) - r~ lex) is
constant for i == 2, 3, ... , vex).
If I == P + qi + 'i is the function defining a linear representation of a
matrix X and if a is the leading address of a given element, then the leading
address of the succeeding element in the row (or column) is sin1ply a + r
(or a + q). Freq uently, the succession must be cyclic, and the resulting
sum must be reduced modulo veX) x r (or p(X) x q). The inherent
convenience of linear representations is further enhanced by index registers,
which provide efficient incrementation and comparison of addresses.
Linear representation of a structured operand req uires that all COlTI
ponents be of the san1e dimension in 1t. This common din1ension n1ay,
however, be achieved by appending null elements to the shorter C0I11
ponents. The convenience of the linear representation n1ust then be
weighed against the waste occasioned by the null elen1ents. Moreover, if
several vectors or matrices are to be represented and if each is of unspecifIed
total dimension in 1t, it n1ay be in1possible to allot to each an infix suffi
ciently large to permit linear representation. Consequently, a linear
representation is not always practicable.
3.2 Representation oj'structured operands 109
Nonlinear representations
Since the use of the grid matrix imposes only the condition of solidity
for each component, it permits an allocation which is sufficiently general
for most purposes. The grid matrix serves in two distinct capacities: (1)
as a useful conceptual device for describing an allocation even when the
actual allocation is implicit in the program, and (2) as a paranleter
which enters directly into an algorithm and explicitly specifies the allo
cation.
If the grid matrix is used in a program as an explicit specification of the
allocation, then the grid matrix must itself be represented by the physical
vector. There remains, therefore, the problem of choosing a suitable
allocation for the grid matrix itself; a linear allocation is illustrated by
Fig.3.lb.
If the grid matrix rex) itself employs a linear representation, its use
offers advantages over the direct use of a linear representation of x only if
the total dimension of r in 7t is much less than the total dimension of x in
7t when linear representations are employed for both. This is frequently
the case, since each element of a grid matrix belongs to the index set of 7t
(that is, to lO(V(7t))), and the dimension of each element in 7t is therefore
both uniform and relatively small. Program 3.3 shows the use of the grid
matrix rex) and the encoding matrix C in determining the kth conlponent
of the vector x.
Program 3.3. A linear representation is assumed for rex), with elenlent r/(x)
represented by the infix ((p + qi + ~i) 1 aJJ)!Tt. Moreover, each eleI11ent of rex)
is assunled to be represented in a base b nunlber systenl. Step 1 detennines the
leading address of the representation of r lk(X). Step 2 specifies.1' as the base b
value of this representation, i.e., as the leading address of p(x k ). Steps 3 and 4
specify d as the dimension of x k in Tt, and step 5 therefore specifies z as the
representation of x k'
Steps 7-9 perfornl the decoding of z = p(x k ) to obtain z as the actual value of
xl." Since this process is normally perforI11ed by hUI11an or I11echanical I11eanS
(e.g., a printer) outside the purview of the programmer, it is here expressed directly
in terms of the encoding matrix C rather than in tenns of its representation. The
left-pointing exit on step 7 is followed only if z does not occur as an entry in the
encoding matrix.
The fornl chosen for the grid matrix is one of several possible. The two
columns could, for example, represent the leading and final addresses of
the corresponding representations or the dimensions and final addresses.
The present choice of leading address / and dimension d is, however, the
nlost convenient for use in conjunction with the notation adopted for
infixes; the logical vector (/ 1 ad) selects the appropriate infix.
110 Representation of variables 3.2
7 h+-h-l
.1' Leading address of p(xJJ.
d Dimension of p(x k ) in rt.
8 z : ljCk =I
Z p(x k ).
9 z +- liCk -------+ C Encoding matrix for com
ponents of x.
Legend
Program 3.3 Detennination of Z = p(x k ) and z = x k fronl a linear representa
tion of the grid matrix rex)
Chained representations *
If a linear representation is used for a vector, then the deletion of a
component (as in a compress operation) necessitates the moving (i.e.,
respecification) of the representations of each of the subseq uent conl
ponents. Similarly, mesh operations (insertion) and permutations
necessitate extensive respecification. The use of a grid matrix rex)
obviates such respecification in x, since appropriate changes can instead be
lnade in rex), where they may be much simpler to effect. If, for exanlple,
x is the vector represented as in Fig. 3.1 a, and z is a quantity of dimension
six in 7t, then the mesh operation
nlay be effected by specifying the physical infix (70 1 aY)/7t by p(z) and by
Program 3.4. The loop (1-3) is executed l{X) 10 (k - 11) times, with the result
that at step 4 the paranleter.{ is the leading address of p(y /.'). Step 4 therefore
specifies d as the dinlension of p(y k), that is, as the base b value of r /'(y). Step 5
112 Representation of variables 3.2
Legend
x +- v/x
executed on a forward-chained representation of x. The unused segments
representing the components of ii/x are returned to a backward-chained
stack or pool of available cOlnponents. A linear representation can usually
be used for logical control vectors such as v; in any case the problellls
involved in their representation are relatively trivial and will be subordi
nated by expressing each operation directly in ternlS of the logical vectors
and not in terms of the physical components representing them.
Program 3.5. In the major loop (6-23), k deternlines the index of the current
component VI,', and i andj determine the leading addresses of p(x /,) and p(x/; !1),
respectively. These three parameters are cycled through successive values by
steps 7, 8, and 12 and are initialized by steps 2,5, and 12. lf VI,: = 0, the infix
p(x/;) is returned to the pool by steps 21, 22, 23, and 6 so as to construct a back
ward chain.
The paranleter.r specifies the leading address of p(x 1 ) unless l'(X) = 0, in which
case J: is null. Step 1 ternlinates the process if ll(X) = 0, and otherwise step 4
respecifies.r as the null elenlent. If V = 0, this null value of.r renlains ~ if not, the
first nonzero component of v causes a branch to step 14. Since T = 0, step 15 is
executed to respecify :c as the leading address of p( (vi X)I). Step 16 then specifies 11,
the leading address of the last conlpleted conlponent of vlx. Step 15 is never
again executed.
Components of vjx other than the first nlust each be chained (in a forward
chain) to the preceding one. Hence the leading address i of a newly added conl
ponent nlust be inserted in the last preceding conlponent (whose leading address
is h). This is normally done by steps 18, 19, and 6 ~ step 20 respecifies h. If, how
ever, the component X k - 1 were also included, it would appear as the last conl
pleted component of vlx and would already be chained to the new C0111pOnent XI,'.
This situation is recognized by step 17 and occasions a branch to step 16. Step 16
then respecifies h and repeats the loop without executing steps 18, 19, and 6.
The process ternlinates when the cycle through the chained representation of X
is conlpleted, that is, when i returns to the original value of .r, preserved as t by
step 3. Step 10 is then executed, ternlinating the process directly if r(vlx) = 0.
114 Representation of variables 3.2
Otherwise, step 11 is executed to close the chain of v j x, that is, to insert f , the
leading address of p((vjX)l)' in the representation of the last con1ponent of vjx.
~c 0
O-origin indexing for rt only
4
~(; +- 0
v Logical vector.
5 k +-1
k Index of v.
6 bl((r f a 9 )jrt) +- S i Leading address of p(x,J.
j Leading address of P(X k +1 ).
7 k+-k+l
h Leading address of last
8 i +-j preceding C0111pOnent of
# vjx.
9
p Leading address of last
10 ~) ;
preceding component of
11 b L((h f a 9 )jrt) +- x pool of available seg
ments.
12 j +- b _L((i f a 9 )jrt)
g Dimension in rt of ele
13 VI.: 0 ments of grid matrices.
14 * x 0 b Base of representation of
elements of grid matrices.
15 x +- i
16 h +- i - Legend
17 Vk - 1 0
18 r+-h
19 s +- i
20 h +- i
21 r +- i
22 s +-p
23 P +- i
component. This notion is fonnalized in the chain list nlatrix of Sec. 3.4.
The same scheme can also be employed to produce an efficient combined
representation of two or more vectors which share certain COInnlon
components. If, for example, x j = Z!" , and chained representations are
used for both x and z, then x may be represented in standard form except
that component x j incorporates a secondary address, which is the leading
address of Zk+l. Moreover Z has a standard representation except that
Zk-I is chained to Xj' with an indicator to show that the secondary address
of the succeeding conlponent is to be used. Deletion of any vector
conlponent in such a shared systenl must occasion only the corresponding
change in the address chain of the vector, the actual representation of the
conlponent being deleted only when no associated address renlains.
Partitions
If the set a is the range of the cOlllponents of the physical vector 1t, and
if SOIne element, say aI' is reserved as a partition sYlnhol and is excluded
from use in the normal representation of quantities, it can be inserted to
denlark the end (or beginning) of an infix of 1t. If the vector Y is repre
sented by a single infix of 1t such that the beginning of component Yj+l
follows imnlediately after the terminal partition of Yj' then the structure of
Y is completely represented by the partitions, and Y is called a partitioned
representation. A partitioned representation can be used for more cOlnplex
operands, such as matrices, if a set of two or nlore distinct partition
symbols are provided, one for each level of structure. The distinct
partition symbols can, of course, be represented by multiple occurrences of
a single symbol a l rather than by distinct Inenlbers of a.
A partitioned representation is sinlilar to a double-chained representa
tion without end-around chaining in the following particular: beginning
from component Yi, the component Yj can be reached only by scanning
all intervening components between i and j in increasing or decreasing
order according as i < j or i > j. The fIle notation introduced in Sec.
t .22 clearly provides the operations appropriate to a partitioned repre
sentation of a vector, with conventions which suppress all inessential
references to the partitions themselves.
The use of a partition to demark the end of an infix is particularly
convenient when the infix must be processed conlponent by conlponent
for other reasons, as in the use of nlagnetic tape or other serial storage.
The partition also appears to be more economical than the grid lnatrix,
which it replaces. This apparent economy is, however, somewhat illusory,
since the reservation of a special partition symbol reduces the infornlation
content of each nonpartition component by the factor log2 (v(a) - 1) -:
log2 v(a), where a is the range of the conlponents of 1t.
116 Representation of variables 3.2
Partitions can be employed in chained representations. For example,
the dimension in 7t of each component of a chained representation y can be
specified implicitly by terrninal partitions instead of explicitly by the vector
r 2(y) of the grid matrix. Thus if the elements of rI(y) are of dimension !S
in 7t, then w1/p(Yj) = aI' and (a 9 1\ w1)/p(Yj) = p(x j ), where x is the
vector represented by y. Program 3.6 shows the determination of p(xk )
from a chained representation y with terminal partitions a l .
Legend
Program 3.6 Deternlination of p(xk ) from a chained representation of x with
terminal partitions a 1
Program 3.6. The program is similar to Progranl 3.4 and the step nunlbering
indicates the correspondences. The dimension d is so determined (steps 4a-d) as
to exclude the terminal partition itself from the quantity z specified by step 5.
Since only the first column of the grid matrix is incorporated in the partitioned
representation, step 6 excises a prefix of dimension g rather than 2g as in
Progranl 3.4.
Pools
Components of the physical vector 7t in use for the representation of one
quantity must not be allocated to the representation of sonle other quantity.
The construction of a chained representation therefore poses one problem
not encountered in its use, namely, the specification and observation of
restrictions on the availability of components of 7t. The restrictions can
3.2 Representation of structured operands 117
Summary
Since any structured operand can first be reduced to an equivalent
vector, the problems of representation can be discussed in terms of vectors
alone. The characteristics of the linear, chained, and partitioned repre
sentations of a vector may be sunlmarized as follows. A linear representa
tion permits the address of any conlponent to be computed directly as a
linear function of its indices and hence req uires no scanning of the vector.
However, the strict limitations which it imposes on allocation ITIay en
gender: (1) conflicts with allocations for other operands, (2) waste of storage
due to the imposition of a common dimension in 1t for all cOlnponents, or
(3) uneconomical execution due to the extensive reallocations occasioned
by the insertion or deletion of other than terminal conlponents.
The concept of the grid matrix is helpful even when the corresponding
allocation is ilTIplicit in the progranl. The explicit use of a grid ITIatrix
which is itself in a linear representation removes the restrictions on the
allocation of the vector itself while retaining the advantage of direct
address computation. The address computation differs frolTI the linear
case only in the addition of a single reference to the grid nlatrix and hence
requires no scanning. The difficulties enulTIerated for the direct linear
representation are not elinlinated but merely shifted to the linearly
represented grid matrix itself, where they nlay, however, prove much less
serious.
A chained representation allows virtually arbitrary allocation, relatively
simple operations for the insertion and deletion of components, the direct
representation of more complex structures such as trees, and econonlical
joint representations of vectors which have one or more COITIpOnents in
COmITIOn. However, a chained representation requires extra storage for
the grid matrix which it incorporates and occasions additional operations
for scanning when the components are selected in other than serial order.
The required scanning can be reduced by the retention of auxiliary
information which allows the chained representation to be entered at
several points.
A partitioned representation req uires the allocation of a single infix of
1t, and selection requires a fine scan, i.e., a component-by-component scan
of 1t to detect partition synlbols. Partitioning removes the need to
incorporate the grid matrix explicitly and does not impose a conlnlon
dimension in 1t for all components.
3.3 Representation oj'rnatrices 119
Mixed systems employing conlbinations of linear, chained, and parti
tioned representations are frequently advantageous. Block chaining, for
example, involves the chaining of blocks, each consisting of an infix of 1t
and each serving as a linear representation of some infix of the represented
vector. Alternatively, each chained block may be a partitioned represen
tation of some infix.
h = vi + j,
k = i + /-1j.
Consequently,
i = lh -:- vJ = /-1 10 k,
and j = v 10 h = l k -:- /-1 J.
The dependence of h on k can be obtained directly by substituting the
foregoing expressions in the identity
h= v x lh -:- vJ + v 10 h
to yield h = v x (/-110 k)+ lk -:- IlJ.
Similarly, k = /-1 x (v 10 h) + lh -:- vJ.
The pernlutation h which carries the row list r into the colunln list C
(that is, C = hJor) can be obtained directly from the foregoing expression
for h as follows:
The expression for the kth component of h is identical with the expression
for h above. Hence, if C = hJor, then ck = rhk = 'Ii as required.
If the row list (or column list) is itself represented linearly, then the
address of any component A / is obtained as a linear function of the indices
i and j. If either a file or a chained representation is to be used for the list
vector, then the components are processed most efficiently in serial order,
and the use of column list or row list is dictated by the particular processes
to be effected.
120 Representation of L1ariahles 3.3
If a large proportion of the elements of a matrix are null elements, it is
called a sparse matrix. Sparse matrices occur frequently in numerical
work (where zero serves as the null element), particularly in the treatment
of partial difference equations. A sparse matrix A can be represented
conlpactly by the row list r == Uj A, and the logical matrix U, where
U == (A 1= 0). The matrix A may then be obtained by expansion:
A == U\r.
Alternatively, the column list c == (A i= O)jjA may be used. The
transformation between the column list c and row list rmust, in general,
be performed as a sequential operation on the elenlents of U. Since it is
frequently necessary to scan a given nlatrix in both row and column order
(e.g., as either pre- or post-multiplier in a matrix multiplication), neither
the row list nor the column list alone is satisfactory. A chaining systelTI
can, however, be devised to provide both row and colunln scanning.
Let L be a matrix such that L 1 is a list of the nonzero elements of a
matrix A in arbitrary order, L/ is the column index in A of elenlent L 1 i,
and L 3 i is the row index in L of the next nonzero element following L 1 i in
its row of A. If L/ is the last nonzero element in its row, La' == 0. Letj~
be the row index in L of the first nonzero element of row AJ, and let
jj == if Ai == O. The following example shows corresponding values of
A, L, and j:
8 2 7
6 0 0 9 5 3
0 3 0 0 6 1 5
A 0 0 0 0 L== 3 2 f==
7 8 0 4 9 4
0 0 5 0 7 1
4 4 0
2 k +-li
Ii Row index in L of first nonzero
3 k 0
element of row Ai.Ii = if Ai = O.
0
Legend
Program 3.7 Determination of the row vector Ai fronl a row-chained represen
tation of A
the index k and by the use of the logical vector u = (L 3 = O) for deter
mining the end of each row.
The construction of a column-chained representation is analogous to
that of a row-chained representation, and the two representations can be
combined in a single matrix L which gives both row and colunln chaining
employing but a single representation (that is, L 1 ) of the nonzero elelnents
of A.
The full left and right lists seldom prove more convenient than the more
concise left and right lists. Except for the special case of a homogeneous
tree~ both the right list and the left list are awkward to use for path tracing.
This function is better served by the chain list matrix, to be defIned as a
formalization of the chaining schenle suggested in Sec. 3.2.
which, together with the logical vector (Ml == O)~ fonns a leaf'list nlatrix
that describes the tree. Moreover~ if the values of the leaves are distin
guishable from the components of the degree vector~ the logical vector
(Ml == 0) may also be dropped.
p +- ( O) I-origin indexing
scan.
4 m +-0
r Indicated number of roots of
6
subtree.
7 i+-i+1
P Partition vector of Z, that is,
8 m+-m+l Pj = p:(T j ).
9 r +- r +1 - Zli
Legend
10 r : 1 <
11 p +- p E8 (m)
Example 3.2. Huffman minimum redundancy prefix code. If b is any set such
that ll(b) > 1, then any other finite set a can be encoded in b, that is, represented
by b. (The sets a and b may be called the "alphabet" and "basic alphabet,"
respectively.) If l{a) <; v(b), the encoding nlay be described by a nlapping vector
k such that p(a i ) = b k . Ifl'(a)., v(b), then each a i nlust be represented by a
vector Xi := b. For exa~lple, if a = lO( 10) and b = l (2), then the decinlal digits a
nlay be encoded in the so-called 8421 systenl:
(2 (4) ~ Xi = a i'
124 RejJresentation of variables 3.4
0.380 (0)
(1 000)
(1001)
(101)
(1 1 0)
(111)
In so-called fixed length coding the vectors Xi have a conlmon dinlension d, and
the decoding of a nlessage m (consisting of the catenation of vectors Xi) involves
the selection of successive infixes of dinlension d. If the probability distribution
of the characters Q I occurring in messages is not unifornl, more compact encoding
may be achieved by using variable length codes and assigning the shorter codes to
the more frequent characters. Decoding of a nlessage in variable length coding
can be perfornled only if the boundaries between the successive Xl are indicated in
some way.
The boundaries between characters in a message in variable length code nlay
be demarked by special partition symbols (which is inefficient) or by using a
prefix code in which no legitimate code point Xi is the prefix of any other legitinlate
code point, including itself. The index vectors of the leaves of any tree possess
this property; conversely, any set of prefix codes can be arrayed as the leaves of
sonle tree. Hence if each character of the set to be encoded is assigned as the leaf
of a conl11l0n tree, and if each character is encoded by the associated index vector,
a so-called prefix code is attained. Figure 3.10 furnishes an exanlple of a binary
code (i.e., the branching ratios do not exceed two) constructed in this nlanner.
O-origin indexing is used. The discussion will be limited to binary trees.
] f ii is the freq uency of the ith character and 1i is the length of the assigned code
(i.e., the length of path to the root), then the nl0st efficient code is attained by
minimizing the scalar producti x 1. This nlay be achieved by the following con
struction, shown to be optimal by Huffman (1952). First, the characters to be
encoded are all considered as roots, and the two roots of lowest frequency are
rooted to an auxiliary node (shown as a null elenlent in Fig. 3.10), which is then
assigned their combined frequency. The process is repeated until only two roots
relnain. The tree of Fig. 3.10 is optilnal with respect to the frequencies shown to
the left of the leaves. The appropriate conlbined frequencies are shown to the left
of each of the nonleaves.
Programs 3.11 and 3.12 show the construction of the tree T representing a
3.4 Representation of trees 125
T ~c
2 fJ.l(T) : 2
3 T ~ (8/I)ST
4
T +- \\(0) EB a 2 //T, al, -a 2//T\\
5 I +- (8/I)Sf
6 I ~ \ + ja2 jj, al, a2 jI\
H uffnlan code for a set of characters c i with frequencies Ii' the former in ternlS
of the tree itself and the latter in ternlS of its left list.
Program 3.12. The tree T of Program 3.11 is represented by the left list node
vector z, in conjunction with the inlplicit degree vector d = 2 x (z = oe). The
algorithnl differs fronl Progranl 3.11 primarily in the reordering of the subtrees
(steps 6-9). Step 7 appends to x the left list of the ith subtree (of the reordered
tree) selected by the partition vector p according to the conventions of Progranl
3.9. Step 1a prefixes x by the new null root, and steps 11-12 redefine p
appropriately.
Program 1.21 can be applied to the left list produced by Progranl 3.12 to deter
nline the associated index nlatrix (in a a-origin systenl), and hence the actual
codes assigned.
It is not essential that the characters be assigned to leaves in precisely the order
specified by Programs 3.11 and 3.12, and it is sufficient that the dinlension of the
leaf index increase monotonically with decreasing frequency of the character. It
is therefore unnecessary to carry the characters themselves through the process ~
it suffices to deternline the structure of the tree, sort the corresponding index
matrix to right list order (which is ordered on dinlension of the index vectors),
and assign the characters (in decreasing order by frequency) to successive leaves.
Since the structure of such a tree (whose nodes have a conlnlon irrelevant value
and whose nonleaves all have a conlmon branching ratio equal to the nunlber of
roots) is sufficiently determined by the moment vector ~(T), the process of Pro
gram 3.12 can be simplified.
126 RejJresentation of variables 3.4
tree.
4 i +- 1
fi Frequency of ith sub-
5 x +- e(O) tree of z.
7 x +- x CD (((p t ex j
-
I) ~ exPi)lz) subtree of z.
9 i : v(f)
ascending order on
11 P +- (O/f)Sp
Legend
12 p +- (1 + (p ~- ex 2 )) CD -a2 /p
Program 3.12 Construction of the
13 f +- (Olf )Sf left list z of the binary Huffnlan
code for characters c with fre
14- f +- ( -1- lex 2 /f) ffi -a 2 1f
quency f
1 I n 18 0 0 0
3 a 4 1 n 0 18 1
2 2 g 16 26 0 0
2 b 7 2 g 0 16 2
3 0 u 0 0 0 0
2 g 9 0 u 14 0 3
4 0 t 0 0 0 0
4 c 11 0 t 19 0 4
5 3 a 24 9 1 0 0 z 0 3 a 6 24 5
6 2 b 8 20 0 0
1 n 15 2 b 2 8 6
7 0 v 0 0 0 0 a k 0 0 v 0 0 7
8 0 k 0 0 0 0 3 h 16 0 k 20 0 8
9 0 z 0 0 0 0
0 j 0 0 z 1 0 9
10 0 0 0 0 0 0 0 I 0 a 0 17 0 10
11 0 f 0 0 0 0
0 f 0 0 f 15 0 11
12 0 r 0 0 0 0 0 d 0 0 r 21 0 12
13 0 y 0 0 0 0
0 r 0 0 y 0 0 13
14 2 s 4 19 0 0 0 e 0 2 s 23 4 14
15 0 d 0 0 0 0 2 i 19 0 d 12 0 15
16 0 j 0 0 0 0 0 0 0 0 j 26 ,')
16
17 3 m 3 14 23 0
3 nl 21 3 nl 7 3 17
18 2 i 22 25 0 0 0 v 0 2 i 0 22 18
19 0 w 0 0 0 0
0 p 0 0 w 0 0 19
20 3 h 10 17 7 0
0 q 0 3 h 0 10 20
21 0 e 0 0 0 0
0 u 0 0 e 0 0 21
22 0 p 0 0 0 0
2 s 24 0 P 25 0 22
23 1 x 13 0 0 0 1 x 26 1 x 0 13 23
24 4 c 11 15 12 21 0 t 0
4 c 9 11 24
25 0 q c 0 0 0 0 w 0 0 q ::; c 25
26 0 1 0 0 0 0 0 y 0 0 1 0 ::)
26
A full chain list Inatrix The right chain Filial-heir chain list
list matrix
(a) (b) (c)
Table 3.13 Chain lists of the tree of Fig. 1.16
2 }+-O
Legend
r
Program 3.14 Determination of the path p = T fronl the right chain list
matrix P
The set of (j + 1)th level nodes of the subtree T i are collectively called
thejthfilial vector of node i, and the first member of the first filial vector of
node i is called the heir of node i. (For brevity, the first filial vector of a
node will also be called its filial vector.) If each node is chained only to its
successor in the filial vector containing it and to its heir, the resulting
representation is called a filial-heir chain list. Fornlally, the filial-heir
representation of a tree T is a matrix F of dimension Il(T) x 4 such that
F 2 is a node vector of T, F 1 is the associated degree vector, Fa is a .filial
chain such that F:/ == j if node F 2 i is the successor of node F./ in the
smallest filial set containing it and F:/ == ifnode F 2 i has no such successor,
0
EXERCISES
The symbols a and c will be used exclusively to denote lower case and capital
alphabets defined as follows:
a = (0, a, b, c, , z, . , , , #, *, +).
c = (0, A, B, C, , Z, . , , , #, *, +).
The expression 1t S; x will be used to specify the set x as the range of the conl
ponents of 1t.
3.1 For each of the following cases, specify a suitable encoding n1atrix and
fornlat vector and show the explicit value of the infix of 1t which (in a solid
representation) represents the given exanlple vector x:
(a) the decinlal digits d = LUCIO) in a ranked fixed-length code for 1t S; lO(2).
Exanlple: x = (6, 8, 9).
(b) the set a in a ranked fixed-length code for 1t S; lO(2).
(c) the set a u c u lO( 10) in a fixed-length code for 1t S;; lO( 10).
(d) the set a u c in a two-case code (with single-character shift) for 1t S;; (I.
(See Brooks and Iverson, 1962.)
Exanlple: x = (T, r, 0, y, " N, . , Y, .).
(e) the set a in a Huffman prefix code for 1t S; lO(2). Assunle the frequency
distribution given in Dewey (1923).
Example: x = (t, r, e, e).
3.2 For each of the cases of Exercise 3.1 write a progranl which decodes the
infix (i 1 a.J)/1t, that is, which produces the vector z represented by the infix. The
auxiliary physical vector 1t l S;; S may be employed to represent the first colun1n of
the encoding matrix, where s is the set encoded. Perfornl a partial trace of each
program for the example value used in Exercise 3.1.
130 Representation oj'rariahle.\'
3.3 The ordered set of nlonths nl = (JANUARY, FEBRUARY, ... ,
DECEM BER) is to be represented by the physical vector 1t S;; C U l()( 10). For
each of the following types of representation, specify a particular representation
and show the values of the relevant conlponents of 1t:
(a) a linear representation (en1ploying null elenlents for tllling to a conlnl0n
dinlension in 1t).
(b) a solid representation for each elen1ent of 111 and an appropriate grid
nlatrix itself represented linearly.
(c) a chained r~presentation.
(d) a double chained representation.
3.4 (a) For each of the cases of Exercise 3.3, write a progran1 which selects
n10nth mIl'.
(b) Trace each progranl for the case k = 2.
(c) For case (d) of Exercise 3.3, write a progra111 \vhich selects 111 1,' by
forward chaining if k l'(rn) -:- 2, and by backward chaining if
k . vern) -:- 2.
3.5 For each of the cases of Exercise 3.3, write a progran1 which "'prints out"
the set of 1110nths in a n1inin1un1 nun1ber of n-character lines, inserting a single
null between successive n10nths except where (i) further nulls n1ust be added to
prevent the continuation of a single word fron1 one line to the next, or (ii) no null
is needed between two successive \vords, the flrst of which is coternlinous with
the line. I n other words, produce a nlatrix Z of row di111ension n and of nlinin1unl
colulnn din1ension such that (Z ~E)/Z = (p(nl 1 ) p(rn 2) p(nll~)'
and such that each ro\v Zi nlay be partitioned into one or n10re vectors of the
fornl p(rn/J O , all but the last of \vhich nlust be of din1ension l{p(nl l.,)] + I.
3.6 Assunling a linear representation for each of the logical vectors involved,
and a forward-chained representation for each of the ren1aining operands, write
progranls for the following operations. Assunle in each case that the argunlents x
and y need not be retained, and aSSU111e the use of a backward-chained pool
where necessary.
(a) z ~ x, u, y
(b) z ~ lx, u,y/
(c) z ~ k i x
(d) z ~ k 1x
3.7 Repeat Exercise 3.6(a), using separate grid nlatrices for x, y, and z instead
of chained representations. Specify a suitable linear representation for each of
the grid n1atrices.
3.8 (a) If a chained representation is used for a vector x, then the selection of a
specitled con1ponent can be 111ade faster by providing a nUlnber of
alternative starting points for the required scan. State precisely the
quantities required in such a process and \\;Tite a progranl showing its
usc.
(b) If provision is nlade for starting the scan at any conlponent of x, the
chained representation nlay itself be sinlplified. Sho\v precisely what
Exercises 131
the Sill1plified forll1 is and identify the type of representation to which it
is equivalent.
3.9 Frequently a vector x kept in a partitioned representation (for efficient use
of storage) ll1USt be "unpacked" to a linear or other 1110re accessible fornl for
efficient processing. The converse operation of "packing" is also required. Let
the partitioned representation be a file (I) enlploying an interconlponent partition
AI' and a ternlinal partition A~, and write both packing and unpacking progranls
for each of the following cases. Assunle that the nlaxinlunl dill1ension in 1t of any
conlponent is n.
(a) A solid linear representation eInploying null fill.
(b) An allocation prescribed by a grid nlatrix G with G'2 = !lE.
3.10 Let 1t S; lO(2), let the set a be encoded in a five-bit code such that (2E)
p(ll/) = i,and let each conlponent of the vector x be an (uncapitalized) English
word. Using a-origin indexing throughout, specify a suitable partitioned repre
sentation in 1t for the vector x, and repeat Exercises 3.9(a) and 3.9(b), using it in
lieu of the files.
3.11 For each of the following pool organizations, write a progranl to convert
a given ll1arked pool into a backward-chained pool:
(a) dinlension-ordered.
(b) address-ordered.
3.12 For each of the following queue disciplines, write progranls which take
fronl and return to the pool an infix of length n. Use secondary linking and
relegate to a Inarked pool any infix which is too short for linking. In each case
choose the type of chaining best suited to the particular queue discipline.
(a) LI FO (last-in-first-out).
(b) FI FO (first-in-first-out).
(c) Dinlension ordered.
(d) Address-ordered (utilize the possibility of fusing adjacent infixes).
3.13 Give a conlplete specification of a schenle for representing a tree T by a
full chain list matrix which is not in right list order. Write a prograll1 (expressed
in ternlS of the physical vector 1t) which deternlines the path vector T i for a given
index vector i.
3.14 Give a conlplete speciflcation of a schenle allowing joint representation of
those conlponents shared by two or nl0re of a fanlily of vectors Xl, x'2, ... , Xii as
suggested in Sec. 3.2. Write progranls to (i) select conlponent x/, and (ii) delete
conlponent x/.
3.15 Let 1t ;; II U l(lO), and let xl, x 2 , . . . , x n be a fanlily of vectors whose
cOll1ponents belong to the set o:l/[a U l(lO)]. Let the average and the nlaxin1unl
dinlensions of the vectors xi be a and Ill, respectively. AssunlC that the chaining
index is represented in decinlaI, with each digit represented by one con1ponent
of1t. Deternline (as a function of 17! and n) the value of a below which a chained
representation provides nlore con1pact storage than a linear representation with
null fill.
132 Representation oj' l'ariahle.~'
3.16 Write a program which uses the n1ininlization operation u.. I' l x to
determine the ordering pernlutation vector p -( - 01/( a 'I b).
3.17 Let U = (X 0) and r = UIX jointly represent the sparse l11atrix X.
(a) Write a program \vhich detern1ines (as a function of U and r) a suitable
row-chained and column-chained representation of X.
(b) Write a program defined on the representation produced in part (a) to
conlpute the product Y = X x X, itself represented in the fonn V =
(Y 0) and p = VIY.
(c) Write a progranl to deternline the trace (that is, +IIIX) of X fronl the
representation produced in part (a).
3.18 The unique assignment of Hufflnan codes produced by Progranl 3.12 is,
in general, only one of nlany equally efficient assignnlents, since the syn1bols to be
coded need only be assigned, in decreasing order on frequency, to the leaves of
the code tree in increasing order on their levels. Show that the structure of the
tree produced can be sufficiently described by its nloment vector alone, and write
a progran1 for the construction of a Huffman code based on this fact.
3.19 Following the notation and ternlinology used in Progranl 3.9 for the
analogous case of a left list write a progranl which detern1ines fron1 the right
list R of a tree T, the partition vector p which partitions it by levels.
3.20 Write a progranl which detern1ines the right list R = cx~/]T as a function
of the left list L = cx 2 /[T. Incorporate tests of well forn1ation.
3.21 Let [X8~]j) denote the pth power of the square nlatrix X with respect to the
,operators 0 1 and 02' that is, [X8~]1) = XCl~ X~<~ ... ~j~X to p factors.
(a) Show that ([C ]Ji)/ = 1 if and only If the-re is a ~path of length p fronl
node i to node in the graph (n, C).
(b) Show that [C ]1 = 0 for some p
1
I'(C) if and only if (n, C) contains no
circuits.
(c) If (n, C) contains no circuits, the connection nlatrix C is said to be "con
sistent. n The result of part (a) can be used to check consistency. Progran1
the alternative nlethod of Marin10nt (1959).
(d) If H = C I, then ([H ::~ ]1
1
)/= 1 if and only if i =.i or there exists a path
fronl node i to node.i of length n p + I. Show that for any connection
n1atrix C, [H /: ]/1 converges to a linlit.
3.22 Devise progranls to determine
(a) whether a given connection nlatrix C represents a tree.
(b) the left list of the tree (n, C).
(c) the right list of the tree (n, C).
(d) a node list n and connection matrix C as a function of
(i) a left list I.J
(ii) a right list R.
3.23 Show that (n, C) and (n p , CpP) represent the sanle graph for any pern1Llta
tion p.
3.24 lf (n, C) is a tree and if K = C ; C, then C can be detern1ined as a function
of K (see Ross and Harary, 1960). Write a progranl for deternlining G' fr0l11 K.
chapter 4
SEARCH TECHNIQUES
1 In 0 0 1
2 tu 0 1 0
3 G== w 0 1 1
4 th 1 0 0
5 f 1 1 1
and fonnat vector f == (0, 1, 1, 1), then scanning can be accomplished by
cOInparing p(k), the encoded representation of the key k, with successive
rows of fiG to determine the rank r of the row on which agreeInent occurs.
Moreover, the key transformation
0,5). Had p(f) (that is, fiG:)) been chosen as (1,0, 1), the mapping vector
would not have been required. Finally, the key transfonnation
t'(k) == 1 + p3(k)
has the range d == (1, 2), is not uniq ue, and req uires a subseq uent scan of
one or other of the equivalence classes e 1 == (tu, th), and e 2 == (m, w, f),
represented by (010, 100) and (00 L 0 II, Ill), respectively.
Although a strict ranking operation maps elenlent k; into the integer i,
any biunique mapping onto the index set ll(V(k)) will frequently serve as
well. For, if p is the permutation required to cOInplete the ranking process,
and if a subsequent permutation j is required (as in step 4 of Progranl
1.12a), the two permutations can be combined in the single pern1utation
q == jJp. Sinlilarly, the ranking of a set k may be considered as equivalent
to the ranking of any set obtained by pernluting k.
4.1 Scanning nlethods .fc)r rank ing 135
The two nlain methods of scan are called directed and controlled. A
directed scan is begun in one of twopossiblc directions fronl a given initial
point i~ either ascending (that is~ i~ i I~ i + +
2~ ... ) or descending. The
direction chosen may be determined by a conlparison bet\veen the given
argunlent and the set element k i at the initial point i. A controlled scan is
executed in a seq uence which is detennined by successivc cOlnparisons
between the argument and each element scanned. In an effective controlled
scan~ each comparison 11lust determine thc choice of the next clenlcnt for
conlparison so as to (approximately) lllinilllize the expected nunlber of
eleinents scanned. The directed scan is clcarly well suited to the use of a
file or chained representation~ which illlposes serial access to the eleinents,
whereas the controlled scan is 1l0t.
The scan Iength (i. e. ~ the n uIn be r 0 f key eIenl ents scann ed) \\/ iII be used as
a Ineasure in analyzing and evaluating scanning 11lethods. The sanle
nleasure and the same analysis apply also to the converse situation, \vhere
the rank of an element is given and the element itself must be obtained
from a chained or other representation which pennits only serial access.
An alternative related nleasure is the norlllalized scan length or scan
jj'action~ defined as the scan length divided by the nunlber of elenlents in
the set of keys.
A scan is said to be fooled if each exccution begins at the sanle point f.
A rooted scan may be advantageous \vhen the freq uency distribution of the
argunlents is nonunifonn and the nlost frequent keys can be grouped near
the root. A scan is called catenated if each execution is begun at the end
point of the preceding scan. A catenated scan Inay be elllployed in lIsing
a file when the intervals between successive scans are so short as to allo\\'
little or no time for return to a fixed root or \\;'hen the arguinents are
arranged in the saIne relative order as the items in the file.
Directed scan
A directed scan is called cyclic if elenlent k 1 follo\\/s k in an ascending
j
V:l
~
~
~
~
~
s
~.
Type of Scan pre! b) for Scan Fraction f
~
~
Aver Maxi
Initial Initial Cvclic or
No.1 Position Direction Noncyclic o :S b b < 1~ 1~ h' 2 age mum
Table 4.1 Scan fraction of directed scans for unifonn distribution of argunlents (from Iverson, 1955)
em
~
'"'"'"
4.1 Scanning 117ethods.fc)} ranking 137
are sUllllllarized in Table 4.1. Certain of the results are also plotted in
Fig. 4.2.
Initial direclionfixed (fixed scan). The ascending direction will beassulllcd.
If the scan is rooted and cyclic, the root may, without loss of generality, be
assulned to be one. The expected scan length for a set is then given by
"(h)
e == 2: f(kJ X i == f + II
j= 1
1.0
...c
VI
~
D.. 0.5
Figure 4.2 Plot of cunlulative probabilities of Table 4.1 (Nul11bers refer to the
entries in Table 4.1)
138 Search techniques 4.1
in decreasing order on frequency. If the distribution of arguments is
uniform (that is,fi === llv(f)), then the expected scan length is (v(k) 1) -:- +
2, and the expected scan fraction is approximately one half.
If a fixed scan is cyclic and catenated, the expected scan length depends
on the distribution f, but if the arguments are independently distributed,
then the expected scan length is independent of the ordering of the scanned
set k. This may be shown as follows. The expected length lr of a scan
t
rooted at r is given by Mr f, where M is the square matrix such that
Mr === (r - I) t LI . The probability of beginning a catenated scan at r is
the probability of ending the previous scan at r, that is, fro Consequently,
e === 1 ~ I === 1 t M t 1
Since, in general, 1 t M~- 1 === 1 t M x f, then 2e === (1 ~. N ;~ 1),
where N === M + M. If, for example, v(k) === 4, then
1 2 3 4 2 6 6 6
4 1 2 3 6 2 6 6
M=== and N===
3 4 1 2 6 6 2 6
2 3 4 1 6 6 6 2
Initial direction gil'ing shortest scan. For a fixed root r, the mlnlnlunl
expected scan is achieved if the items are disposed on either side of the root
so that the frequency fi is a monotone decreasing function of Ii - 1'1, the
scan length in a direct scan to the argument. In a cyclic scan, the position
of the root is inlmaterial; in a noncyclic scan it is best centered at the floor
(or ceiling) of (v(k) + 1) --:- 2. In a O-origin system this expression
becomes v(k) --:- 2.
For an arbitrary frequency function, the expected scan length is given by
the scalar productf >/ 1, where 1 == ILl - rEI. For a uniform distribution,
the results are given in entries 2 and 5 of Table 4.1. For a catenated scan,
the corresponding results appear in entries 2 and 6.
The possibility of choosing the initial direction so as to give the shortest
scan to the argument depends on the infonnation available. If the elements
of k are strictly ranked on some function g(k;), then the shortest direction
from root r to argument x can, in the noncyclic case, be deternlined by a
cOlnparison of g(x) and K(k r ). For the cyclic case this does not suffice, and
it is necessary* to know the index in k of the argument.r. This case is
therefore of interest primarily in selecting a specifled elenlent fronl a
serial-access representation and is of little interest in an actual ranking
operation. However, any double-chained representation or reversible tIle
can be used in a noncyclic as well as a cyclic manner and hence adnlits of a
choice of direction which is best in the noncyclic sense.
Initial direction to nearer (farther) terminal. If the value of the root I' is
known for each individual scan in a catenated scan, the direction to the
nearer terminal can be determined by c0I11paring r with the I11id point
* Approximating functions may, however, be used for estinlating the index and the
probable best direction.
140 Search techniques 4.1
(v(k) + 1) -:- 2. A noncyclic scan starting toward the nearer end is clearly
less efficient than one starting toward the argument, but it may be useful
when the most direct route to the argument cannot be determined. The
expected scan fraction is shown in entry 7 of Table 4.1; its analysis will
illustrate the method used in constructing the entire table.
It is assumed that the number of elements v(k) is sufficiently large that
the scan fraction / may be considered as a continuous variable. Let
pr (/ ::::;: b) be the probability that scan fraction f does not exceed b, and let
the function be represented in three parts such that pr c.r : : ;:
b) == prj (j' s:: h)
in the ith half-unit interval in b. Let x be the normalized initial position of
a given scan. Then 0 ::::;: x ::::;: 1, and, since the scan always begins toward
the nearer terminal, the fraction of the set covered in a scan of length b is
the same for the starting point (1 - x) as for x. Using this symmetry,
attentioncan be restricted to valuesof x in the range 0 to ~~' For the function
pr1 ( [ ::::;: b), the value of b is also restricted to the range 0 to J.
Consider fixed values of b and x with h ::::;: J. If 0 ::::;: x ::::;: b/2, the fraction
of the file covered by a scan of length b is given by b - x, for the scan
begins at x, proceeds a distance x to the nearer terminal, and returns to
the point b - x. If b/2 ::::;: x ::::;: b, the fraction covered is clearly x, for the
scan will reach the nearer terminal but will not return past x. If h ::::;: x ::::;: t,
the scan does not reach the nearer terminal, and the fraction scanned is
therefore b. Since x is uniformly distributed, the function pr1 (j' :s: b) is
obtained by integration as follows:
J == =-17 + b.
pr 1 (f ::::;: b) == 2 [l o
b/2
(b - x) dx + ~".
(1)
/1/2
x dx +
l~ i
b
17 dx
2
2
The factor of two arises from the syn1n1etry in x and the restriction of .r
to the interval 0 :=:;: x ::::;: -!. Similarly,
Controlled scan
The seq uence followed in a controlled scan.is comnl0nly detennined by
a conlparison which determines the relative ranking of any pair of elements
x and y in the set k. It will therefore be assumed that comparison of the
argument x == k h with the element k j determines whether h < j, h == j, or
i~l
k ~ v(k)
j +-lCi + k) -:- 2J
>
k +-j - 1
i +-j + 1
h ::=:> j. The subsequent scan may then be limited to one or other of the
two subsets a/-- 1 jk and aJjk. Themaxinluln dimension of the subset renlain
ing to be scanned is therefore minimized by choosing j == l( v(k) + I) -:- 2j.
If each subsequent element for comparison is chosen so as to (approxi
mately) halve the dimension of the set renlaining to be scanned, the
process is called binary search. Program 4.3 shows the details of binary
search ~ i and k are the indices of the tenninal elenlents of the renlaining
subset, and j is the index of the element selected for cOlnparison.
If v(k) == 2k , then anyone of 2J -- 1 different argunlents Inay be isolated on
the jth comparison, for j E ll(k), and the one remaining argunlent will be
located on the (k + 1)th cOlnparison. Hence for a unifornl distribution of
arguments, the expected number of c0I11parisons required in a binary
search is gi ven by
e,/2 /,) == (1 .2 + 2 . 2 1 + 3 . 22 + ... + k .2 /,'-1 + (k + 1)) -:- 2 /,'.
142 Search techniques 4.1
It can be shown (e.g., by induction on k) that
.i ~ l(p(M) + 1) ~ 2J
>
.i ~ M 2 j
.i M 3
+- j
bee bee
2 cab 3 cab
3 cat 0 4
4 dog
6 foe foe
7 had 6 8
had
8 hoe 0 9
9 nod
Since a key transformation maps the set of keys k into a set of integers
(the set of derived keys d), any unique key transformation produces a
derived key which can be used to select the component of a mapping vector
directly and thus complete a ranking operation without the use of a
scan. If the transformation is not unique, it lnay still be used to partition
the original set k into v(d) subsets for scanning and so reduce the expected
scan time. Ideally a key transfornlation should be both simple and unique
and should produce a derived set d having a narrow spread; in practice,
compromises must be made.
Let k be the domain and d the range (in ascending order) of a key
transformation t(k i ) and let e J be the equivalence class in k which maps
into d j , that is, t(x) = d j for all x E eJ. The coalescence of t ill k is then
defined as the vector c such that c j = v(e J ), for j E ll(V(d)). Since the
equivalence classes are disjoint and collectively exhaust k, then +/C =
v(k). The spread of t ill k is defined as 1 + d" - d i . Thus if k is the set
(Sunday, Monday, ... , Saturday), and if t maps each day into the rank
(in the alphabet) of its leading letter, then d = (6, 13, 19, 20, 23), the
spread s = 18, c = (1,1,2,2,1), and +/c = v(k) = 7.
The key transformation is biunique if and only if C = . Moreover, if
the transformation t is biunique, the ranking operation (i.e., the deternlina
tion of the index of the argument in k) can be completed by a nlapping
vector whose components are selected by the index j = t(kJ - d l + 1,
and whose dimension is equal to the spread of t in k. The key transfornla
tion of the preceding example is biunique when restricted to the set x =
(Sunday, Monday, Tuesday), the set of derived keys is (13, 19, 20), and
the mapping vector m = (2, 0, 0, 0, 0, 0, 1, 3) of dimension eight serves
to cOlnplete the mapping if its cOlnponents are selected by the index
j = t(x;) - 12.
A key transformation is called j-or/gin if d l = j. Since the origin can be
changed by subtraction of a constant, attention will be restricted to
I-origin transformations. The spread of a I-origin transformation is
clearly d".
4.2 Key tran.~/()r}}latiol1s 145
A biuniq ue key transfonnation is always
o 3
3
0 3 0
1 2 1
2 6 0 2
3 3 1 4
4 1 1 5
5 4 1
6
M=
9 0
10 0 0
1 9
7 0
8 0
9 0 1 10
10 0 2
11 5 1 5
7 11
2
1 k +- 0 O-origin indexing
2 j+- -1
x Argument.
3 j +-j +1 i Current node.
4 j vex) j Current index of argument.
5 i +- k + xl' k Index of first node reachable from
node i and finally the rank of x.
6 k +- MOi
M Leaf list matrix.
7 k M M/i = 1 <=> h is a leaf.
1
8 Mi T-
1 0 M o Combined leaf and chaining vector.
Legend
I = [(c + e) ~ cJ = [1 + C~ CJ -:-- 2.
2(+/c) C ~ e
For a fixed dimension of the derived set d (and hence of c), and for a
necessarily fixed value of +/C = v(k), the value of I is clearly minimized
if the coalescence vector c is uniform, i.e., if the components of c are all
equal. Hence the expected scan length is minimized by a key transforma
tion whose equivalence classes are of equal dimension.
A given key transformation is frequently employed to rank a variety of
subsets of its domain k rather than k itself. For example, if k is the set of
English words in a given dictionary, then one of the subsets to be ranked
may be the set of distinct words in a particular sample of English text. If
a particular subset of k is specified, then the coalescence of the key trans
formation in the specified subset x can be determined, and the transforma
tion can be chosen accordingly. More generally (as in the case of samples
of English text), the active domain x may be only partially specified. The
transformation should then be chosen so that its coalescence is nearly
uniform for the expected active domains. If, for example, k is the set of
all five-letter sequences, and if each active domain is the set of five-letter
sequences beginning with a specified letter, then the key transformation
used should depend only on the last four letters. If the set of derived keys
produced by a key transformation has a spread s and a random uniform
distribution within that spread, then the expected length of a scan (of the
equivalence classes) can be shown * to be 1 + v(k) -:-- 2s.
Scanning of the equiralence classes. If an element x is to be ranked in k by
a scan of k itself, no auxiliary information is required since the rank of
component k j is simply its index j. If some permutation of k is used
instead, then an auxiliary ranking vector r (i.e., a permutation vector)
nlust provide the rank in the given set k. Specifically, if y = pJk is used
for ranking, then r is the permutation vector inverse to p, and the rank of
element Yj is r j. Finally, if the vector Y is itself to be scanned in some
prescribed order other than simple cyclic order, the order may be repre
sented by a chaining vector q.
The vectors y and r or y, r, and q can be combined into a two-column
* See Johnson (1961) and Exercise 4.4.
4.2 Key tran,sjonnations 149
Fl i = 0, i d.
All remaining elements of the sets (i.e., aI/e') are represented in arbitrary
order in a two-column ~~overftow" matrix V.
The scan procedure is given by the Program of Fig. 4.9a. If the given
argument x is not equal to F~(r\ then the overflow matrix V is scanned in
ascending order. The left-pointing exit indicates that x k.
For a uniform distribution, the expected scan length is clearly given by
where (l( V) = v(k) - v(d). The expected scan length is therefore large
unless the average dimension of the equivalence classes (that is, lICk) -:- v(d))
is close to unity. For a known nonuniform distribution, the expected scan
can be reduced by placing the most freq uent element of each eq uivalence
class in F and ordering the elements in V according to their freq uency.
(b) Orerfiow with chaining. The two-column matrices F and V used in
method (a) can each be augmented by a third column chaining vector
which chains each equivalence class. Thus F~k is the row index in V of
element e/" if it exists, and is otherwise null. Similarly~ if V/I = e)i, then
V 3 h is the row index in V of e,~ :-1 if it exists, and is otherwise null. The
program is given in Fig. 4.9h. The expected scan length for a uniform
distribution can, as shown earlier, be expressed in terms of the coalescence
vector c as follows:
(c) Single table lrith chaining. In the overflow methods [(a) and (b)],
certain rows of the matrix F go unused, and a saving in storage can be
k = (Sunday, Monday, Tuesday, Wednesday, Thursday,
t(k i ) = 1
Friday, Saturday)
+ (610 n i ), where
1
2
3
4
F=
Friday
Sunday
Tuesday
0
6
~I
:J
"*
j +-
x : Fli
t(x) ~
Ul
0
5 0 0
;+-0
e,..;,
(n i is the rank in the alphabet of the first letter of ki) ~
n
1 Monday
i : {leV) ~
z = (2, 2, 3, 6, 3, 1, 2), where Zi = t(k i ), 2 V= Thursday ~
3 Saturday ~
d = (1, 2, 3,6), and s = 6. i +- i +1 ~.
.# ~
V)
x : VIi
Data of examples Overflow
(a) j +- V 2 i
1 Friday 6 0 1 Friday 6 0
2 Sunday 1 1 I -4 ; +- lex) 2 Sunday 1 4 ~ i +- lex)
3
4
F= Tuesday 3 2
:I Ox: 3
4 T=
Tuesday
Monday
3 5
2 7
n
0 0
F1i 5 Thursday 5
5 0 0
6 Wednesday 4 j +- F 2 i 6 Wednesday 4
x: T 1'
7 Saturday 7 : I
i ; +- Fai ; +- Tai
1
2 V =
I Thursday
Monday 2
5
3
j +- T 2 i
3 Saturday 7 0
L
x : VIi
Single table with chaining
Overflow with chaining i +- Vai (c)
(b) I I
j +- V 2i em
".
N
em
~
N
1 Sunday 1 2 1 Friday 6
2 Monday 2 7 i+-l(x) 2 Sunday 1 i +- leX)
3 Tuesday 3 5 3 Monday 2
4 T= Wednesday 4 i+-m i 4 T= Tuesday 3 x Tli
5 Thursday 5 5 Thursday 5
6 Friday 6 6 Wednesday 4 Tli
7 Saturday 7 a 7 Saturday 7
x . Tli i +--- /l(T) h(i + 1)
m = (6, 1, 3, c, 0, 4) i +- T 3i Tli +-.r
-go
S5
....
til
~
152 Search techniques 4.2
effected by combining the three-column matrices F and V in a single table
T of column dimension
Let u be a logical vector such that U/l 1(fl(F)) = d. Then ii/IF constitutes
the unused and u//F the used rows of F. Let v be a vector of dimension
f1( T) obtained by appending a zero suffix to u. The first two columns of
T are then defined as follows:
Bucket files. In certain files the locations divide naturally into blocks or
buckets of n successive locations each, such that the entire contents of any
bucket can be scanned in virtually the same time required to scan anyone
location in the bucket. Such a file is called a bucket file (Peterson, 1957).
In a magnetic disc file, for example, each track forms a bucket. Each of the
foregoing methods of scanning equivalence classes can be adapted to suit
the characteristics of a bucket file. The equivalence classes can be
grouped in buckets, with chaining provided only from bucket to bucket.
yields a mapping onto the range lO(l03) which has both uniform coalescence
and uniform spacing. On the active domain x, whose elements are all
represented by vectors p(x) such that a 3 jp(x) = (2,4, 7), however, all
elements "cluster" in the single derived key 247.
The deleterious effects of such correlations among elements of the
active domain can be reduced by employing key transformations which
depend on all components of the representation and do so in a manner
which shows no systematic relationship to the structure of the representa
tion. The mid-square method, for example, consists in squaring the given
key and extracting the middle digits of the resulting product. A commonly
used transformation is the taking of residues modulo some number m
such that m ~ vex) and is either prime or contains few factors.
* Note added in proof: M. Hanan and F. P. Palermo offer an important solution
to clustering by the application of Bose-Chaudhuri codes. R. T. Chien and C. v.
Freiman have remarked a similar application of Fire codes (private communications).
4.3 Multiple keys 155
If in some mapping operation the access to both the key set and the set
of correspondents is serial (or partially serial), considerable advantage can
be gained by replacing the vector T 2 of ranks by the suitably reordered set
of correspondents, that is, T/ becomes the correspondent of the key T/.
For, the ranking operation on the argument k j which gives access to the
element T 1 i = k j also gives immediate access to the correspondent T 2 i in
the same row Ti. This is equivalent to eliminating the permutation
operation (through reordering of the set of correspondents) and coalescing
the ranking and selection phases so that together they require a single
access to the (partially) serial memory.
For a single functional correspondence, the coalescing of the ranking
and selection phases can (by a suitable ordering of T) be accomplished by
the single-table process (Fig. 4.9c) without introducing the mapping
vector m of process (d). Frequently, however, a number of related func
tional correspondences must be provided between pairs of a family of
vectors OJ so ordered that 0/' corresponds to 0/ for all i, j, and k. In an
accounting system, for example, 0 1 , O2 , 0 3 , and 0 4 might be, respectively,
the vector of account numbers, names, addresses, and balances in a given
ledger. Those vectors which may occur as arguments in a mapping process
are called key vectors ~ those which never occur as arguments are called
satellite rectors.
o may be reordered (and augmented by a suitable chaining vector) so as
to permit the use of the program of Fig. 4.9c for some selected key set Oi.
However, for any other key set OJ' the order will, in general, be unsuitable.
The program of Fig. 4.9d may, however, be used together with an appro
priate mapping vector m j and chaining vector qj. For the sake of uni
formity and the advantage of allowing an arbitrary ordering for 0, the
distinguished key set Oi may also be provided with a mapping vector m i
and treated like the rest.
The generalized binary search of Program 4.4 can be applied to the case
of multiple keys by providing a pair of chaining vectors (M2 and M 3 ) for
each key. The open addressing system is clearly unsuited to Inultiple keys.
REFERENCES
Burks, A. W., D. W. Warren, and J. B. Wright, (1954) ""An Analysis of a Logical
Machine Using Parenthesis-free Notation," Mathenzatical Tahles and Other Aids
to COlnputation, vol. VIII, pp. 53-57.
Cramer, Harald, (1951), Mathenzatical Methods o.lStatistics, Princeton University Press.
EXERCISES
4.1 Give a formal proof of the fact that the binary search of Program 4.3 will
not work properly if steps 5 and 6 are replaced by the statenlents k +- j and i +- j,
respecti vely.
4.2 (a) The argument k i in the set of keys k = (kI , k 2 , , kl') occurs with the
relative unnormalized frequency i. For the case v(k) = 10, design the
nlatrix M which will minimize the expected scan length when applying
Program 4.4.
(b) Show how additional keys may be incorporated in the system of part (a)
without revising the entire matrix M. Discuss the effects on the expected
scan length.
4.3 Consider the ledger L defined as
3 1 2 5 AD A M S, S. H. 0 0 435 o ASHooooooo
o1 6 8
B A K E R, J. C. 0 0 76 o ELMoooooooo
7 9 2 6 FOX, R. L. 0 0 0 0 435 o LAURELoooo
3 4 2 0 FOX, R. L. 0 0 0 0 435 o LAURELoooo
L= 1 9 2 5 H ILL, K. 0 0 0 0 0 118 o LINDENoooo
2 4 8 6 JON E S, J. C. 0 0 61 oMAPLEooooo 0
9 1 2 7 JON E S, J. C. 0 0 736 o LINDENoooo
6 1 3 5 KIN G, K. M. 0 0 0 76oELMooooooo 0
the resulting nlatrix M and the specific progranl used for selecting the row
Mi deternlined by an argument a E Xl.
(c) Specify simple key transfornlations on each of the sets k'!. and k 3 which
yield derived sets of dimension not less than three when applied to the
active donlains x'!. and x 3 , respectively.
(d) Augnlent the nlatrix M of part (b) by a pernlutation vector and a chaining
vector suited to each of the key transfornlations of part (c).
(e) Write a program which selects, as a function of a and j, the row of M
corresponding to the argunlent a E x j , for j = I, 2, or 3.
4.4 Let t i = f(k i), where f is a key transfornlation such that t S; LI ( t'(k)). The
vector t takes on n ft distinct values (where n = l'(k)), which are assunled to be
equiprobable. For n = 2, the cases are (I, I), (I, 2), (2, I), and (2, 2), with
expected scan lengths 1, -}, I, and }.
(a) Show that the over-all expected scan length is 1.25 for n = 2.
(b) Calculate the expected scan lengths for n = 3 and for n = 4.
(c) Generalize the result of part (b) to show that the expected scan length
rapidly approaches 1.5. [See Johnson (1961) for an alternative derivation.]
4.5 Design an open addressing system for the ledger L of Exercise 4.3 and the
key transformation of part (a).
4.6 Program and discuss the extension of binary search to Ill-way search.
4.7 Let I i be the time required to scan over the ith elenlent of a vector x which
is represented in a chained (or other serially-scanned) representation, and let Ii
be the frequency of occurrence of the argunlent Xi. Discuss the role of the
"standardized frequency" s = I -:- I in deternlining the optinlUI11 ordering of the
vector x.
4.8 The ne{[{hbors problenl requires that the near neighbors of an object in
n-dimensional space be found. The technique used depends on the dinlensional
ity and the particular criteria of adjacency.
(a) The position of a vehicle on a turnpike is represented by the distance in
nliles of the vehicle from the south end, and Pi is the coordinate of the ith
patrol car. Write programs to:
(i) identify the patrol car nearest an accident at position a,
(ii) identify the two cars nearest to each other.
(b) A three-column nlatrix V specifies the locations and radio call-signs of a
fleet of nlerchant vessels on a flat lake, where VIi is the call-sign of the ith
vessel, V 2 i is its distance in miles fronl the meridian tangent to the lake on
the west, V 3 i is its distance in miles fronl the perpendicular tangent to the
lake on the south. Write a progranl to deternline the call-sign of the neigh
bor nearest to a distressed vessel whose call-sign c is given.
(c) The matrix of part (b) is used to specify call-signs and locations at til11e f
of a fleet of b0l11bers over a flat territory. When each b0l11b is released,
neighboring planes must be at a safe distance. Construct a progranl which
will find the call-signs of all pairs of planes within r nliles of each other at
time f.
(d) In a certain hydrodynamic calculation, the nl0tion of each elel11entary
158 Search techniques
volUlne of fluid is traced. The ith elementary volume is described by row Li
of a matrix: (4 t ( 3 ) 1L represents the three space coordinates (in a recti
linear system), and a7 1L represents the remaining parameters. At each
time step the parameters of volume i are redetermined by those of the four
elements nearest it. Write a program to determine a 4 1Li as the set of
indices of the four nearest neighbors of element i. Hint. Attach an explicit
index vector before sorting.
chapter 5
lVIETAPROGRAMS
-) a+-x+y a+-x+y
b+-axr b+-axr
e +- s - t e +- s - t
d +- en d+-1
z+-b+d ~ i+-n
< i+-i-1
z+-b+d
Analysis in p Analysis in q
(a) (b)
v q
Tree
(a)
2 /\ 1 0 0 0
2 /\
2 V 1 1 0 0
2 V
0 P 1 1 1 0
0 P (/\, V, p, 1\, q, r, V, S, -, t)
2 /\ 1 1 2 0
2 /\
0 q 1 1 2 1 0 q Left list vector or Lukasiewicz form
0 r 1 1 2 2 0 r (d)
2 V 1 2 0 0
2 V
0 s 1 2 1 0
0 s
1 - 1 2 2 0
1 - ((q /\ r) V p) /\ (s V 7)
0 t 1 2 2 1 0 f
Parenthesis form
Full left list Left list (e)
matrix matrix
(b) (c)
Figure 5.2 Representations of a conlpound statenlent
5.2 Lukasiewicz notation 163
Figure 5.2 shows several alternative representations of a compound
statement. The tree representation (5.2a) is perhaps the most graphic, and
the other forms may be viewed as alternative representations of the tree.
The common parenthesis form of Fig. 5.2e, for exalnple, specifies the
requisite structure primarily by grouping the nodes of each subtree within
parentheses with the root in the middle of the group. As shown in Sec.
3.4, the left list nlatrix of Fig. 5.2c can be supplanted by the simpler left
list vector of Fig. 5.2d, providing that the degree of each operator p is a
known function 6(p).
The left list vector notation for a COIn pound statelnent is also called
Lukasiewicz, * Polish, or parenthesis~ff'ee notation. The Lukasiewicz and
the parenthesis notations will hereafter be referred to asY:;-notation and
Y-notation, respectively.
Phrase Value
(V, 0, 0)
(V,O, 1)
1
(V, 1,0) 1
(V, 1,1) 1
(/\ , 0, 0)
(/\,0,1)
(/\,1,0)
(/\,1,1) 1
(-,0) 1
(-,1)
Table 5.3 Phrases in~-systeln for syn1bolic
logic based on operators and, or, not
If sonle infix of z is a phrase, and if the infix is replaced by its val ue, then
the resulting vector y is called an2-reduction of z. If y is any Y-reduction
of z which cannot be further reduced, it is called a conlplete reduction of z
or, since it can be shown to be unique, the complete reduction of z. Com
plete reduction of z will be denoted by 2(z). A formula z is said to be
singular* if its complete reduction is a single literal, i.e., if l'(Y(z)) = 1 and
2(z) E 1. Thus q = (/\,1, V, 0,1) and r = (1) are singular, but s =
(/\ , 1, 1, 0) and t = (/\ ) are not.
For exanlple, complete reduction of the singular formula z = (/\ , ,1,
/\ , 0, I, V, 1, -, 1) may be performed as follows:
z = (/\, V, 1, /\, 0, 1, v, 1, -, 1)
Zl = (1\, V, 1, /\, 0, 1, v, 1, 0)
Z2 = (/\, V, 1, /\,0,1,1)
Z3 = ( /\, V, 1, 0, 1)
=
Z4
Z5 = ------
(/\,1,1)
1 s+-O
Z Given formula.
3 y +- e(O) y Reduced suffix of z.
4
i +- v(z) +1 i Index of Z in descending scan.
5
i+-i-l j Dimension of phrase to be
reduced.
6 y +- (Zi) CD y
s Singularity indicator.
7 (5(y}) : 0
8 j +- bey}) +1 Legend
9 j : v(y)
>
10 ~v -- ~ ( a. j / y)
11 y +-(x) CB aj/y
Two formulas are said to be equiL'alent if they have the same value for
each possible specification of their variables. If z is any formula whose
operators are all symmetric (i.e., whose operands commute), then any
reordering of the component singular formulas of z which leaves the span
of each operator unchanged leads to an equivalent formula. For example,
since the operators /\ and V are symmetric, the formulas
z = (/\, V, /\, V, }i', /\, u, v, r, q, p)
and q = (/\ ,p, V, q, /\, r, V, }i', /\, u, v)
be the unique (cf. Program 3.9) partitioning of (iljz into its conlponent
singular formulas. Then yi yi-tl yi I,' -1 represents a k-tuply
rooted tree and r(y! E8 yi~l 1) == k. Moreover,
yff)J
d .
== Inax [s(y]) + d - jJ
j=l
d .
== d + max [s(yJ) - jJ.
j=l
Since the component formulas are in minimax fonn, the s(yJ) are individ
ually minimal, and the maximum over S(yi) - j is clearly mininlized by
arranging the s(yj) in ascending order. This concludes the proof.
To ensure that each component fornlula is itself optinlal, it suffices to
apply this reordering procedure in turn to the successive singular fonnulas
encountered in scanning the given formula from right to left, as shown in
Program 5.5.
Program 5.5. The vector y is the suffix al'!z pernluted to optinlal [orI11, p is its
partition vector* (that is, ((p; a j - 1 ) 1aPj)/y is thejth singular formula of y), and
g is its maximum suffix dispersion vector (that is, gj is the nlaxinlunl suffix dis
persion of thejth singular formula of y). The nlain control parameter i is decre
mented on step 11, and, if it is not zero, the degree d = ()(z i) of the next
component to be added to y is examined. If Z; is not an operator, the branch
to step 8 occurs with h = I. The component Zi is then a fornlula of length 1
and steps 8-10 add it to y and nlake the appropriate changes in p and g.
If Zi is an operator (of degree d), the loop 15-22 scans the vector g and reorders
the first d component formulas of y so that their nlaximunl suffix dispersions are
brought to ascending order. This is accol11plished by the sinlple, but not necessar
ily efficient, sorting process of conlparing successive pairs of adjacent conlponents
of g and interchanging the corresponding C0l11pOnent fornlulas of y (by rotation
* The conventions used for p are those established in the subtree partitioning of
Program 3.9.
168 M etaprogranls 5.3
s (v(p) = 1) Legend
12 +-
13 d +- O(Zi)
14 d : v(p)
>
15 h +- 0
16 h+-h+l
>
17 h d
:::;
18 gh gh+l
19 u +- (p t a h 1) ,} aPh +Ph+l
20 y +- \ti/y, U, Ph t (u/y)\
21 gh~gh+l
22 Ph~Ph+l
x +y x z,
with the understanding that the entire expression need not be enclosed in
parentheses.
The problem posed by the use of such conventions can be segregated by
considering a cOfnplete parenthesis notation in which all inlplied paren
theses are included, i.e., in which each operator and its associated operands
are enclosed in parentheses. The analysis of a statelnent in parenthesis
notation can therefore be performed in two steps, a translation to conl
plete parenthesis notation according to the prescribed conventions, fol
lowed by the analysis of the resulting statement. The present discussion
will be limited to expressions in conlplete parenthesis form.
The conlplete parenthesis notation will be referred to as ;~-notation and
the terminology adopted for Y/-notation will be extended analogously.
Thus, z = ([, [, x, +, y, ], x, r,]) is a ;~-formula more conlnlonly
denoted by (x + y) X r. To avoid confusion with the nonnal use of
parentheses (e.g., in enclosing vectors), brackets will be Llsed (as in the
foregoing vector z) to represent the cfJ-notation.
170 M etaprogranls 5.4
The discussion will be limited to a system of unary and binary operations
only (i.e., ()(p) = I or 2) and will again be illustrated by the systelTI of
logical operations. Assuming a vocabulary of the form v = x ffi 1 ffi p c)
([, ]), the rules of composition for :YJ-notation may be formulated as
follows:
1. If rEX (B 1, then v is a singular &-fornlula.
2. If z is any singular formula, if U E p, and if ()(u) = 1, then (D (B (u) 8j
z ffi (]) is a singular :3'-form ula.
3. If Y and z are both singular formulas, if b E p, and if ()(b) = 2,
then (D y ffi (h) 8) z (]) is a singular &-formula.
In particular, ([, y, /\, z, ]) is singular but (y, /\, z) is not; (y) is singular
but ([, .If, ]) is not, and ([, -, y, ]) is singular but (-, y) is not.
:c
p Set of operators.
5 <I)2+(x E p) +-
6 <1>2 +-1<1>3
Legend
The translation frolll &- to 2-notation can be performed with the aid
of one auxiliary file or stack vector. Program 5.6 shows a suitable process
which will correctly translate any singular formula, but which includes no
tests for singularity. It is noteworthy that all left parentheses are simply
ignored. A sinlilar process can, of course, be designed to use only the left
parentheses and to ignore all right parentheses. * Any translation which
tests for singularity clearly must use all parentheses. If the !f- and &
notations enlploy different sets of operator symbols (e.g., /\, V, , and
x, +, r--;), the appropriate translation can easily be incorporated in the
progranl.
Program 5.6. The original staten1cnt is assun1ed to be recorded on a file <1>1'
with partitions Al following each syn1bol and with a null itenl and partition A2 at
* Oettinger (1960) analyzes three types of parenthesis notation: left, r(~ht, and COfll
plete.
5.4 Translation fr0l11 c0l11plete parenthesis 171
the end. File <1>2 receives the resulting.!f-formula in reverse order, i.e., fronl right
to left. The stack file <1>3 receives each operator synlbol as it is read from <1>1 and
transfers thenl one at a time in reverse order (i.e., by a backward read) to <1>2 at
each occurrence of a right parenthesis. A trace of the progranl shows, for
example, that the :3J-formula ([, [, x, V, y, ], !\ ,[, - , z, ], ]) translates correctly
into the ~-formula (:r, y, V, z, - , 1\ ) reversed fronl normal order.
A partial test of singularity can be provided by testing each component
for compatibility with its predecessor, the ordered pair being declared
compatible if and only if it can occur in some singular formula. For
example, an operator may be followed by either a left parenthesis or a
variable, but not by a right parenthesis or another operator. These first
order compatibility constraints can be expressed, in terms of the following
classes: left parenthesis, unary operator, binary operator, variable or
literal, and right parenthesis. These classes will be denoted by [, u, b, l\ and
], or alternatively by 1, 2, 3, 4, and 5. The constraints are sumnlarized in
the nlatrix M of Program 5.7.
The test of singularity provided by the first-order constraints is not
complete, * but can be conlpleted by the following expedient.-j- The
auxiliary file which receives the operators (file <1)3 of Program 5.7) also
receives the left parentheses in their turn. The following tests are then
added:
Program 5.7. The current conlponent .r is read fronl the input file on step 8 and
its class k is deternlined before the repetition of the nlain loop at step 5. Step 5
* The tests provided in conlpilers have frequently been lirnited to essentially this type.
See, for example, Carr (1959).
t This procedure is due to Oettinger (1960).
172 M etaprogralns 5.4
2
s+-O
[ u b v ]
3
<1>3 +- 0, Al 1 2 345
4
j+-2 [1 1 101 0
Mi. u2 10010 First-order
5
k . 0
M b3 10010 compatibility
6
<1> 2+(k<4) +- x
v4 00101 constraints.
7
j+-k ]5 00101
Al
8
x +- <1>1
Al Mlci j accepts k:,>Mki = 1.
9 P +- <1>3
<1>3 Auxiliary stack file.
10 s +- 1 <1>2 Output in se -notation
11 k +- 1 (reversed order).
<1>1 Input in Y'-notation
12 x
(terminated by 0, AI)'
A P Set of operations.
15 P +-1<1>3
b(X) Degree of operator x.
16 i= P : [
s Singularity indicator.
17 k +-4
18 x : ] Legend
19 k +- 5
A
20 ~ X +-1<1>3
21 i x:p
Program 5.8. The resulting jP-fornlula is produced on file (I):! in reverse order.
Each operator encountered is recorded in the auxiliary file together with a preced
ing left parenthesis, and it also causes a right parenthesis to be recorded in the
output (steps 9-11). Each variable encountered is recorded (step 12) in the output
file and initiates a transfer fronl the auxiliary file to the output file which terrni
nates (step 19) only when an operator of degree two is encountered or (step 13)
when the file beconles exhausted. I n the latter event, steps 14 and 15 are executed
as a final check on singularity-exhaustion of the stack file, exhaustion of
the input file, and the first zero value of the suffix dispersion III nlust occur
together.
174 Metaprograms 5.5
rr((I) +-- E
8 i ;{' :p
m Suffix dispersion assuming
9 (D
3
+- [ total weight is unity.
s Singularity indicator.
10 (})3 +-- X
11 cI>2 +- ]
Legend
12 cI>2 +-- X
Al
13 P +--1<1>3
#
m 0
14
15 Al x +-- cD 1
16 s+--l
17 <D 2 +-- P
18 P
-=F
19 (5(p)
REFERENCES
Burks, A. W., D. W. Warren, and J. B. Wright, (1954), "An Analysis of a Logical
Machine Using Parenthesis-free Notation," Mathematical Tables and Other Aids to
COll1putation, vol. VIII, pp. 53-57.
Carr, J. W., III, (1959), "Digital Computer Programming," Chapter 2 of Grabbe, Ramo,
and Wooldridge (Eds.), Handbook 0fAutonlation, Conlputation, and Control, vol. 2,
Wiley, New York.
Lukasiewicz, Jan, (1951), Aristotle's Syllogistic from the Standpoint of Modern Formal
Logic, Clarendon Press, Oxford, England, p. 78.
Oettinger, A. G., (1961), "Automatic Syntactic Analysis and the Pushdown Store,"
Proc. Twe((th SYfnpositlfn in Appl. Math., April 1960, published by American
Mathematical Society.
Exercises 175
EXERCISES
5.1 For each of the follo\ving logical functions, exhibit an equivalent (i) tree,
(ii) .5I'-fornlula, and (iii) Y-fornlula:
(a) ,l(~r, y, z) = :r /\ (y V z).
(b) g(w, x, y, z) = (w V (y z /\ (x V (w = y.
(c) the function of part (b), limiting the operators elnployed to and, or, and nol.
5.2 Let,l(:r, y, z) be a logical function of three variables, let q be an equivalent
formula in :JP-notation, and let r be an equivalent fornlula in YJ-notation. Write
progranls to determine the intrinsic vector i(t') (cf. Sec. 7.2) as a function of
(a) the :1'-formula q.
(b) the 2"-fornlula r.
5.3 Leta=(/\,-,O,V,I,O)
b = ( , 0, V, 1, 0)
c = (/\, V,O, V~ 1,0)
d=(/\, V,q,f, -,I)
e = ( V, q, r, , I)
q = ([, [, q, \/, 1', ], /\ , [, - , I, ], ])
r = ([, q, V, f, ], /\ ,[, - , I, ], ])
Trace the operation of
(a) Progranl 5.4 for each of the cases z = a, Z = b, and z = c.
(b) Program 5.5 for z = d.
(c) Progranl 5.6 for (1\ containing q.
(d) Program 5.7 for (}\ containing q and for q)l containing r.
(e) Program 5.8 for (1\ containing d and for q\ containing e.
5.4 Write a program for translating from :5I'-notation to Y-notation which is
analogous to Program 5.6 except that it ignores right rather than left parentheses.
5.5 Write a program to extend the mininlax transformation of Progranl 5.5 to
the case of an operator set of the form p = pI p2, where pI and p:!. are a set of
symmetric and asymmetric operators, respectively.
5.6 Write a program which extends the nlinimax transfornlation of PrograITI 5.5
to include ordering on the variables and operators so as to bring the fornlula to
unique canonical form, as suggested in Sec. 5.3.
5.7 Write a progranl which will recognize all identical singular subfornlulas
occurring in a singular ,~-fornlula z and which will produce a record of the
associations in some convenient forn1.
chapter 6
SORTING
Copy operations
A serial sorting process is executed as a sequence of copy operations.
A copy operation is defined as follows: all items from a given set of input
files are transferred to a given set of output files, and each item read froIn
any input file must be transferred to some output file before a further itenl
is read from the same input.
~
47,92, 38, 79, 20, 59, 98
I34, 36, 70, I 67, 96, '24
I ~ I47, 92, I 38, 79, I 20, 59, 98, I 34, 36, 70, I 67, 96,I 24, I 15, 76,I 22, 43, 63 I
I15, 76, I 22, 43, 63 I ~ V)
o
~
~.
I I
7
~
15,34,36,47,70,76,92
I 38, 43, 63 67, 79, 96' I15, 34, 36, 47, 70, 76, 92, I 22, 38, 43, 63, 67, 79, 96, I 20, 24, 59, 98 I
122,
j
20, 24, 59, 98
CD')
?'
~
6.1 Serial sorting nlethods 181
Two-phase merge. The (m + 1) files used will be labeled <1>11, <1>21, ... , <l> m l ,
and <1\2, the last serving as initial input and as input during each classi
fication phase. Program 6.4 describes the entire merge.
Program 6.4. The merge phase is shown on the left and the classification phase
on the right. The heart of the former is the loop 7-17. Its operation is controlled
by the logical vectors v and z (of dimension nl), which specify the set of exhausted
files and the set of ineligible items, respectively. An eligible iten1 is one which has
been read to one of the nl item positions Pi, is not a dumn1y, (i.e., null), and
possesses a key which equals or exceeds r, the key of the item last recorded.
The merge phase terminates on step 7, when all files are exhausted. Step 8
initializes the vector z (ineligible item positions) to the value v (exhausted files),
and step 9 increments the output string count s. Each execution of the subloop
10-14 records on cD 1 2 the item Pj having the smallest key of all eligible items.
Steps 10-12 select and record the item Pi and preserve its key as the variable r.
Step 13 reads the next item fron1 file j. If this exhausts the file, the branch to
step 15 is followed. Step 15 adds j to the set of exhausted files and step 16 adds it
(since the new item Pi is a final dummy) to the set of ineligible items. Step 17
then repeats the subloop if any eligible items remain and otherwise re-enters the
major loop at step 7. If the files are all exhausted, step 7 branches to step 18 to
begin the classification. If not, the production of a new maxin1al string is begun
on step 8.
If step 13 does not exhaust the file, it is followed by the decision on step 14,
which repeats the subIoop if r does not exceed the new key, and otherwise adds}
to the set of ineligible items on step 16.
The necessary initialization is perforn1ed by steps 3-6. Step 3 rewinds all input
and output files. Steps 4 and 5 perform the initial read from each unexhausted
file to the corresponding item position and respecify v to indicate any file which
may be exhausted by the initial read itself. The vector v is itself specified external
to the process, so that the initial set of input files may be restricted at will. On
subsequent repetitions of the merge, all files are n1ade available by step 2.
The classification phase begins by terminating the output file with a dun1n1Y
item and final partition A2 , and rewinding it. The /11 input files are also rewound
to serve as output files in the subsequent Ill-way classification, and the process is
terminated by step 20 if the output string count is equal to one. Step 21 redefines s
as the maximum number of strings to be allotted to the output files in turn, and
step 22 reads the first item from the input file (I\~.
The output files are selected in ascending order by the index i. The variable j,
which counts the strings, is initialized on step 25 and decren1ented on step 30 each
time a break is indicated by the preceding comparison on step 29. When s strings
182 Sorting 6.1
eD 1 +- O , A2
2 v+-O eD 1 2 +- 0, A2 18
3 17(<1)) +- f 17(eD) +- f 19
4 vip, vjq +- vl<D 1 S 1 20
5 v +- v V (q = A2 ) S +- rs -:- ml 21
s -(-0 p <D 1 2 22
6 +-
7 v i -(- 0 23
8 Z -(- v i +- i +1 24
9 s ~- s +1 j +- s 25
10 j +- ((zlk(p))/Llh r +- k(p) 26
11 r -(- k(pj) ([)i l +- P 27
A2
12 <D 1 2
+- Pj P +- (1)1 2 28
A2 :S;
13 Pj +- (D/ r k(p) 29
:S; -F
14 r k(pj) j -(-j - 1 30
>
15 vj -(-- 1
16 Zj +- 1
I-origin indexing
17 Z
-:I
(I> i] Input to merge; output of
classification, i E L](nl).
(1)1 2 Output of merge ~ input to
classification.
P Item positions
File partitions J) = 111.
q
Legend
v Exhausted files
Z Ineligible items
s String count.
y Key of last recorded item.
have been recorded, the branch from step 30 to step 24 is followed to repeat the
process for the next output file.
The classification phase is terminated by the occurrence of the partition A2 on
step 28. Step 1 then records a dummy and a final partition on each output tile
(including any which may have received no items), and step 2 resets the vector v
to zero.
The merge phase with which Program 6.4 begins is essential, even though
the original data are contained in a single file, since it also serves to
determine the string count s needed in the subsequent classification. The
need for the string count can be avoided by using a classification process
which merely assigns successive strings to successive files in cyclic sequence.
This process does not satisfy the definition of a simple classification, and
it will be given the name string classification. String classification is
frequently more convenient to use than silTIple classification, particularly
in processes such as the single-phase merge to be described next. Two
successive strings assigned to a given file in string classification can coa
lesce to form a single string, but the probability of such an occurrence is
small, especially in later stages.
Single-phase merge. The two phases of the merge sort can be coalesced in
a single revision operation employing nl input and r1'1 output files. The
two rows of files (1)1 and <D 2 serve alternately as input and output on
successive stages.
Program 6.5. The Dlain subprocess (15-22) differs fronl the corresponding seg
ment (10-17) of Progranl 6.4 only in the control of file selection, the alternation
of input and output being controlled by the alternator G, which alternates between
1 and 2 (step 2) on successive stages. The classification is controlled by the variable
i, which selects the particular output file on step 17 and which is itself cycled
through the integers I to /11 by step 14. When all files are exhausted, the branch
to step 2 is followed, resetting the vector v to zero, ternlinating the output files,
and rewinding all files. The final output is contained in file <1\([.
Elimination ojjile rewind. Each of the sorting processes described req uires
a rewind of all files between successive stages. If the files enlployed are
capable of backward read, the processes can be ITIodified so as to elinlinate
the need for rewind. Since each file is alternately read and recorded in
successive stages, each will always be read in a fixed direction and recorded
in the other. Since the space needed for recording is not known in advance,
it is necessary to do all recording in the forward direction and therefore
to read in the backward direction. The changes required will be illustrated
by modifying the single-phase merge of Program 6.5.
Program 6.6. Since the alternate forward record and backward read effectively
reverse the order of all files on successive stages, alternate stages nlust assenlble
184 Sorting 6.1
(a, s) +- (1, 0)
2 a+-2 11 (a + 1)
3 V+-O I-origin indexing
12 Z +- v Legend
13 s +- s +1
14 i +- m 11 (i + 1)
15 j +- (zlk(p))j l l)l
16 r +- kePi)
17 <1>~-a +- Pj
A2
18 Pi +- <1>ja
~
19 r : k(pj)
>
20 Vj +- 1
21 Zj +- 1
22 Z : E
=F
2 7T(eD) +- E
I-origin indexing
3 v+-O
4 c : 1
eD l Input to odd
5 c +- 1
stage
eD 2 Output of odd
6 stage
7 Item positions v = m.
P
i= q Partitions
8 s : a-I
v Exhausted files
9
Z Ineligible items
10
s String count.
11 r Key of last recorded item.
12 a Alternator (1, 2).
13 vip, vlq +- vi ceDa c Direction of read.
14 v +- v V (q = A2E)
Legend
15 i+-O
16 v : E
17 Z +- v
18 s +- s +1
19 i+-ml l (i+l)
21 r +- k(pj)
22 eD~-a +- Pj
).,A 2
23 Pi +- ceD ja
~
24 (3 - 2a)r (3 - 2a)k(pj)
>
25 V j +- 1
26 Zj +- 1
27 Z : E =f:.
j +-q
2 1T(<D) +- E O-origin indexing
j +-j - 1 <
3 ~
<DO Input to classification; output
P <D oo
Al 0
4 +- of merge.
(b(q)) l-d k(p); b <1>1 Output of classification; input
5
+-
to merge (v(<D 1 ) = b).
6
<D~.3 +- P
d Positional representation of
7
<D 1 +- O , A1 key (v(d) = q).
13 < i : b
14 <D oo +- 0, Al
Program 6.7. Steps 1-7 constitute the classification and steps 8-14 the n1erge
phase. File <Do 0 is the single input to the classification and the single output of the
simple merge. The remaining b files are denoted by <D /, i E l O(b).
The component of d which controls the current classification (step 6) is selected
by j, which scans d from right to left. In the (implicit) specification of d on step 5,
b serves as an auxiliary variable (cf. Sec. 1.19). When the classification phase ends
with the exhaustion of <1>0 0 (step 4), the output files are terminated, all files are
rewound, and the simple merge is performed (steps 10-13) to re-collect on <D oo the
results of the preceding classification. This file is then terminated by step 14, and
the entire process is repeated from step 2 for the next lower value of j. The
dimension of d is specified (by compatibility with e(q) on step 5) as q, and q is,
of course, specified externally.
188 Sorting 6.1
Program 6.8. The alternator a again determines the roles of the two sets of files
(input or output) on successive stages, but because of the use of O-origin indexing,
it alternates between the values 0 and 1. The classification process (steps 7-11)
differs from that of Program 6.7 only in the control exercised by the alternator a,
and in the specification of a sequence of input files <1>oa, <1>l a, , <1>~_I instead of
the single file <1>0.
j +- q
O-origin indexing
2 a+-l
3
7T(<1 +- f <1>0 Input to odd stage }
j+-j-l < <1>1 Output of odd stage v = b.
4
d Positional representation of
5
a +- ii
key (v(d) = q).
6
i+-O b Base of representation.
Al
7
P +- <1>i a j Stage = q - j.
(bE(q)) ~d k(p); b i Current input file.
8
+-
a Alternator (0, 1).
9 <1> ii +-p
dj
10 i+-i+l
Legend
11 < i : b
The program is deficient in two respects: it requires that the original input be
on a full set of b files, and it produces a final output on b files rather than one.
The first defect may be remedied by the use of an externally specified logical
vector v to designate the input files which are unused in the original stage. The
second may be remedied by a final simple merge following the qth stage. This
could be added as a separate program, but it can be effected more simply by
replacing q with q + 1 to ensure that the high order digit do is identically zero and
so restricts the final output to a single file.
c= CO @ c i @ ... Cl/(b) 1
Output Files
Pass
Step Input File Fraction
Remaining from previous steps Copied
Total 2.8
The first step copies each item whosejth key digit is (0, 2,4,7) to file <1\, itenls
(1, 5, 6) to file (1)2, and items (3, 8, 9) to file (T)~3. After each step, only the previous
input file and the next input file are rewound, in this case <I>o and <I>1. In step two,
itenls (2,7) are copied to (1)2, and therefore.follow the group (1, 5, 6), as indicated
by the separate parentheses. Sinlilarly, items (4) and (0) are copied to (1)3 and <Do,
respectively.
Step three is preceded by a rewind of (1)2 and (1\. Since items (I) and (2) occur
in separate groups in the new input file $2' they can be copied to <1)0 in separate
groups (I) (2) as indicated. Similar remarks apply to items (6) and (7) copied to
(}\. The three subsequent steps complete the required ordering, producing in the
original input file (1)0 the ordered array (0) (1) ... (9). The rightnl0st colunln
shows that fraction of the original file (assuming a unifornl distribution of the key
digits) copied on each pass. The total at the bottonl indicates that the expected
execution tinle is equivalent to 2.8 full passes.
The partial pass process of Table 6.9 is described more concisely by the
O-origin matrix M of Table 6.10. Element M/ specifies the file to which
6.1 Serial sorting I1zethods 193
Digit 0 1 0
Digit 2 0
Digit 2 1 2 0
Digit 3 3 0
Digit 4 1 3 0
Digit 5 2 3 0
Digit 6 2 1 0
Digit 7 1 2 1 0
Digit 8 3 1 0
Digit 9 3 2 0
Input 0 1 2 3 1 2
Table 6.10 Matrix M describing the partial pass COIUlllll sort of Table 6.9
items with key digit s are to be assigned in the rth step of the process, for
r E lO(6) and s E lO(10). The eleventh and last row of M specifies the input
files, i.e., M/l is the index of the input file in the rth step. An algorithlll
based upon the matrix M is described by Program 6.11.
Program 6.11. The subloop 14-18 perfornls the classifIcation according to the
element M/, using file i (specified by M/) on step 9) as input. When the file is
exhausted, the branch to step 6 increnlents r and repeats the process unless
r = v(M). Equality indicates conlpletion of the jth colullln and causes a branch
to step 4 to decrenlent j and reset r. The conlparison on j is deferred to line 13
so as to follow the ternlination and rewind of the new input file. The branch on
step 10 prevents the recording of a dunlnlY itenl on the original input file. The
previous input file is rewound by step 8.
Program 6.11 includes the two-phase column sort as a special case, for
if (b + 1) files <Do, <D 1 , , <Db are available, the nlatrix shown on p. 194
specifies a process essentially identical with that of Progranl 6.7. File (1)/1
corresponds to file <D oo, the first column of M deternlines the b-way
classification, and each of the b succeeding colunlns specifies the copying
of one file in the b-way simple nlerge onto file <1)/).
The method of partial passes is frequently used in the sorting of alpha
betic data. Variants include the 1-} pass-per-character nlethod of sorting
194 Sorting 6.1
1 h
2 b
I
M== .
~ 0
j +-q
O-origin indexing
2 1+-0
3 7T(D) +- <D i Files (i = 0 for initial data).
M As in Table 6.10.
4 j +-j - 1
d Positional representation of
5 I' +- - 1
key (r(d) = q).
8 7TD i ) -+- 0
i Current input file.
9 1 +- M/)
10 j - r q - 1 Legend
11 <[); +- C), Al
12 7T( <I);) +- 0
13 j 0 <
Al
14 P +- (1) i
15 ~d +- kCp); b
(b(q))
16 s +- dj
17 k +- M/i
18 <!)k +- P
Step I 2
4 (merge) I 5 1
6 (merge) I 7 1 8 (merge) 1
9 _ _1 10 III (merge) ~
<Do 00 000-002 000-002,010 000-012 00-012,020 000-022 000-022, 10 000-022* 000-102 g-.
<1>1 000 OIl 021 11 11,100 11* ~
<D"
<D;
1
2
1,01
2,02
1,01,001
2,02,002
1,01 *
2,02*
I*
2,02,012
1
2,02*
1,022
2*
1*
2 2, 12
101
2, 12, 102 2, 12*
h (0,0,0) (0,0,0) (0,0,0) (0, 1,0) (0, 1,0) (0,2,0) (0,2,0) (1,0,0) (1,0,0) (1,0,0) (I, 1,0)
I I 2 1 3 2
j
s
(0,0,0)
1
(0,1,0)
2
(0, 1,0)
I 2
(0,1,2)
1 2
(0,1,3)
1
(0,2,3)
2
(0,2,0)
2
1
<Do 000-102, 110 000-112 000-1 12, 120 000-122 000-122,20 000-122* 000-202 000-202,210 000-212 000-212,220 000-222
$1 * 121 * 21 21,200 21* * 221 *
<1>2 111 * 122 * 22 22, 201 22* 22,21 I 22* *
<1>3 2,12,112 2,12* 2* 2 * 202 * 212 * 222 *
h (1, 1,0) (l, 2,0) (1,2,0) (2,0,0) (2,0,0) (2,0,0) (2, 1,0) (2, 1,0) (2,2,0) (2,2,0) (0,0,0)
i 1 2 3 3 3 I 3 2
j
s
2
(0, 2, 1)
1 2
(0,2,3)
1
(0,3,3)
2
(0,3,0)
1 2
(0,3,1)
1 2
(0,3,2)
-I
Legend
1T(<I +- e(b)
2 [+-0
~l
3 h +- (q) [:0 20
4 S +- (q) [+-1 21
5 i+-O 1T(<I>O) +- 0 22
6 j+-O P +-l<1>i 23
7 i +- (eijlO(b))h j ) : q - 1 24
8 j+-j+l k +-(c o = 0) 25
Al
9 Sj +- i
P +-1cI> Ck 26
10 c +- eijlO(b)
<1>0 +- P 27
11 k +- (j = q - 1) 1\ (co = 0)
P +-l<D Ck 28
12 <l>Ck +- 0, Al k+-k+l 29
<D Ck +- 0, Ao < k : b 30
13
k+-k+l )+-)-1 < 31
14
< k : b hj b 10 (h j + 1) 32
15 +-
Al
16 P +- f<l>i i +- Sj 33
17 (bE(q)) ~ d +- k(p) ; b <1>0 +- 0, Al 34
18 k +-d j 1T(<I>O) +- 0 35
19 <l>Ck +- P
to the preceding stage deternlines the vector .ijlO(h) of output indices, fronl which
the new input index is selected as the h}h conlponent. The succes~ive values of h,
i, j, S, and c listed in the example of Table 6.12 I11ay be helpful in tracing the
operation of the prograI1l.
When classification on the last digit is conlpleted, the last subblocks are nlerged
(steps 25-30) onto the output file (1)0. The variable j nlust then be decrenlented
and the component h j incremented I1lodulo b (steps 31-32). When h j conlpletes
a cycle (becomes zero), the corresponding subblocks are exhausted and j nlust be
decrenlented repeatedly until the corresponding h j does not beconle zero. The
nlain process through increasing values ofj is then repeated.
Although h j deternlines the input subblock for the classification on digit j, it
deternlines the index of the input file only indirectly (step 7) through the vector
of output files, itself determined by the input file i used in the classification on the
preceding digit. When j is increasing, the value of i is sinlply the value fronl the
preceding stage. However, when j is decrenlented (steps 31-32), it is necessary to
deternline the input i used in an earlier classification. A record of the value of i
corresponding to each classificationj is therefore kept (step 9) in the vector s, and
is used to redefine i on step 33.
Each file to be used as output (except possibly file (1)0) is first closed with a
partition (steps] 2-] 5) to demark the beginning of each of the subblocks to be
recorded. Since back\\-'ard read is to be used, two dunlnlY itenls are provided so
that the branch on Al coincides with the read of a dunlnlY itenl. An extra read
then disposes of the extra dunlnlY. Any partition recorded on the final output
file (1)0 before a fully classified subblock (when j = q - ]) would renlain in the
final output. Step]] prevents this by initializing the index k to ] ifj = q - ] and
the index of the first output file (that is, co) is zero. In a sinlilar manner, step 25
prevents the attenlpt to copy file (1)0 to itself during the nlerge phase (steps 25-30).
Step 28 reads the extra dumnly item recorded in the partitioning operation of
steps ]2-]5.
All files are read backward except the initial input in the first stage. This
behavior is controlled by the logical variable.r (steps] 6 and 20-2]). The branch
on step 20 fails the first tinle only, allowing/'to be respecified as 1 and rewinding
file <1>0. On subsequent stages, the branch to step 23 causes the read of the extra
dunlnlY partition.
Cascade sort. * The cascade sort is a partial pass merge sort, with each
stage proceeding as follows. The strings are initially distributed (unequally)
among In of the 111 + 1 available files; an m-way merge to the empty tile is
perfonned until sonle input is exhausted; an In - 1 way merge to the
newly emptied file is then performed from the relnaining inputs, and so on
to a final two-way merge. The effectiveness of the process depends on a
particular initial distribution of the input strings.
Table 6.14 illustrates the process for 190 strings distributed anlongfive
of six available files as shown in the first row. Succeeding rows show the
* Due to Betz and Carter (1959). See also Gilstad (1960) and Carter (1962).
6.2 Eraluarion (~l serial sorting Incthods 199
0 IS 29 41 50 55
1 5 9 12 14 15
2 1 2 3 4 5
3 1 1 1 1 I
4 0 0 0 0 I
Three Inajor factors enter the eval uation of a serial sorting process:
the amount of program storage required~ the nun1ber of serial-access flies
used, and the execution time. The first two factors require little analysis~
and attention will be limited prin1arily to the third.
Because the execution tinle of a serial sorting process is nOrInally
determined almost cOITIpletely by the tilTIe required to transfer inforInation
to and from the serial files~ the execution time is assun1ed to be directly
proportional to the number of passes of the files required. Each phase
corresponds to a pass or (as in partial pass Inethods) to SOITIe fraction of a
pass, and the number of passes per stage is detern1ined by sun1n1ation over
the component phases.
The constant of proportionality relating actual execution tin1e and
number of passes depends on such factors as the average length of the
items, the reading and recording rate of the serial files~ and (in processes
* The jth column of the table refers not to a specific file but to that tIle which
ranks jth in nunlber of strings.
200 Sorting 6.2
requiring rewinding after each phase) the rewinding speed. Since these
factors are specific to particular eq uipn1ent and particular tasks, and since
the nature of the dependence is obvious, the present analysis is lin1ited to
consideration of the nun1ber of passes.
Consideration is also given to related orderings (cf. vector keys and
categories), which may, in the use of colurnn sorting, be achieved more
efficiently jointly than separately.
r == pogm sol,
for which the approximation r -~ log;n So will be used. The number of
passes is then 2r for a two-phase merge and r for a single-phase merge. If
the "cost" of the process is assumed to be of the form
t2 2 logm 2 S 2 log(2k-I) S
tI logml s logk S
2 logs k 2 10gIo k
logs (2k - I) loglo (2k - 1) log('(2k - 1)
This ratio increases monotonically fron1 1.26 at k == 2 to an asymptotic
value of two. Since it exceeds unity, the single-phase process is to be
preferred.
* An odd number n would prejudice the result against the single-phase method, since
one file would necessarily be left idle.
6.2 Era/ltation oj'seria/ sorting nlcthods 201
s, it is desirable to determine the relation between 17 and the expected value
of s for a random distribution of the keys.
Let k be a vector of dimension 17 + 1 whose first n conlponents are the
successive keys of the file and whose last cOIllponent is infinite, and let
f = (k < lk), d = (k = lk), and b = (k ~.~ lk).
If, for example,
k=793558214x,
then
lk = x 7 9 3 5 5 8 2 1 4
f= 1010001100
d= 0000100000
b= 0101010011.
Since the ordering on x can be performed before rather than after the
ordering on y, the roles of p and q may also be reversed, and the case
showing the larger reduction may be chosen. If three or Inore orderings
are prescribed~ the foregoing method may be applied to evaluate each of
the possible sequences of ordering.
Internal sorting normally enjoys a much higher basic execution rate than
does serial sorting, but for large volulne files the limited size of internal
storage nlay make serial sorting necessary. The anlount of serial sorting
may, however, be reduced by some use of internal sorting. For example, a
preliminary internal sort can produce maximal strings whose lengths are
limited only by the size of the internal store and thus reduce the number of
strings presented for subsequent serial merge sorting.
The present section is devoted to methods of reducing serial sorting by
the auxiliary use of internal sorting. For this discussion, the only knowl
edge assumed concerning the internal sorting process is its capacity to
order a specified number of items.
Two classes of processes arise, one for aiding merge sorting and one for
aiding colUInn sorting. The aid to Inerge sorting is the simpler, since it
consists merely in assembling long strings by internal sorting before
beginning the serial merge sort. A serial column sort, on the other hand,
may be aided by a final internal sort performed after the column sort.
If k = k(x) is a key vector associated with x, and if In is any positive
integer, then a serial column sort performed on the key vector
k l = lk -:- n1J
produces the vector XO CB Xl x P , where the vector Xi contains all
items such that k/ = i. If the infix vectors Xi are then copied in turn from
the file, individually reordered on the key
k 2 = (nl)!ok
and recorded, the resulting file will be ordered on k. Table 6.15 shows an
exanlple for m = 4.
If internal storage allows the internal ordering of as ITIany as n iteIns,
then the reordering of the infixes Xi can be accoInplished by an internal
sort provided that m is so chosen that v(x ::::~ n for all i. If the original
1
)
Xl 7
x2 9 2 1
1 3 x3 2 0
x7 3 0
2 , XO ( x3
x7
2 0 2
x3 2 0 2 7
3
1 3
j l x6
3 0 3
1 0
Xl 4
x4 11 2 3
Xs 6 1 2
Xs
x6
6
4
1 2
1 0 I) Xl
{ Xs
Xl
6
7
1
J
2
3
x6 4 1 0 x2 9 2 1 I x2
( x2 9 2 1
x7 3 0 3 x4 11 2 3 j l x.t 11 2 3
the matrices a X correspond to the files <D i a of the serial process, each item
being represented by a single matrix element. Items are read sequentially
from and to fields; for each input field Xi, an index r j indicates the
I(
elenlent axr ) to be read next; for each output field (3- a )Xi, an index 5 i
indicates th~ element next to be specified. The items in each field occupy
the leading positions in the field, and the index of the first unoccupied
element in the input field a Xj is indicated by the parameter t j The process
is described by Program 6.16.
Program 6.16. As in Program 6.5, the vectors v and z specify exhausted fields
(files) and ineligible fields (positions), respectively. The paranleters a and tare
initially specified external to the process. At the beginning of each stage the
vector v (exhausted fields) is specified (step 3) according to the unit conlponents
of t. At each stage except the first, t is respecified (step 2) by the final value of s
from the previous stage. When only one string renlains, each conlponent of s,
save the first, will remain at its initial value of unity, and this condition is used to
terminate the process at step 1. The vector k represents the keys of the current
itenls in the 111 input fields and is initiaJ)y specified (step 7) by the keys of the
colunln vector aXI of initial items of the input fields. The renlainder of the
process is closely analogous to Progranl 6.5.
al/S
2 t-+-s
I-origin indexing
3 v -+- (t = )
aXkh I tern k of input field
4 s -+-
h. !i = lJl.
current input indices, and the suffix wmjb defines the ternlinating
locations t.
The strings produced by the merge from the sets of 111axinlal strings in
x a can be transferred without classification to the single output field
X 3 - a If the vector b is redefined by the beginning locations in the output
field of successive sets of rs' --:-
n11 items, it can be used to define the input
fields in a subsequent merge from the field X:l-a. Progranl 6.17 shows a
convenient arrangement of the process.
2 I-origin indexing
3
Input field. "
4
\v = n
Output field. f
5 b i+ I +- U Beginning of }
6
subfield d in v = 111 + 1
output.
7 r +- aftljb
I ndex of current in
8 t +- wmjb put for subfield d.
Index of first loca
9
tion following sub
10 g +- (V/lI)'jl field d. V= In
18 h -(- s 1
0 (/1 + 1)
j Index of current input sub
field.
19 i -(- i + 1
n Total number of iten1s.
20 bi -0(-- 11
v Legend
21 Z +-
Program 6.17 Internal single-phase
22
merge using two fields
23 r +- kj
24
25 rj +- ~j + 1
26 1I+-11+1
27
28
>
30 vj +- 1
31 Zj +- 1
32 Z 208
6.4 Internal sorting rnethod~,' 209
Program 6.17. As in Progranl 6.16, the vector r determines the current set of
items being examined, and t determines the terminal value of r. The first II/
components of b initialize r (step 7), and the last 111 components initialize t
(step 8). The index u determines the component of the output vector X3~a, which
is next to be specified (step 24).
The remainder of the process differs fronl Progranl 6.16 primarily in the deter
Inination of b. The counter Iz allows i and b i to be respecified (steps 19-20) at
the end of each group of s strings. The last specified component of b is deter
mined separately by step 5. Step 6 redeternlines v for the next stage.
Since some of the initial values so' vo, and bO may be unknown, the
initialization of the process (steps 1-3) merits some attention. If So alone is
known, VO and bO may be chosen as follows: VO == (il; b I O = 1, b 20 = veX).
The effect is to perform the first merge from a single input area. Con
sequently, the first stage performs no rearrangement but does determine
the vectors v and b.
If So is unknown, S may be determined by a preliminary string count.
Alternatively, it may be set to any integer val ue y =2 So. The process
remains valid, but the required number of stages is increased by [Iogm yl
pogm Sol
Since So cannot exceed the number of items, the initial value s = veX) is
always valid, and for an assumed random distribution it exceeds the
expected value by a factor of two only. If greater initial order is expected.
it may be desirable to modify Program 6.17 to allow a small initial choice
of s, accompanied by automatic respecification in the event that it proves
too small. The modification may be simply made by inserting a branch on
eq uality of i and m, following step 18. The branch would succeed only in
the event that the initial specification of s were too small and should lead
to a process for increasing (e.g., doubling) s and repeating the process.
The case m = 2 is of especial interest, partly because it leads to signi
ficant simplification in the storage allocation, and partly because the
advantages which large values of m enjoy in serial sorting largely disappear
in internal sorting. These advantages arise fronl the reduction in the
number of stages with increasing m, with a nearly constant time per stage
due to the fixed reading and recording time for the serial files. In internal
sorting, the time required for key comparisons and address calculations in
choosing the minimum key becomes relatively more inlportant and, since
the key comparison time is an increasing function (frequently linear) of nl.
the advantage may lie with small values of m.
The sinlplification of storage allocation arises in the following way. A
two-way string classification on n items may be used in conjunction \vith a
single output field with a total capacity of n items by assigning the odd
numbered strings from the beginning of the field in normal order and the
210 Sorting 6.4
S2 : veX)
I-origin indexing
2 a+-2 II (a + 1)
4 S +- r
xa Input field.
5 i +- 1 X3-a Output field.
6 r +--00 rk Index of kth input sub
7
> r1 : r2
field, k E (1, 2).
Sk Index of output subfield,
8 .i +- 1 + (k(Xr1a) > k( X r2 a)) k E (1, 2).
S
9 r : k(Xria) j Index of current input
subfield.
10 j +- 2 11 (j + 1)
S i Jndex of current output
11 r : k(Xrja) subfield .
12 .i ~- 2 11 (j + 1) r Key of last recorded
itenl.
13 i +-21 1 (i + 1)
a Alternator (1, 2).
14 r +- k(Xria)
15 x:3-a +- X a Legend
S i ri
16 r +-r + Qj
17 S +- S + Qi
even-numbered strings from the end of the field in reverse order. Thus if
sj is the jth string of 2k strings so classified, the output field would contain
the array
Sl EB S3 EB S5 EB ... EB S2k-l EB S2k EB ... EB S6 EB S4 EB S2,
-----------~ -0(----------
where the arrows indicate the increasing directions of the associated
strings. The restriction to an even number of strings in the foregoing
example is clearly not essential. The corresponding two-way internal
single-phase merge is described by Program 6.18.
Program 6.18. Since the current index vectors r (for input) and S (for output)
may always be initialized as shown in steps 3 and 4, and since termination of a
phase occurs when r 1 exceeds r 2 (step 7), explicit use of the vectors band t of
Program 6.17 is obviated. The only added complication lies in the different
treatnlent of indices r 1 and Sl' which must be increnlenfed whenever used, and of
6.4 Internal sorting rnethods 211
indices r 2 and S2' \vhich nlust be decrenzenfed. This treatnlent is effected by
addition (steps 16-17) of the rows Qi and Qi of the nlatrix
Program 6.19. O-origin indexing is used, and the vectors XO and X(( (for a = 0
or I) serve as input and output fields. The classification on the key digit d i is
performed so that all itenls which agree in thejth colunln of the key fornl an infix
in the output X((, and so that the value of d j associated with successive infixes
increases monotonically. The output indexing is deternlined by the vector s,
which is, in turn, initialized by the vector b. The value of b for the succeeding
stage is deternlined by steps 13 and 14 *, according to the value of the next higher
key digit dj~l. The initial value of b is assunled to be defined externally. It I1lust
Radix exchange. Radix exchange is a form of the repeated block sort for a
base two key. The high-order column of the key is scanned first. The fIrst
zero itenl (item with key digit zero) is exchanged with the last unit itenl, the
second zero item is exchanged with the second last unit itelTI, and so on,
until the first stage of the block sort is completed. The zero itenls no\\'
form a prefix in the vector of iteITIS, and the unit itenls fornl a suffix. The
process is then repeated on the next colUlnn of the key, first on the prefIX
obtained from the first stage, and then independently on the suffix. Each
214 Sorting 6.4
column of the key is treated in turn, the exchange process being performed
independently on each of the disjoint infix vectors defined by the preceding
stage. O-origin indexing will be used throughout the discussion.
Primary control of the process is exercised by the vector b, whose
successive components specify the beginning index of successive subblocks
(infixes). In particular, bo = 0, and bl'~l = vex), and at the beginning of
the kth stage, v(b) = 21\'~1 + 1. The storage required for b is therefore
significant but may be reduced by either of two expedients. The vector b
may be replaced by a logical vector u such that U/lo = b. Determination
of the next component of b then requires a scan of the components of u.
The use of U is thus normally unattractive except in a computer equipped
for the efficient determination of the location of the leading nonzero
component of a logical vector. The second expedient leads to a more
complex but more practicable process. The straightforward use of b will
be treated first.
Program 6.20. Steps 10~21 perfornl the exchange on the subblock with indices
k in the interval b i :::; k ,,;: b if1 . The indices Yo and Y1 designate the pair last
exchanged, k is the index of the current item exanlined,.i is the current colunln
of the key, and a is an alternator which is zero during the forward scan of the zero
section and unity during the backward scan of the unit section. The alternator a
determines which of the indices Yo and Y 1 will initialize k (step 13), the direction
of the scan (step 14), the type of key digit (0 or 1) sought in the next itenl to be
exchanged (step 18), and which of the indices Yo and Y 1 is to be redefined by k
(step 19) when the search ternlinates through failure of the branch at step 18.
If a does not beconle zero from negation on step 20, the process is repeated fronl
step 13 with a = 1, producing a backward search for a unit digit in the key. If a
beconles zero, both the forward and backward scans have been conlpleted, and
the required item exchange is perfornled on step 21. The final exit fronl the entire
loop 13~21 occurs when k = Y ii , that is, when k = Yo on a backward scan or
k = Y 1 on a forward scan. In either event, the final value of k is the beginning
index of the new subblock defined by the exchange, and it is used inlnlediately to
specify C I: on step 16. The vector c is eventually meshed with b (step 6) to re
specify b as (b o, co, b 1 , c 1 , . . . ). The initial specification of b on step 1 ensures
that the first subblock treated is the entire vector x.
The number of subblocks which must be distinguished at anyone titne
can be reduced to the dimension of the key by a method due to Hilde
brandt and Isbitz (1959). The process is controlled by a partition vector p
of dimension q + 1, whose successive nonzero components specify the
beginning indices of the subblocks of x remaining to be ordered. At each
stage, the first remaining subblock is exchanged on the appropriate key
digit d j , i.e., for j increased by one over the value used in generating the
particular subblock. When the exchange occurs for j = q - I, the order
ing of the two leading subblocks is complete, and they are rernoved froln
6.4 Internal sorting Inethods 215
b +- (0, vex))
2
j+-O
O-origin indexing
3 j +-j + 1
x Vector to be ordered.
4
j : q
bi Beginning index of sub
5 v +- (2e) 10 LO(2 J + 1)
block i,
6
b +- \b, v, c\
v(b) = 1 + 2j
ei Beginning index of ith new
7
i +- -1
subblock,
8
i--i+l vee) = 2).
v ~esh vector
9 bi vex)
(0, 1, 0, 1, ... , 0),
10 r o -- b i - 1 l{V) = 2)+1 + 1.
r1 b i+ 1 d Base 2 representation of
11 +-
key
12 a -- 0 (ll(d) = q).
13 k +- r a
j Current column of d.
k ~- k - 2a + 1
r Indices of last pair
14
exchanged
15 k : r(l
(v(r) = 2).
16 ci +- k
a Alternator for scan of zeros
or ones.
17 (2e(q l-d +- k(x k )
18 d j : a
Legend
19 ra +- k
=F
20 a +- ii
21 x To --) x
r1
0 P +- e(q + 1)
k +- vex) O-origin indexing
2 j+-O
x Vector to be ordered.
3 j+-j-l
P Partition vector determin
4 Po +- PI ing beginning of subblocks
5 j+-j-l
< (v(p) = q + 1).
d Base two representation of
6 Pq-j : 0
key (v(d) = q).
7 k +- Pq_j j Current column of d.
8 Pq_j +- 0 r Indices of last pair ex
changed (v(r) = 2).
9 j : q - 1
a Alternator for scan of
10 j +-j +1 zeros or ones.
11 r o +- Po - 1
12 Pq_j +- k Legend
13 rl +- k
14 a +-0
15 k +-r a
16 k +- k - 2a +1
17 k : r{i
18 (2e(q))l-d +- k(x,,)
19 dj : a
20 ra +- k
21 a +- ii
22 x ro ~ x r1
prefix removal phase) to Pw-I and resetting P/(j to zero. Incidentally, this
procedure ensures that P It and Pv always occur as PI and P2 In practice,
these two components need not be reset if the scan for Pw is begun with P3'
Program 6.21. The reader nlay find it helpful to trace the progranl (i.e., record
successive values of all paralneters in tabular fornl) for a sinlple case of a few keys
6.4 Internal sorting rnethoc!\' 217
of snlall dinlension. The exchange phase (15-22) is identical with that of Prograrl1
6.20 except that the specification of C i is onlitted. If j ,- q - I at the conclusion
of the exchange, j is incremented (step 10), r o is reset to Po - 1, and the ne\\'
partition k specifies both P(l-J and r 1 . If) = q - I, the prefix renl0val is executed
by steps 4-8. Step 4 respecifies Po, and the scan of steps 5-6 (which begins with
P3) locates a nonzero component PQ-J which is advanced to P'l~ (J; 1) (indirectly by
steps 7, 10, and 12) and is reset to zero by step 8. Steps 0- I provide the initial
specification of Po and (via step 12) of Prj'
Bubble sort. The basic operation of the bubble sort is the conlparison and
possible transposition of a pair of adjacent itenls so as to place the smaller
of the two keys earlier in the sequence. The first stage consists of such
operations performed in sequence on the item pairs (X t , l' X,.), (X" ~,X'I I)'
I
Original Original Stage Stage Stage Stage Final I
Xl 8 3 3 3 3 x5
x:! 6 8 4 I- -4 4 x6 I
-
x3 II 6 8 6 6 x~
x4 9 11 6 8 8
x5 3 9 1I 9 9
Xl
x4 I
x6 4 4 9 11 11 x:3
I I
i ~- 0
2 t 0 I-origin indexing
3 t+-O
x Vector to be ordered.
4 i+-i+1
i Stage number.
5 j +- vex)
t Termination indicator.
6 j i
7 j+-j-1 Legend
~
8 k(x j ) k(x j +1 )
9 Xj~ x j +1
10 t +- 1
I-origin indexing
Program 6.24 Forward and backward bubble sort with string indicator
Odd-ecen transposition sort. Like the bubble sort, the transposition sort
has unity storage ratio and involves the comparison and possible trans
position of adjacent items. Each stage consists of two phases or half~stages.
In the first half-stage, each item with an odd index (except the last if it is
odd) is compared with its successor; in the second, each item with an odd
index (except the first) is compared with its predecessor. Table 6.25
provides an example.
Xl 8 6 6 6 6 3 3 3 Xs
x2 6 8 8 8 3 6 4 4 x6
x3 11 9 9 3 8 4 6 6 x~
x4 9 11 3 9 4 8 8 8 Xl
Xs 3 3 11 4 9 9 9 9 x4
x6 4 4 4 11 11 11 11 11 x3
I
Table 6.25 Odd-even transposition sort example
220 SortinO" /) 6.4
Program 6.26. The subloop 6-10 perfornls the first or the second half-stage of
the transposition sort according as the alternator a is 0 or 1. Final ternlination
occurs on step 3 as soon as one half-stage is conlpleted without a transposition
occurring, except that a mininlunl of two half-stages nlust be executed. The
nlininlunl of two half-stages is assured by the initialization of t on step 1.
1~ t ..(- 2
2 a +-1
I-origin indexing
3 t +- t - 1 <
x Vector to be ordered.
4 a +- ii
t Termination indicator.
5 j +-a Alternator (0, 1).
a
6 j+-j+2 j Scan index.
7 > j vex)
9 x j - I +--)0 Xj
10 t +- 1
* This result may be combined with the fact (established in Sec. 6.5) that the nunlbcr
of transpositions required is finite to establish convergence of the transposition sort.
For, if the set is not ordered, each half-stage must effect at least one transposition.
6.4 Internal sorting Inetlzods 221
and transpositions in a given half-stage are independent and nlay therefore
be executed in parallel.
Ranking sort. If one new iteITI is added to a vector of itenls already ordered
on a given key, the resulting vector can be ordered by ranking the ne\V
iteITI, i.e., by comparing it with the iteITIS of the original vector in succession
until the appropriate rank of the added item is deternlined. Moreover. 11
repetitions of this process, which draw the new iteITIS fronl a given vector
of n itenls, will order the entire vector. Table 6.27 shows an exaITIple in
~I~
Itenls Keys Set 1 3 Itcnls
--- ---
Xl 8 8 6 6 6 xi)
x~ 6 8 8 8 6 I 4 x(}
x3 11 11 9 R 6 x~
X,l 9 11 9 8 Xl
Xs 3 11 9 x.I
x6 4 11 x: 3
I I
Table 6.27 Ranking sort example
Program 6.28. The index i controls the selection of successive itenls to define
the itenl z (step 4) which is to be added to the ranked set by steps 6-9. The indexj
controls the selection of successive itenls of the ranked set for conlparison with ::,
and each execution of the subloop ternlinates if either j beC0I11eS zero or if
k(z) k(x j ). In either event, step 9 inserts the new itenl ;:, into the position x j l'
which was last evacuated. Fronl the initialization of the index i it is clear that the
process is actually c0I11pleted in vex) - 1 rather than l'(x) stages.
Ranking by insertion. Since each stage of the ranking sort ranks one ne\V'
item in an already ranked set, the determination of its position can be
accomplished by a binary search. This sharply reduces the required
number of comparisons. However, once the new position is deternlined.
each of the succeeding items in the ranked set must be moved down one
place to make way for the new item to be inserted. This ITIethod is par
ticularly good where (due either to suitable facilities in the COITIputer or to
the use of a chained representation for the vector of items x) such block
transfer is easy to perform.
222 Sorting 6.4
~ i ~ 1
I-origin indexing
2 i vex)
j Scan index.
5 j -+- i
z Current item to be ranked.
6 j -+-j - 1
~
7 k(z) k(x j ) Legend
8 x j +l -+- xj
9 x j +l -+- Z
i -+- 1
2 u -+- ai(v(x)) 1\ aj(v(x))
I-origin indexing
3 ujx +-- ~ (ujx)
X Vector to be ordered.
4 i : vex)
12 j -+-l(p + q) -:- 2J
Legend
13 < k(x i ) : k(x j )
14 j : p
15 p-+-j
Repeated selection sort. The process of scanning all items of a vector for
the smallest key and transferring the selected item to a separate output
area (in a serial or random access file) will be called selection. Repeated
selection on the successive remaining (unselected) items will serve to
transfer the items in the order determined by the specified key. This
method of ordering is called a (first-degree) selection sort.
If the given vector of n items is divided into m subvectors of at n10st
rn -:- ml items each, then a selection from each subvector will produce a
vector of m items, thejth of which is the smallest of the jth subvector. A
selection performed on the m items of the resulting vector will then select
the smallest item of the entire original set. I f the selected item canlC
from the kth subvector, it is then replaced by a further selection on the
remaining items of the kth subvector. Repetition of the process n tilnes
serves to order the items. Because selection is performed on two levels,
the process is called second-degree selection.
In general, the smallest (first-level) item may be selected from a set of v?
second-Ieeel items, each of which is selected as the smallest of V: 3 third-Ieeel
itenlS. The process can clearly be extended to any desired number of levels.
If p levels of selection are used, the process is termed pth-degree selection or
repeated selection. It may be represented as a singular homogeneous tree
T of height p + I, as illustrated by Fig. 6.30.
Figure 6.30 shows the initial filling of the lower levels in a third-degree
selection sort performed on the sixteen items at the top of the tree T, with
veT) == (1,2,2,4). The keys are indicated in parentheses. The positions
of the third level are the nodes (I, I, I), (I, I, 2), (I, 2, I), and (I, 2, 2).
They are first filled by items x3 (6), x6(2), xn( I), and X 14 (4), respectively, each
selected as the smallest among the second-level nodes of the corresponding
subtrees TO,I,U' T O ,1.2)' T O ,2,1)' and T O ,2,2)' respectively. The first position
of level two is then filled by x 6 (2), selected as the smallest among the second
level nodes in its subtree, and so forth. Figure 6.3 I shows the continuation
of the process through the selection of the first two output iten1s.
If e is some value which exceeds the absolute value of all keys, then the
selection process may be made more uniform by assuming either that the
entire top level of the tree is occupied by the items to be sorted or that
224 Sorting 6.4
1 2 3 4
xl(9)
x3(6) ~::)
x4(16)
x6(2)
~, xs(3)
~::: x7(7)
x8(l3)
~lt
~ X12(5)
~XlO(ll)
Xll (8)
~
xg(l)
~X13(l5)
~~M+
X14(4)
xls(12)
X16(lO)
Figure 6.30 The tree T representing the third-degree selection sort for v(T) =
(1,2,2,4)
~Xl(9)
x2(14)
x3(6)
x3(6)
x4(16)
x6(2), xs(3)
~X5(3)
x6(2)
xs(3), x7(7)
x7(7)
~X9(1)
xlQ(ll)
X12(5)
Xu (8)
X12(5)
Xg(l), X14(4)
kT(;ilJS)I) +- e
2 z +- k(Tl)
I-origin indexing
3 m +- (1)
=i T Leaves of T contain items
4 z : e
to be sorted or dummy
5 <1>21 +- o ,A 2 items with key = e; other
levels contain dummy
6 7T(<1>l) +- 1 items with key = -e.
7 z : -e e -e < k < e for all legiti
mate keys k.
8 <1>2 1 +- Tl, Al
s Scan index.
9 z : e
m Index of minimum in
10 (T wl/ m )1 +- (T m )1 e2jT'Wl/s'
13 z +- e Legend
14 Sv : vll(s)(T)
15 S +-- S + WI
~
16 z : kT s )l)
17 z +-- kT s )l)
18 m +-S
k(r p ) +- e
2 z +- k(r 1 ) I-origin indexing
3 m +-1
r Right list node vector
4 z : e
(]T)2 of tree of Program
5 <1>2 1 +- 0, A2 6.32.
6 7T(eD l) +- 1
e -e < k < e for all legit
imate keys k.
7 z -e s Scan index.
9 z : e z Mininlum key.
eD 21 Output file.
10 r p +- r m
p I ndex of previous node
13 z +- e Legend
13a } +-0
14 } : b
15 }+-}+1
15a s +-s + 1
~
16 z : rs
17 z +- k(rs)
18 m +-s
Program 6.33 Repeated selection sort of Progranl 6.32 executed on the right
list node v~ctor r of a unifornl rooted h-way tree
228 Sorting 6.4
I-origin indexing
2 P3 +- PI
3 (T'W 1/s)1 +- cD I
I T Tree as in Program 6.32.
k(Tl)
e -e < k < e for all legiti
4 Z +-
mate keys k.
5 m +- (1)
s Scan index.
> z . -e m Index of minimunl elig
6
ible node in
7
e2 jTwl/ s .
8 z Minimum key.
9 w Minimum eligible key.
<J)I Input file terminated by
10 (Tw1/m)1 +- (T m)l
1
dummy item with final
11 S +- m ffi (0)
partition A2
12 v(s) : v(T) > cD 21 Output file.
Legend
16 s +- S + WI
> w : k((Ts)l)
17
18 z : k((Ts)l)
19 z +- k((Ts)l)
20 m +- s
21 z : e
22 w : -e
23 w +- -e
the original group. If not, the item must be allowed to retain its position
but must be excluded from consideration in the sorting process. In general,
sorting with replacement allows the production of longer output strings
than would be otherwise possible with the same internal storage capacity.
The expected increase is discussed in Sec. 6.5. Since the process terminates
with the original positions completely occupied by ineligible items, the
production of a further string can be begun simply by declaring all itenls
eligible. .
k(w) +-- e
I-origin indexing
2 P5 +-- PI
3 j +-- vex) +I x Vector to be ordered.
4 z +-- -e j Index of first ineligible itenl.
i Scan index.
5 w +-- <1>11
w Current item.
6 z : k(w)
z Key of last recorded item.
i=
7 j+--j-I <D 1 1
Input file.
8 j +-- vex) +1 cD 21 Output file.
13 W~Xi
14 k(w) : e
15 1
<1>2 +-- w
16 z +- k(w)
the item with the smallest key among those scanned and (unless k( w) = e) is then
recorded in the output file and used to define z, the key of the last recorded item.
The main loop is then repeated from step 5, where w is respecified by reading
an item from the input file. If the new item is eligible, the branch from step 6 to
step lOis followed; if not, the parameter j (denoting the index of the first in
eligible item xi) is decremented and w is interchanged with the last eligible item xi
before continuing to step 10. Since step 10 initializes i to the value j, the sub
sequent scan is limited to eligible items.
Ifj becomes zero on step 7, the entire set of items including w is ineligible.
Step 8 then restores j so as to make all items eligible and hence to start a new
string.
The branch from step 5 and the subsequent redefinition of step 5 on step 2
serve to introduce "infinite" keys after the input file is exhausted. The immediate
redefinition of j and z (steps 3 and 4) to make all items eligible may appear
redundant. It may be verified that this procedure (1) does not increase the number
of strings produced, and (2) avoids the potential error of omitting the last string.
6.5 Eraluation of internal sorting methods 231
Address table sort. A sorting process can, in principle, be accomplished in
two distinct phases, a determination of the permutation required and the
execution of the permutation. In serial sorting, however, the process
cannot be effectively divided in this way. For, because of the limitation
to rank-preserving operations, the items could not be transferred directly
to their final positions even if they were known. In internal sorting this
limitation does not apply. A sorting process embodying these two separate
phases is called an address table sort. The method offers advantages when
the time required to transfer a complete item is large compared to the time
required to transfer its key.
The permutation p must be so determined that the permuted vector
y = xp is ordered on the key. Let KI be the vector of keys associated with
the given vector x, i.e., K/ = k(xJ, and let K2 be the identity permutation
LI For example, if the successive keys of the vector x are 17, 9, 6, 11,4, 8,
and 3, then
~).
9 6 11 4 8
2 3 4 5 6
In evaluating internal sorting methods, both the execution time and the
internal storage requirements must be considered. For the execution
times of the internal merge sort and the internal colurnn sort, the analysis
of the corresponding serial sorting process applies directly. Since their
storage requirements have also been discussed in Sec. 6.4, the present
section will be limited to the special internal sorting methods.
The measures of interest in the evaluation of these methods are four:
I. the scan length
2. the number of stages required
3. the number of transpositions required
4. the storage ratio.
232 Sorting 6.5
The number of con1parisons executed is frequently used as a measure of
execution time of a sorting process. However, since the selection of the
keys to be compared (involving instruction modification and possibly
extraction of key digits) usually accounts for the largest share of the time
required in a comparison, the scan length (number of items scanned) is a
more suitable measure. In a process which scans m items serially (as in
each individual stage of a bubble or a ranking sort), the number of com
parisons is (m - 1) and differs but little from m. However, for m small
(as in the successive selections in a repeated selection sort); the ratio
m -7- (m - 1) is significant, and the scan length is the better measure.
Moreover, in a process which does not use a serial scan, the number of
comparisons may differ markedly fron1 the scan length. For example,
each half-stage of the interchange sort requires approximately ml2
conlparisons for a scan of m items.
The storage ratio is defined as the ratio of the number of item storage
locations required to the number of items entering the sorting process
(without replacement). The four measures will be determined for each
method in turn and then compared. Table 6.37 summarizes the main
results for an assumed random initial distribution of keys. The entire
analysis is based on the assumption of distinct keys.
Bubble sort
In the bubble sort, both the number of stages and the scan length depend
on the initial order. The minimum and maximum number of stages
required are one and (n - 1), respectively. The minimum and maXimUl11
scan lengths are nand (n 2 + n - 2)/2, respectively.
The expected number of stages required for a randonl initial order is
determined * as follows. If, at any stage, all itenls are ordered except for
one which occupies a position below (later in the seq uence than) its proper
position, then one further stage will complete the ordering. On the other
hand, each item which appears above its terminal position at a given stage
will be ITIoved down by exactly one position in the next stage. Conse
quently, the number of stages required will be determined by d, the
maximum upward displacement from its final position of any item in
the original array. More precisely, if x is the given vector of items, y the
corresponding vector of ordered items, p is a permutation such that
y = xp ' and d = II - p, then d = ((e["d)/d)I.
If the maximum upward displacement is d, then (assuming that all
keys are distinct and that the final order is consequently unique) the last
item in the final order (i.e., Y 71) can initially occupy anyone of the (d + I)
components of the suffix W d +1/X. Similarly, y ii-I may initially occupy any
one of the remaining (d + 1) positions of the suffix w d t-2/ X, and so on for
each of the (n - d) items of the suffix wn-d/y. The number of possible
initial arrangements of the items ofwn-d/y is therefore (d + 1)(n-d). Since
the d leading items (i.e., ad/y) can occupy anyone of the d remaining posi
tions without restriction, the number of possible initial arrangements with a
* The exact expression for the number of stages is due to Friend (1956).
234 Sorting 6.5
II c(n)
-
I 0.798
5 0.853
10 0.877
IS 0.893
20 0.904
30 0.918
40 0.927
50 0.934
er = ~ d x pr(r = d) = I d x pr(r = d)
d=O d=1
= l(ni
n! d=1
l
d(d + l)(n-(l) d! - nil d X d(n-d+H(d -
(/=1
I)!).
It is shown in the appendix to this chapter that zen) approaches the value
V 7rn/2 for large n, and Table 6.36 gives coefficients c(n) for the approxi
mation
zen) = c(n)"/7rn/2.
6.5 Ecaluation oj' internal sorting Inethods 235
Since one extra stage is needed to determine that the ordering has been
completed, the expected number of stages is given by
e(J = n + 1 - ,zen).
Since the scan length in the jth stage of the bubble sort is known to be
n - j + ], a similar analysis can be used to determine the expected total
scan length es' The result is
n2 - n - 2
es =
. 2 + (n + l)z(n + I) - nz(n),
or approximately,
es
~
--
Ir - ')
11 - 2
+ AI
/37T11
. 2 .~ 4
Ranking sort
The number of stages in the ranking sort is clearly (n - I). The ex
pected scan length on the kth stage is determined as follows. The item may
rank in anyone of (k + I) positions; first, second, ... , last, with eq ual
probability. There are therefore (k + I) cases requiring scan lengths of
2,3, ... ,k, (k + I), (k + I). The last case requires a scan length of
(k + I) rather than (k + 2), since the process terminates on comparison
with the last item regardless of the outcome of the comparison. The total
scan length for the (k + I) cases is therefore
k+2.) k2 + 5k +2
(I r - 1= ,
r=2 ' 2
and the expected scan length is consequently 2 + kj2 - Ij(k + I).
Summing over the (n - 1) stages and denoting the expected total scan
length by e yields the relation
11/-] n-1 1
e = 2( n - I) + - I k - I -
2 7;=1 7;=1 k + 1
= /12 + 7/1 - 4 _ i !.
4 7;=1 k
But
n I l I
I -
= y + loge n + - - --.)
1.'=1 k 211 1211
approximately, * where y = 0.5772 ... is Euler's constant. Hence
e=
n2 + 711
1.577 - log/, 11 - -
1
+ -1-2 ,
4 211 1211
* See, for example, Cramer (1951) p. 125, or Woods (1926) p. 171.
236 Sorting 6.5
correct to two decimal places for all n > 1. The maximum and minimum
scan lengths are (n 2 + n - 2)/2 and 2(n - 1), respectively.
The ranking sort takes advantage of initial order and the minimum scan
length of 2(n - 1) is achieved for an initially ordered set of items. Ranking
by insertion with binary search, as described by Program 6.29, requires
approximately 2 + [log2 jl comparisons on the jth stage. Hence if n = 21.:,
the number of comparisons required is given by
where a is the ratio of the time required for an item transfer to the time
required for the scan of a single key. As remarked in Sec. 6.2, the optimum
value of m is obtained as a solution of the equation
a
loge m = 1 + -.
In
since n = m P
The storage ratio rem) is therefore given by rem) =
m/(m - 1), a function which decreases with m, rapidly approaching unity.
This ratio also represents the increase in execution time entailed in the
initialization of the lower level positions. If sorting with replacement is
used, initialization is required for the first string only.
The ratio rem) changes significantly for the first few values; thus
r(2) = 2.00; r(3) = 1.50; r(4) = 1.33.
If the expected scan time sCm) is taken as a second criterion, then (since
5(2) = 5(4) > 5(3)), the value m = 2 will be eclipsed by both 3 and 4.
N
W
QO
I S~
Item Transfers or
Stages Scan Length Transpositions
Sorting Method Storage
Ratio
Expected Max. Min. Expected I Max. I Min. Expected Max. Min. ~
-- --- -- -- ---
Merge (m-way) IOgm(i) logm n ]
11 IOJm(~) n logm n n n IOglfl(~) n log mn n 2
Ranking n -1 n-] In -] n2 + 7n
- - 4 - - 1.6 -logl'n-
I
~
n
2
+n- 2 I 2n _ 2 n'2 - n n'2- n
2 4
I + 2) + 2) I n- - n
Ranking by insertion (binary search)
n
I n~ In~ 1 1
n(log2 n
nmp = nm logm n
n(log2 n
nmp
2n -
nmp
2
4
np
n'n:n I 0
np I~
(branching ratio m) m-l
With optimum allocation (2 fields). t Parallel operation possible: estimates are lower bounds.
])~ ,
== \ TTfl/2
Table 6.37 Characteristics of internal sorting methods (for random distribution of distinct keys)
CO')
Q"\
U.
Appendix 239
fen) = s + ct,
APPENDIX TO CHAPTER 6
~
z( n) = -1 L. s! sri
_,
.'i
n! s=l
and since g(s) is monotone increasing with a large positive derivative for
large values of s, only the upper end of the integral is significant. It can
therefore be shown that
lim V(n) = lim L(n).
n-+oo n-+oo
Consequently,
lim L(n) = lim zen).
n-+oo n-+oo
= n Ll (1- t)"+Herddt,
lim L(n)
n-+ 00
= n
I I
0
" 3
e-t/2-(n+~)(t~/2+t 13+ , .. ) dt.
For n large, only small values of t will be significant, and all terms in the
exponent may be dropped except -en + 1/2)t 2 /2. Similarly, the upper
limit of integration may be extended to infinity. Hence,
1
00
lim L(n) = n
2
e-(n+~)t !2dt = n(7T/(2n + l))~i
n-+oo 0
. (7Tn/2)~.
REFERENCES
Ashenhurst, R. L., (1953), '"Sorting and Arranging," Theory of Switching, Report No.
BL-7, Harvard Computation Laboratory, Section I.
Betz, B, K., and W. C. Carter, (1959) '"New Methods of Merge Sorting," 14th A.C.M.
Conference, Cambridge, Mass.
Exercises 241
EXERCISES
6.1 A given file contains a set of 15 items Xl' .. , X I5 with associated decinlal
keys 68, 9,60,14,60,73,79,15,67,5,9,41,57,9,41. For each of the serial files
used, show the contents at the conclusion of each stage of the following processes:
(a) a two-phase classification and merge using
(i) four files.
(ii) three files.
(iii) six files.
(b) a string classification and merge using four files.
(c) a single-phase merge using
(i) four files.
(ii) six files.
(d) a single-phase merge without rewind.
(e) a base ten column sort using eleven files.
(f) a base ten column sort using twenty files.
(g) a base ten column sort using four files and the partial pass column sort.
(h) a column sort using four files and a base three representation of the keys.
6.2 Modify Program 6.4 so that it will work properly without dumnly terminal
items (i.e., each terminal partition A2 is to be associated with a legitimate item).
6.3 Write a program for the string-doubling merge sort.
6.4 (a) Write a program for a base b column sort which uses backward read to
eliminate rewinding.
(b) Program a variant of the two-phase colunln sort (b + 1 files) so as to
242 Sorting
eliminate the copying of the "zero items" in each merge phase. Deter
mine the relative efficiency of the method.
(c) Program an analogous variant of the nl-way two-phase merge sort so as
to eliminate the copying of part of the items in the classification phase.
6.5 Construct the matrix M (cf. Table 6.10) specifying an efficient partial pass
column sort for the following cases:
(a) base ten and three files.
(b) base ten and five files.
(c) base eight and three files.
(d) base ten and four files using no rewind, i.e., files are to be read backward.
(e) base ten and three files using no rewind.
6.6 Using a set of matrices 1M, 2M, ... , l JM, of the form of Table 6.10, write a
progran1 to generalize the partial pass sort to the case of a mixed radix key.
6.7 (a) Reprogram the amphisbaenic sort (Progran1 6.13) so as to reverse the
order of the final output. (This is the form used in Nagler, 1959.)
(b) Program a generalization of the amphisbaenic sort which makes use of
partial passes within columns of the key.
6.8 (a) Program a modification of the bubble sort process which on odd
numbered stages bubbles the smallest item to the leading end and on
even-numbered stages bubbles the largest item to the tail end.
(b) By examining all 4! cases show that for four items the eApected nunlber
of stages is slightly less for the method of part (a) than for the un
modified bubble sort.
(c) Program a bubble sort using a string indicator s as in Program 6.24 but
using backward scan only.
6.9 (a) Program a modification of Program 6.17 to specify So = 2 and to auto
matically double So and restart if necessary.
(b) Compare the efficiency of the program of part (a) with that of the
straightforward program in which the number s of strings is assumed
equal to the number of items.
6.10 (a) Derive the relation loge 171 = 1 + alnl which must be satisfied by an
optimal value of n1 in order to minimize the cost function c = (n1 + a)
logrn n (cf. Sec. 6.2).
(b) Determine the optimal integral value of 171 for each of the cases a = 0,
1, e 2
6.11 For the amphisbaenic sort on a set of bt items with distinct keys belonging
to the set lO(b t ), determine
(a) the number of item transfers required.
(b) the number of file reversals (change of direction of read and record)
required.
6.12 Write a program describing the odd-even transposition sort in terms of
vector operations so as to show clearly the parallel nature of the process. Treat
all items with odd indices (plus a dummy item) as a vector and all even itenls (plus
a dummy item) as a second vector.
Exercises 243
6.13 (a) For each of the following sorting methods, determine whether initial
order of the items is preserved, i.e., whether the relative initial order of
all item pairs wjth duplicate keys is preserved.
(i) sinlple classification and merge.
(ii) classification and simple merge.
(iii) ranking sort.
(iv) bubble sort.
(v) odd-even transposition.
(vi) radix exchange sort.
(b) Prescribe a simple modification of the key which will ensure the preser
vation of initial order.
6.14 For the sequence of keys given in Exercise 6. I, show the explicit execution
of the following internal sorting processes:
(a) bubble sort with replacement (six item positions).
(b) pth-degree selection sort with replacement, with p = 3 and 111 = 2.
(c) ranking by insertion.
6.15 If the key is represented in a base b nunlber system, with each digit repre
sented in turn in a ranked binary code, then ordering can be achieved by a base
two column sort on successive columns of the underlying binary representation.
(a) Show more generally that ordering can be achieved by a base 21.' column
sort on (the base two value of) successive groups of k binary digits.
(b) Program the process suggested in part (a), including the deternlination of
an optimum value of k for a given nunlber n of available files. Assume an
original key of q digits, each represented by r binary conlponents. Do not
neglect the problenl of terminal conditions.
(c) If b = 10, r = 4, and if the successive decinlal digits are coded alternately
in a (ranked) bi-quinary and qui-binary systenl, the binary digits can be
grouped in twos and threes so as to allow column sorting with a nlaxinlunl
of five output files. Progranl a suitable process.
6.16 Program a sequence of rotations of infixes of the vector x which will
reverse the order of its components. (See the Ranking by insertion progranl
(6.29) for the case when the key defines a complete reversal of order.)
6.17 Assuming that an item transfer takes c times as long as a comparison of
two keys, determine a criterion for the choice of 171 in an 171-way internal revision
merge for the following cases
(a) assuming 2111 comparisons per item (n1 comparisons for eligibility and 111
for minimization).
(b) assuming that a ranking sort is used on the 111 itenl positions.
6.18 (a) Let z be a vector key of dinlension three whose successive components
represent department nunlber, job nunlber, and nanle, respectively.
Two lists are to be produced, one ordered by name within department
within job and the other by departnlent within job within name. Deter
lnine a mapping vector p such that y = zp is the vector key of least
dimension which contains the two required orderings.
(b) Let yi = Zpi be a set of vector keys defining a set of related orderings.
244 Sorting
Determine a vector key of nlinimum dimension which contains all of
the related orderings for the case pI = (3, 4, 1, 5), p2 = (3, 1, 5, 6), and
p3 = (1, 4, 3, 5).
(c) Analyze the effect of related orderings on the expected execution time
of a merge sort. (Consider the effects of duplicate keys on expected
string lengths.)
6.19 Let D be a O-origin matrix of the form of Table 6.14 which describes the
cascade sort, i.e., row Dj describes the distribution of strings at the completion of
stage j. Using the special matrices of Sec. 1.13, write concise expressions for the
lnatrices F and B such that
(a) Dj-I = B ~ Dj
(b) Dj = F ~ Dj-I
(c) Show that F and B are inverse.
(d) Determine the dominant eigenvalue of B when v(B) = 3, and show its
relation to the power of the cascade sort for four files. (cf. Sec. 6.1 and
Carter (1962).)
6.20 Determine the relative efficiencies of serial column sorting and serial merge
sorting for the following conditions. Internal sorting, with a maximum of 100
item positions, is to be used as an aid to each of the processes, and the time for the
internal sorting is assumed fixed. There are 10,000 items with 4-digit decimal
keys, and each key value is associated with at most four items. The initial arrange
ment contains 3500 maximal (increasing) strings.
6.21 Program an address table sort.
6.22 (a) The determination of the permutation vector required in the address
table sort can be considered as a mapping from each item onto its rank
in the set. Show that for distinct keys this mapping can be performed
by counting for each item the number of items having a smaller key.
(b) Progranl the method of part (a). (This is known as a counting sort.)
6.23 (a) Program a two-phase internal merge sort.
(b) Program a two-phase internal column sort.
6.24 Program an extension of Program 6.33 (pth-degree selection executed on
the right list node vector e 2 /]T) to
(a) sorting with replacement.
(b) the case of a singular homogeneous tree with dispersion vector veT).
(C) cover both cases (a) and (b).
6.25 I f the transfer from a serial file can proceed concurrently with other opera
tions, it is frequently advantageous to associate two fields of internal storage
(called buffers) with each file and to transfer the next group of items to one of the
fields while executing necessary operations on the items of the other. Buffers may
be used similarly for output files.
(a) Program an nz-way single-phase merge using two buffers for each of the 2m
serial files.
(b) Program a base b single-phase column sort using two buffers for each of the
2b serial files.
Exercises 245
6.26 If the nunlber of input buffers serving 111 files is reduced to I1Z -t- 1 sonle
advantage nlay still be gained by "predicting" the file whose (currently) associ
ated buffer will next be exhausted, and initiating a transfer from it to the idle
buffer. Repeat parts (a) and (b) of Exercise 6.25 for (111 + 1) and (b + 1) input
buffers, respectively. (See Friend, 1956.)
6.27 Repeat parts (a) and (b) of Exercise 6.25 with the nunlber of output buffers
also reduced as in Exercise 6.26.
6.28 Since a given initial arrangement may be easier to bring to descending than
to ascending order on the keys, and since a final reversal of order nlay be easy to
achieve (by backward read in the case of serial files or by address modification in
the case of internal storage), it nlay be advantageous to choose ascending or
descending order according to some estimate based on the initial arrangenlent.
Write a program which first counts the number of ascending strings and then
executes a ranking sort by insertion to produce either ascending or descending
order according to which appears to be the easier to achieve.
6.29 For the first few values of n, compute and compare the following alterna
tive evaluations of the expected nunlber of stages in a bubble sort
'It sn-s s !
(a) n + 1 - zen), where zen) = 2: -,- .
.'i 1 n.
(b)n+l-v1Tn/2.
(c) c/n!, where c is the total count of all stages required for the n! possible
initial arrangements of n distinct keys.
chapter 7
THE LOGICAL CALCULUS
o= (V\=F ~) (7.1)
/\ Ix = V Ix (7.2)
(7.4)
X ~ y = X ~ Y.
246
7.2 Canonical forms 247
The foregoing relation may be verified as follows:
== (X = Y)/.
== (( X ~\ y) i= (x <= y)).
Z/ == (a i 1\ c j ) 1\ (qi 1\ rJ
== (a i 1\ qi) 1\ (c j 1\ r j )
== (a 1\ q)i 1\ (c 1\ r)j.
Consequently, Z == (a 1\ q) c; (c 1\ r).
Intrinsic vector
T i(f) P = T /\ X
0 0 0 1 Xo 1\ Xl 1\ x2
0 0 1 0 Xo 1\ Xl 1\ x2
0 1 0 1 Xo 1\ Xl 1\ x2
0 1 1 0 Xo 1\ Xl 1\ x2
1 0 0 1 X o 1\ Xl 1\ x2
1 0 1 0 Xo 1\ Xl 1\ x2
1 1 0 0 X o 1\ Xl 1\ x2
1 1 1 0 Xo 1\ Xl 1\ x2
.((X) = i(() XP
= (x o 1\ Xl 1\ x2) V (x o 1\ Xl 1\ x2 ) V (x o 1\ Xl 1\ x 2)
Table 7.1 The disjunctive canonical form
I(x) = (x o 1\ Xl 1\ x2 ) V (x o 1\ Xl 1\ x2 ) V (x o 1\ Xl 1\ x2 )
= (x o V Xl) 1\ x2
A pplying the usual notation for operations on variables to operations
on functions as well (e.g., j denotes the function inverse to f, and f 1\ g
denotes the conjunction of the functions I and g) permits the expression
of certain easily derived identities concerning intrinsic vectors:
it!) = j(f)
i(1 V g) = i(/) V i(g).
Characteristic vectors
A vector which represents a function j' is called a characteristic rector of
f The intrinsic vector i(f) is but one of several useful characteristic
vectors.
The expression Y ~ x denotes the function obtained by first negating
each Xi for which Yi = 0 and then taking the conjunction (that is, and
function) over the resulting vector. Such a function is called a minterm in
x, and the components of the minterm rector
P = T x
comprise all possible minterms in x with no repetitions.
The component Pi is a function of x which assumes the value one if and
only if x = Ti. Consequently, for any functionJ,
f(x) = V /(i(f)jp)
= V /(i(f) /\ p)
= i(f) p.
This relation is illustrated by Table 7. I.
The expression
f(x) = i(f) i P = iCf) > (T ~ x)
is called the disjunctive canonicalform of the function f since it displays.r as
a (unique) disjunctive (that is, or) function of minterms, each of which
involves all variables and conjunction and negation only. The disjunctive
is one of several canonical forms of interest, each of which assumes the
form
f(x) = Y(f)g~ sex),
where the characteristic vector y(f) is a function off only, and the specific
vector sex) is a function of the argument x only. Each of the four forms of
interest is characterized by the particular over-all operator 0 1 occurring
in it and is therefore called the disjunctice, conjunctice, exclusice disjunctice,
or equicalence canonical form * according to whether 0 1 is the operator
v,A,i=,or=.
The characteristic vector and the specific vector appropriate to each forn1
will be characterized by its over-all operator 01' Thus
* The functions x V !J, x A y, (x i-: Y), and (x = y) are, except for the trivial functions,
the only associative commutative functions of two variables.
N
til
Q
~
~
a-
O'Q
T I i(n II y(j. \/) I T~X II y(j./\) I ~X ITO y(f.=) I XX I Y(f,V) I y(J,ic ) y(f.=) r:;'
o 0 0 1 1
-
X'o /\ Xl /\ x 2 1 Xo V
T
xl V Xz
y(f/)
1/\1 A 1
X T
oV0 V0
;<7)
o I 0
Y(f"AJ
0 0 0
-
~
~
~
~
o 0 1 0 0 Xo A xl A x 2 0 Xo V xl \j Xz 1 /\ 1 !\ x 2 o I 0 V 0 V X'2 1 1 0 s:::
0 1 0 1 1 Xo A xl !'\ x 2 1 Xo xl V X2 o 1 /\ xl !\ 1 oV Xl V 0 0 I 0 0 0
~
0 1 1 0 0 Xo /\ xl !\ X2 0 Xo V Xl V x 2 o 1 /\ xl 1\ X 2 o V Xl V X2 0
1 0 0 1 1 Xo /\ xl I, XZ 1 Xo V xl \I x 2 o Xo !\ 1 1\ 1 Xo V 0 V 0 0 I 0
I
0
I
0
1 0 1 0 0 Xo !\ xl !, Xz 0 Xo V Xl V Xz o Xo !'\ 1 A X z Xo V 0 V X2 0
I
1 1 0 0 0 Xo A xl !\ X2 0 Xo V Xl \I x 2 X o /\ xl /\ 1 o XOVXIVO 0
1 1 1 0 0 Xo ;\ xl !\ X2 0 Xo \I Xl V X2 Xo A Xl !\ X2 o Xo V Xl V x2 0
(Xo II Xl /\ i;) V (0) V (XO /\ Xl !'I X2 ) V (0) V (X O !\ Xl ;\ X2 ) V (0) V (0) V (0) (Disjunctive)
_ _ _ (1) !'I (X O V Xl V X2 ) 1\ (1) ;\ (X O V Xl V X2) /\ (1) /\ (XO V Xl V x2 ) A (xo V Xl V x 2) A (Xo V Xl V x2 ) (Conjunctive)
j(x) = (x o V Xl) !\ X2 =
[(1) i (X 2) # (0) # (0) # (0) F (0) #- (x o A Xl) #- (x o A Xl /\ x 2 )] (Exclusive disjunctive)
[(1) = (x2) = (1) = (1) = (1) = (1) = (Xo V Xl) = (xo V Xl V x2)] (Equivalence)
Table 7.2 Intrinsic, characteristic, and specific vectors
fD)
-.I
N
7.2 Canonical forms 25]
The forms are defined formally by the following expressions. *
Y(f, V) X (T ~- x) (Disjunctive) (7.11a)
y(f, /\) 0 (T ~ x) (Conjunctive) (7.11b)
f(x) = y(f, efe) :(c (f 0 x) (Exclusive disjunctive) (7.11c)
Y(f, =) v (T X x) (Equivalence) (7.11d)
Table 7.2 shows the intrinsic vector of the function f of Table 7.1
together with corresponding pairs of characteristic and specific vectors.
These may be substituted in equations 7.11 (a-d) to verify that they do
represent the function f
Since x = Tk = Tk for some k, equation 7.1la may be written as
ik(f) =f(Tk ) = y(f, V) X (T). Tk ).
Consequently,
y(f, V) X (T ~ T) = y(f, V) X S(V) (7.12a)
Y(f, A) 0 (T = T) = y(j~ A) '0 S( /\ ) (7.12b)
i(f) =
y(f, #) ~~ (T~, T) = Y(f, #) ~ S( # ) (7.12c)
junctive specific) matrix. Since i(f) is a function of y(f, 0) and S( 0), the
(7.22)
by equations 7.7, 7.17, 7.19, and 7.13. Characteristic vectors of a function
and of its inverse are displayed in Table 7.2.
The relation between y(f, =F) and y(f, =) may now be obtained by
applying equation 7.22 to equation 7.12c to yield
i([) i([) S( #-) ~ Y(f, #-) S( =) vY(f, =) 1(/) S( =) v r(f, #-) S( #-) ~ r(f, =) ~
~
Y(f, #-) S( #-) ~ i(/) Y(f, #-) (Y(f, =) = eO) S( #-) ~ .(/) (y(f, #-) #- eO) y(f, =) ~
~.
~
Y(f, =) S( =) v i([) (Y(f, #-) = eO) Y(f, =) S( =) v 1(/) r(f, #-) (y(f, =) #- eO) -...
~
~
~
i(/) 1(/) S( =) v Y(f, #-) S( #-) ~ y(f, =) i(/) S( #-) ~ y(f, #-) S( =) v y(f, =) ~
~
y(f, #-) S( #-) ~ 1(/) (Y(f, #-) #- eO) y(f, =) S( #-) ~ i(/) y(f, #-) (y(f, =) = eO)
('~(F' n 1) f ) Y(f, V) I E I e
EO I e
7.3 DECOMPOSITION
A logical function f(x) is said to be decomposable on u if it can be written
in the form
f(x) = g(h(ulx), ulx),
where g and h are logical functions. Since.f, g, and h are functions of
v(u), (1 + +Iu), and +Iu variables, respectively, then if +Iu > 1, both
g and h are functions of fewer variables than f Decomposition on u such
that + lu > 1 thus permits f to be expressed in terms of simpler functions
g and h and provides an important basis for simplification techniques.
Every function is decomposable on u = e. Moreover, if + lu = 1, then
e
u = i for some i, and
f(x) = [Xi 1\ f(x 1\ i)] V [Xi 1\ f(x vel)].
Since bothf(x 1\ e) andf(x V e ) are expressible as functions ofeilx, then
i i
the foregoing expression is of the required form with h(ulx) = Xi. Con
sequently, all functions are trivially decomposable for + Iii = or
+Iii = 1.
Disjunctive canonical form
Ashenhurst (1957) determines nontrivial deco~positions of f(x) by
arraying the intrinsic vector i(f) in a 2 +/u x 2 +/u matrix F defined as
follows:
F/ = ik(f),
where k = (2e) ~ k
i = (2e) (ulk)
and j = (2e) ~ (ulk).
If, for example, u = (1, 0, 1, 1, 0), then the index k of each component ik(f)
appearing in F is given by the matrix* C of Table 7.4. The table also shows
* Ashenhurst (1957) calls the matrix C a decomposition chart and represents F by
circling each element of C which corresponds to a nonzero component of ;([).
N
Ut
~
~
T ;(f> h b 'f r Xr dVr Xr nVr u=(l 0 1 1 0) ~
/\
I c /\ m /\
~
o
1
2
3
4
0 0
0 0
0 0
0 0
0 0
0
0
0
0
1
0
0
1
1
0
0
1
0
1
0
1
0
0
0
1
1
0
1
0
1
0
0
0
0
0
1
1
0
0
1
0
0
0
0
0
1
1
0
0
1
0
0
0
0
0
c= 0
'0 2
3
10
11
4
5
12
13
6
7
14
15
16
17
24
25
18
19
26
27
20
21
28
29
22)
23
30
31
~.
~
-..
~
~
5 0 0 1 0 1 0 0 1 0 1 0 a
0 ~
(~
6 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 1 0 0 ~
D (D
7 0 0 1 1 1 1 0 1 0 0 0 1 0 0 1 1 0 1
8 0 1 0 0 0 0 0 0 1 0 1 0 F= 0 0 1 1 0 1 S2"
C")
9 0 1 0 0 1 1 1 0 1 0 1 0 0 1 0 1 0 0
10 0 1 0 1 0 0 0 0 0 0 0 0
11 0 1 0 1 1 0 1 0 0 0 0 0
12 0 1 1 0 0 0 0 0 1 0 1 0 b = (0 0 0 1 0 0 1 0)
13 0 1 1 0 1 1 1 0 1 0 1 0
14 0 1 1 1 0 1 0 1 0 0 0 1
15 0 1 1 1 1 0 1 1 0 0 0 1 c=(l 0 1 0 0 0 0 1)
16 1 0 0 0 0 1 1 0 0 1 1 1
17 1 0 0 0 1 1 0 0 0 1 1 1
18 1 0 0 1 0 0 1 0 0 0 0 0 d = (0 0 0 0 1 0 0 0)
19 1 0 0 1 1 0 0 0 0 0 0 0
20 1 0 1 0 0 0 1 1 0 0 0 1
21 1 0 1 0 1 1 0 1 0 0 0 1 m =(0 0 0 1 1 0 1 0)
22 1 0 1 1 0 1 1 0 1 0 1 0
23 1 0 1 1 1 0 0 0 1 0 1 0
24 1 1 0 0 0 1 0 0 0 1 1 I n=(l 0 1 0 1 0 0 1)
25 1 1 0 0 1 1 1 0 0 1 1 1
26 1 1 0 1 0 0 0 0 0 0 0 0
27 1 1 0 1 1 0 1 0 0 0 0 0
28 1 1 1 0 0 1 0 1 0 0 0 1
29 1 1 1 0 1 0 1 1 0 0 0 1
30 1 1 1 1 0 0 0 0 1 0 1 0
31 1 1 1 1 1 1 1 0 1 0 1 0
CD')
--I
~
7.3 Decomposition 257
corresponding values of i(/) and F.
- 1\
Let P = T c:t-' x,
q = T .~ (ii/x),
and r = T~ (u/x)
be the minterm vectors of x, ii/x, and u/x, respectively. * Then the matrix
P = q ~ r
Since the first and last terms on the right of equation 7.26 are, respec
tively, functions of ii/x and of u/x only, the function f(x) is decomposable.
The required functions are simply
h = h(ii/x) = a Xq = a /~ [T ) (u/x)]
f(x) = g(h, u/x) = h ;\ [c X rJ (7.27)
r = T ~ (u/x)
Since equation 7.27 does not represent the most general possible function
of hand u/x, it appears that the characteristic matrix F = a A c does not
* Although denoted by the same symbol, the matrices T are of differing dimensions
as required by compatibility.
yet,
'j- Since i(/) is equal to V ), it may be substituted for it in the disjunctive canonical
form.
258 The logical calculus 7.3
The fourth term of equation 7.28 is identically zero and is included only
for formal completeness; hence
F = (ii I~ b) V (a c) V (E 1\ d). (7.23')
Equation 7.24 now becomes
M = li A b) V (a A c) V (E A d) 1\ (q A r), (7.24' )
and since conjunction is distributive over disjunction, equation 7.8 may
again be applied to yield
M = li 1\ q) l, (b 1\ r V a 1\ q) .~ (c 1\ r V E 1\ q) A (d 1\ r.
(7.25')
Moreover, since V I V II(X \/ Y) = (V VI IIX) V (V VI IIY), equation 7.9
may again be applied to yield
f(x) = V I V /IM
= a X q) 1\ (b :~ r V a /. q) /\ (c X r V (E X q) 1\ (d X r. ( 7.26' )
Since q is a specific vector of the disjunctive canonical form (i.e., a
minterm vector), it is some column of the specific matrix S( V). Since
S(v) = I, q therefore contains exactly one nonzero component, and
consequently (li X q) = (a X q), and E X q = 1. Equation 7.26 can thus
be rewritten in decomposed form as
h = h(iilx) = a q = a /\ (1' .~ (iilx
f(X) = g(h, ulx) = (ii 1\ (b Xr V (h /\ (c Xr V (d Xr) (7.27')
r = T (ulx)
It is interesting to note that no use has been made of the fact that b, c,
and d are disjoint. Relaxation of this restriction does not, however,
increase the generality of the matrix F, since a V E = E, a V = a, and
a V a
= E. It does suggest, however, that the matrix F nlay be expressed
more compactly as
F = (a A m) V (a n n),
* In Ashenhurst's terminology, the matrix F must be of the following form: each
column is either all zeros, all ones, the vector u, or the vector ii.
7.3 Decornposi t ion 259
= x2 /\ (x o =1= x3 ),
c 'y r = (x o /\ x3 ) V (xo /\ x2 /\ x3 ),
and d / r = X o /\ x2 /\ xa.
Alternatively, the use of the vectors m and n yields the solution
j{x) = {h /\ [(in /\ x2 /\ x3 ) V (xo /\ x3 )]} V {h /\ [(xo /\ x3)
V (x o /\ (x 2 = x3 ))])
The entire decomposition process is described by Program 7.5. Steps
1-7 determine the characteristic matrix F appropriate to the decomposition
u. The loop 2-7 is repeated for each value of k from zero to 2 (u) - I. J1
Step 2 determines k as the vector (of dimension v(u)) whose base two
value is k. Steps 3 and 4 then specify the indices i and j appropriate to k,
and step 5 specifies element F/.
Step II determines d as the vector which specifies all full column vectors
of F, that is, d j = I if and only if F j = e. Step 12 determines e as the
corresponding vector specifying the zero columns.
If d and e together exhaust the columns (that is, d V e = e), then band
c (and a arbitrarily) must be set to zero. Since this is done by steps 8-10,
the exit branch on equality at step 13 terminates the process correctly. If
(d V e) -#- , then any column of the matrix (d V e) / F can be used to
specify a; step 14 uses the first column. Step 15 determines b as the vector
260 The logical calculus 7.3
7 ' k : 2 J1 (u)
i Row index for F.
8 a ~ e( +ju)
j Column index for F.
a Nontrivial column of F.
11 d+-e~F
b (bjF)j = a.
12 e+-e~F
e (ejF)j = a.
13 e : (d V e) ~ d (djF)j = e.
15 b~a~F
Legend
16 e ~a ~ F
17 L e : (b V e V d V e) ~
f(x) = (((h 1\ (b ~
-/\
r * (h 1\ (c ; r) * (d ~ r
(7.27")
r = T v (ujx)
a (a i~ eO)
1 0 0 0 0 1
(i I) (i) (I)
1 0 1 1 1 1
F = 1 0 1 1 1 1
0 0 0 0 0 0
b= (0 0 0 1 1 1 0 1)
c = (I 1 0 0 0 0 1 0)
d= (0 0 0 0 0 0 0 0)
The program may be made completely general (i.e., for y(f, by simply 0
replacing EO with y( 1, 0) in the foregoing steps, and replacing E with
yeO, 0) in steps 8, 9, 10, and 12.
REFERENCES
Ashenhurst, R. L., (1957), "The Decomposition of Switching Functions," Proc. 0.( all
International Symposiunl on the Theory of Switchin/{, Harvard University, pp. 74
116.
Exercises 263
Calingaert, P., (1960), "'Switching Function Canonical Forms Based on Commutative
and Associative Binary Operations," presented at the October meeting of the
American Institute of Electrical Engineers.
Muller, D. E., (1954), "'Application of Boolean Algebra to Switching Circuit Design
and to Error Detection," Trans. Institute of Radio Engineers, New York, vol.
EC-3, pp. 6-12.
EXERCISES
S.1 CONVENTIONS
Basic conventions
(a) I-origin indexing assumed in this summary.
(b) Controlling variables appear to the left, e.g., ujx, b y, k t x, and
u r x.
(c) Dirnension n may be elided (if determined by compatibility) fronl
e(n), ek(n), ak(n), wk(n), and lj(n).
(d) The parameter j may be elided from operators Ij, OJ' .L, and i j , and
from the vector li if j is the index origin in use.
(e) The parameter k may be elided from k t x if k = 1.
Branching conventions
jf
(a)
Ix:y~
I
The statement to which the arrow leads is executed next if (x/!!y)
= I; otherwise the listed successor is executed next. An unlabeled
arrow is always followed.
(b) x : y, r ~ S
S.3 RELATIONS
Equality a=b a and b are identical 1.15
Membership aEb a = b i for some i
Inclusion b;2a aj E b for aU j
as;b
Strict inclusion b:J a b ;2 a and a ~ b
acb
Similarity b={1 b ;2 a and a ;2 b
Complementary The relation which holds if and only
relations if ff/l does not. Examples of comple
mentary pairs~ E, ; ~, =P; >', >.
Combined (ored) A list of relations between two vari
relations ables is construed as the or of the
relations. Thus xc:::> y is equivalent
to x :$ y. When equality occurs as
one of the ored relations, it is indi
cated by a single inferior line, e.g.,
~ and s;.
Z +-xy
c+-aXu
C +-au
V(lV) = h
Prefix of weightj W +- aj(h)
First k of Wi are unity
where k = min (j, h).
Suffix of weightj w+-wj(h) Last k of Wi are uni ty
where k = min (j, h).
Maximum prefix W +- 'Y./u W is the max length prefix in fl.
Example:
1.1
Example:
C ~Ab C j = A bj l.17
C + B Sit A Ci = Bi S/tAi
K ~ 0/t/X Ki = (),JXi
1.10
W~rJ./U Wi = rJ./Ui
W~w/U Wi =w/Ui
W ~ a/U Wi = a/Ui
W~T/U Wi = T/Ui
W~UrX Wi = Uir Xi l.18
W~U[X Wi = Uit Xi
Summary of notation 271
C+--AGJffiB C j = A j CD B j 1.9
C <- u//B C j = u/B j
c+--V//B c = V1/B 1 8:) ...
... CD V..IBv
C +-- u\\B ii//C = 0, u//C = B
C <- V\\b iJ//c = 0, V//C = b
C +-- \\A, u, B\\ ii//C = A, u//C = B
C +-- \ la, V, b\\ U//C = a, V//C = b
C~ //A, u, B// ii//C = iii/A, u//c = u//B
C //A, V, B//
+-- Ui/c = Vi/A, V//C = V//B
C +-- I/a, V, b// C = IE.I\a, V, E.\\b/
W
+- [](p, q)
[)(P,q)
Wi
:J
W~+l-j
t
= (i + j ~ j
Elision of
P and q if
dimensions
W~+l-iJm =
+- m),
Lower left W +- ~(p,q) min (p, q)) determined
by
Lower right W ~- [](p,q) Wf1+ 1 - i
v+l-i compatibility.
S.8 TRANSPOSITION
Diagonal C+-B
C+-B
, z =
C.i 1.12
/'
Counter diagonal C+-B cV(B)+l-~ = Bi
f1(B)+l-z J
t i
Vertical C+-B Cf'(B)+l-i
Vector Y +- x Yi = x v+ 1
i
t
Y +- x
Union c+-bua c = b CD (a ~ b)
S.ll FILES
File <D/ A representation of a of the form 1.22
(Pv aI' P 2 , a 2 , , av(a), PV(ll) + 1, 0,
File array
Full (T> Array offiles <1>/, for i E ll(/Il),
j E ll(v(D)).
Row Row of files <I>/, for} E ll(v(<l).
Column Column of files <1>/, for i E l\/I(<I).
Compression
Row u/(f) Selection as in corresponding opera
Colunln u//(I> tions on nlatrices.
274 Summar.y of notation
S.12 TREES
Path i C +-Ai C 1 is the i 1 th root of A; c j is the ijth 1.23
node of the nodes on level j reach
able from node c j _ 1 .
Node i C +- (Ai)ll(i) The final node of path Ai.
Full tree W +-- E Each node ofW is unity and the ~ 1.23
structure of W is deternlined by
compatibility.
W +-- E(k) Each node of W is unity; W is honlo
geneous (i.e., all nodes on any level
have a comnlon degree) and 'J(W) = k.
Zero tree W +-- E Each node of W is zero and the
W +--0 structure of W is deternlined by
compatibility.
W +- E(k) Each node of W is zero; W is honlo
geneous and v(W) = k.
Path tree W +-- UE u/W = 0; u/W = E; structure ofW
determined by conlpatibility.
W +-- UE(k) u/W =0; u/W = E; Wishonlo
geneous and 'J(W) = k.
Level tree W +-- u E u//W = 0, u//W = E; structure of W
determined by conlpatibility.
W +--uE(k) u//W = 0; u//W = E; W is honlo
geneous and 'J(W) = k.
Maximization W+--VrA W = U 1\ (A = 111E), where 111 is the
Minimization W+--VlA maximum (mininlum) over all nodes
of VIA.
Maximum path W ~- ex/V W is obtained from V by zeroing all
prefix nodes of every subtree rooted in a
zero node.
Maximum path W +-- w/V W is obtained fronl V by zeroing
suffix every node which contains a zero
node in its subtree.
Forward path W ~- a/A (Wi)l'(i) = 1 if (Ai),'(i) differs fronl all
set selector preceding nodes of path Ai.
Backward path W *--T/A (Wi)"(i) = 1 if (Ai)l'(i) differs fronl all
set selector other nodes of its subtree.
W ~- ex//V j j
Maximum level e /W = a/e /V
prefix
Maximum level W ~- w//V ej/W = w/ej/U
suffix
Forward level W+--a//A ej/W = a/ej/A
set selector
Backward level W ~- T//A ej/W = T/ej/A
set selector
Index
of a vector, 21
reduction, 17
Decrement, 78, 83
Equivalent formulas, 166
Degree, 47
Euclidean algorithm, 67
Design, hardware, 71
of serial sorti ng, 199
logical, 101
Exchange sort, 212
systern, 71
Exclusive disjunctive canonical fonn,
Dickson, L. E., 65
Exclusive-or, 12
Difference, set, 29
reduction, 17
Din1ension, 5, 13, 38
Execution, instruction, 72, 77
-ordered pool, 11 7
sequence, 2, 5
colurnn, 14
tin1e of a sort, 199
elision of, 16
Exit arrow, 3
Directed graph, 46
Expected nurnber, of strings, 200
Director, 159
Disjoin t cycles, 32
vectors, 29
Fanlilies of operations, 8
Fields, 205
Distributor, 78
Domain, 10
variables, 40
end of, 90
Dynamic relocation, 74
stack, 172
of a rna trix, 14
Filial-heir chain list, 127
staten1ent, 160
First-order con1pa tibili ty, 171
End of ftIe, 90
Fonnula, 163
Entry arrow, 3
nlinimax fonn, 166
function, 12
singular, 164
280 Index
Fortuitous function, 133 Identity, permutation vector, 33
Forward chain, 113, 117 Immediate branching, 181
Fraction, scan, 135, 140 Implicit allocation, 108
Freiman, C. V., 154 specification, 38
Friend, E. H., 233, 245 I nclusion, set, 28
Full list matrix, 50 Index, 31
rna trix, 22, 26
accumulator, 73, 79, 83
vector, 15
graph, 48
inverse, 37 Indexability, 76
key, 176 Indexing, a-origin, 71, 186
magnitude, 11 1-origin, 12, 16
mapping, 34 additive, 73
program, 159, 169 j-origin, 14, 43
sign, 11 operation, 31, 36
trivial, 248 Indicator, busy, 94
unit, 248 fetch mode, 95
zero, 248, 253 limit, 90
Indirect addressing, 32, 74, 76, 84
Gap, record, 98 Infix vector, 16
Garner, H. L., 67 Information store, 72
Gassner, B. J., 239 Initial node, 46
Genera tor, 159 Initialization, 6
Gilstad, R. L., 198 Input file, 41
Goldstine, H. H., 1, 178 Input-output channel, 75, 95
Graeffe method, 65 instructions, 88
Graph, directed, 46 I nsertion, of an instruction, 75
index, 48 ranking by, 213
Gray code, 63, 104 sort, 212
Greek letters, 9 Instruction, arithmetic, 84
Grid matrix, 106, 118 branch, 73, 80
Group, abelian, 69 channel, 100
rota tion, 26 cOlnputer, 71
convert, 87
Hanan, M., 154 counter, 73
Harary, F., 132 enable, 75
Hardware designer, 71 execution, 72, 77
Height of a tree, 47 fetch, 72, 76
Homogeneous tree, 58, 121, 226 input-output, 88
Huffman code, 51 logical, 78, 83
Huffman, D. A., 123, 129, 132 shift, 85
Integral variable, 10, 13
Identities, logical, 246
Interchange, 7
Identity function, 253
Interchange sort, 232
matrix, 26
Interlock, channel-uni t, 95
Index 281
Internal sorting, 176, 204, 205, 212, 231, Left list, 122
239 rotation, 8
Interpreter, 160 Length, of a path, 46
Interruption, 75, 81 scan, 135, 148
Intersection, set, 28 Level compression, 54, 60
Interval vector, 15, 33 of a tree, 46
Intrinsic vector, 248, 261 tree, 57
Inverse characteristic vector, 253 of structure, 39
functions, 37 LIFO discipline, 117
mapping, 34 Limit indicator, 90
permutation, 33, 139 Linear representation, 108, 110, 118
Italics, 10 List, chain, 121
Iterns, 176 column, 22
dummy, 179 index, 51, 60
eligible, 179, 181, 213, 229 leaf, 122, 145
relatable, 177, 189 left, 122
Iteration, 4 matrix, 49, 108, 121, 161
Iverson, K. E., 69, 129 processing, 110
right, 226
j-origin indexing, 14, 43 row, 22
Jacobsen, W. H., 146 vector, 122, 163
Jacobson, N., 28, 65, 69 well formed, 52
Johnson, L. R., 121, 148, 155 Literals, 9
Jordan box, 68 Load and store, 78
elimina tion, 68 Logic, symbolic, 24
Justification, 23 Logical design, 101
iden ti ties, 246
Key, 133, 176 instructions, 78, 83
clustering, 154 operations, 11
derived, 134, 144, 148 trees, 60
multiple, 143, 155 variable, 10, 13
transformation, 134, 144, 147 vector, 18
vector, 176, 189 Loop, 2
Kronecker delta, 12 Lukasiewicz, J., 160, 163
Kunz, K. S., 64 Lukasiewicz notation, 122, 161, 169
full, 22, 26
Nagler, H., 195,242
grid, 106, 118
Newton-Raphson formula, 64
identity, 26
Node, 46
index, 49, 53, 73, 125
vector, 46, 49, 226
list, 49, 108, 121, 161
Nonlinear represen ta tion, 109
nlultiplication, 4
N ormaliza tion, 23
product, 23, 33
Notation, Lukasiewicz, 122, 161, 169
row-chained, 120
parenthesis, 161, 169
sparse, 22, 120
parenthesis-free, 122, 163
special, 26
Polish, 122, 163
specific, 251
sunlnlary, 7
superdiagonal, 26
translation of, 169, 173
trace of, 27
Null character, 31, 58
triangular, 26
element, 6, 10, 16, 32
Mauchly, J. \V., 178, 191 scan, 25, 28
Maximal path, 46 vector, 16, 17
Maximization, 36, 58 N umber base, choice of, 190
Maximum prefix, 9, 22, 58 NurTIber systerTI, 12, 14, 27
suffix, 22, 58, 165 Numerical variable, 10, 13
McCracken, D. D., 195
Member of a vector, 28 Odd-even transposition sort, 213, 232
Mernory, 72 Oettinger, A. G., 171
allocation, 105 Open addressing system, 153
Merge, 177 Operand, classes, 8
sinlple, 177, 191,205 structured, 8, 13, 45, 106
single-phase, 179, 183, 206 Operation, arithmetic, 11
sort, 178, 199, 204 binary, 13
string-doubling, 186 code, 72
two-phase, 179 column, 8, 18
IVlesh, 19, 110 compound, 8
Metaprogranl, 71, 159 copy, 177, 189
Microprograms, 71 count, 212
Mid-square method, 154 elerTIentary, 11, 13
Minimax form of -formula, 166 families, 8
Minimization, 36, 58 indexing, 31, 36
Minterm vector, 249, 257, 258 logical, 11
Miscellaneous symbols, 10 mapping, 58
Mixed radix, 27 ordering, 36
1\10ment, of a tree, 47 rank-preserving, 177, 189, 231
vector, 47, 60, 125 ranking, 31, 34, 133
Moore school, 178, 191 revision, 183
Muller, D. E., 252 row, 8, 18
Multiple keys, 143, 155 set, 28
scalar, 13 shift, 78
Index 283
moment, 47
identity permutation, 33
ordered, 45
index, 48, 60
path, 57
interval, 1.1, 33
permutation, 58
intrinsic, 248, 261
reduction, 58
key, 176, 189
representation, 121
list, 122, 163
rooted, 47
logical, 18
Triangular matrix, 26
moment, 47, 60, 125
merge, 179
of rna trices, 39
path, 46
Union, 28
physical, 105
vector, 15, 30
program, 7
Univac, 106
root, 52
selection, 22
Variable, 9
similar, 29
arbitrary, 10, 13
special, 15
auxiliary, 38
specific, 249, 258
integral, 10, 13
stack, 166, 170
logical, 10, 13
tag, 79
numerical, 10, 13
unit, 15, 30
representation, 105
Vector, 13
chaining, 155
disjoint, 29
\Vright, H. N., 12
dispersion, 47, 59
filial, 128
Zero, extension of a path, 61