LIBRARY OF THE UNIVERSITY OF ILLINOIS AT URBANA-CHAMPAIGN top. 2. The person charging this material is re- sponsible for its return to the library from which it was withdrawn on or before the Latest Date stamped below. Theft, mutilation, and underlining of books are reasons for disciplinary action and may result in dismissal from the University. To renew call Telephone Center, 333-8400 UNIVERSITY OF ILLINOIS LIBRARY AT URBANA-CHAMPAIGN DEC 2 9 19? L161— O-1096 Digitized by the Internet Archive in 2013 http://archive.org/details/treetransformati376maru • 6 ^ Report No. 376 P /JZ^L^ti A TREE TRANSFORMATION SYSTEM RASED ON NUCLEOL Kiyoshi Maruyama February, 1970 ftt DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF ILLINOIS AT URBANA-CHAMPAIGN URBANA, ILLINOIS The person charging this material is re- sponsible for its return on or before the Latest Date stamped below. Theft, mutilation, and underlining of books are reasons for disciplinary action and may result in dismissal from the University. University of Illinois Library -1 fflt L161— O-1096 Report No. JjG A TREE TRANSFORMATION SYSTEM BASED ON NUCLEOL hy Kiyoshi Maruyama February, 1970 Department of Computer Science University of Illinois at Urb ana- Champaign Urbana, Illinois 61801 This work was supported in part by the Department of Computer Science at the University of Illinois at Urb ana-Champaign, Urbana, Illinois; the National Science Foundation under NSF Grant GJ-217; by BUILD, the project which is sponsoring cooperation on EOL-lj. between the University of Illinois and the University of Colorado; and submitted in partial fulfillment of the requirements for the degree of Master of Science in Computer Science, February, 1970. Ill ACKNOWLEDGMENT I would like to express ray appreciation to my supervisor, Dr. Jurg Nievergelt, Associate Professor of Computer Science and Mathe- matics, for his endless guidance and supervision of this thesis. I also acknowledge the assistance of Mrs. Nurit Bracha, graduate student in Computer Science. I would like to acknowledge the personnel of the IBM 360/75 service area of the Department of Computer Science at the University of Illinois at Urbana-Champaign for their assistance. This work was supported by the Department of Computer Science at the University of Illinois, by the National Science Foundation under Grant, and by BUILD, the project which sponsors cooperation between the Universities of Illinois and Colorado. iv TABLE OF CONTENTS Page 1. INTRODUCTION 1 2. TREE PROPERTIES AND DEFINITIONS . 2 2. 1 Rooted ordered trees 2 2.2 Node ordering and parenthesized tree expressions k 2. 5 Definitions 5 3. TREE TRANSFORMATIONS 8 3.1 Tree transformation rule 8 3. 2 Pattern matching and replacement 10 3» 3 Examples ...» 12 k. TREE TRANSFORMATION ALGORITHMS 18 k.l Simple tree transformations « . . 18 4.2 Generalized tree transformations 20 5. CONCLUSION 22 LIST OF REFERENCES 23 APPENDICES A. NUCLEOL PROGRAMS FOR THE TREE TRANSFORMATION ALGORITHMS . . 2k B. NUCLEOL AND TREE ALGORITHMS kl 1. INTRODUCTION Processing of tree-structured data is of importance in various areas. In particular recent emphasis on transformational grammars for linguistic analysis has raised interest in programming languages where tree transformations can be specified in terms of very general pattern matching and replacement rules. This thesis presents a formal definition of a very general class of tree transformations, and describes the implementation of this system in terms of the list processing language NUCLEOL [1], The plan of this thesis is as follows. In Chapter 2, I introduce some properties of trees and definitions. In Chapter 3* I define the class of tree transformations studied in this thesis, and the pattern matching and replacement process which it implies. In Chapter k I introduce two systems which are special cases of the class considered: a simple tree transformation and a generalized tree transformation, whose implementation is described in Appendix A. Appendix B contains an introduction to the NUCLEOL language, with examples of programs for a number of standard tree algorithms. 2. TREE PROPERTIES AND DEFINITIONS Tree structures have been the object of extensive mathematical investigations for many years, long before the advent of computers, and many interesting facts have been discovered. In this chapter I introduce some definitions and properties of trees which not only give more insight into the nature of tree structures but also have important applications to computer algorithms. 2.1 Rooted ordered trees In this thesis, "tree" will always mean "rooted ordered tree", since these trees are of greatest interest in linguistic analysis and many other computer applications. We define a tree T formally as a finite set V of one or more nodes (or vertices) such that i) There is one specially designated node called the root of the tree, r(T); and ii) The remaining nodes are partitioned into m(> 0) disjoint ordered sets T. , T_, ... , T and each of these sets in turn is a tree. The 12 m trees T. , . . . , T are called the subtrees of r(T). Their roots r(T, ) 1 m — — — — l ) , ... , r(T ) are called the sons (or direct descendants ) of r(T) , and r(' is called their father (or direct ancestor ). We define a node v itself as a descendant. r(T n ) and r(T ) are said to be the le ftmost and 1 m .-.y.ost sons of r(T), respectively. A direct ancestor v of a node w is d rectly dominate w. A node v is an ancestor of (or dominates ) a node v, if there Is a sequence v. , v_, ... , v such that v. directly 1' 2 k i for i = 1, 2, .. . , k-1. ree I is a subtree of a tree T if and only if either it is a itree of the root. r (T) or else it is a subtree of some subtree of the root r(T). The definition of tree just given is recursive, i.e., we have defined a tree in terms of trees. Of course, there are nonrecursive ways to define trees, but a recursive definition seems most appropriate since recursion is a frequently used characteristic of tree structures. The number of subtrees of a node v is called the degree of that node, denoted by d(v). A node of degree is called a leaf (or terminal node ). The level (or depth) of a node v, denoted by i(v), in a tree T is defined as follows: the level of the root of a tree T, i(r(T)), is 0, and the level of any other node v is 1 plus the level of its direct ancestor. A node v of a tree T is called the deepest common ancestor of nodes v ,v , . . . ,v of T if and only if v dominates v , . . . ,v , and the level -L c: K. J- ri of v is the highest in the set of levels of all possible common ancestors of v ,v , . . . , v . It is obvious that the deepest common ancestor is unique. In addition to single trees it is useful to consider ordered sets of trees, such as the set of subtrees of the root of a tree. We define a forest to be an ordered set of (zero or more) disjoint trees. In applications, nodes usually contain some information, which we call the label of a node. Labels are character-strings over some alphabet which need not be specified here. A tree with labeled nodes is called a labeled tree . Two trees T and S are isomorphic if and only if they have an equal number n > of subtrees T. of r(T) and S. of r(S), and in addition T. and S. J J J J are isomorphic for j = 1, . . . ,n. Two labeled trees are isomorphic if and only if they are isomorphic as unlabeled trees, and in addition corresponding nodes have the same label. A (labeled) tree S is an initial part of tree T if either S is a single node (and, in addition, label of r(S) = label of r(T)) or else the k number of subtrees S ,S p >...,S of the root r(S) is equal to the number of subtrees T..,T^,...,T of the root r(T), and S. is an initial part of T. 1 2 ' ' m * l l for i = 1,2,. . . ,m. 2. 2 Node ordering and parenthesized tree expressions It is frequently necessary to enumerate all the nodes of a tree in a linear order, or equivalently, to "traverse" a tree systematically so that each node is "visited" exactly once. Many algorithms are facilitated if we can talk about the next node following or preceding a given node in such a sequence. Here we consider only one method of enumeration, known as traversing a tree in prefix order (other orders are described in Appendix B). It is convenient to define prefix order more generally for a forest as well as for a single tree as follows. To traverse a forest in prefix order: if it is empty, then termi- nate; otherwise, traverse its first tree in prefix order, and thereafter traverse the forest of the remaining trees in prefix order. To traverse a tree in prefix order: visit the root, and thereafter traverse the forest of its subtrees in prefix order. The following sequence is an example such that nodes of the bree in Figure 2.1 are enumerated in prefix order. ABCDEFGHI if Fig. 2.1 A 5 Even if every node hts a unique label in a tree, the sequence of these labels in prefix order does clearly not determine the tree uniquely; some additional information about the degree of each node is necessary to convert a sequence of node labels in prefix order into an expression which uniquely represent a labeled tree. One way to achieve this is to add, for every node which is not a leaf, a pair of parentheses which embraces the sequence of labels of all the subtrees of this node. The result of doing this is a parenthesized prefix expression, which determines a unique labeled tree. The following sequence is the parenthesized prefix expression of the tree given in Figure 2. 1. A B F G ( ( C D E ) ( ( H ) I ) ) 2. 3 Definitions In this section I define some new terms which will be useful for defining tree transformations in later chapters. Definition 1 (rightmost and leftmost descendant) A node v is a rightmost (leftmost) descendant of a node w if and only if v is a rightmost (leftmost) son of w or is a rightmost (leftmost) descendant of the rightmost (leftmost) son of w. Definition 2 (contiguity) A node v is contiguous to a node w if and only if there are node v ' and w ' such that : i) w' is the next brother of v' , ii) v is a rightmost descendant of v' and iii) w is a leftmost descendant of w' . Definition 3 (contiguous sequence) A sequence of nodes: v n ,v ,...,v is said to be a contiguous J- 2 K. sequence if and only if v. is contiguous to v. , for i = 1,2,..., k-1. For example, a sequence CDEKLJisa contiguous sequence of tree in Figure 2. 2. Fig. 2. 2 Tree T We now extend some of the relations defined earlier among nodes to hold between subtrees as well as their initial parts. Definition k (dominance and direct dominance) Let two subtrees (or initial parts) of a tree T be T and T , (directly) dominates T ? if and only if r(T ) (directly) dominates r(T 2 ). • ■ •-Lon ' i (contiguity) '.wo subtrees (or initial parts) of a tree T be T and T , contiguous to T 2 if and only if rC^) is contiguous to r(T ). -• • (contiguous sequence) A ice T^Tg,... ,T. of subtrees (or initial parts) of a tree T 7 is said to "be a contiguous sequence if and only if r(T.) is contiguous to r(T. + ), for i = l,2,...,k-l. 8 3. TREE TRANSFORMATIONS The tree transformations considered here consist of a single rule of the form "p -* r", where p is a pattern and r is a replacement. The rule is applied to a tree T by matching p against T; if this match is successful then the matching portion of T is replaced by a tree which is defined by r and the matching portion of T. In linguistic analysis and other applications, one is, of course, interested in more elaborate tree transformations, which consist of several rules and additional comments for specifying the sequence in which individual rules are to be applied. This thesis is not concerned with the latter aspect. The special tree transformations implemented here, how- ever, form part of a larger system in which the control junctions (sequenc- ing of rules, I/O, etc. ) are programmed in PL/l (see [2]). 3- 1 Tree transformation rule The following is the most important formal definition of tree transformation rule, in which pattern and replacement are defined recursively. ::= -* : : = ( * ) "placement> ::= ( * ) lement> ::= ( • pea> ) any ::= ::= ::= ::= ::= : : = # £ At this moment , , , and are not specified. They "will be discussed in later sections in specific cases, but their function in tree transformations can roughly be described as follows: allows the specification of a relation which must hold between the parts of a tree which are matched against the patterns listed between the parentheses. Examples of relations which will be considered are direct dominance and contiguity, but we do not now want to restrict the possible relations which one might consider. allows the naming of matched subtrees of the object tree so they can be used in the replacement part. and represent additional constraints which may be imposed (optionally) on matching nodes. Finally, is unspecified in order not to decide on a particular character set. The grammar just given assigns to any pattern and any replacement a tree (the conventional "parsing tree"), which we call the pattern tree and replacement tree , respectively. A rule is said to be well - defined if and only if the replacement tree is an initial part of the pattern tree (disregarding all node labels); otherwise it is ill-defined. 10 The following two examples show a well-defined rule and an ill-defined rule. Example 1 (well-defined rule). ( ( ) ) -» ( ( ) ) Example 2 (ill- defined rule). ( ) -* ( ) Hereafter, we always assume that given rules are well defined. 3. 2 Pattern matching and replacement The process of applying a transformation rule R: p -* r to a given tree T (called the object tree) proceeds in two steps: First, (the matching process), p is matched against T in an attempt to find a subtree S of T (the matched subtree). If this matching attempt is success- ful, we say R is applicable to T, and we continue with the replacement process; if the match is unsuccessful, we say R is inapplicable to T, and the process stops without altering T. Second (the replacement process), the matched subtree S of T is repla r a subtree 3" which is determined by both the replacement part r le and by the matched subtree S. wo processes will now be described in detail. 11 Matching process The matched subtree S is the first subtree of T in prefix order , of which the pattern tree p is an initial part, subject to the following conditions : 1) The level and degree of every node w of S must satisfy the requirements, if any, stated in the of the corresponding node v of the pattern tree p. 2) The label of every node w of S must be equal to the level of the corresponding node v of p, except if v is the special , * (which "matches every node"). 3) The special of the form (*) is considered to be a single node in the pattern tree p, which matches the corresponding node w of G if at least one of the 's contained in it does. k) A in the pattern tree p assumes as its value the sub- tree of T to whose root it matches. Replacement process Each node in the matched subtree S is replaced by the unique (of the replacement r) which corresponds to the (of the pattern p) to which it matched. In this replacement, the special #, a), and are treated as follows: l) § : no replacement is done. 2)- a) : delete corresponding as well as the subtree whose dominant is . 3) : replace by the subtree which was assigned to this during the matching process. 12 3. 3 Examples The following examples of tree transformations are presented to illustrate the previous definitions. Notation and restrictions: 's are denoted by single capital letters (A, B, C, ...). is restricted to level and denoted by a subscript "r" (for root). is restricted to degree and denoted by a subscript "t" (for terminal node). Tree variables are denoted by T , T , ... . 's are restricted to two: direct dominance, denoted by "d", and contiguity, denoted by "c". R(T) denotes the result of applying rule R to tree T. In each example, the tree of Figure 2.3(a) will be used as the object tree. Its corresponding parenthesized prefix expression is shown in Figure 2.3(b). (a) Tree T A *D p p ( D E ) ( F ( I .r ) II ) ) (b) Parenthesized prefix tree expression of T . 3 The object tree Example 1. R (T) P $ F H R x : ( * T x H t ) - ( ( T x ) ^ ( T 1 F ) ) d A T5 P V P HP Rj_(T) : ( ( D E ) ( ( ( I J ) ) ( ( I J ) F ) ) ) 13 where T x : ( I J ) * : F Fig. 2.k The tree of R-,(T) Example 2. R (T) * B C R 2 : ( ( D t E ) ( ( K F ) T x * ) ) d d any # # D G -(((T 1 E)S)(##5>)) ABDG G G R 2 (T) :((((IJ)E))( F (IJ))) 14 where T x : ( I J ) Fig. 2.5 The tree of R ? (T) Example 3. R (T) R : ( B F t G H ) - ( # # a) G ) c SB C R,(T) : ( ( D E ) ( F G ) ) 15 D E Fig. 2.6 The tree of R,(T) Example k. R. (T) A R k : < B F t ( I t Tl ) T 2 ) s s - ( # T x ( K T 2 ) T x ) S B C G R^(T) :((DE)(J(KH)J)) where T : J T 2 : H K H Fig. 2.7 The tree of R, (T) Example 5. R 5 (T) * C R 5 : ( T x ( F t T 2 H ) ) d x c # DF ( T 2 ( ( T 2 ) a T-l ) ) A G D F G B R 5 (T) : ( ( I J ) ( ( ( I J ) ) ( D E ) ) ) 16 B G where T : ( D E ) , T : ( I J ) I J Fig. 2.8 The tree of MT) 17 Example 6. R/-(T) A R 6 : ( D E ( F G H ) ) c c n g -(#•>( #^o# ) ) A B S R 6 (T) : ( ( D ) ( -F H ) ) Fig. 2.9 The tree of R/-(T) 18 k. TREE TRANSFORMATION ALGORITHMS In this chapter I introduce two kinds of tree transformations, called simple and general , respectively. An algorithm for performing the pattern matching and replacement process is described for each case. if.l Simple tree transformations A simple tree transformation is one whose pattern is generated by the single relation "direct dominance" and by ' s of type and , only. Formally (see definition of rule in section J.l), we say that a simple tree transformation is a rule defined by: : : = -* : : = ( * ) : : = ( * ) : : = : : = In other words, both the pattern and the replacement are fixed, explicitly given trees, whose terminal nodes may be tree variables. With these restrictions, there is a simple algorithm for the pattern matching and replacement process. The skeleton of this simple tree transformation algorithm, called T, is shown in Fig. J+.l, in a notation which shows the relationship among .'•ograms of which it consists. ch node represents an external procedure (a "wfs" in NUCLEOL Logy arrows labeled represent transfer of control between 19 begin terminate Fig. J+.l Structure of the STT algorithm ALGORITHM STT (Simple Tree Transformation) MAIN. [Initialize the system and carry out the matching and replacement process] MAIN. 1 Initialize the system, then go unconditionally ((^, ) to ROOT- MAIN. 2 [Matching process] Match the pattern to the subtree (whose root is determined by ROOT) of the object tree. If the match fails (oi), go to FAIL. MAIN. 3 [Tree variables] Construct a table for tree variables and their corresponding subtrees of the object tree. Using this table replace tree variables by act al subtrees (deletion and attachment of subtrees). MAIN, k [Replacement] Replace the matched subtree of the object tree by the (modified) pattern. MAIN. 5 [Termination] Go to RESET (<£). 20 ROOT [Match root of pattern tree J Find the next node in the object tree whose label is equal to the label of the root of the pattern tree, and if found (oj ) , go to MAIN. 2 . If no such node is found, then the rule is not applicable, go to RESET ((?-). FAIL [Restore object, tree] Since the pattern did not match the subtree whose root was chosen by ROOT, restore the object tree, then go to ROOT ( ::= ( * ) ::= ( * ) ::= and are as defined in Chapter J. Notice that one can define new relations between matching subtrees in terms of the . g. , the relation "brotherhood" can be defined in ms of direct dominance. The. relation of brotherhood between nodes A, B , can be expressed as -elemei. - <.-lement> ABC ( A B ( ( T-. ) ( T p ) ( T ) ) . d d d d * 21 This general tree transformation algorithm, called GTT, consists of four main parts : i) the main part, which consists of NUCLEOL wfs "MAIN" alone, controls transfers to all other parts, ii) the matching part, which consists of wfs's "MATCH', "ROOT", "D-DOMI", "CONTl", "ANYONE" and "FAIL", checks whether or not the given rule is applicable to the object tree, iii) the replacement part, which consists of wfs's "TRANS", "D-TO" and "C-TO", and iv) the terminal part consisting of wfs "RESET". The NUCLEOL program which implements general tree transformations is shown in Appendix A, Algorithm GTT. 22 5. CONCLUSION Recent emphasis on transformational grammars for linguistic analysis has raised interest in programming languages in which tree trans- formations can be specified in terms of very general pattern matching and replacement rules. Our definition of a transformation rule consisting of pattern and replacement (see section 3«l)> based recursively on elementary patterns and relations among patterns, appears to be a natural generalization of all the transformation rules which have been used in transformational grammars. The system described here, which can carry out a single trans- formation on an object tree, is currently being augmented by a control language (based on PL/l) which allows the specification of sequences of transformations [2]. 23 LIST OF REFERENCES 1. Nievergelt, J. , Fischer, F. , Irland, M. I. , Sidlo, J. R. "NUCLEOL - A Minimal List Processor", Proc. Purdue Centennial Year Symposium on Information Processing, pp. 92-103, 19&9« Also, University of Illinois, Department of Computer Science, Report No. 32^, April, 1969. 2. Bracha, N. Forthcoming M.S. Thesis, University of Illinois, Department of Computer Science. 2k APPENDIX A NUCLEOL PROGRAMS FOR THE TREE TRANSFORMATION ALGORITHMS 25 .i,k hum SI T: / * **•** im j ec r tr i- h ***** / IS 'TREE' S(X *C 'A' $( iC •»« 4( -*C •«)• $C «E« $ '• $C »C J>' SC «E« tC T, • $) $) $)X /***** PATTERN *****/ $S 'LHS' MX SC 'B' $( $C 'Oi $( $CV Ml' $) $CV »T2« $) $)X /***** REPLACEMENT *****/ *S 'RHS' MX $C 'A' $( $C 'B' M $CV ' T2 • J.) $C »D« $( $CV »T1» $) $) $)X /*#*** TEMPORARY STORAGE *****/ $S 'TEMP' $(X $)X /***** TABLE EOR TREE VARIABLE CORRESPONOfcNCE *****/ $S 'CORRESPO' $(X $CV «TO» $)X /***** OPERATION ROOM *****/ $S 'OP ROOM' $(X $)X /***** REFERENCE EOR MATCHING FAILED *****/ $S 'FAIL3' $(XN $CK »RSTR« $R 'TEMP' 'TEMP' •TEMP' $RR 'TREE' •TREE' 'LHS 1 •LHS» $RL 'CORRESPO' $C 'A =D» $B '1' $(FU $CK 'MOVE' $RL 'CORRESPO' $CK 'RSTR' $R $CK »SHET» $RR $CK 'MOVE' $RR $CK »SHFT» $RR $CK 'RSTR' $R $CK 'SHFT' $RR J(N $CK 'TEST' $R 'SYS FREE' S)N $)S $R 'ROOT' $)X /****« REFERENCE EOR RESETTING PROGRAM *****/ $S 'RES FT' $(XN $CK •RSTR' %R •TREE' $CK •RSTR' $R 'LHS' 4CK •RSTR' $R •RHS' $CK 'RSTR' $R •TEMP' tCK 'RSTR' $R •CORRESPO* $CK •RSTR' $R T)P_ROOM' $R 'SYS STOP' SIX 26 /***** REFERENCE FOR MATCHING OF THE ROOT OF THE TREE *****/ $S 'ROOT' $(XN S(N SCK 'TEST' SRR 'TREE' $C • = • SRR 'LHS' $(FU SCK 'SHFT' SRR 'TREE* SCK 'TEST' fRR 'TREE' SC 'A = D» SB »1« $(S SR 'RESET' $) $)S S)FU SCK 'SHFT' SRR 'TREE' SCK 'SHFT' SRR »LHS' SCK 'TEST' $RR 'TREE' SC ' = • $RR 'LHS S(S SCK 'MOVE' SRR 'TREE' SRR 'TEMP' SRR 'MATCH' $) $(FU SCK 'SHFT' SRL 'LHS' $)N SR 'ROOT' S)X /***** MAIN PROGRAM *****/ SSN 'MAIN' $(XN SCK 'SHFT* SRR 'TEMP* SCK 'SHFT' SRR 'CORRESPO 1 SCK 'SHFT' SRR «OPJ*OOM» SCK 'SHFT' SRR 'TREE' SCK 'SHFT' SRR 'LHS' SCK 'SHFT' SRR 'RHS' SR 'ROOT' S(N SCK 'TEST* SRR «LHS« $C 'A *D» SC »V SIS SCK 'COPY' SRR 'LHS' SRL 'CORRESPO* SCK 'SHFT' SRR 'LHS' S(N SCK 'TEST' SRR 'LHS' SC • = • SRR «TFMP» S(FU SCK 'COPY* SRR «TFMP» $RL 'CORRFSPO' SCK 'MOVE' SRR 'TEMP* SRL 'TEMP' SIN sjs sck 'shft' srr 'lhs' sck 'shft' srr 'temp' S)N S(FU SCK 'TEST' $RR 'TEMP' SC » = ' SRR 'LHS' S(FU SCK 'TEST' SRR 'LHS' SC 'T =D' SC '(' MS SCK 'SHFT' SRR 'LHS' SCK 'TEST' SRR 'LHS' SC 'A =D' SC 'V S(FU SR 'FAIL3' S) SCK 'COPY' SRR 'LHS' SRL 'CORRESPO' SCK 'SHFT' SRR 'LHS' SCK 'TEST' SRR 'LHS' SC 'T =D' SC ')• 1>tFU SR »FAIL3» $) tCK 'SHFT' iRR 'LHS' t)S S (FIJ SR • FA 1 I i' S ) t )N SJS ICK 'SHFC SRR »TFMP« 27 )S SCK • SHF1 ' *k* • I M'i ' S)N UN jCk »TF,Sl' tPR »1EMP» *C »A = 0« *B •!■ SCK "TEST 1 $RR »LHS» *C 'A =D' $B »1» t(MJ $R • HA J L3 • $) $CK 'SHFT' $RR 'RHS' $CK 'TEST' 4RR «RHS' $C »T =0» $C '(• t(S $CK 'COPY' $RR »RHS» $RR 'OP_.POOM' %)N SCK 'COPY' $RL 'RHS' $RR 'OP_ROOM» S(NFU $CK 'TEST' $RR »OP_ROOM» $C 'A =D« $C 'V $(S SCK »RSTR» $RL 'CORRESPG' $(N $CK 'TEST' $RR 'CORRESPO' $C » = $RR 'OP_ROOM» $(FU $CK 'MOVE' $RR 'CORRESPO' $RL 'CORRESPO' $CK 'TEST' $RR 'CORRESPO' $C 'A =D» $C 'V $)S $)S tCK 'SHFT' $RR •CORRESPO' *CK 'MOVE' $RR •OP_ROOM» $R •SYS $(N $CK 'TEST' $RR 'CORRESPO* $C FREE' 'A =D' iC 'V $(FU $CK 'COPY' $RR 'CORRESPO' $RL •OP_ROOM» $CK 'MOVE' $RR 'CORRESPO' $RL 'CORRESPO' $)N $)S /** TREE VARIABLE IS NULL **/ $CK 'TEST' JRL 'OP_ROOM» $C 'T = D» $C »(• $(S SCK 'TEST' *RR «OP_ROOM» $C 'T =D' $C » ) ' $(S iCK 'SHFT' $RR *OP_ROOM' $CK 'MOVE' $RL 'OP ROOM' $R 'SYS FREE' $)N $)N $CK 'SHFT' $RL »OP_ROOM» $)NFU $CK 'SHFT* SRR «OP_ROOM» $CK 'TEST' $RR 'OP_ROOM' $C »A =D' $B M' t)S $CK 'MOVE' $RL 'TREE' $R »SYS_FREH» $(N $CK 'TEST' $RL »OP_ROOM' $C 'T =D» $C '(• $(FU $CK 'MOVE' $RL 'OP_ROOM» $RR 'TREE' $)M $)S $CK 'MOVE' $RL 'TEMP' SR 'SYS_FREE« $CK 'RSTR' SRL 'CORRESPO' $(N SCK 'TEST' SRR 'CORRESPO' $C • = • $CV 'TO' 28 $(FU $CK «MOVt» $RR 'CORRESPO* $R »SYS„FRt=E« $)N $)S *CK 'COPY' $R 'TREE' $R •SYSPRJNT* SR 'RESET* $) 29 sc •B« $( $CT •D« $CT •H $C •G' $( %) $)X ALGORITHM GTT: /***** TREE OBJECT *****/ $S 'TREE' $(X SCR 'A' J( $CT 'E' $) *C 'C $( $CT 'J' $) *CT «H' $) /***** PATTERN *****/ $S 'PATTERN' $(X $C 'A' $( $CV »T1« $C 'C« $(S $CT • (- ' $C ' G • $( $CV 'T?' $) $C »H» .*) $) $)X /<•**** REPLACEMENT *****/ $S 'REPLACE' S(X $C 'S' $( $CV 'Tl' $C »D» $(S SC »F» $( $CV 'T2' $) $C •#• $C '3' $) $) $)X /***** TEMPORARY MEMORY EOR PATTERN *****/ $S 'LHS' MX $)X /***** TEMPORARY MEMORY EOR REPLACEMENT *#***/ $S 'RHS' S(X $)X /***** OPERATION ROOM FOR TREE *****/ $S »OP_ROOM« $(X $)X /***** CORRESPONDENCE TABLE *****/ $S 'CORRESPO' $(X $CV 'TO' $)X /***** TEMPORARY STORAGE *****/ SS 'TEMP' ${X $)X /***** REFERENCE FOR BACKING UP SCANNER TO THE ORIGINAL POSITION *****/ $S 'SET_SCAN» $(XN $(N $CK 'TEST' $RR 'LHS' $C • = • $C 'SCAN' $(FU *CK 'MOVE' SRR 'LHS' $RL 'LHS' $)N $)S $CK 'MOVE' $RR 'LHS' $R »SYS_.FREE' $RR 'MATCH' $)X /***** REFERENCE FOR RESETTING EACH WFS AND TERMINATION *****/ SS 'RESET • $(XN $CK •RSTR' $R 'TREE* £CK •RSTR' $R •PATTERN' $CK •RSTR' $R •LHS' tCK 'RSTR' $R 'RFPLACi- • $CK 'RSTR' SR •RHS' 30 iCK »RSTR» iR »OP_ROOM» SCK »RSTR' iR 'CORRESPO* SCK «RSTR» $R 'TEMP* $R «SYS_STOP» $)X /***** REFERENCE FOR MATCHING FAILED *****/ SS 'FAIL' SUN $CK 'RSTR* SR 'IHS» $CK "SHFT* $RR •LHS» $(N SCK 'TEST* $RR »LHS» SC 'A =D» SB •!' S(FU SCK 'MOVE' $RR »LHS' SR 'SYS.FREE' $)N $)S SCK 'RSTR' SR «OP_ROOM' SCK 'SHFT' SRR «OP_RUOM' S(N SCK 'TEST* SRR 'OP_ROOM« SC 'A «D« SR '1 ' $(FU *CK 'MOVE 1 SRR »OP_ROOM» $R »SYS_FREE' i)N $)S $(N SCK 'TEST' SRL 'CHRRESPO* SC 'A = D» $B 'l • S(FU SCK »MOVF' SRL 'CORRESPO' iR 'SYS.FREE' i)N i)S SR 'MATCH* S)X /***** ROOT OOES MATCHING FOR THF ROUT OF A TREE *****/ SS 'ROOT' i(XN SCK 'TEST' $RR «LHS' SC »T =0» iC »C SIS SCK »TEST« iRR •LHS 1 iC ■ ■ • iC •*» i(FU SCK 'TEST' SRR »LHS' SC ■ » ■ iR« 'TREE 1 $(FU iCK 'SHFT' iRR •TREE* $CK 'TEST' iRR 'TREE' iC 'A =0« iB »l« i(S iR 'RESET* i) S)FU t)s SCK 'TEST' SRR 'TREE' SC »A *l)» iC »T» i(S iCK 'SHFT' iRR 'TREE' SCK '1EST' iRR 'TREE' iC »A «D» iB •!• t(S iR 'RESET' i) tlFIJ SCK 'TEST' iRR 'TREE' iC »T *D» iC 'C S(S SR 'ROOT' i) % I FIJ l ) t)NFII /** 'jHOUI ij hi- anyone **/ 31 SfFU *CK 'SHI-T» tRR 'LHS' t(N $(NS SCK 'TEST' tRR 'TREE' SC • = • $RR 'IMS' $(FU SCK 'SHET' $RR 'LHS' $CK 'TEST' $RR 'LHS' $C 'T =1)' SC • ) • MS $CK 'RSTR' $RL «LHS» $(N tCK 'SHET' $RR 'TREE' $CK 'TEST' $RR 'TREE' $C «T =0' $C 'C 4(FU $GK ' '| f-Sl ' $RR • TRFF ' *C 'A =D« $B • 1 • $(S $K 'RESET' $) *)FU $)S $)N *)EUN $)EU 4)S $CK 'TEST' $RR 'TREE' $C 'A =!)' $C 'T $CK 'MOVE' iRR 'LHS' $RR 'TEMP' $CK 'RSTR' $RR 'LHS' $CK 'SHFT' $RR 'LHS' $CK 'MOVE' $RL 'LHS' $R 'SYS_EREE' $CK 'MOVE' $RR 'TEMP' $RR 'LHS' $)N $CK 'SHFT' $RR «LHS» $CK 'SHFT' $RR 'TREE' $CK 'COPY' *RR 'TREE' iRR 'OP_ROOM« $CK 'COPY' $RL 'TREE' $RL »OP_ROOM« $RR 'MATCH' S)X /***** REFERENCE FOR AMY ONE OF THE SPECIFIED NODES *****/ tS 'ANYONE' $(XN iCK 'SHFT' *RR 'LHS' $(N $CK 'TEST' $RR 'LHS' $C • = • $RR 'OP ROOM' $(FU $CK 'SHFT' $RR »LHS» $CK 'TEST' $RR 'LHS' $C 'T =0' $C •)• $(S $R 'FAIL' $) $)FU $)S $CK 'TEST' $RR 'LHS' $C 'A =D» $B '0' $(FU $CK 'TEST' $RR 'LHS' $C 'A *A» $RR 'OPRIiRM' $(FU $R 'ANYONE' $) t)S $CK 'MOVE' $RR 'LHS' $RR 'TEMP' $CK 'RSTR' $RR 'LHS» $CK 'SHFT' $RR 'LHS' SCK 'MOVE' $RL 'LHS' $R »SYS_FREE» $CK 'MOVE' SRR 'TEMP' $RL 'LHS' $CK 'SHFT' $RR 'OP_ROOM» $RR 'MATCH' t)X 32 /***** RFFERENCF FOR DIRECT DOMINATION *****/ $S 'D_DnMlNE« $(XN SCK 'TEST' SRR »LHS» SC »T rO' SC •{• SIS SCK 'TEST' SRR 'LHS' SC • = « SRR 'OP ROOM» $(FU SCK 'SHFT* SRR 'LHS' SCK 'TEST' SRR 'LHS' $C 'A =D' SC 'V $(FU $R 'FAIL' $) SCK 'COPY' SRR 'LHS' SRL 'CORRFSPO' SCK 'SHFT' SRR »LHS« SCK 'TEST* $RR 'LHS' SC 'T =D» SC • ) ' S(FU SR 'FAIL' S) SCK 'SHFT' SRR 'LHS' SCK 'MOVE' SRL 'LHS' SR 'SYS FREE' SRR 'MATCH' S) $)SN J $(FU SCK •TEST ' SRfl 'LHS' SC * ■ ' SRR • OP _ ROOM' S(S SCK < 'TEST* SRR 'LHS' SC 'A =D» SB «0» $(FU SCK 'TEST' SRR 'LHS' SC 'A =A» SRR 'OP ROOM' S(FU SR 'FAIL' S) $)S S)SN S(FU SCK ' 'TEST' SRR 'LHS' SC 'A =0' SC «V SIS SCK 'COPY' SRR •LHS' SRL 'CORRESPO' SCK •SHFT' SRR •LHS' SIN SCK 'TEST' SIFU SCK ' SRR »LHS» SC • = • SRR «OP_ROOM« MOVE' SRR »OP_ROOM» SRL 'CORRESPO' SIN SIS SIN $(FU SCK •TEST' SRR 'LHS* SC ' * ' SC •*' SIS SCK 'MOVE' SRR 'LHS' SR »SYS_FREE« SIN SCK 'TEST' SRR 'LHS' SC • = • SRR »OP_ROOM' SIFU SCK 'COPY' SRR »UP_ROOM» SRL 'LHS' SCK 'MOVE' SRR 'OP_ROOM» SRL «OP_ROOM« SIN SIS SCK 'SHFT' SRL 'LHS' SCK 'SHFT' SRL 'OP_ROOM' SIN SIFU SR 'FAIL' S) l)N S)N S)N ICM 'SHFT' SRP 'LHS' SCK 'SHFT SRR M)P RUUM» 33 SCK 'TEST' SRR 'LHS' SC M = D» SC '(• S(S SRR 'MATCH' $) SCK 'TEST' SRR 'LHS' SC »T ■'!)• SC •)• $(S SCK 'TEST 1 $RR 'LHS' SC 'A =0« $B '1' S(S SRR 'MATCH* S) SCK 'TEST' SRR 'LHS' SC ' = ' SRR «0P ROOM' $(S $CK 'SHFT' SRR 'LHS* SCK 'SHFT' SRR »OP_ROOM' S)N SRR 'MATCH' $) $R 'D DOMINE' $)X /***** REFERENCE FOR CONTIGUITY *****/ $S 'CONTI* $(XN SCK 'TEST' SRR 'LHS' $C »T =D» $C »C» S(S SCK 'TEST' $RR 'OP_ROOM' $C »T =D« $C •(• S(S SCK 'MOVE' SRR »OP_ROOM» SRL »OP_.ROUM» $)N SCK 'TEST' SRR 'LHS' SC 'A =D« $C »T» $(S S(N SCK 'TEST' SRR »OP_ROOM« SC 'A =0» SC «T' S(FU SCK 'SHFT' SRR »OP_ROOM« $)N $)S SCK 'TEST' SRR »LHS» SC • = » SRR «OP_ROOM« $(FU SR 'FAIL* S) $)S $(FU SCK 'TEST' SRR 'OP_ROOM' SC » = ' SRR »LHS' S(FU SCK 'TEST' SRR 'OP_ROOM« SC 'T =0' SC 'C $(S SCK 'TEST' SRR «OP_ROOM» SC 'A =0' SC »T« S(S SR 'FAIL' $) $)FU SCK »SHFT' SRR «OP_ROOM» $)FUN $)S SCK 'SHFT' SRR 'LHS' SCK 'SHFT' SRR »OP_ROOM« $)N S(FU SCK 'SHFT' SRR 'LHS» SCK 'MOVE' SRL 'LHS' SRR 'LHS' S(N SCK 'TEST' SRR 'OP_ROOM» SC »T =D' SC »)» $(FU SR 'FAIL' $) SCK 'SHFT' SRR 'OP_ROOM' SCK 'MOVE' SRL 'OP_ROOM' SRR 'OP. ROOM' SCK 'TEST' SRL »LHS' SC ' = • SRL 'OP_ROOM' $(FU SCK 'MOVE' SRR 'OP_ROOM» SRL 'OP_ROOM' $)M S)S $) X 3fc *CK 'MOVE' *RR •LHS* $RL »LHK» $CK »MfJVfc' tRR •OP_RUOM» $RL 'OP_ $)N $CK •TFSi' *RR •LHS' $C "T *l)« $C • ( • IIS *RR 'MATCH' *) *CK •T EST ' $«R •LHS' J.C ' T al)» *C • ) ' S(S *CK 'TrS 1 • *kk •LhS' $C »* -l>« *H ${S $RR 'MATCH' $1 *R »CUi\M I ' *) , $R • c; i in i ] • ROOM' • 1 • SS ' MA $CK t(S JCK JR $(N MAIN P TCH' •TES $CK •COP 'ROOT *CK $(S t )S tCK K F %k& ROGRAM *( XN I • 4R 'COPY Y« $R i 'TEST $CK $(S $(FU HJR TREE MATCHING *****/ R 'PATTERN' $C • *RR 'PATTERN' L 'PATTERN' $Rk n =u' $c ' ( ' $RR 'LHS' S)N •LHS' i) N $(FU $)N *CK • $RR •TEST' $R 'D. $CK $(S •LHS 1 $C »T =0« $C M « $RR »LHS» $C 'A =()• $R «0» DOMINE' $)N /** DOMINATION #*/ •TEST" *RR *)N $(FU 'LHS' $B $CK »SHFT» $RR $CK 'TEST' $RR $C MS $CK 'SHFT' *R 'CONTI' /** CONTIGUITY **/ $R 'ANYONE' $)N $C 'A =0' •OOOOl' •LHS' 'OP_ROOM« •T =0' $C • ( ' *RR 'OP ROMM' $ ) M /** FI fHER ONE **/ $)N $CK tCK $CK $(S *(FU * )N 'COPY' $C 'SCAN' 'RSTR' SRL 'LHS' • I EST • $RL 'LHS' 5.R 'SET SCAN' $R $CK 'TEST' $PL $(S % (hi) tR J.R $RR 'LHS' /** CHFCK RELATION $C »A = D' *B »0' •l)_.)OMINE' $)f\l •LHS' $C 'A =!.)• $B '00001' 'SE7_SCAN' $R 'CONTI' $)N •SET SCAN' $R 'ANYONE' $ ) w :* / •TEST" $PR 'LHS' $C 'A =e|J' $B • 1 • • F^S T • 1>RR 'UP U W 'l-AIL' S ) • MA IN' ROfJM 4C «A =0' $B * / 35 ,:■ .. nf-j. -Fyf- (MCE rOK Rh PLACE'-'iNV* I' Oh lll'l-l ! WA I ' ir'v m.'I **** + ./ SCK • r b S f • &RR 'wHS' $L ' = ' iC •#• SIS *CK • M » J V t- • SRR »L W S« SRL 'OP_RGOM' *CK »SHEM SRR «RHS» $LK • TEST' ARL M!P_R00fi' $C 'A =■ ■ D ' tC » " ftIKU *CK ' MOVE' f>RR 'Of_«OOM' $K 'SYS EkE*--' s'\ iCK »TE;>P $RR ' IMS' $C »1 s|i $RR »RHS» *(EU $CK 'MOVE' *RR •CIP.ROOH' $R 'SYSEREi-' 5.CK 'MOVH' $RR ' LHS 1 $mL • Oi J ._RUOM ' «, i N J. 1 M S *(F SCK ■ TtrST ■ *RR •RHS* SC 'A = [)• $C «T« *(S *CK • MuVH' SRR 'RHS' $KL 'OP.„RCHJM' $CK 'TEST' $RR 'LHS' SC 'A = D» SC «V SIS SCK 'MOVE 1 SRR 'LHS* SR 'SYS FREE' S)N *{FIJ SCK 'MOVE 1 $RR 'LHS' SR ' SYS._ FREE ' SCK 'MOVE' SRR 'OP..ROOM' $R 'SYS. FREE' SCK 'TEST' SRR 'LHS' $C »T =1)' if. »(' $5FU $)N *(FU $CK 'TEST' SRR 'RHS» SC » = ' SC • 31 • SIS SCK «SHFT» SRR 'RHS' SCK 'TEST' SRR 'LHS' SC 'A =D' $C ' C. ' SIS SCK 'SHFT' SRR 'LHS' S)N SIFO SCK 'MOVE' $RR »LHS' SR 'SYS_EREE' SCK 'MOVE' SRR »OP_ROOM» SR 'SYS_FREE» $CK 'TEST' $RR 'LHS' SC »T -0» $C ' ( ' $>EO $)N SIFO SCK 'MOVE' $RR »RHS» $RL 'OP.ROOM' $CK 'TEST' SRR 'LHS' SC 'A =0' SC 'V $CK 'SHFT' $RR 'LHS' SIEU SCK 'MOVE' $RR »OP_ROOM» $R 'SYS_EREE' $)N SCK 'TEST' SRR 'LHS' SC 'T =f» $RR 'RHS' SIFO SCK 'TEST' $RL »OP_ROOM' SC 'A =0' $C »V SI S SCK 'TEST ' SRR ' LHS • SC 'T =T' $RR 'RMS' SIFO SCK 'MOVE' $RR »LHS« SR 'SYS_FREF' SCK 'MOVE' $RR 'DP ROOM' $R 'SYS FREE' S)N S)NS SIEU SCK 'MOVE' SRR 'RHS' SRL 'OP ROOM' $)N 36 S)N S)NS $)N S)N SIU SCK 'TEST» SRR 'RHS' $C 'T ■D» SC '(• SIS SCK 'MOVE' SRR «RHS r SRI. 'nP_ROUM' S)N $CK •TEST' SRR 'LHS' SC 'T -l)» SC •(• SIS SCK 'MOVE* SRR 'LHS r SR 'SYS FREE' $)N $)FUN $CK 'TEST' $RR 'LHS' SC »T = 0» SC »C» SIS SR ' D_TO' $) SCK 'TEST' SRR »LHS' SC »T =D< SC •)• SIS $CK 'TEST* SRR 'RHS' SC »T = D' SC •(♦ $(S SCK »MOVE' SRR 'RHS' $RL 'OP_R()OM» $RR 'TRANS' S) SCK 'SHFT' SRR 'LHS' $CK 'SHET' SRR 'RHS' SCK »SHET' SRR 'OP_ROOM» $)FUN SRR 'TRANS' $)X /***** REFERENCE FOR REPLACEMENT FOR CONTIGUITY TO *****/ SS 'C_TO« S(XN SCK 'TEST' SRR 'LHS* SIS $CK 'TEST' SRR 'LHS* SIS SCK 'TEST* SRR »OP_ROOM» $C 'A =D' $C »T» S(FU SCK 'SHFT' S)S SIFU SCK 'TEST' SRR 'LHS' $C » = • SRR 'OP ROOM' SIFU SCK 'SHFT' $C 'T = $C 'A = •OP_ROOM' SRR »OP_ = D» = D« SC .ROOM SC SC »/ t »c» IV =D» S)N •LHS' SRR $C •OP_ i .ROOM t ' SRR S)N $)S S)N SCK 'TEST* SRR 'RHS' SC ' = • SC •#' SIS SCK 'SHFT' SRR 'LHS' SCK 'SHFT' SRR »OP_ROOM» SCK 'SHFT' SRR 'RHS' SCK 'TEST' SRR 'LHS' SC »T =T» SRR »RHS» SIFU SCK 'MOVE' SRR 'OP_ROOM« SR 'SYS FREE' SCK 'MOVE' SRR 'LHS' SRL 'OP_RO()M« S)N SCK 'TEST' SRR 'LHS' SC »T =T» SRR »OP ROOM' SIFU SCK 'MOVE' SRR »OP_ROOM» SRL 'OP RUUM' $)M S)NS SIF SCK 'TEST' SRR 'RHS' SC ' = » SC »3« SIS SCK 'SHFT* SRR « RHS ' SIN SCK 'MOVE' SRR 'DP. ROOM' $R 'SVS FRFF' SCK 'TEST' SRR MM ROOM' SC 'T =!)» SC • ( » HS' SR SCK 'TEST' SRR 'LHS' SC »T =!)• Sf S)FIJ SIN SCK 'MOVE' SRR 'LHS' SR 'SYS ERf-E' 37 $)FU $)N S(FU $CK 'TEST' *RR • RHS • $C 'A =D' $C »T» $(S $CK 'MOVE' $RR 'RHS' $RL 'OP.RUDM' $CK 'MOVE' SRR 'LHS' $R »SYS_FREE» SCK 'MOVE' SRR 'OP_ROOM' $R »SYS_FREE« $CK 'TEST' $RR »LHS' $C »T =1' $RR »OP_ROOM' $(FU $CK 'MUVE' $RR «OP_ROOM« $RL »OP_ROOM» 4)N $)NS $(FU $CK 'MOVE' $RR »RHS» $RL »OP„ROUM» $CK 'SHFT' $RR 'LHS' $CK »MOVE' $RR «OP_ROQM» $R »SYS_FREE' $CK 'TEST' $RR 'OP_ROOM» $C »T =T' $RR 'LHS' $(FU $CK 'MOVE' $RR »OP_ROOM» $RL »OP_ROOM» $)N $)NS $)N $)N $(U $CK 'TEST' $RR • RHS ' $C »T =D» $C •(' $(S $CK 'MOVE' $RR 'RHS' $RL »OP_ROOM» $)N $)NFU $CK 'TEST' $RR »LHS» $C »T =D» $C '(' $(S $RR 'TRANS' $) $CK 'TEST' $RR 'RHS' $C »T =D» $C '(» $(S $CK 'MOVE* $RR 'RHS' $RL »OP_ROOM» $R 'C_TO' $) $CK 'TEST' $RR 'LHS' $C 'T =D» $C •)' $(S SCK «SHFT» $RR 'LHS' $CK 'SHFT' $RR 'RHS' $CK 'SHFT' $RR «OP_ROOM' $RR 'TRANS' S) $R »C TO' $)X /***** REFERENCE FOR TREE REPLACEMENT *****/ $S 'TRANS' $(XN $CK 'SHFT' $RR 'LHS' $CK 'SHFT' $RR 'RHS' $CK 'SHFT' $RR 'OP_RGOM» $CK 'TEST' $RL 'RHS' SC 'A =D« $C 'V $(FU $CK 'TEST' $RL 'RHS' $C » = ' 4C »#' $(FU SCK 'MOVE' $RL 'OP'ROOM' $R 'SYS.FREF' $CK 'MOVE' $RL 'RMS' $RL »OP_ROOM« S)N 38 $)NS $(N $CK 'TEST' $RR 'LHS' $C 'T = = D» $C M' $(S $CK •TEST' $RR 'LHS' 1 sc •A =D' $B •0' $(S $CK •TEST' $RR •RHS' $C 'T =D' sc $(S $CK $CK $CK $R ' •SHFT' •SHFT» •SHFT* >D_TO» $RR $RR SRR 'LHS» 'RHS» •OP_ROOM» $)N $(FU $CK $CK 'MOVE' •MOVE' $RR $R $RR $R »LHS' •SYS_FREE» 'OP_ROOM» •SYS FREE' ' ( $R 'D_TO» JIN $)N $(FU $CK 'SHFT' $RR 'LHS' $CK 'SHFT* $RR 'RHS» $CK 'SHFT' $RR «OP_ROOM» SR 'C_TO» SIN $>N $(FU $CK 'COPY' $C 'SCAN' $RR 'LHS' $CK 'RSTR' SRL »LHS' $CK 'TEST' SRL 'LHS' $C 'A =D« $B '0' $(S $(N $CK 'TEST' $RR »LHS' $C ' = » $C 'SCAN' $(FU $CK 'MOVE' SRR 'LHS' SRL 'LHS' $)N %)S $CK 'MOVE' $RR 'LHS' $R 'SYS.FREE' $R »D_TO» $)N 4(FU $(N $CK 'TEST' SRR 'LHS' $C • = » $C 'SCAN' $(FU $CK 'MOVE' $RR 'LHS' $RL 'LHS $)N SIS SCK 'MOVE' $RR 'LHS' $R 'SYS FREE' SR 'C TO SIN S)N SCK 'TEST' $RR 'LHS' $C 'A =D» SB $)S SRR 'MAIN' S)X /***»* MAIN PROGRAM FOR TREE TRANSFORMATION *****/ SSN 'MAIN' S(XN SCK • SHH I • SRR 'TREE' SCK 'SHFT' iRR"*'«PATTFRN» SCK »SHFT» SRR »LHS' SCK 'SHF1 ' JRR 'OP ROOM' 39 $CK 'SHFT* SRR 'CORRESPO' SCK »SHFT' $RR »TEMP» $CK 'MOVE* $RR 'PATTERN' $RL 'PATTt-RN' SCK 'SHFT' SRR 'REPLACE' $CK 'SHFT' SRR 'REPLACE' $CK 'SHFT' SRR 'RHS' $R 'MATCH' $CK 'COPY' SR »OP_ROOM» SR 'SYSPRTNT' SCK 'COPY' SR «LHS» SR 'SYSPRINT' SCK »RSTR' $RL 'LHS' $CK 'RSTR' SRL 'OP_ROOM» SCK 'TEST' SRR 'REPLACE' SC 'T =0' $C »(' S(S SCK 'COPY' SRR 'REPLACE' SRR 'RHS' $)N SCK 'COPY' SRL 'REPLACE' SRR 'RHS' SCK 'COPY' SR 'RHS' $R 'SYSPRINT' /** TREE REPLACEMENT **/ $R 'TRANS' SCK 'COPY' $R 'LHS» $R 'SYSPRINT' SCK 'COPY' $R 'RHS' $R 'SYSPRINT' SCK 'COPY' $R »OP_ROOM« SR 'SYSPRINT' SCK 'RSTR' SR 'OP_ROOM« SCK 'SHFT' SRR »OP_ROOM» /***** EXCHANGE TREE VARIABLES *****/ SCK 'TEST' SRL 'CORRESPO' SC »A =D» SB »1' S(S SCK 'RSTR' SRR 'OP_ROOM» $}N $(FU SCK 'TEST' SRR »OP_ROOM» SC 'A =D' SC 'V $(S SCK 'RSTR' SRL 'CORRESPO' $(N SCK 'TEST* SRR 'CORRESPO' SC • = ' SRR 'OP_ROOM» $(FU SCK 'MOVE' SRR 'CORRESPO' SRL 'CORRESPO' SCK 'TEST' SRR 'CORRESPO' SC 'A =0' SC 'V S)S $)S SCK 'SHFT' SRR 'CORRESPO' SCK 'MOVE' SRR «OP_ROOM» SR »SYS_FREE» $(N SCK 'TEST' SRR 'CORRESPO' SC 'A -D' SC 'V S(FU SCK 'COPY' SRR 'CORRESPO' SRL »OP_ROOM» SCK 'MOVE' SRR 'CORRESPO' SRL 'CORRESPO' $)N S)S /** TREE VARIABLE IS NULL **/ SCK 'TEST' SRL 'OP_RDOM» SC 'T =D' SC '(• $(S SCK 'TEST' SRR 'OP„ROOM« SC »T =D' SC ')• $(S SCK 'SHFT' SRR 'OP_ROHM» SCK 'MOVE' SRL »OP_ROOM» SR «SYS_FRFF« S)N S)N SCK 'SHFT' SRL 'OP_ROOM» $)NFU ko S)S SCK SCK SCK SCK $(S S)N SCK /#* S ( FUN $)S S(N 'SHFT' ' TEST SRR $RR •OP_Rni)M» 'OP ROOM* $C ' A =D' SB •MOVE' SRL 'TEST' SRR SCK 'MOVE' SCK 'MOVE' »TRhfc» SR «SYS_EREE' •TREE' SC »T =D' $C • ( • SRR 'TREE 1 SR 'SYS_EREE' SRL 'OP ROOM' SRR ^TREE' •MOVE 1 SRL »OP_RnOM» CLEAR UP WFSS **/ SCK 'TEST 1 SRL 'LHS« S(FL SCK »«UVfc' SRI SRL 'TREE' SC 'A =0' SR ' 1 ' ' LHS« SR ' SYS Ek; i- ' ♦ i iv SCK 'TEST' SRL 'RHS' SC 'A = ()• SB '!• $(EU SCK 'MOVE' SRL 'RHS' SR »SYS_EREE' $)M S)S SCK 'RSTR' SRL 'CORRESPO' S(N SCK 'TEST' SRR »CORRESPO» SC » = r SCV 'TO' $(FU SCK 'MOVE' SRR 'CORRESPO' SR 'SYS_EREE« S)N S)S SCK 'COPY' SR 'TREE' SR 'SYSPRINT' SR 'RESET' S)X kl -APPENDIX B MJCLEOL AND TREE ALGORITHMS k2 The purpose of this appendix is to introduce readers to NUCLEOL programming by discussing a number of programs for typical tree algorithms -- ranging from simple algorithms which convert one tree representation into another, to the rather elaborate Alpha- Beta pruning technique for the mini- mal evaluation of game trees. Familiarity with NUCLEOL to the extent described in [1] is assumed. ALGORITHMS FOR SEQUENTIAL TREE EXPRESSIONS Algorithm FE and Algorithm EP (Parenthesized prefix and postfix expressions) In Chapter 2 we introduced the notion "parenthesized prefix tree expression"; here we introduce a similar concept called "parenthesized postfix (or endorder) tree expression". Postfix (or endorder) enumeration of nodes for a single tree as well as for an ordered forest may be defined as follows: To traverse a forest in postfix order: if it is empty, then termi- nate, otherwise traverse its first tree in postfix order, and thereafter traverse the forest of the remaining trees in postfix order. To traverse a tree in postfix order: visit the root, and thereafter traverse the forest of its subtrees in postfix order. A parenthesized postfix expression is obtained by adding a pair of parentheses to enclose all the sons of every nonterminal node. For example, the parenthesized postfix tree expression of the tree v igure 1 enumerated by the above process will be ( ( D K ) B ( F ( T J ) G H ) C ) A h3 Fig. B-l A tree where the corresponding parenthesized prefix tree expression is given by: A(B(DE)C (FG(IJ)H) ) The algorithm PE transforms from parenthesized prefix expression to parenthesized postfix expression and the algorithm EP does the converse transformation. ALGORITHM PE kk /***** IREE $S 'TREE* $) $C IN PREEIX ORDER *****/ $(X *C 'A' *( tC »R» •C $( SC »F» *C 'G' $)X M $C •H» •IJ' $( • I ' /***** TEMPORARY STORAGE *****/ $S 'TEMP' $(X *)X /***** MAIN PROGRAM *****/ $SN •PRETOEND' $(XN tCK «SHET» SRR 'TREE' $CK 'SHFT« $RR • TEMP' $(N $)S $ ) X $(S $)N $CK $CK tCK $R $CK 'SHET' SRR «TREE» •TEST' $RR "TREE* $C $CK $CK $CK $CK $CK •MOVE' 'MOVE' 'MOVE* 'SHET' •RSTR' $RL $RR SRR tRL SRL 'TREE' •TREE' ' TEMP' •TREE' •TREE' T =D' $RR $RL $RR •TEMP* •TREE' •TREE' •SHET' 'TEST' $PR $RR •TREE' • TREE' $C •A =•)• $B « 1 • 'COPY' $R •SYS STOP' •TREE' $R 'SYSPRINT' ALGORITHM EP: k5 /*?*** TREE IM PREIIX ORDER **#**/ IS • TkE^' J!X $C 'i- 1 *C ' G' M i( *c »IM $ k> • E» $) $C •«• $( * ( $c • I« $) $c •H» S> ) *c •C ' i ) *)X / ... * v -!< * iEMPORARY *S 'TEMP' t(X S I'i.iK 1 Ar,(- * )X • / vbw •E-NinopRb' t (N $)S $)X $CK $(S $(XN *L,< •TEST' $RL $CK $CK $CK $CK 'MOVE' 'MOVE* •MOVE • »SHET» 'RSTR • 'SHET' •TEST • $RL $RL •COPY' $R •SYS STUP' ' S j 'ET« •SHET' • SHET' • TREE' SRL $RR $RL $RR $RL $RR $RR SRR •TREE' •TREE' 'TEMP' •TREE' •TREE' •TREE' 'TREE' $C • I REE' •TEMP' • I R E E • •T =!)• $C ) • $RR $RR $RL •TEMP' •TREE' •TREE' •A s|.) $B • 1 • 'TREE' $R 'SYSPRINT' 46 Algorithm TL (Generation of parenthesized tree expressions from level sequences) A sequence of levels of nodes in prefix order gives a unique tree structure (unlabeled). For example, a sequence of the levels of node in prefix order 0122122332 corresponds to the tree structure in Figure B-2, whose parenthesized tree expression is given by ((()())(()(()())())) / 2V level 2 , %3 level 1 1 level 2 level 3 Fig. B-2 Levels and degree of nodes In general, such a sequence has a form £ 1 £ 2 """ £ i h + 1 """ £ n where n is the number of nodes in the tree, and I. is the level of the i-th node in prefix order. Any two neighboring levels I. and I. satisfy one of the following relations : 1) I. = I. , - 1 node i directly dominates node i + 1. 11 + I 2) £. = I. n nodes i and i + 1 are brothers, 11 + I ' 3) t. > l. , all other cases. 1 l+l 'he algorithm TL generates a parenthesized tree expression from a ordered sequence of levels. hi ALGORITHM TL: /***** LEVELS IN PREFIX ORDER *#***/ SS 'LEVEL* $(X $D »0' $D •!• $0 »2» $1) '2' $D ' 1' $D »2* $D ' 1« SD '2' $1) »2' $0 '2» S)X /.***** STORAGE FOR THE RESULTING TREE *****/ SS 'TREE' $(X $)X /**#** MAIN PROGRAM ****♦/ SSN 'PROG' $(XN 5.CK 'SHFT' $(N $(NF SCK 'SHFT' SRR SCK 'TEST* $RL S(S SCK 'SHFT' SCK 'COPY' $CK »SHFT» SRR 'LFVEL' SCK «SHFT' SRR 'LEVEL* •TREE' SCK 'COPY' $( $) SRR 'TREE' 'LEVEL' $C ' < • SRR 'LFVFL' $RR 'TREE' $( $) $RR 'TREE' $RR 'LEVEL' S)N $)F $(N SCK $(S 'TEST' $RL SCK •TEST' $RR $ SRR 'TREE' SRL SRR 'TREE' SRL 'LEVEL' $U SCK 'TEST' $RL S(S $CK 'MOVE' $CK 'SHFT' SCK 'SUB' $)NWU $)FU SCK 'COPY' SR 'TREE' SR 'SYSPRINT' $CK 'TEST' SRR 'LEVEL' SC 'A = D* SB $)S SCK 'COPY' SR SR 'SYS_STOP« $) X ' SRR 'LFVFL' 'TREE' 1 SRL 'LFVFL ' •TREE' SR 'SYSPRINT* ka Algorithm TD (Generation of parenthesized tree expressions from degree sequences) A sequence of degrees of nodes in prefix order also gives a unique tree structure. For the tree of Figure B-2 this sequence is given by 2200302000 In general, such a sequence may be expressed as cL d„ a --- d. d. . --- d 123 ii + 1 n where d. is the degree of the i-th node in prefix order (d. = indicates terminal node). The algorithm TD generates a parenthesized tree expression from a given prefix ordered sequence of degrees of nodes. k9 ALGORITHM TO: $S 'DEGREES' THIS WES DESCRIBES TREE IN POLISH PREFIX NOTATION SIX $D '3' $D '2' SL) '0' $D '0' SD '1' SU '0' $D '3' S>> '()• $D »0' $IJ »0» $)x SS 'TREE' WILL CONTAIN THE GIVEN (REE CONVERTED TO PARENTHESIS NUT A 1 IOM SIX THE SMALLEST WFS HAS A SCANNER AND A PAI^ OE PARENTHESES MARKED BY AN ATTRIBUTE "X" $)* $SN 'PROG' THE ATTRIBUTE "N»(EOR "NEUTRAL") MARKS THIS THE EXEC SCANNER S(XN SCK 'SHET' SRR 'DEGREES' SCK 'SHET' $RR 'TREE' SCK 'COPY' $( $) THIS IS A IMMEKIATF ARGUKt-NI $RR 'TREE' $(N BEGINNING OE MAIN LOOP SCK 'SHFT« SRR 'DEGREES' SCK 'TEST' SRL 'DEGREES' SC ' = • SD »0' $(E SCK 'SHET' SRR 'TREE' SIN SCK 'COPY' S{ S) SRR r TREE» SCK 'SUB' SRL 'DEGREES' $0 '1' SRL 'DEGREES' SCK 'TEST' SRL 'DEGREES' SC • S)S $)N SIS SCK 'MOVE' SRR 'TREE' SRL 'TREE' SIN SCK 'TEST' SRR 'TREE' SC »T =D« $(S SCK »SHFT« SRR 'TREE' S)N S)F S)N SCK 'TEST' SRR 'DEGREES' SC »A =D» SB S)S END OE MAIN LOOP SCK 'COPY' $R 'TREE' SR 'SYSPRINT' $R 'SYS_STOP' $)X SC SD 50 Algorithm TA (Lexicographic Binary Tree Generation) Algorithm TA generates from an arbitrary sequence A.^ , A , ... , A of words (character strings) over some ordered alphabet a parenthesized binary tree expression with the following properties: 1) The root is labeled A 2) For every node (labeled A.) its left son (labeled A.) if it exists, and its right son (labeled A, ) if it exists, the relations A. < A. < A, 1 j In hold (where "<" means "precedes in lexicographical order"). For example, the sequence of single-character words HLEBACFNJKMI will be converted to: H E B L J N ( ( ( AC ) F ) ( ( I K ) ( M ) ) ) whose tree is shown in Figure B-3, where nodes labeled "*" have been added to make the resulting tree perfectly binary. H, K M Fig. B-3 In general, sequences consisting of the same words in different ord' e ■ .';<• to dd ffere eee. ALGORITHM TA 51 /***** ARBITRARY ALPHABETS *****/ SS 'DATA' SIX SC 'G' SC 'J' $C SC »C SC 'D' SC »F« SC «K» SC »H' SC •E' $C »B' • 1 • $)X sc A» /***** STORAGE FOR THE RESULT *****/ SS 'TREE' $(X $)X /#**** MAIN PROGR $SN 'PROG' $(XN $(N $CK S(S $(F $CK $CK S(F $CK $(S S)N S)NF $)NS S)S S(F SCK S(S S)N $(NF *CK 'COPY' SR •TREE' SR • SYSPPTMT' $CK ' TEST' SRR 'DATA' $C ' A =D' SB mi $)S $R • SVS.STOP' S)X 53 ALGORITHMS FOR POLISH NOTATION Polish expressions are sequences of operands (or arguments) and operators (each of which has a degree associated with it, which is the number of operands it requires), formed according to the following rules: 1) An operand is a Polish expression. 2) If OL , a , ... , a are Polish expressions, and Q is an operator of degree n, then Q, OL a . . . a is a Polish express- ion. We assign ranks to operands and operators as follows : 1) An operand has rank 1. 2) An operator of degree n has rank 1-n. Let S : S S . .. S be any sequence of operators and operands, and let r. be the rank of S. for i = 1, 2, . . . , n. Define the sequence XL , Z_, . . . , Z of partial sums as T. 2 n h - \ r i • i=k Then S is a Polish expression if and only if (l) each Z. is positive for i=l, ... , n and (2) L = 1. In the following examples, operands are denoted by single digits and operators by capital letters. The examples use four operators of degree 2 (A, S, M, D) and one operator of degree 1 (N). Algorithm TP This algorithm generates a prefix parenthesized tree expression from a given Polish expression. For example, let a Polish expression be ASN1S23M1+DN56 , then the algorithm generates A S N S M D N ( ( (1)( 23))U((5)6))) which represents the tree of Figure B-k* % Fig. B-K ALGiiR I l mm TP: 55 / =i< * ! SS TUN $c ****/ / * * *s JC /** s>S I I t / * POLISH NOT A POLISH' $(x •S« J!) , 2' $1) »3' JC »M' D »5« $1) '6« J)X * OPERA Tl IRS I ABLE *** OPERATOR" J(X JC 'A' M' tl) '2« JC »0' * TREE HE THE FORMULA ** TREE' J(X JC •*' J)X * TEMPORARY STORAGE **** TEMP' $(X J)X JC • S« JL) JL JC • iv • $i) JC t iv i JD JL) / •2' JC jc •s« • IM • JD '2' • 1 • J, ) A *** MAIN ' PROG' J(N J)S $ ) X PRU $( JCK J(S J)N J(RJ J)N JCK $CK JR GRAM ** : XN JCK JCK JCK $CK ♦ JRR • TEST' J(N JCK 'TES I ■ **/ •SHET' •SHET' 'SHET' •SHET' 'POLISH' JRR J)S JCK JCK $(N J)S JCK JCK JCK JCK S(N J)S J(EU JCK 'SHt-T' JCK 'SHET' JRR JRR 'KJLIS' ' JRR ' TREE' JRR 'OPERAFUR' JRR 'TLMP' JC »T =()• JC »C •POLISH' $C » = ' JRR 'OPERATOR' JRR 'OPERATOR' $)IM 'OPERATOR • •COPY* JRR 'OPERATOR' •RSTR' JRL 'OPERATOR' JCK 'TEST' JRR 'TREE' J(EU JCK 'SHET' JRR JRR 'TEMP' JC • TREE' J )N •MOVE' JRR 'TREE' JR •MOVE' JRR •COPY' J( •SHET' $RR JCK 'COPY' JCK 'SOB' JCK ' I E S T » • SY ^_EREE' • TREE' 'POLISH' JRL J) JRR 'TRBE' ' TREE' $C ' *' JRR ' I REE ' JRR ' TEMP ' JO • 1 • JRR MEMP« JC ' JRR J(N J)S JCK JCK •COPY 'TEST JCK 'TEST' JRR $(hU JCK 'SHET' TREE' JC • = ' JRR ' "I R B E ' $ ) IM •MOVE' JRR 'TREE' JR 'SYS_FREE' •MOVE' JRR 'POLISH' JRL 'TRPE' • JR 'TREE' JR 'SYSPRInT' • JRR "POLISH" JC »A =1)' JB •!» 'SYS STOP' JC i * i JO ' ' JC • * » 56 Algorithm PL This algorithm computes the level I. of the i-th element (operator or operand) in a Polish expression. By level we mean the level of the node labeled with this element in the tree corresponding to this express- ion. Algorithm PL, however, works directly on the Polish expression without converting it into parenthesized form. The algorithm proceeds in the following two steps. Step 1: Form the partial sum sequence Z. , Z , ... , Z . Step 2: Beginning at the left end, form two sequences I. , I. , ... , and c. , c_, . . . , c as follows : 12 n 1 2' ' n i) i x = 1, c ± = ; ii) for i = 1, 2, ... , n-1 if Z. < Z. _ then set i. , = i„ i l+l l + l 1+1 and c . n = : l + l ' if Z. > Z. then determine the last k which is less than or equal to i such that c = and JJW then set i. = i , c. _ = 1 and c, = 1. i + l ki + 1 k After termination of the algorithm PL on a Polish expression, I. the level of the i-th element in the expression. 57 ALGORITHM PL: /***** POLISH NOTATION ***#*/ $S 'POLISH' $(X SC 'A' $C 'S' SC 'N' $1) '1' $C »S' $0 '2' SO '3' SC 'M' $0 '4' SC *0» SC « IM ' SD • b « $0 '6' $)X /-**** RANKS OF THE NOTATION *****/ $S 'RANKS' S(X S)X /***** PARTIAL SUMS *****/ SS 'P_SUMS' S(X S)X /***** LEVELS OF THE TREE OF THE FORMULA *****/ 4S 'LEVEL' $(X S)X /#**** c TABLE *****/ $S 'C( I ) • $(X S)X /***** OPERATORS TABLE **#**/ $S 'OPERATOR' $(X $C 'A' SD '2' $C 'S» $D '2' $C 'M» SD '2' SC 'D» $D '2' $C 'N' $D '1» $)X /***** MAIN PROGRAM *****/ $SN 'PROG' $(XN $CK «SHFT» $RR 'POLISH' SCK 'SHFT' SRR 'RANKS' $CK 'SHFT' $RR 'P_SUMS' SCK 'SHFT' SRR 'LEVEL' $CK 'SHFT' SRR »C(I)« SCK 'SHFT' $RR 'OPERATOR' S(N $CK 'TEST' $RR 'POLISH' $C »T =D' SC »C» $(S $(N SCK 'TEST' SRR 'POLISH' $C ' = ' SRR 'OPERATOR' SCK 'SHFT' SRR 'OPERATOR' S(FU SCK 'SHFT' $RR 'OPERATOR' $)N $)S SCK 'SUB' SD '1' SRR 'OPERATOR' SRL 'RANKS' SCK 'RSTR' SRL 'OPERATOR' S)N $(FU $CK 'COPY' $D '1' $RL 'RANKS' $)N $CK 'SHFT' SRR 'POLISH' SCK 'TEST' SRR 'POLISH' $C 'A =D' $B '1' $)S SCK 'COPY' $R 'RANKS' $R 'SYSPRINT' $CK 'SHFT' SRL 'RANKS' SCK 'COPY' SRR 'RANKS' SRR • P_SUMS ' $(N SCK 'ADD' SRL 'RANKS' SRR »P_SUMS« SRR 'P_SDMS' SCK 'SHFT' SRL "RANKS' SCK «1EST» SRL 'RANKS' $C 'A =D' SB '1' S)S 58 SCK S( SM SCK S)S S ) X SCK S(F S) $CK $CK SCK SCK S(S $ )N SCK $(S $)N SCK S(S Y' $R 'P^SUMS' SR 'SYSPRINT' •TEST' SRR «P_SUMS' SC • = • $D »1' SCK •COPY' SC 'NOf Wt-LL l)F-lNM.»t $K •SysPKImm SR 'SYS STOP' S )N •COP SCK SR •COPY* •COPY' 'SMFT • •TEST' $U ' 1 • SRL SU '0' SRL SRR «P_SUMS f URL 'P SUMS' •LEVFL' 'C< I ) • $C SCK 'ADO' SCK 'COPY' SCK 'SHU ' 'TEST' SRL SCK 'ADD' SCK 'COPY' SCK «SHFT» SRL SO SRR •LEVEL' •0' SRL »P SUMS' SI) < • • 1' 'P_SUMS» $C SRL 'LEVEL' SD '1' SRL SRR 'P SUMS' «C( I ) ' • = i $r> • l • »C( n» SRR SRL SRR SRL l p s 1 1 to S ' •LEVEL • *P SUMS' •LEVFL • •TE SCK S(N ST' SRL «P_SUMS' •COPY' $0 '1' SCK 'SHFT' SRL SCK 'SHFT' SRL SCK 'TEST' SRL $C ' > • SRL 'C(I)' •LEVEL' •C( I ) • •C( I ) ' SC SRR 'P SuMS' $0 '0' $)S SCK SCK SCK S(N S)S SCK SCK •COPY' SRR 'MOVE' SRL •COPY' SD SCK 'SHFT' SCK 'SHFT' SCK 'TEST' •LFVFL' SRR 'POLISH' •Cm* $R »SYS_FREE' '1' SRL 'C( I ) • SRR 'LEVEL' SRR 'C(I)' SRR 'C( I ) ' SC »A =D« SB ' 1 ' 'MOVE' •SHFT' SRR SRR 'POLISH' »P SUMS' SRL 'LEVEL* Y' SR 'LEVEL' SR 'SYSPRINT' 'TEST' SRR «P_SUMS» SC 'A =D' •SYS STOP' SB 1 » 59 ALGORITHMS FOR GAME TREES Many problems in artificial intelligence involve the searching of large trees of alternative possibilities. Game playing and theorem proving are examples. This section is concerned with the problem of efficient evaluation of game trees by the "minimax" procedure and its refinement, called "alpha-beta pruning". A further improvement of the latter procedure may be obtained by reordering the nodes of the tree so that more alpha and beta cutoffs are obtained, but this will not be discussed here. The more sophisticated techniques are most useful on very deep trees. Algorithm MM (MiniMax Procedure) We assume a tree is given each of whose leaves is labeled with a number. The minimax procedure assigns a number to the root of this tree by repeating the following step as long as possible: if N is a node which has only leaves (L , L , ... , L ) as direct descendants, then label N with the number a) the maximum of the numbers which label L , ... , L , if the level of N is even, or b) the minimum of the numbers which label L , ... , L , if the level of N is odd, then treat N as a new leaf (the program listed will delete L , ... , L, , so it terminates with a tree which consists of the labeled root alone). 60 ALGORITHM MM: /***** CAMfc TREE *****/ $S 'TREE' J(X J( J( J( J( $( $1) MO' J) J( $n '«• JO MO' JD '17' JD '4« JD «3« J) $) $1) »4» JD »5« J) JD '3' $) $0 '7' JD '4' $) J)X /***** REFERENCE OPERATORS *****/ JS 'OPERATOR' J(X JC • <= • JC • >= ' J)X /***#« TFMPORARY STORAGE *****/ IS 'TEMP' ${X J)X /***** RFFERENCE FOR SETTING OPERATOR *****/ SS 'SFTOP' J(XN JCK 'TEST' $RL 'OPERATOR' JC ' ( • J(S JCK 'SHFT' JRR 'OPERATOR' $)N $(F JCK 'SHFT' JRL 'OPERATOR' $)N $RR 'PROGRAM' J)X $C I =0 /***** MAIN PROGRAM *****/ $SN 'PROGRAM' $(XN $CK 'SHFT' JCK 'SHFT' JRR 'TREE' JCK 'SHFT' JRR 'TEMP' J(N JCK 'COPY' JR 'TREE' JR JCK 'TEST' JRR 'TREE' JC $(S JR 'SETOP' JCK 'SHFT' JRR 'TREE' $CK 'TEST' JRR 'TREE' $C J)NF JCK 'TEST' JRR 'TREE' JC »T =T' SIS JCK 'SHFT' JRR 'TREE' JCK 'TEST' $RR 'TREE' JC JCK 'TEST' $RR 'OPFRATOR •SYSPR1NT • •T = D» JC •T =0' $C jd »o» » f » J( s $(S ICK 'MOVE' J)N JCK 'MOVE' •SHFT' JRR $RL 'TREE' $RR 'TREE' • T =T' $1) '0' $RR 'OPERATOR' JRR 'TREE' $R 'SYS FREE' JRL 'TREM •TRFE' J( F JCK $ )N SCK ' IHST • JRR J)NF $)NF 'TEST' JRR • TRH ' JC J( F •TEST' JRR 'TREE 1 JC J( S ICK 'MOVfc ' JRL »TREI ■ %( ' r .Hh F ' 1RR • TREh ' JR 'SYS FREE' 'TREE' JC »T =1 JlJ 'A =A' JRL 'TEMP' •T =D« ' * JC • ) • JRR • 1 HMR» 6i i lINi S)S tCK iCK $(S S)N ' SETOP' ' MHVH • •M!i\/f ' ' ll-ST' SRI «;RW $RL <>CK 'SHFT' J>R 'SYS STOP* •TREE' SR 'TfrMP' $RW •TREE* $C $RL 'TREE* •SYS. FREE' • TREE' •1 s T • * 1 1 •0 62 Algorithm AB (Alpha-Beta Procedure) Alpha-beta is a refinement of the minimax procedure described above, which is more efficient, but is equivalent in the sense that both procedures will always assign the same value to the root of the tree. It saves time by not searching certain branches of the tree. Under certain conditions the value of a branch does not affect the value which is ulti- mately backed up to higher levels of the tree. Hence, there is no point in evaluating such a branch. When the alpha-beta program detects such a condition, it stops work on one branch and skips to the next. This event is called an alpha or beta cutoff, depending on whether the level of the node where it occurs is even or odd. Alpha is a number defined by the valves of the successors of a max position (odd levels), and alpha cutoffs occur among successors of a min position (even levels). And beta is established at even levels and generates cutoffs at odd levels. The action of beta cutoffs is exactly the inverse of that for alpha cutoffs. The effect of alpha and beta cutoffs is to make the tree grow more slowly with depth. Thus the advantage of alpha-beta over the simple minimax procedure increases with the depth of the tree. 65 ALGORITHM AB: / /***** GAME TREE *****/ $S 'TREE' t(X %{ 4D '6' $D '8' $( $U »3» $( if »8« $ D » 9 ' S ) 4) $D '10' $( %i $D «3' $1) » V • $0 '6' $) *) i) $)X /***** REFERENCE OPERATORS *****/ 4S 'OPERATOR' ${X *C ' < • $C ' > • *)X /**$** TEMPORARY STORAGF *****/ $S 'TEMP' S(X $)X /***** REFERENCE FOR SETTING OPERATOR ****#/ $S 'SETOP' $(XN $CK 'TEST' *RL 'OPERAIOR' $C $C • ( ' $(S $CK 'SHFT' *RR 'OPERATOR' $)N $(F $CK 'SHFT* $RL 'OPERATOR' $)N $RR 'PROGRAM' $)X 1 =1) /***** MAIN PROGRAM *****/ tS 'MINIMAX' MX $D '100' $)X $SN 'PROGRAM' $ ( XN $CK 'SHFT' $RR 'OPERATOR' $CK 'SHFT' $RR 'TREE' $CK 'SHFT' $RR 'TEMP' $CK 'SHFT' $RR 'MINIMAX' t(N $CK 'TEST' $RR 'TREE' $C »T = D« $C •( $(S $R 'SETOP' 'TREE' •TREE' $C $RR $RR $CK 'SHFT' $CK 'TEST' $)NF $CK 'TEST' $RR $CK 'TEST' $CK 'SHFT' •SETOP' 'MOVE' $RR •TEST' 4RL »T =D« $C ( $(S its $R JCK $CK $(S $CK $(S t(S $)N $(F $CK iCK $)N $CK S)NF $(F 'TEST' $RR $CK 'TEST' JCK 'MOVE' $CK 'COPY' •MOVE' $RL 'SHFT' $RR •TEST' $RR $CK 'COPY' 'TREE' $C $RR 'TREE' $RL 'TREE' 'TREE' $R •TREE' $C •TREE' $C JRL 'TREE' SRR 'TREE' *RR 'TREE' •TREE' $R •TREE' »T =T» $U '0' $RR 'OPERATOR' $RR 'MINIMAX • •SYS..FREE' •T =T« $D «0» •T =T' $D '0' iRR 'OPERATOR' $RR $R 'SYS_FRFE» $RR 'MINIMAX' •SYS FREE* •TREE' 'TREE' $C 'T =T« 40 $RL 'TREE' $RR 'MINIMAX' 64 $)M S)NF S(F SCK 'SHFT' SRR 'TREE* SCK 'TFST' $RR 'TREE' $C »T =T« $0 '0' $(S SCK 'TEST' SRL 'TREE' SRR 'DPERAIOR' SRR MRFM $(S SCK 'MOVE' SRR 'TREE' SR 'SYS FRFE* $)N $(F $CK 'MOVE' $RL 'TREE* SR 'SYS FREE* $CK «SHFT» $RR 'TREE* $)N SCK 'TEST' SRR 'TREE' SC 'T =T» $0 »0» $)NF $(F SCK 'COPY' SRL 'TREE' SRR 'MINIMAX' S)N $)N $)N $CK 'TEST' $RR 'TREE' SC 'A =A' $RL 'TEMP' $(F SCK 'TEST' SRR 'TREE' $C 'T =D« SC •)• S(S SCK 'MOVE' $RL 'TREE' SRR 'TEMP' $CK 'SHFT' SRR 'TREE' $R 'SETOP' SCK 'MOVE' $RL 'TREE' SR 'SYS. FREE' $CK 'MOVE' SRR 'TEMP' SRR 'TREE' SCK 'TEST' SRL 'TREE' $C »T =T« SO '0' S(S SCK 'SHFT* SRL 'TREE' S)N S)NF S)NF SCK 'COPY' $R 'TREE' $R 'SYSPRINT' SCK 'COPY' SRR 'MINIMAX' SR 'SYSPRINT' S)S SR 'SYS_STOP' $) X