~. ~ ~ . . . . .
G. E. FORSYTHE, Editor
ALGORITHM 230 m a t r i x . If t h e m a t r i x is s i n g u l a r t h e p r o c e d u r e e x i t s to a n a p p r o -
MATRIX PERMUTATION p r i a t e label in t h e m a i n p r o g r a m ;
begin i n t e g e r i,j,k,l,pivi,pivj,p; r e a l pivot; i n t e g e r a r r a y
J. BOOTHROYD (Reed 18 Nov. 1963)
r,c[1 :hi ;
English Electric-Leo Computers, Kidsgrove, Stoke-on- comment s e t row a n d c o l u m n i n d e x v e c t o r s ;
Trent, England f o r i : = 1 s t e p 1 u n t i l n d o r[i] : = c[i] : = i;
procedure matrixperm(a,b,j,k,s,d,n,p); value n; r e a l a,b; comment find i n i t i a l p i v o t ; pivi : = pivj : = 1;
i n t e g e r a r r a y s,d; integcr j,k,n,p; for i := 1 step 1 until n do for j := 1 step 1 until n do
eonlmcnt a procedure using Jensen's device which exchanges i f abs (a[i,j]) > abs (a[pivi,pivj]) t h e n b e g i n pivi : = i;
rows or c o l u n m s of a m a t r i x to a c h i e v e a r e a r r a n g e m e n t s p e c i f i e d pivj : = j e n d ;
b y t h e p e r m u t a t i o n v e c t o r s s,d[1 :n]. E l e m e n t s of s s p e c i f y t h e eomment start reduction;
original s o u r c e l o c a t i o n s while e l e m e n t s of d s p e c i f y t h e d e s i r e d for i := 1 step 1 until n do
d e s t i n a t i o n l o c a t i o n s . N o r m a l l y a a n d b will be c a l l e d as s u b - b e g i n l : = r[i]; r[i] : = r[pivi]; r[pivi] : = l; l : = c[i];
s c r i p t e d v a r i a b l e s of t h e s a m e a r r a y . T h e p a r a m e t e r s j,k n o m - c[i] : = c[pivj]; cipivj] : = l;
i n a t e t h e s u b s c r i p t s of t h e d i m e n s i o n a f f e c t e d b y t h e p e r m u t a - i f eps > abs (a[r[i],c[i]]) t h e n
t i o n , p is t h e J e n s e n p a r a m e t e r . A s a n e x a m p l e of t h e u s e of t h i s begin comment here include an appropriate output pro-
p r o c e d u r e , s u p p o s e r,c[1 :n] to c o n t a i n t h e row a n d c o l u m n s u b - c e d u r e to r e c o r d i a n d t h e c u r r e n t v a l u e s of r [ l : n ] a n d
s c r i p t s of t h e s u c c e s s i v e m a t r i x p i v o t s u s e d in a m a t r i x i n v e r - e l l : n ] ; g o t o singular e n d ;
s i o n of a n a r r a y a[l:n,l:n]; i.e. r[1], c[1] a r e t h e r e l a t i v e s u b - f o r j : = n s t e p --1 u n t i l i + 1 , i - - I s t e p --1 u n t i l 1 d o a[r[i],c[j]]
s c r i p t s of t h e first p i v o t r[21, c[2] t h o s e of t h e s e c o n d p i v o t a n d : = a[r[i),cij]]/air[i],cli]]; alrli],c[i]] : = 1/a[r[i],c[i]];
so on. T h e t w o calls pivot : = 0;
matrixperm (a[j,p], a[k,p], j,k,r,c,n,p) for k := 1 step 1 until i--l, i+1 step 1 until n do
a n d matrixperm (alp,j], alp,k], j,k,c,r,n,p) b e g i n f o r j : = n s t e p --1 u n t i l i-t-l, i - - 1 s t e p --1 u n t i l 1 d o
will p e r f o r m t h e r e q u i r e d r e a r r a n g e m e n t of rows a n d c o l u m n s b e g i n a[r[k],c[j]] : = a[r[k],c[j]] -- afr[i],c[j]] X a[r[k],c[i]];
respectively; i f k > i /k j > i A abs (a[r[k],c[j]]) > abs (pivot) t h e n
b e g i n i n t e g e r a r r a y tag, loc[1 :n]; i n t e g e r i,t; r e a l w; b e g i n pivi : = k; pivj : = j ;
comment set up initial vector tag number and address arrays ; pivot : = a[r[kJ,c[j]] e n d c o n d i t i o n a l
f o r i : = 1 s t e p 1 u n t i l n d o tag[i] : = loc[i] := i; end jloop ;
comment start permutation; a[r[k],c[iJl : = -a[rli],c[i]] X a[r[k],c[i]]
for i := 1 step 1 until n do e n d kloop
b e g i n t : = s[i]; j : = loc[t]; k : = d[i]; end /loop and reduction;
ifj#k then begin for p := 1 step 1 until n do comment r e a r r a n g e r o w s ; matrixperm (a~],p],a[k,p],j,k,r,c,n,p);
b e g i n w : = a; a : = b; b : = w c u d ; comment rearrange columns;
tag[j] : = tag[k]; tag[k] : = t; matrixperm (a[p,jl,a[p ,k ],j,k,c,r ,n,p )
loc[t] : = loc[tag[jJ]; loc[tag[jl] : = j end matrixinvert
e n d jk c o n d i t i o n a l
e n d i loop [EDITOR'S NOTE. O n m a n y c o m p i l e r s matrixinvert w o u l d r u n m u c h
e n d matrixperm f a s t e r if t h e s u b s c r i p t e d v a r i a b l e s rill, c[i], r[k] were r e p l a c e d b y
s i m p l e i n t e g e r v a r i a b l e s ri, ci, rk, r e s p e c t i v e l y , i n s i d e t h e j l o o p . - -
G.E.F.]
ALGORITHM 231 '~
MATRIX INVERSION
A L G O R I T H M 232
J. BOOTHROYD (Reed 18 Nov. 1963)
HEAPSORT
English Electric-Leo Computers, Kidsgrove, Stoke-on-
J. W. J. WILLIAMS (Recd 1 Oct. 1963 and, revised, 15
Trent, England
Feb. 1964) .... :
procedure matrixinvert (a,n,eps,singuIar); v a l u e n,eps; a r - Elliott Bros. (London) Ltd., Borehamwood, Herts, Eng-
r a y a; i n t e g e r n ; r e a l eps; l a b e l singular;
eomn~ent i n v e r t s a m a t r i x in i t s o w n s p a c e u s i n g t h e G a u s s -
land
J o r d a n m e t h o d w i t h c o m p l e t e m a t r i x p i v o t i n g . I.e., a t e a c h comment The following procedures are related to T R E E S O R T
s t a g e t h e p i v o t h a s t h e l a r g e s t a b s o l u t e v a l u e of a n y e l e m e n t in [R. W. F l o y d , Alg. 113, Comm. A C M 5 (Aug. 1962), 434,. a n d
t h e r e m a i n i n g m a t r i x . T h e c o o r d i n a t e s of t h e s u c c e s s i v e m a t r i x A. F. K a u p e , J r . , Alg. 143 a n d 144, Comm. A C M 5 (Dec. 1962),
p i v o t s u s e d a t e a c h s t a g e of t h e r e d u c t i o n a r e r e c o r d e d in t h e 604] b u t a v o i d t h e u s e of p o i n t e r s a n d so p r e s e r v e s t o r a g e s p a c e .
s u c c e s s i v e e l e m e n t p o s i t i o n s of t h e r o w a n d c o l u m n i n d e x All t h e p r o c e d u r e s o p e r a t e o n s i n g l e ~word i t e m s , s t o r e d as
v e c t o r s r a n d c. T h e s e a r e l a t e r c a l l e d u p o n b y t h e p r o c e d u r e e l e m e n t s 1 to n of t h e a r r a y A . T h e e l e m e n t s a r e n o r m a l l y so
matrixperm w h i c h r e a r r a n g e s t h e rows a n d c o l u m n s of t h e a r r a n g e d t h a t A[i]=<A[j] for 2 ~ j ~ n , i = j + 2 . S u c h a n a r r a n g e -
Volume 7 / Number 6 / June, 1964 Communications of the ACM 347
m e n t will be called a heap. All] is always the least element of end
the heap. end;
The procedure S E T H E A P arranges n elements as a heap, A[i] := in
I N H E A P adds a new element to an existing heap, O U T H E A P end I N H E A P ;
extracts the least, element from a heap, and S W O P H E A P is procedure O U T H E A P (A,n,out) ;
effectively the result of I N H E A P followed b y O U T H E A P . I n integer n; r e a l out; real array A;
all cases the array A contains elements arranged as a heap on comment given array A, elements 1 to n of which form a heap,
exit. n_>l, O U T H E A P assigns to out the value of A[1], t h e least
S W O P H E A P is essentially the same as the t o u r n a m e n t sort m e m b e r of the heap, and rearranges the remaining members
described by K. E. I v e r s o n - - A Programming Language, 1962, as elements 1 to n - 1 of A. Also, n is a d j u s t e d accordingly;
pp. 223-226--which is a top to b o t t o m m e t h o d , b u t it uses an b e g i n S W O P H E A P (A,n--1, A[n],out);
improved storage allocation and initialisation. I N H E A P re- n := n--1
sembles T R E E S O R T in being a b o t t o m to top m e t h o d . H E A P - end O U T H E A P ;
SORT can t h u s be considered as a marriage of these two p r o c e d u r e S E T H E A P (A,n) ;
methods. v a l u e n; integer n; r e a l a r r a y A ;
The procedures may be used for replacement-selection sort- c o m m e n t S E T H E A P rearranges the elements All] t o A[n]
ing, for sorting the elements of an array, or for choosing the to form a h e a p ;
current m i n i m u m of any set of items to which new items are begin integer j ;
added from time to time. The procedures are the more useful j:=l;
because the active elements of the array are m a i n t a i n e d densely L: INHEAP(A,j,A[j+I]);
packed, as elements All] to A[n]; ifj<n t h e n go t o L
p r o c e d u r e S W O P H E A P (A,n,in,out); end S E T H E A P
v a l u e in,n; integer n; r e a l in,out; r e a l a r r a y A;
c o m m e n t S W O P H E A P is given an array A, elements A[1]
to A[n] forming a heap, n=>O. S W O P H E A P effectively adds
the element in to the heap, extracts and assigns to out
ALGORITHM 233
the value of the least member of the resulting set, and leaves SIMPSON'S RULE FOR M U L T I P L E
the remaining elements in a heap of the original size. I n INTEGRATION
this process elements 1 to ( n + l ) of the array A may be dis- FRANK OLYNYK* (Recd 24 Dec. 1963)
turbed. The maximum n u m b e r of repetitions of the cycle Case Institute of Technology, Cleveland, Ohio
labeled scan is logan; *Partially sponsored by the National Science Foundation under Grant GP-642.
begin integer i,j; real letup, letup 1;
i f in~A[1] t h e n out := in e l s e real p r o c e d u r e Simps (X, xl, x2, delta, f);
begin i := 1; v a l u e xl, x2, delta; r e a l X, xl, x2, delta, f;
A [ n + l ] := in; c o m m e n t this last s t a t e m e n t is only c o m m e n t This procedure calculates a single integral by Simp-
necessary :in case j = n at some stage, or n = 0 ; son's rule in such a way t h a t it can be called recursively for t h e
out := A[1]; evaluation of an i t e r a t e d integral, xl and x2 are the lower a n d
scan: j := i + i ; upper limits, respectively, which m a y be any m a t h e m a t i c a l l y
if j ~ n then meaningful expressions. Hence in using Simps for multiple
begin temp := A[j]; i n t e g r a t i o n the region is not limited to rectangular boxes. The
temp 1 := A [ j + i ] ; algorithm t e r m i n a t e s when two successive evaluations pass t h e
i f temp 1 <temp t h e n t e s t involving delta. The formal p a r a m e t e r f stands for t h e
begin letup := lemp 1; expression to be integrated.
j := j + l As an example of the use of Simps,
end; 1
i f temp < in t h e n fo d x f o (1-~')'g(x'y) dy
b e g i n A[i] := temp;
i:=j; would be evaluated by
go t o scan Simps(x, O, 1, delta, Simps(y, O, sqrt(1 - xT2), delta2, g(x, y))).
end Simps has been w r i t t e n and run in ALGOL 60 on t h e Univac
end; 1107 at Case I n s t i t u t e .
A[i] := in [EDITOR'S NOTE. Experience of W. M e K e e m a n suggests t h e
end wisdom of choosing delta2 < delta.--G.E.F.];
end S W O P H E A P ; begin
p r o c e d u r e I N H E A P (A, n, in); Boolean turing; real zl, z2, z3, h, k;
value in; integer n; real in; real array A; turing := f a l s e ;
c o m m e n t I N H E A P is given an array A, elements A[1] to ifxl = x2thenbeginzl :~ 0; go t o b o x 2 e n d ;
A[n] forming a heap and n_>_O.I N H E A P adds the element in ifxl > x2thenbeginh := x l ; xl := x2; x2 := h;
to the heap and adjusts n accordingly. The cycle labeled turing := t r u e end;
scan m a y be r e p e a t e d log2n times, but on average is r e p e a t e d X := xl; zl := f; X := x2; z3 := zl := zl + f;
twice only i k := x2 -- xl;
b e g i n integer i,j; box :
i := n : = n + l ; z2 := 0; h := k/2;
scan : i f i > 1 t h e n f o r X := xl + h s t e p k u n t i l x 2 d o z2 := z 2 + f ;
begin j := i + 2 ; zl := zl + 4 X z 2 ;
i f in<A[j] t h e n i f h X abs((zl -- 2 X z 3 ) / ( i f zl = 0 t h e n 1.0 e l s e zl)) < delta
b e g i n Ali] := A[j]; t h e n go to box2
i := j ; e l s e z3 := zl;
go t o scan zl := zl -- 2 X z2;
348 C o m m u n i c a t i o n s o f t h e ACM V o l u m e 7 / N u m b e r 6 / J u n e , 1964
k:=h; 2. T h e i d e n t i f i e r G A U S S - S E I D E L may not contain a hyphen.
go to box; 3. I n t h e f o u r t h l i n e a f t e r t h e label START c h a n g e y[i] t o
box2: Y[i].
if turing then h := --h; With the above errors corrected, GAUSS SEIDEL was suc-
Simps := h × zl/3 c e s s f u l l y r u n o n t h e S t a n f o r d 7090 c o m p u t e r i n W i r t h ' s E x t e n d e d
end Simps ALGOL, a n d o n t h e V i r g i n i a ALGOL c o m p i l e r f o r t h e B u r r o u g h s
2O5.
CERTIFICATION OF ALGORITHM 40 The following improvements would be desirable.
I. A v o i d r e p e a t e d r e f e r e n c e t o t h e s u b s c r i p t e d v a r i a b l e Y[i]
CRITICAL PATH SCHEDULING [B. Leavenworth, inside the j loop.
Comm. ACM ~ (Mar. 1961), 152; 4 (Sep. 1961), 392; 2. P e r m i t t h e u s e r t o i n i t i a l i z e t h e a r r a y X t o a n a p p r o p r i a t e
(Oct. 1962), 513] v a l u e a t t h e s t a r t of t h e i t e r a t i o n .
IRVIN A. HOFFMAN (Recd 7 Feb. 1964) 3. M o d i f y tol t o b e a r e l a t i v e e r r o r , r a t h e r t h a n a n a b s o l u t e
Woodward Governor Co., Rockford, Ill. error.
4. I n c o r p o r a t e a g u a r d a g a i n s t n o n c o n v e r g e n c e .
The Critical Path Scheduling algorithm was coded in FAST for
the NCR315. The modifications suggested by Alexander [Comm.
ACM 4 (Sept. 1961)] were included. Results were correct in all
tested cases. However, the example of the I, J vectors given in
the comment is incorrect, as it would cause the exit out3 - I~
missing.
Revised Algorithms Policy • M a y , 1964
[EDITOR'S NOTE. T h e r e a r e a l s o t w o s e m i c o l o n s w h i c h s h o u l d A contribution to the Algorithms department must be in the form of
an algorithm, a certification, or a remark. Contributions should be sent in
b e r e m o v e d f r o m t h e c o m m e n t of A l g o r i t h m 4 0 . - - G . E . F . ]
duplicate to the editor, typewritten double-spaced in capital and lower-case
letters. Authors should carefully follow the style of this department, with
especial attention to indentation and completeness of references. Material
CERTIFICATION OF ALGORITHM 201 to appear in bold-face type should be underlined in black. Blue underlin-
SHELLSORT [J. BOOTHROYD, Comm. A C M 6 (Aug. ing may be used to indicate italic type, but this is usually best left to the
Editor.
1963), 445] An algorithm must be written in the ALGOL 60 Reference Language
M. A. BATTY (Recd 27 Jan. 1964) [Comm. A C M 6 (Jan. 1963), 1-17], and normally consists of a commented
procedure declaration. Each algorithm must be accompanied by a cmnplete
English Electric Co., Whetstone, Nr. Leicester, England driver program in ALGOL 60 which generates test data, calls the procedure,
and outputs test answers. Moreover, selected previously obtained test answers
T h i s a l g o r i t h m h a s b e e n t e s t e d s u c c e s s f u l l y u s i n g t h e DEUCE
should be given in comments in either the driver program or the algorithm.
ALGOL C o m p i l e r . W h e n t h e f i r s t s t a t e m e n t of t h e a l g o r i t h m w a s The driver program may be published with the algorithm if it would be of
replaced by the statement major assistance to a user.
m := n; Input and output should be achieved by procedure statements, using
one of the following five procedures (whose body is not specified in ALGOL) :
t o i m p l e m e n t S h e l l ' s o r i g i n a l c h o i c e of m l : = n / 2 , a s l i g h t i n c r e a s e procedure inreal (channel, destination)" value channel; integer channel;
i n s o r t i n g t i m e w a s o b s e r v e d w i t h m o s t of t h e c a s e s t e s t e d . real destination; c o m m e n t the number read from channel channel is
assigned to the variable destination; . . . ;
procedure outreal (channel, source); value channel, source; Integer channel;
REMARK ON ALGORITHN[ 214 real source; c o m m e n t the value of expression source is output to channel
channel; . . . ;
q-BESSEL FUNCTIONS In(t) [J. M. S. SimSes Pereira, procedure ininteger (channel, destination);
Comm. A C M 6 (Nov. 1963), 662] value channel; integer channel, destination; . . . ;
procedure outinteger (channel, source);
J. M. S. SIM5ES PEREmA (Recd 6 Jan 1964) value channel, source; integer channel, source; . . . ;
Gulbenkian Scientific Computing Center, Lisbon, Portu- procedure outstring (channel, string); value channel; integer channel;
s t r i n g string; . . . ;
gal If only one channel is used by the program, it should be designated by 1.
Corrections : Examples:
outst~ing (1, 'z ='); outr~l (I, x);
1. Insert a dummy statement labeled C just before the final end. for i := 1 step 1 u n t i l n d o outreal (1, A[i]);
2. Add a statement go to C just before the label B. ininteger (1, digit [17]);
3. Add a colon in the clause for k := 1 step 1 until j do ... It is intended that each published algorithm be a well-organized, clearly
commented, syntactically correct, and a substantial contribution to the
ALGOL literature. All contributions will be refereed both by human beings
CERTIFICATION OF AND REMARK ON and by an ALGOL compiler. Authors should give great attention to the cor-
rectness of their programs, since referees cannot be expected to debug them.
ALGORITHM 220 Because ALGOL compilers are often incomplete, authors are encouraged to
GAUSS-SEIDEL [P. W. Shantz, Comm. ACM 6 (Dec. indicate in comments whether their algorithms are written in a recognized
subset of ALGOL 60.
1963), 739] Certifications and remarks should add new information to that already
A. P. BATSON (Reed 6 Jan. 1964) published. Readers are especially encouraged to test and certify previously
uncertified algorithms. Rewritten versions of previously published algo-
University of Virginia, Charlottesville, Vs. rithms will be refereed as new contributions, and should not be imbedded
NIKLAUS WIRTIt (Recd 6 Jan. 1964) in certifications or remarks.
Galley proofs will be sent to the authors; obviously rapid and careful
Computer Science Div., Stanford U., Stanford, Calif. proofreading is of paramount importance.
Although each algorithm has been tested by its author, no liability is as-
[EDITOR'S NOTE. TWO s u b s t a n t i a l l y e q u i v a l e n t c o n t r i b u t i o n s
sumed by the contributor, the editor, or the Association for Computing
were received on the same day, and so the editor has merged Machinery in connection therewith.
them.--G.E.F.] The reproduction of algorithms appearing in this department is explicitly
permitted without any charge. When reproduction is for pdblication pur-
The following errors were detected. poses, reference must be made to the algorithm author and to the Communi-
1. T h e p r o c e d u r e c a n n o t c o m m u n i c a t e t h e s o l u t i o n t o t h e o u t - cations issue bearing the algorithm.--G.E.F.
s i d e b l o c k u n l e s s X (or Y) is m a d e a p a r a m e t e r of t h e p r o c e d u r e .
Volume 7 / Number 6 / June, 1964 Communications of the ACM 349