Pattern Recognition, Vol. 21, No. 6, pp. 623-629, 1988.
0031- 3203/88 $3.00 + .00 Pergamon Press pie Pattern Recognition Society
Printed in Great Britain.
PARSING OF edNLC-GRAPH GRAMMARS FOR SCENE ANALYSIS MARIUSZ FLASIIqSKI Department of Computer Science, Jagiellonian University, 31-501 Krak6w, ul. Kopernika 27, Poland (Received 29 September 1987; in revisedform 18 March 1988) edNLC-graph grammar, introduced by Janssens,"1 is a strong formalism for generating scene representations. This grammar generates directed node- and edge-labelled graphs, EDG-graphs. A method of construction of unambiguous string EDG-graph representation is briefly described. The characteristics of edNLC-graph grammar for syntactic pattern recognition allows us to construct the parsing algorithm. The deterministic top-down syntax analyzer is constructed for the subfamily of an edNLC-graph grammar, called an ETL/1-graph grammar. An ETL/l-graph grammar is parallel to a finite state string grammar. The notions introduced in the paper are useful for researches in less restricted edNLC-graph grammars, for example grammars analogical to context-free string grammars. Abstract--An
EDG-graph
edNLC-graph grammar
Parsing algorithm
l. INTRODUCTION There are three structures used for representing scenes in syntactic pattern recognition: a string, a tree and a graph. The graph structure is the strongest formalism. The use of graphs for describing scenes is of frequent occurence in the literature dealing with image processing, whereas the use of graph grammars for pattern recognition is rare. This is connected with difficulties concerning building a syntax analyzer for graph grammars. The main problem is the necessity of considering an embedding transformation during syntax analysis. Some suggestions of solving this problem concern graphs and graph grammars in a particular form. "'2) The family of EDG-graphs 131 is a convenient tool describing scenes. This family is generated by edNLC-graph grammars. 14JThis graph grammar is a very strong generating formalism. For these reasons, we propose an algorithm of syntax analysis of edNLC-graph grammar in this paper. In Section 2, important definitions and notations concerning EDG-graphs, introduced in Ref. (5), are briefly reviewed. In the next section we define subfamilies of edNLC-graph grammars: L/l-graph grammar and ETL/1-graph grammar. 16~An algorithm of syntax analysis for ETL/1-graph grammar is introduced and analyzed in Section 4. This section also contains some definitions allowing us to describe problems which may appear during syntax analysis, especially error situations. Considerations on time complexity of the parser are contained in Section 5, which also gives some concluding remarks.
Syntactic pattern recognition
2. PRELIMINARIES
Let us define EDG-graph as in Ref. (3). Definition 2.1 A directed node- and edge-labelled graph (EDGgraph) over Z and F is a quintuple: H = (V, E, E, F,W), where: Visa finite, non-empty set of nodes, Z is a finite, non-empty set of node labels, F is a finite, non-empty set of edge labels, E is a set ofedges of the form (v, 2, w), where v, we V, 2 e F, W: V--* E is a node labelling function. A set F may be considered as a family of non-symmetric binary relations, t2) It means that there exists an edge label 2 - ~for each edge label 2 such that edges (v, 2, w) and (w, 2- t, v) describe the same spatial relation between primitives represented by nodes v and w. We say that these edges are "semantically equivalent", which may be written as: (v, 2, w) = sem = (w, 2- t, v) and 2- ~is an inverse edge label for 2. For example, edges: "left" and "right", "below" and "above" are "semantically equivalent". Now, we introduce a relation of simple ordering, ~<, on the set of edge labels F = {Vl..... )'nl71 ~< ... ~< Yn}, so as to construct an unambiguous string representation of a graph. If it is impossible to define this relation in a natural way, then we do this arbitrarily. The subfamily of EDG-graphs will be formulated by the following definition. 623
624
MAglUSZFLASlI'~SKI
Definition 2.2 The family of OS-graphs, ordered and semanticallyequivalent edge set, is a set S = {HI: 1. H = (V, E, ~, F, W) is an EDG-graph, 2. for each 2 • F there exists an inverse edge label 2-1•F, 3. F is simply ordered by a relation ~<, 4. for each o • V: if there exists (v, 2, w) • E then there does not exist (v, ~,, z) EE such that k = ~ or (z, ~, v)eE such that k = fl-l.}. Condition 4 means that a node of OS-graph cannot be connected with two other nodes by edges describing the same relation. The representation of a scene with the help of OS-graphs will be presented by the example.
/
// /
/
I I I
•
X
a}
Example 2.1. Let us take a set of edge labels describing relations in a two-dimensional space which is illustrated by Fig. l a and ordered: p<~r<<.s<~t<~u<~v<~x<<.y. The picture shown in Fig. 2a can be represented by graphs 01 and 02 shown in Figs 2b and c. These graphs are "semantically equivalent" in the sense that they represent the same picture but they are not OS-graphs. If we want to describe the scene by an OS-graph we
b)
c}
d)
Fig. 2. The scene representation with the help of EDG- and OS-graphs.
Y l
have to introduce a set of edge labels which describe spatial relations in a more precise way. We may, for example, represent the scene by an OS-graph, g3, shown in Fig. 2d, when we use a set of edge labels presented in Fig. lb. A detailed consideration concerning the problem of scene representations by OS-graphs is presented by Flasifiski.(2) Let us discriminate some node of graph in order to attain unambiguity of a process of graph analysis.
p
/
Definition 2.3 Let a scene P consisting of n objects KI, ..., K,, described by Cartesian coordinates (xl, Yl)..... (x~, y,) be represented by an EDG-graph G. A node v,• G corresponding with an object K, is called a S-node, if:
Y
(x~ Ys) = min { rain [(x~, yj)]}. j = 1,...,n
×
i~l,...,n
Now, we briefly describe an algorithm of transformation of OS-graph into a form convenient for construction of a linear representation presented by Flasifiski.(s) Before this, we remind that for a certain graph node another node is termed adjacent if an edge exists which connects these two nodes.
Algorithm 2.1.
T Fig. 1. The ordered set of edge labels.
1. We start from the S-node of a graph v0 and we index it with 1, as is shown, for example, in Fig. 3a. 2. We index all the nodes which are adjacent to vo with the help of a relation ~< in a set of labels of edges connecting the node vo with adjacent nodes according to an increasing order: i = 2..... k, remembering that
Parsing of edNLC
s
1
"
v
2
"
625
node), (il... i,) is a string of node indices, to which edges going out from nk come in (in increasing order), (el ... e,) is a string of edges labels ordered in such a way that an edge having a label ex comes into a node having an index ix. For example: b2 , which may be briefly written b2 , 3 3 (tps) tps (356) 356 is a characteristic description of a node b2 belonging to the graph shown in Fig. 3d.
s
1
Definition 2.6
2
6
2
6
Fig. 3. The transformation of the OS-graph into the IE-graph.
edges coming into v0 may be considered as inverse edges going out from v0. This is shown in Fig. 3b. 3. Next, we successively choose nodes which are indexed i = 2 ..... k and we index all the nodes which are adjacent to them and which have not been indexed up to this moment. This is shown in Fig. 3c. 4. We repeat point 3 for all the nodes. 5. After indexing we transform all the edges so that they are directed from a node having a smaller index to a node having a greater one. Definition 2.4 An indexed edge-unambiguous EDG-graph (IEgraph) is an OS-graph which has been transformed by algorithm 2.1. At the end of this section we introduce an unambiguous string description of IE-graph. We define this description in the same way as has been defined for graphs, called t)-graphs, by Shi. °> Definition 2.5 Let n k be the node, having an index k and a label n, of an IE-graph, g. A characteristic description of the node nk is a quadruple: nk £
(el ... e,) ' (il ... i,) where r is an out-degree of nk (out-degree of the node designates the number of edges going out from this PR 21:6-F
Let us look at an IE-graph g = (V, E, E, F, tF), where: V = {nl . . . . . nk}, I~(i = 1. . . . . k) is a characteristic description of a node n~. A string 11... Ik is called a characteristic description of a graph g. Forexampte:al b2 b3 c4 a5 d6 is acharac3 3 2 0 0 0 rst tps vr 234 356 46 teristic description of the graph shown in Fig. 3d. 3. CHARACTERISTICS OF edNLC-GRAPH GRAMMARS FOR SYNTAX ANALYSIS
Let us begin with a definition of edNLC-graph grammar. (4) Definition 3.1 An edge-labelled directed node-label controlled / e d N L C / g r a p h grammar is a quintuple: a = (E, a, F, P, Z), where E is a finite, non-empty set of node labels, A ~ E is a set of terminal node labels, F is a finite, non-empty set of edge labels, P is a finite set of productions of the form (l, D, C), in which leE, DeEDG~, r (D is an EDG-graph over E, F), C: F x {in, out}~Y, x E x F x {in, out} is the embedding transformation, Z is an element of EDGE, r, called the axiom. A formal description of a derivation for edNLCgraph grammar and its characteristics are contained in Ref. (3). Now, remember that, for example, to define the embedding transformation for some production in the following way: C (x, in) = {(a, c, y, out)} means that: (1) each edge labelled by x and coming into the node of the left-hand side of the production ought to be replaced by: (2) the edge: (a) connecting the node of the graph of the right-hand side of the production and labelled by a with the node of the rest-graph, i.e. the derived graph
626
MARIUSZ FLASIlqSKI
without the removed node corresponding to the left-hand side of the production, and labelled by c, (b) labelled by y, (c) and going out from the node a. Let us define the succeeding subfamilies of edNLCgraph grammar in such a way that it would be possible to construct a deterministic top-down syntax analyser scanning only one node in each step. The full discussion of conditions imposed on edNLC-graph grammar by these definitions is contained in Flasifiski.(6) Definition 3.2
Let H = (V, E, r., F, ug) be an IE-graph. A node having an index 1 is called the node of the first level. Some node v is called the node of the n level, when: (1) there exists (w, 2, v) e E such that w is a node of the n - 1 level, (2) for each (u, 2, v) e E or (v, 2, u) e E: u is a node at least of the n - 1 level. For example, looking at the graph shown in Fig. 3d, let it be that the node a~ is of the first level, nodes b:, b3, c4 are of the second level and nodes as, d6 are of the third level. Definition 3.3
Let G =(Y~, A, F, P, Z) be an edNLC-graph grammar. The grammar G is called the L/l-graph grammar, if: 1. P is a finite set of productions of the form (1, D, C), where: (a) leE, (b) D is an IE-graph having a characteristic description: Y~ Y2 ... Ym, or Yi, where: Yi is a characteristic rl
r2
...
rm
0
Z =go=~gl:=~...=~gn.
This derivation is called the regular left-hand side derivation, if." (1) For each i = 0 . . . . . n - 1 we apply a production for a node having the least index in a graph g~. (2) Node indices do not change during a derivation. One can easily notice that condition lb of definition 3.3, which states that the nodes of a graph of the right-hand side of production are at most of the second level, allows us to ascribe indices to all nodes of a graph during some step of a derivation without the necessity to change indices during the next steps. The condition included in definition 3.4 influences the method of construction of the embedding transformation. The embedding transformation ought to be constructed in such a way that after its application we always receive an IE-graph. Definition 3.5 makes the process of derivation unambiguous. This condition is connected with the fact that changing the order of application of edNLC-productions brings about different results. We want to avoid such an effect. Point 2 of this definition ensures us against both incorrect acceptance and incorrect rejection of the node being analyzed. After these brief notes, we introduce the following two definitions. Definition 3.6
Let G = (Y, A, F, P, Z) be a closed L/l-graph grammar. A pair (b, x), b e E , x e F , is called the potential previous context for a node label a e Z, if there exists the IE-graph, g = (V, E, Z, F, qQ belonging to a certain regular left-hand side derivation in G such that:
ri
E1 E2 ... Em -Ei 11 I2 ... Is Ii description of the node Yi, i = 1,..., m, Y~ e A, i.e. Y1 is the terminal node, Yi, i = 2, ..., m is the node of the second level, (c) C: F × {in, out} ~ Z × Y × F × {in, out} is the embedding transformation. 2. There are no two productions having the same left-hand side and the same label of the node indexed by 1. 3. Z is an IE-graph having a characteristic description which satisfies conditions defined in point lb. -
given by:
-
Definition 3.4
A L/l-graph grammar is called the closed L/l-graph grammar if, for each derivation of this grammar, Z = go ~ gl 0 . . . ~ g,, a graph gi, i = 0 . . . . . n is the IE-graph. Definition 3.5
Let a derivation of a closed L/l-graph grammar be
(bk, x, aOeE, where k and I are indices of nodes bk and al, respectively. Definition 3.7
A closed L/l-graph grammar is called the ETL/Igraph grammar, if: for each production (1) A ~ X1 X2 ... Xm if (hi, y), rl
I2
...
Fm
E1 E2 ... E . ll
I2
...
Im
• .., (bk, y) are potential previous contexts for A, then an element of the embedding transformation for the production (1) is of the form: C(y, in) = {(X~, bl, zl, in)..... (Xi, bk, Zk, in)}, where i = 1..... m and if i = 1, then zi--- y ( i = 1..... k). As we can see, the embedding transformation of ETL/1-graph grammar is constructed in such a way that a potential previous context is preserved during a derivation for all the nodes of a derived graph. If the grammar did not preserve potential previous context,
Parsing of edNLC then the characteristic descriptions of the analyzed nodes would change. Thus, we would accept or reject these nodes in error. 4. AN A L G O R I T H M O F SYNTAX A N A L Y S I S
Before we define an algorithm of syntax analysis, let us consider the following example. Example 4.1
Let us assume that we analyze the graph g shown in Fig. 4a with the help of a parser for ETL/1-graph grammar. Then, we assume that in some step of a derivation we have obtained the graph g~ shown in Fig. 4b. One can easily notice that if we now analyze nodes of graphs g and g~, indexed by "2", then we will observe an incompatibility of characteristic descriptions of these nodes. The node of the graph g indexed by "2" is described by a2, whereas the node of the 2 ps 45 graph g~ indexed by "2" is described by a 2 . This 2 tp 34 situation results from the fact that a node indexed by "5" does not exist in the graph g~ and that the edge t going out from the node "2" comes into the nonterminal node "3" in this graph. Thus, after application of a production for the node "3" the situation may change. Therefore, we store in a memory the following quadruple (3, 2, d, s), where: "3" is an index of the nonterminal node into which the
i!
627
considered edge is coming now, "2" is an index of the node from which the considered edge is going out, d is a label of the node into which the considered edge should come (according to the characteristic description of the analyzed graph, g), s is a label which the considered edge should have (according to the characteristic description of the analyzed graph, g). At this moment, we analyze nodes of graphs indexed by "3". We look for a production of the form B ~ b .... because the node of g is labelled by b and the node of g~ is labelled by B. Let us assume that such a production is of the form illustrated in Fig. 4c and it has the following embedding transformation: C(s, in) = {(c,f, s, in)},
C (t, in) = {(d, a, s, in)}. After its application we obtain the graph g2 which is shown in Fig. 4a. Now, we search in a memory for a quadruple whose first element is equal to "3", because we have applied the production for the node indexed by "3". Finding this quadruple, we compare it with the situation in the obtained graph, g2. The edge labelled by s should go out from the node indexed by "2" and it should come into the node labelled by d. As such an edge exists in graph g2 we remove the quadruple from memory. After considering the above example, we may introduce the following definitions. Definition 4.1
Let us have IE-graphs g and ~. Two nodes nkE g and tike ~ having the same index are called context-identical nodes, if they have the same characteristic descriptions. Definition 4.2
Let us have IE-graphs g and ~. Two nodes nk~g and ~ik~ ~ having the same index and described by: nk , ~k , respectively, are called potentially r
el ...er
oJ
el ...e¢
il ...it 11 . . . ~ context-identical nodes, if: l. n k =
B
2 3 1~)
s b)
~B3 c)
Fig. 4. The derivation of the ETL/1-graph grammar.
nk,
2. r = F , 3. for e a c h j e { 1 ..... r}: ifij = ~, then ej = 5, 4. for each c e { 1..... r}: if ic # ?c, then the node ic is labelled by terminal and the node fc is labelled by nonterminal. A node ic is called the potentially contextual node for node/1 k. An edge labelled by ec is called the potentially contextual edge for node n k. A node f,, is called the quasi-contextual node for node nk. Returning to example 4.1 and Fig. 4, let us note that the nodes of graphs g and g~, shown in Fig. 4a and b, indexed by "1" are context-identical, whereas nodes indexed by "2" are potentially context-identical. Elements of the stored quadruple of the form (q, k, no
628
MARIUSZ FLASII~SK1
ec) have the following meaning: q, an index of the quasi-contextual node for the node indexed by k-nk, ec, a label of the potentially contextual edge for nk, no a label of the potentially contextual node for nk. The quadruple is called the description of a potentially contextual identity. Before presenting a parser algorithm we introduce the following notations: G, an analyzed graph, H, a graph derived during parsing, Z, an axiom of an edNLC-graph grammar, D, a set of descriptions of a potentially contextual identity, n (/), a label of a node belonging to a graph G and indexed by i, ri (i), a label of a node belonging to a graph H and indexed by i, m, a number of nodes of the graph G. Now, we define parsing procedures. Procedure: CHOOSE(A, a, k) if there exists a production of which A is a label of the left-hand side, and a is a label of the first-level node of the right-hand side graph then k: = number of such a production else k: = 0. Procedure: PRODUCTION(H, i, k) the application of kth production for ith node of the graph H. Function: CONID(G, H, i) the Boolean function checking whether nodes indexed by i of graphs G and H are context-identical. Procedure: PCONID(G, H, i, D, err) if nodes indexed by i of graphs G and H are potentially context-identical then a description of a potentially contextual identity is added to the set D else err: = 2. Procedure: CHECK(D, H, i, err) if there exists a description of a potentially contextual identity (q, k, ec, no)e D such that q = i, then begin err: = 3 in the case of incompatibility of a potentially contextual node for the node indexed by i; err: = 4 in the case of incompatibility of a potentially contextual edge for the node indexed by i; remove (q, k, ec, nc) from D in the case of compatibility of all elements end.
Algorithm 4.1. The parsing algorithm. H: = Z; err: = 0; for i: = 1 to m do if err = 0 then begin if a(i) is a nonterminal node then begin CHOOSEI-ti (i), n (i), k];
if k = 0 then err: = 1 else {a demanded production exists} " PRODUCTION(H, i, k) end; if not CONID(G, H, i) then PCONID(G, H, i, D, err); CHECK(D, H, i, err) end One can easily notice that a graph G is accepted when err = 0 and a set D is empty. If the graph G is not accepted, then the kind of error is recognized according to the value of a variable err. 5. CONCLUSIONS
The parsing algorithm presented above is the algorithm of a deterministic top-down syntax analyzer. Before considering what time complexity it has, let us note that the ETL/1-graph grammar for which the parser has been constructed is a grammar parallel to a finite state string grammar. Therefore, the parser for ETL/1-grammar is, in fact, a deterministic finite state automaton supplemented by procedures connected with the embedding transformation. Let us consider time complexities of the following procedures: C H O O S E - - O(k), where k is the number of grammar productions, P R O D U C T I O N - - O ( r ) , where r is the maximum number of nodes of the right-hand side of production, CONID, PCONID--O(q), where q is the maximum number of elements of the indices, edge labels, string in a characteristic description, CHECK--O(d), where d is the maximum number of elements of the set of descriptions of a potentially contextual identity. Denoting a number of nodes of an analyzed graph by n, we assume that all the procedures have time complexity O(n), because k, r, d, q < n. Thus, the algorithm 4.1, the parsing algorithm, has time complexity O(n2). These theoretical considerations has been checked by several experiments. The parser for the edNLC-graph grammar GRAPHPARS has been implemented in the Department of Computer Science, Jagiellonian University. The results of these experiments will be the subject of a paper which is being prepared now. The ETL/1-graph grammar has been defined in such a way that it would be possible to construct a syntax analyzer easily. It means that a lot of restrictions have been imposed on the definition of this grammar, which may be troublesome during constructing such a grammar. Therefore, basing on the definitions contained in this paper, at present, we are carrying out researches into defining an edNLC-graph grammar being analogical to a context-free string grammar. On the other hand, investigations of the error-correcting
Parsing of edNLC parser and the probabilistic ETL/1-grammar are led in order to recognize distorted patterns. The results of these researches will be the subject of further publications. SUMMARY The use of graphs for describing scenes is of frequent occurence in the literature dealing with image processing, whereas the use of graph grammars for pattern recognition is rare. An edNLC-graph grammar, introduced in Ref. (4), is a strong formalism for generating scene representations. This grammar generates directed node- and edge-labelled graphs (EDG-graphs). For these reasons, an algorithm of syntax analysis of edNLC-grammars for pattern recognition is presented in the paper. First of all, some possibilities of scene representation with the help of EDG-graph are considered. The ordering relation on the set of edge labels of a graph and the definition of the family of IE-graphs is introduced in order to define an unambiguous string representation of a graph. The string representation is defined on the basis of a formalism presented in Shi. ~ljSecondly, the definitions of the subfamilies of edNLC-graph grammars are introduced. The L/l-graph grammar is parallel to the finite state string grammar. The problem of the embedding transformation appearing during syntax analysis is solved by defining the ETL/1-graph grammar. Definitions of a context identity of nodes, a potential context identity of nodes and a description of a potentially contextual identity allow us to describe
629
problems appearing during derivation of edNLCgraph grammar. They are very useful for defining a syntax analyzer because it is a top-down parser. The situations which may appear during syntax analysis of ETL/1-graph grammars are considered in examples. Then, procedures of the parser are defined. After that, the parsing algorithm is considered and the cases of the end of its working are presented. At the end, the problem of time complexity is considered. Possibilities of using notations introduced in the paper for defining an edNLC-graph grammar being analogical to a context-free string grammar are discussed.
REFERENCES
1. Q. Y. Shi and K. S. Fu, Parsing and translation of attributed expansive graph languages for scene analysis, IEEE Trans. Pattern Anal. Mach. lntell. 5, 472-485 (1983). 2. M. Flasifiski, Analysis of scene represented by directed node- and edge-labelled graphs, Podst. Sterow. 1-2, 53-64 (1987). 3. D. Janssens, G. Rozenberg and R. Verraedt, On sequential and parallel node-rewriting graph grammars, Comput. Graphics Image Process. 18, 279-304 (1982). 4. D. Janssens and G. Rozenberg, On the structure of node label controlled graph languages, Info. Sci. 20, 191-216 (1980). 5. M. Fiasifiski, Linear representation of EDG-graph for syntactic pattern recognition, Univ. lagell. Acta Scient. Lift. Sched. lnformat. 4, 17-32 (1987). 6. M. Flasifiski, Characteristics of edNLC-graph grammar for syntactic pattern recognition, submitted for publication.
About the Author--MARxUSZFLASII~ISKI received his M.Ph. degree with Honours in Computer Science from
the Jagiellonian University, Poland, in 1984. He works in the Department of Computer Science,Jagiellonian University as an assistant lecturer. Mr Flasifiski has published about 10 papers in various scientificjournals. His research interests include computer graphics, pattern recognition, theory of formal languages, theory of translation and artificial intelligence.