Sei sulla pagina 1di 6

M. S.

LYNN, Editor

Matrix Reduction Using the elements of A, i.e. r nonzero elements such t h a t no two
occur in the same row or colmnn. An n-partial solution
Hungarian Method For The defines a solution or possible schedule for the class, teacher
or hour which owns the array A. An examination of A
Generation of School Timetables has two stages:
Stage 1. T h e existence of at least one possible sched-
JOHN LIONS ule is determined (Feasibility Test);
J. Kates & Associates, Toronto, Ontario, Canada Stage 2. Any nonzero element which does not be-
long to some possible schedule is changed to zero
(Matrix Reduction).
The application of Kuhn's Hungarian Method to the problem
The problem of Stage 1 is already well known as part of
of matrix reduction as needed in Gotlieb's method for time-
the Assignment problem (for which a bibliography is
table generation is described. The method is suited to both
given in [2]). This paper is based on the " H u n g a r i a n
hand and computer calculation. Devices to improve the effi-
M e t h o d " for the Assignment problem first given b y H. W.
ciency of the basic algorithm are discussed.
K u h n [3], and also described by Friedman and Yaspan [4].
1. I n t r o d u c t i o n I n the context of the timetable problem, Stage 2 is the
important problem, consuming a very high percentage of
The scheme proposed by C.. C. Gotlieb [1] for the solu-
the computation effort, and the need for an efficient al-
tion of the high school timetable problem envisages the
gorithm for its execution paramount. For Stage 2, Gotlieb
construction of a 3-dimensional array, each point of which
proposed the method of the "tight set search," whose
represents the meeting of a particular class with a particu-
theoretical basis is a theorem by P. Hall [5] on the exist-
lar teacher at a particular hour of the day. The array
ence of sets of distinct representatives. The tight set
elements m a y be divided into two groups, according as
search is quite efficient when n is small (say n K 10), but
their values are zero or nonzero. (For convenience the
since it requires of the order of 2 = operations it rapidly be-
nonzero values are usually m a p p e d into the single value
comes impractical once n exceeds about 20. Yet m a n y
unity.) An element value of zero signifies the impossibility
schools have over 50 classes, and schools with more than
of the class and teacher meeting at the hour. An element
100 classes are not unknown.
whose value is nonzero signifies the possibility t h a t the
class and teacher m a y meet at the hour. Initially the 3-di- 2. T h e H u n g a r i a n M e t h o d
lnensional array is filled with ones, indicating the possi-
bility t h a t any teacher can meet any class at any hour, but The Hungarian Method for the Feasibility Test in-
as the calculation proceeds ones are changed to zeros volves the iterative application of a procedure called here
according to set rules and predetermined conditions so "EXPAND," whose function is as follows:
t h a t by the end of the computation, at each hour it is Suppose an r-partial solution for the (n X n) array A
possible for each teacher to meet only one class and for is given where r < n, and suppose, in particular, t h a t there
each class to meet only one teacher, and each teacher can is no element of the partial solution in the ith row of A.
meet each class a number of times predetermined for t h a t Then E X P A N D can be used to txT to generate a new,
teacher and t h a t class. The timetable is then inherent in (r--t-1)-partial solution b y choosing an element from the
the resulting 3-dimensional array. ith row and rearranging part or all of the original r-partial
At regular intervals during the computation, Gotlieb's solution. The (rq-1)-partiM solution will involve all the
method requires the examination of each plane section of rows and colulnns involved in the r-partial solution to-
the 3-dimensional array. A plane section belongs to some gether with the ith row and some new colunm. If
class or teacher or hour, and is a 2-dimensional array of E X P A N D fails to generate an (rq-1)-partial solution,
zeros and ones. Moreover it m a y be shown t h a t each array then it can be shown t h a t it will never be possible to find a
is effectively square, in that any seeming excess of rows or solution for the array A. (It m a y be possible to find an
colunms can always be eliminated. (rq-1)-partial solution by choosing another row instead
Suppose A is an (n × n) array of ones and zeros. An of the ith row for E X P A N D , but this possibility is not of
r-partial solution of A is a set of r independent nonzero interest in the present problem.)

Volume 9 / Number 5 / May, 1966 Conmmnications of the ACM 349


For Stage 1 the examination of the array A starts with points are observed:
some given r-partial solution and then involves the ap- (i) Zero elements are ignored.
plication of the procedure E X P A N D successively for (ii) Nonzero elements in a column J for which R E F -
each array row which is not represented in the partial E R E N C E [J] is blank are ignored.
solution until either an n-partial solution is found, or the (iii) The first phase ends as soon as a nonzero ele-
impossibility of so doing has been demonstrated b y the ment is encountered in a column J for which S O L U T I O N
failure of E X P A N D at some point. [J] is blank [cf. (a) above].
Provided Stage 1 has been completed successfully, (iv) If a nonzero element is encountered in a column
Stage 2 can be entered. J for which S O L U T I O N [J] is not blank, but R E F E R -
E a c h nonzero element of A which is not already in the E N C E [J] is blank, then S O L U T I O N [J] is added as a
solution is tested in turn. Consider the testing of an ele- new row into R O W L I S T , and R E F E R E N C E [J] is re-
m e n t in the ith row and j t h column. Obviously this ele- placed b y the n u m b e r of the row currently being searched.
m e n t will belong to some solution of A if there exists a I n Figure 1 an example is shown of a (6 X 6) array of
solution for the subarray A, which is obtained from A b y zeros and ones together with a partial solution of five
striking out the ith row and j t h column. Now the array elements. T h e row with no solution element is the first
A~ is (n-- 1) X ( n - 1) and it is not difficult to see t h a t an and the column with no solution element is the third. I t is
( n - 2 ) - p a r t i a l solution can be obtained for A8 from the not possible to complete the solution immediately since
solution for A. T h e n just one application of the procedure the element in row 1, column 3 is zero.
E X P A N D is needed either to complete the solution for T h e development of the working areas, R E F E R E N C E
A, and hence show t h a t the tested element belongs to a and R O W L I S T , for the example of Figure 1 is shown in
solution of A or to show t h a t the tested element should be Figure 2. E a c h step shown involves the searching of one
changed to zero. (If Stage 1 fails, Stage 2 m a y be regarded row according to the above rules. The first five steps in the
as trivial, since the whole of A m a y be reduced to zeros.) operation of E X P A N D in Figure 2 follow. The value of
The above two paragraphs show the use of the procedure R O W is the n u m b e r of the row being searched.
E X P A N D . A description of this procedure follows, and
STEP I:
then a discussion of ways to reduce the n u m b e r of times (a) R O W = 1 (= R O W L I S T [1]). Nonzero elements are in col-
E X P A N D m u s t be invoked during the matrix reduction. u m n s 1 and 6;
(b) R E F E R E N C E [1] is blank; so replace it by 1 = R O W and
3. P r o c e d u r e E X P A N D put 3 = S O L U T I O N [1] into R O W L I S T ;
(c) R E F E R E N C E [6] is blank; so replace it by 1 = R O W and
Procedure E X P A N D works on the (n × n)-array A. put 6 = S O L U T I O N [6] into R O W L I S T .
A partial solution for A is given in the vector S O L U T I O N
whose J t h element gives the row n u m b e r of the solution STEP 2:
(a) ROW = 3 (= ROWLIST [2]). Nonzero elements occur in
element in column J if one exists or else is blank. Two columns 1, 4 and 6;
working areas are needed, the vectors R E F E R E N C E and (b) REFERENCE [1] is not blank; so the element in column 1
R O W L I S T , which are initially cleared to blanks. R O W - is ignored;
L I S T contains a list of rows to be searched on a first-in- (e) REFERENCE [4] is blank; so replace it by 3 = ROW and
first-served basis. The first m e m b e r of R 0 W L I S T is the put 5 = SOLUTION [4] into ROWLIST;
(d) REFERENCE [6] is not blank; so the element in column 6 is
designated row into which E X P A N D is to introduce a
ignored.
solution element. The procedure m a y be divided into two
STEP 3 :
phases: the first (or forwards) phase involves searching
(a) ROW = 6. Nonzero elements occur in columns 2 and 6, but
various rows selected from R O W L I S T ; the second (or the latter is ignored;
backwards) phase involves retracing and rearranging the (b) REFERENCE [2] is replaced by 6, and 2 is added to ROW-
partial solution. LIST.
When an array row is searched, its elements are ex- STEP 4 :
amined one b y one. The first phase of E X P A N D is com- (a) ROW = 5. All elements are ignored.
plete when either (a) a nonzero element is encountered in STEP 5 :
a column J for which S O L U T I O N [J] is blank; or (b) (a) ROW = 2. Nonzero elements occur in columns 1, 2, 3 and 6.
all rows entered in R O W L I S T have been searched but (b) REFERENCE [1], REFERENCE [2] are not blank; so the
no element satisfying condition (a) has been encountered. elements in columns 1, 2 are ignored.
As soon as (a) occur, the existence is demonstrated of an (c) SOLUTION [3] is blank; so phase one is finished.
(r-t-1)-partial solution, which is actually constructed b y At the end of Step 5, the existence of an expanded partial
the second phase of E X P A N D . If (b) occurs, then the task solution for the array has been established since all ele-
of E X P A N D is not feasible. (Proofs of these results are ment of the array has been found for which condition (a)
not given here. Instead, the interested reader is referred is satisfied. T h e second phase of E X P A N D ("back track-
to [3].) ing") begins with the introduction of the element satisfy-
Rows are selected for searching in turn from R O W L I S T . ing condition (a) into the solution, and continues with a
During the search of an individual row, the following reshuffling until each row appears no more than once in

350 Communications of the ACM Volume 9 / Number 5 / May~ 1966


the array SOLUTION. The steps to be taken obey the
following rules:
(a) Suppose the new element is A [ I , J]. Then set S O L U T I O N
[J] = I.
(b) If I does not appear more than once as a value of an element
in SOLUTION, then the work of E X P A N D is complete.
I~i~,I~FT- ........ (c) However, if, for the starting partial solution for some value
of K, K ~ J , the value of S O L U T I O N [K] is also I, at least one
I I ~ I I I1~1 I further change to the vector S O L U T I O N is needed. The change
REFERENCE ROW LIST is simply to replace S O L U T I O N [K] by R E F E R E N C E [K]. So
Set I = S O L U T I O N [K] = R E F E R E N C E [K]; Set J = K; and
Fro. 1. Sample problem with initialized working areas (unit
Return to Step (b).
elements in the initial partial solution have been underlined)
Steps in the completion of the example already started
A_fte.r
are shown in Figure 3. At each stage, the position of the
Step i~EFEKENCE B.OWLIST entry in S O L U T I O N which is to be changed in the next
step is shown underlined.
I ~1 ~ I I~ I.W-~ ~1 61 I I I The method so far described is a hand method comparing
I xl ~ ~ I I~ I~/~ 61 ~ I I I favorably with that suggested by Friedman and Yaspan
[4]. For computer implementation some modifications
i xl 6 ~ I I~ I~/~//~ ~ I~ I 1
such as replacing "blank" by "zero" in the above discus-
4 I ~1 6V//.,~ 3 I I~ I V#/A///.4P'//,4P#Az I I
sion are needed. I t is ~lso desirable to maintain a list of
I ~1 ~@~ I I~ I solution elements by row as well as by column, so that
FIO. 2. States of working areas after each step of row search those rows without solution elements can be readily identi-
(or first phase of E X P A N D ) fied, and to simplify the backtracking process. The pro-
cedure E X P A N D coded in ALGOL and showing this modi-
fication is given in an Appendix. Since the matrix is
i(EF ER.ENCE
1 0 0 0 0 [~ searched by row it may Mso be attractive to store the
i F [] 0 0 i matrix as a series of lists, one for each row, giving the
0 I 0 1 locations of the nonzero elements of the row.
I
' After SOLUTION
1 I° ~ ° ~ ° 4. S i n g l e - a n d D o u b l e - E n d e d Searches
~o o ~ o o
\~_ 0____0 _>.2__J(~ Procedure E X P A N D searches the linkages between
rows and columns and may be regarded as developing a
Step 8 tree structure which eventually shows a minimum path
between the designated row of the array (which has no
[] = member of final partial solution
solution element) and some column of the array which has
J~ - nlenlber of both partial solutions
no solution element. This minimum path is then retraced
Fm. 3. States of solution FIO. 4. Rearrangement of to rearrange the original partial solution and to create the
v e c t o r after each step of back- elements in partial solution new partial solution. The method of rearrangement is
tracking (or second phase of (arrows show direction of back- illustrated in Figure 4. Note that any row or column with a
EXPAND) tracking)
solution element in the first partial solution is certainly
represented in the second.
BEGIN ~ ! _Z 2 J S_ 6 The working of the example is shown as a multistage
o " o o o o Row graph in Figure 5. Levels in the graph alternately represent
Step l ! ~ ~ ~ ~ ~ Column rows and columns, with the links alternately showing the
(Row = i)
connection of columns to rows, and rows to columns:
Column J is connected to row I if A [ I , J] ~ 0; row I is
~- Column
(ROW = 3) connected to column J if A [I, J] is an element in the first
Step 3 x x x -- x C o iu~IrL
partial solution. If a row is connected to a column which
(ROW = 6) has Mready been entered (this is the condition R E F E R -
Step4 io ~ _______~]~ . . . . o , o__~/~ o Row E N C E [J] ~ 0), then the corresponding link is shown by
x x x ~I/ x x Colu~nn a broken line and is not pursued further.
At a typical intermediate point of Stage 1 of the array
Columl~ examination, when a first solution for the array is being
(ROW = 2)
found the partial solution will contain several less ele-
FIG. 5. Flow diagram of operation of E X P A N D for example ments than the final solution. Procedure E X P A N D can
of Figure 1 with single-ended row search be used to enlarge the solution to include an element from

V o l u m e 9 / N u m b e r 5 / M a y , 1966 Communications o f t h e ACM 351


a designated row (if this is possible). However it is gener- BEGIN I 2 3 4 5 6

ally not possible to tell beforehand which column of the


array will be represented in the new partial solution for the
first time. (R
::: )
Golmnn

A different situation holds in Stage 2. As previously


explained, a partial solution always lacks just one element
to make it into a solution. If an extra element can be added
to the partial solution it is clear which row and which 1
column, not represented in the original partial solution,
/
will be represented in the new one. Thus in Stage 2 it is
step4 t o ~ o o o
/ Row
(COLUMN = 2) ~ ~)
possible to exploit the duality between rows and columns
of the array to start searching from the e m p t y column
(COLUMN = 3}
as well as the e m p t y row, i.e., to use a double-ended search x Column

instead of a single-ended search. The flow diagram for a


double-ended search applied to the problem of Figure 1 is
given in Figure 6. This is to be compared with the diagram FIG. 6. Flow d i a g r a m of o p e r a t i o n of E X P A N D for e x a m p l e
for the single-ended search shown in Figure 5. of Figure 1 w i t h double-ended row a n d c o l u m n s e a r c h
For the example, the double-ended search involves
searching only four rows and columns, as against five
rows for the single-ended search. Moreover the n u m b e r of [] 1 1 1 0 I

ignorable nonzero elements (represented b y broken lines) 0 [] 1 0 0 0 0 2 0 ' [] I 0 0 0 " 0

is three for the double-ended search and seven for the 0 1 [] 1 0 0 0 3 0 I [] I 0 0 0

single-ended search. Thus the example verifies t h a t the 0 1 1 [] 1 0 0 4 0 I I [] i 0 0

double-ended search is usually more efficient than the 1 1 1 , [] 0 0 5 1 1 i 1 [] 0 0

single-ended search. Against this advantage must be placed 1 0 1 0 1 [] 0 6 1 0 i 0 I [] 0

a more complicated algorithm with increased overhead, 0 0 0 0 0 o [] 7 0 0 0 0 0 0 []

and most probably increased requirements for storage for 1 2 3 4 5 6 7 ! 2 3 4 5 6 7

the matrix. The latter is because the matrix must be FIG. 7 FIG. 8
searched b y column as well as by row. If the array is
stored as lists of nonzero elements per row, then unless
the array is symmetric it will be necessary for efficient I l 0 1 1

computation to store it as lists of nonzero elements per


column as well. 0 0 0

0 1 1 0 0

5. M i n i m i z i n g the Use of EXPAND 0 0

1 0 1 0 I ~Q~ 0
For the timetable problem E X P A N D is the most highly
0 0 0 0 0 0 ,~
nested p a r t of the program; so as well as being coded
1 2 3 4 5 6
efficiently it should also be used efficiently. If after each
examination of an array a solution is stored, then at the FIG. 9
time of the next examination a substantial p a r t if not all
of the solution can be restored to form a partial solution
for Stage 1; after t h a t an application of E X P A N D is
needed for each row which lacks a solution element. How- i=i
ever it is in Stage 2 where the greatest improvement can
be made.

?
i ) ~ ,=--i
If an application of E X P A N D is made to test the feasi- I
.----.a
bility of an element and it succeeds, then subsequently the
J_ _
6 L- b--
solution m a y be rearranged, introducing several new ele-
ments and elinfinating several of the old. The feasibility
I
-/--1 1 I l I I---
of each of the new elements is demonstrated as surely as 1 2 3 4 5 6 7 1 2 3 4 5 6 77

t h a t of the element raider test. If these elements are FiG. lOa FIG. 10b
tagged as being feasible and not tested again, then this
alone will on the average reduce the n u m b e r of calls on
E X P A N D b y about half. However, as is demonstrated Suppose it is desired to test the feasibility of the ele-
now, it is possible to do even better than this. m e n t A [1, 2]. Since it is not already in the solution one
Figure 7 shows an array for which a solution is defined can a t t e m p t to force it into the solution by turning all the
by the elements of the main diagonM. other nonzero elements in row 1 to zero temporarily. This

352 Communications of the ACM V o l u m e 9 / N u m b e r 5 / M a y , 1966


leaves row 1 and colunm 1 without a solution element The set R is called a proper tight set if it is a tight set
(Figure 8). and it has no proper subset which has the tight property.
The reader m a y verify t h a t a single-ended search in- The following result does not necessarily hold for tight
volves searching rows 1, 2, 3, 4, 5. At the beginning of row sets:
5 a nonzero element in column 1 is discovered and the row
search terminates. On backtracking the solution for A is I f R is a proper tight set, then any nonzero element
rearranged. at the intersection of a row of R and a column of
Figure 9 shows a set of nonzero elements linked together C is feasible.
b y heavy lines parallel to either of the array axes and form-
ing a closed circuit. (The example chosen shows a closed This point is illustrated in Figure 11.
circuit which is a sfinple loop; in general the circuit will The example has been chosen so that the elements along
intersect itself several times.) The elements are alternately the main diagonal form a solution. Because of the large
old and new, i.e. members of the old and new solutions group of zeros in the lower left corner, the nonzero ele-
respectively. Together they form what m a y be called a ments in the shaded area are all infeasible. The array
"step set." has three proper tight sets: rows 1 through 3, rows 4
A step set defines a set of rows and columns of the array. through 7, and rows 8 and 9. I n addition to these there
The elements at the intersection of these various rows and are tight sets formed by rows 1 through 7, rows 4 through
columns define the "spread" of the step set. The following 9, and, trivially, rows 1 through 9.
result is true: The operation of E X P A N D to test A [1, 5] would he to
search rows 1, 5, 4, 7, 9, 6 and 8, then to find it could go
All nonzero elements of the spread of a step set no farther, and to conclude the element was infeasible.
are feasible. The state of the arrays at the end of the row search is
shown in Figure 12. Rows 4 through 9 have been found
To show this result it suffices to show for any element of
to form a tight set involving columns 4 through 9 (this
the spread that a new step set can be formed using the
includes the blanked out column). Hence any nonzero
element and elements from the original step set. Suppose
element in any of rows 1 through 3 and also in any of
A [I, J] is a member of the spread of a certain step set.
columns 4 through 9 (such elements are in the shaded area
Eliminate the "new" members of the step set from row I
of Figure 12) are to be turned to zero.
and column J together with their links and add A [I, J]
The method proposed by Gotlieb and advanced more
as a "new new" member, linking it to the "old" members
recently b y Csima [6] requires finding all the proper tight
in row I and column J. I t is not difficult to see t h a t a closed
sets in the array, after which the feasibility or infeasibility
p a t h now exists, which defines a new step set and includes
of all nonzero elements can be determined in a straight-
A [I, J]. Figure 10 illustrates step sets for the elements
forward way. However the method discussed here does not
A [4, 2] and A [1, 4] based on the step set of Figure 9.
isolate proper tight sets. The rows defined by a step set
The latter is shown by a broken line, with the new sets
are a subset, proper or otherwise, of a proper tight set.
shown by solid'lines.
In the example of Figure 7 a step set was found involving
Thus, for the example shown in Figure 7, one applica-
rows 1 through 5 which are part of the proper tight set
tion of E X P A N D , needed to establish the feasibility of
composed by rows i through 6. In the example of Figure 11
A [1, 2], also establishes the feasibility of A [1, 3], A [1,
the tight set found (rows 4 through 9) is not a proper tight
4], A [2, 3], A [3, 2], A [3, 4], A [4, 2], . . . , A [5, 4]--some
13 elements I
A single application of E X P A N D m a y also demon-
_I 0 1 ,o\h, \ o \ ~ . \o \ o \ ~ o
strate the infeasibility of several elements, rather than
1 1 0 z L 2 o
just one. Suppose that upon testing an element is found
1 I 1 s i i .!
infeasible. During the operation of E X P A N D , a set of
0 0 0 4 0 0 0 _l I I I l 0
rows will have been searched. These rows, k in number,
0 0 0 5 0 0 0 i i 0 1 0 I
form the set R, which has an important property, namely
0 0 0 6 0 0 0 0 0 _i I I 0
that if the set C is the set of columns tied to the rows of R
0 0 0 i o l 2 ~l~ \ 7 0 0 0 I 0 1 _I I I
by the solution elements (colunm J is tied to row I if o o o 0 _I 1
0 0 0 8 0 0 0 0 0 0 n _] 1
A [I, J] is a solution element), then each of the nonzero 0 0 0 0 0 0 0 i i 9 0 0 0 0 0 0 n 1 _I
elements in the rows of R lies in one cf the columns of C. 1 2 3 4 5 6 7 B i Z 3 4 5 6 7 8 9
Such a set R is called a tight set, and by a theorem of Hall
PREFERENCE
[5] it can be concluded that all solution elements which
lie in one of the colunms of C must lie in one of the rows
of R, i.e. any nonzero element which lies in one of the ROWLIST

columns of C but not in one of the rows of R cannot be a


solution element and hence n m s t be infeasible. FIG. I i Fro. 12

V o l u m e 9 / N u m b e r 5 / M a y , 1966 Communications o f t h e ACM 353


set, being formed of two proper tight sets; elements such 3. KUHN, H. W. The H u n g a r i a n M e t h o d for the a s s i g n m e n t
as A [4, 8] belong to neither so that further applications of problem. Nay. Res. Log. Quart. 2 (1955), 83-97.
4. FRIEDMAN, L. F., and YASPAN, A . J . An analysis of stewardess
E X P A N D are needed to determine the feasibility of ele-
r e q u i r e m e n t s and scheduling for a m a j o r domestic airline:
ments in rows 4 through 9. Annex A. The Assignment P r o b l e m technique. Nay. Res. Log.
Quart. 1 (1954) 223-229.
6. T i m i n g 5. HALL, P. On r e p r e s e n t a t i v e s of subsets. J. Lond. Math. Soc.
10 (1935), 26-30.
S u p p o s e t h e a r r a y A h a s n r o w s w i t h a n a v e r a g e of m 6. CSIMA, J. I n v e s t i g a t i o n s on a t i m e t a b l e problem. P h . D .
n o n z e r o e l e m e n t s p e r r o w , i.e., t h e r e a r e (mn) n o n z e r o Thesis, U. of T o r o n t o , 1965.
e l e m e n t s i n A . I n t h e w o r s t c o n c e i v a b l e c a s e e a c h ele-
APPENDIX
m e n t of A w o u l d r e q u i r e a s e p a r a t e a p p l i c a t i o n of E X -
P A N D and each application of E X P A N D would involve p r o c e d u r e expand (A, rowsolution, columnsolution, row, n,
infeasible) ;
the examination of the whole lnatrix, i.e., the number
value row, n; i n t e g e r row, n; B o o l e a n infeasible;
of operations would be of the order of (m n) 2. However i n t e g e r a r r a y A, rowsolution, columnsolution;
there is a complex interaction between the actual number begin
of calls on E X P A N D and the average number of rows eomment This procedure performs one i t e r a t i o n of the Hun-
searched in a single application of E X P A N D - - t h e higher garian M e t h o d on the a r r a y A. A partial solution which does
not include a n element in the row " r o w " is defined b y the a r r a y
the average number of rows searched per application, the
" c o l u m n s o l u t i o n " (and equivalently b y the a r r a y "rowsolu-
less the average number of applications, and vice versa; t i o n " ) . T h e partial solution is r e a r r a n g e d to allow a n additional
so that it is difficult to estimate the exact dependence on element t a k e n from the designated, previously u n r e p r e s e n t e d
m and n. Empirical results seem to indicate that the above row to be incorporated in a new enlarged partial solution, w h e n
estimate is conservative. this is possible. If the l a t t e r is not possible, the Boolean vari-
able " i n f e a s i b l e " is set to t r u e ;
For the timetable problem m is always bounded by the
i n t e g e r j, k, m a r k n e x t , m a r k n e w ;
number of hours in a school day (about ten) and decreases i n t e g e r a r r a y reference [l:n], rowlist [l:n];
steadily during the calculation, and hence the number of infeasible := f a l s e ; m a r k n e x t := marknew := 0;
operations required is of the order of n 2. f o r j := 1 s t e p 1 u n t i l n d o reference [j] := 0;
newrow: f o r j : = 1 s t e p 1 u n t i l n d o
7. C o n c l u s i o n b e g i n i f A. [row, j] # 0 A reference [j] = 0 t h e n
b e g i n i f columnsolution [j] = 0 t h e n g o t o b a c k t r a c k ;
A practical algorithm based on the Hungarian Method reference [j] := row;
of H. K u h n has been described for carrying out the exami- m a r k n e w := m a r k n e w + 1;
rowlist [marknew] := columnsolution [j]
nation and reduction of 2-dimensional arrays as required
end
in Gotlieb's method for the solution of the timetable end;
problem. In addition, various devices to improve the effi- i f m a r k n e x t > marknew t h e n g o t o nosolution;
ciency of the algorithm have been described. m a r k n e x t := m a r k n e x t + 1;
row := rowlist [marknext];
RECEIVED DECEMBER, 1965
g o t o newrow;
b a c k t r a c k : /c = rowsolution [row];
REFERENCES
columnsolution [j] := row; rowsolution [row] : = j ;
1. GOTLIEB, C. C. The construction of class-teacher timetables. if k = 0 t h e n g o t o finis;
Proc. I F I P Congress 62 (Munich), N o r t h Holland Publ. Co., j := k; row := reference [k];
1963, 73-77. goto backtrack;
2. ACKOFF, R. L. (ed.) Progress in Operations Research. Wiley, nosolution: iiffeasible : = t r u e ;
1961, 149-150. finis: e n d of expand

ALGORITHMS--cont'd from page 328 T A B L E I. AVERAGE SORTING TIMES IN SECONDS

Algorithm 201 Algorithm207 Algorithm245 Algorithm271


Number Shellsor
t Stringsort Treesort 3 Quickersort
of items I
, Integers Reals Integers Reals Integers Reals Integers Reals
C E R T I F I C A T I O N OF A L G O R I T H M 271 (M1)
Q U I C K E R S O R T [R. S. Scowen, Comm. A C M 8 (Nov. 10 0.01 0.01 0.03 0.03 0.02 0.02 0.01 0.01
1965), 669] 20 0.02 0.02 0.05 0.05 0.04 0.04 0.02 0.02
50 0.08 0.08 0.20 0.20 0.11 0.12 0.06 0.06
CHARLES R. BLAIR (Recd. 11 Jan. 1966) 100 0.19 0.22 0.39 0.40 0.26 0.27 0.13 0.13
Department of Defense, Washington, D.C. 200 0.48 0.53 1.0 1.1 0.59 0.62 0.28 0.30
500 1.5 1.7 2.8 2.9 1.7 1.8 0.80 0.85
QUICKERSORT compiled and ran w i t h o u t correction t h r o u g h 1000 3.7 4.2 6.6 6.9 3.7 4.0 1.8 1.9
the A L D A P t r a n s l a t o r for the CDC 1604A. Comparison of 2000 9.1 10. 13. 14. 8.2 8.7 3.9 4.1
average sorting times, shown in T a b l e I, with other recently pub- 5000 27. 30. 40. 41. 23. 24. 11. 12.
lished algorithms demonstrates QU[CKERSORT's superior per- 10000 65. 72. 93. 97. 49. 52. 23. 25.
formance.

354 Communications o f t h e ACM V o l u m e 9 / N u m b e r 5 / M a y , 1966

Potrebbero piacerti anche