Practical Applications of Precedence Graph

1 downloads 0 Views 441KB Size Report
The paper is organized as follows: In the next section basic notions for gg´s are .... and does not change the label of the embedding edges, i.e. p=(L,R,I) , I ... instances in graphs as follows: Build a chain of ni a-nodes for all i ∈ {1,…r}, a chain of mj b- ..... relations describe parts of the parse tree indirectly and thus supply such ...
Practical Applications of Precedence Graph Grammars Manfred Kaul Fakultät für Mathematik und Informatik, Universität Passau, Postfach 2540, D-8390 Passau, West-Germany, Tel. 0851- 509344, Electronic Mail: unido!unipas.uucp!kaul

Abstract. Precedence graph grammars are of major interest in all those applications of graph grammars, where highly efficient parsers are needed. Up to now there are no other graph parsers with the same performance. Due to the fact, that even regular graph grammars with very restricted embedding relations have a NP-complete membership problem, different kinds than Chomsky-like restrictions have to be imposed on graph grammars. We start with contextfree graph grammars and introduce precedence relations. By demanding conflictfreeness, unique invertibility and some further, more technical constraints, precedence graph grammars are introduced with an O(n2) - membership problem, where n ist the number of nodes of the input graph.Precedence graph grammars are unambiguous, which is especially important for semantic evaluation of the derivation trees. In this paper we show, that in spite of all constraints the proposed graph grammar class has interesting generative power, concerning applications in such areas as e.g. dynamic data structures, program graphs, data and control flow graphs and syntactic pattern recognition. For the last topic an error correcting facility incorporated into the precedence graph parser is of special interest. In general inexact graph matching is NP-complete. In this paper we present a method, that increases the time complexity of our parser only by a factor of n. At last, our method is demonstrated with an example from syntactic pattern recognition. Key words: graph grammar, contextfree, membership problem, graph parser, precedence relations, parallel parsing, hierarchical graph model, inexact graph matching, similarity of graphs, error distance between graphs

0. Introduction There are two different types of applications of graph grammars, short gg´s. First, complex graph transformations can be modelled by gg´s formally. This has been successfully applied to the software engineering area [LeNa 84]. Second, an infinite class of graphs with the same inherent syntactic structure can be compactly specified by gg´s. It is the second type, where graph parsers are of major

M.Kaul: Practical Applications of PGG, 1986

Seite 2 von 22

interest, e.g. in syntactic pattern recognition, syntactic analysis of graphs, in particular dynamic data structures, program graphs, data or control flow graphs. There are some basic results about the complexity of the membership problem for gg´s. Even for very restricted gg-classes, which are called regular in the sense of a graph class Chomsky-hierarchy, with very restricted embedding relations, the membership problem is NP-complete. On the other hand, regular gg´s do not fit to most applications nicely. Obviously, in order to gain gg-classes with both, interesting generative power and very efficiently decidable membership problem, some different kind than Chomsky-like restrictions have to be imposed on gg´s. Our approach is to start with contextfree gg´s, introduce precedence relations and demand them to be conflictfree. Furtheron, for many applications in computer science unambiguity of the gg-class is needed, so we additionally request unique invertibility. Adding some technical constraints we finally gain a gg-class with O(n2)-time complexity, where n is the number of nodes of the input graph. This ggclass is called precedence graph grammars, short pgg´s. Further advantages gained by this approach are: A lot of applications for this gg-class can be given, including e.g. trees, tree-structured graphs, series-parallel-networks, data structures, well-structured flowgraphs, petri-nets, maximal-outerplanar graphs, complete bipartite graphs. The parser can be generated from the pgg automatically. Error handling, which is of special interest in pattern recognition, can be incorporated into the parser easily, increasing time complexity only by a factor of O(n). In this paper a sketch of the proof is given and the method is demonstrated with a small example from pattern recognition. Negative results about the generative power of pgg´s turn out not to cut down the practical applicability severely: E.g. infinite symmetry cannot be generated with pgg´s. But with some examples we can demonstrate, that a preprocessor inserting additional chains and thereby destroying symmetry, can easily be constructed for most pgg´s. The paper is organized as follows: In the next section basic notions for gg´s are repeated. In chapter 2 the gg-classes with NP-complete membership problem are specified in more detail, thereby motivating the restrictions imposed on pgg´s. Precedence relations, conflicts, unambiguity, unique invertibility are introduced in chapter 3 in terms of derivation sequences. Pgg´s and their basic properties, including some results on their generative power, are given in the following chapter 4. The precedence graph parser is given in chapter 5. In section 6 applications are listed and one example

M.Kaul: Practical Applications of PGG, 1986

Seite 3 von 22

is given in more detail. Our error handling method is described in chapter 7 and applied to a pattern recognition problem in chapter 8. Conclusion and references finish this paper.

1.

Preliminaries

We assume the reader to be familiar with basic notions from formal language theory [Har 78], complexity theory [GaJo 79] and graph theory [Har 69]. First let us introduce node and edge labelled graphs. Let ∑V , ∑E be two alphabets of vertex and edge labels resp. G = (VG,EG,LG) is a graph (over ∑V , ∑E) if VG≠∅ is a finite set (the vertex set of G), EG ⊆ {(v,x,w)∈VG×EG×VG| v≠w} (the edge set of G) and LG : VG→∑V is a total mapping (the node labelling of G). EG(v,w) Ù {x ∈∑E | (v,x,w) ∈ EG} is the set of all labels from v towards w. H ‰ G means that H is a full subgraph of G. G|V is the restriction of G to a full subgraph H ‰ G iff VH = V. RG(H) is a full subgraph H´ of G\H iff every node from H´ is adjacent to some node in H. If two graphs G,H are isomorphic, we write G ' H. The label tuple of two nodes v,w ∈ VG is labG(v,w) Ù (LG(v),EG(v,w), EG(w,v),LG(w)). Next let us define graph grammars briefly. Introduction and survey can be found e.g. in [ClEhRo 79]. GG = (∑V,∑T,∑E,P,S) is a graph grammar, short gg, if (i) ∑V,∑T,∑E are finite alphabets (for node, terminal node and edge labels resp.), ∑T ⊆∑V; (ii) P is a finite set of productions (explained below); and (iii) S ∈ ∑V\∑T is the start symbol. The start graph is a single node labelled by S. Node labels from ∑N Ù ∑V\∑T are called nonterminals. A production p = (L,R,I,O) ∈ P consists of two graphs L,R (the left and right hand side of p, short lhs and rhs) over ∑V , ∑E , each of them weakly connected, and the embedding rules I,O ⊆ VR×∑V×∑E×∑E for incoming and outgoing edges resp. If L is one node only, p is called contextfree. In this paper only contextfree productions are considered. s = (p,à,ì,è) is a derivation specification of GG if p = (L,R,I,O) ∈ P, à ' L, ì ' R, è : Vì→ VR is an isomorphism. s is applicable to a graph G if à ‰ G and VG ∩ Vì = ∅. G¯G´ is a derivation step if s is applicable to G, ì ‰ G´, G|(VG\Và) = G´|(VG´\ Vì) and edges between ì and the restgraph are as follows: For all v ∈ Vì, any node w from RG(à) we have EG´(w,v) = { x´∈∑E | ∃ x ∈ EG(w,à) : (è(v),LG(w),x,x´) ∈ I} and EG´(v,w) = { x´∈∑E | ∃ x ∈ EG(à,w) : (è(v),LG(w),x,x´) ∈ O}. Our first example is GG1 = ({S,a},{a},{x,y},{p1,p2},S) with two productions p1 = (S,R1,I1,O1), p2 = (S,R2,I2,O2), graphically described below. R1 is the graph with vertices 1,2

Seite 4 von 22

M.Kaul: Practical Applications of PGG, 1986

, R2 is the graph with vertex 3, 4. The embedding relations are I1 = {(1,a,y,y),(2,a,x,x)}, I2 = {(3,a,y,y),(4,a,x,x)}, O1 = O2 = ∅.

GG

p 1

a

1 S

y

a

y x

a

x

x

a

1

p

2 S

y

y x

y S

a

a

2

x

x

3 y

a 4

A sequence of derivation steps D = (Gi-1 ÷ Gi | 1 ≤ i ≤ n ), n ∈ ‚, si = (pi,ài,ìi,èi), is called derivation sequence, if G0 is the start graph and in every step only new nodes are introduced, that is Vì˙∩Vìº ≠ ∅ ⇒ i = j . Gn is called derivable by GG. S(GG) is the class of all derivable graphs. The language of GG, short L(GG), is the class of all graphs from S(GG), that are labelled with terminals only. v

F [a,x,y] k x a v

y 1

k

a

x

x

a v

y

y 2

y

a v

k-1

L(GG1) = {G ≅ Fk[a,x,y] | k ≥ 2 } is the graph language in our example, which is an infinite set of k-fans Fk[a,x,y] , where a is a vertex label, x,y the edge labels. Note that v1 is the first node, vk is the last node generated during a derivation. The degree of nonterminals in derivable graphs is unbounded. But there is an equivalent gg GG´1 s.t. the degree is at most 3.

2.

Complexity of the membership problem

M.Kaul: Practical Applications of PGG, 1986

Seite 5 von 22

The membership problem for the class of gg´s introduced in chapter 1 is PSPACE-complete [GaJo 79]. The proof is by reduction from the membership problem for context-sensitive string languages. Even if we add the following restrictions on the productions, especially the embedding rules, the membership problem remains intractable: (1.) There is only one nonterminal on every rhs, that has incoming edges only. Every rhs has at most three nodes and two edges. (2.) Each nonterminal in any derivable graph has no outgoing edges, that is outdegree zero. (3.) The gg is neighbourhood preserving, that is, for all derivation steps G ¯ G´, s = (p,à,ì,è), G,G´ ∈ S(GG) we have RG(à) = RG´(ì). (4.) There is only an embedding relation for incoming edges I, that ignores the labels of the neighbour nodes completely and does not change the label of the embedding edges, i.e. p=(L,R,I) , I ⊆ VR×∑E , O is omitted. In spite of all these restrictions, the membership problem remains NP-complete, even if the gg is fixed. In the Chomsky Hierarchy for gg´s, as introduced in [Nag 79], such gg´s are called regular, the embedding relations monotone, elementary, simple, label and orientation preserving, which is almost the bottom of the Chomsky Hierarchy. The proof is by reduction from SUBPARTITION, cf. [AaRoEh 86], which is NP-complete in the strong sense. A SUBPARTITION instance consists of positive integers R = (ni ∈ ‚\{0} | S = (mj ∈ ‚\{0} | j ∈ {1,…,s} ), r,s ∈ ‚\{0}, r ≤ s, such that ∑i=1…r ( ni ) = ∑j=1…s ( mj ) . The SUBPARTITION problem is to decide, whether S is a subpartition of R; i.e. is there a partition T = (Ti ⊂ {1,…,s}| i ∈ {1,…,r} ), (where the Ti´s are nonempty, disjoint and i ∈ {1,…,r} ),

complete), such that for all i ∈ {1,…,r} it is ni = ∑j ∈ T˙ ( mj ) ? Encode the SUBPARTITION instances in graphs as follows: Build a chain of ni a-nodes for all i ∈ {1,…r}, a chain of mj b-nodes for all j ∈ {1,…,s}. Connect one end of all chains via a central $-node. Let L0 be the language of all such graphs encoding positive SUBPARTITION instances. Then obviously, there is a gg GG0 regarding all the restrictions mentioned above with L(GG0) = L0. The idea is, that during derivation of a graph G ∈ L0 via GG0 it can easily be guaranteed, that for every chain of b-nodes of length ni there is a set of chains of a-nodes of length mj, j ∈ Ti, such that ni = ∑j ∈ T˙ ( mj ). Contrary to that, during parsing it is much harder to map a-chains to bchains properly. If P ≠ NP there is no method essentially faster than just exhaustive search through nearly all partitions, which are of course exponential in number. Note, that GG0 is fixed. That is, even if the gg is not part of the input, the membership problem remains intractable. In fact, it does not matter whether the gg is node label controlled (NLC, cf. [JaRo 80]), or edge label controlled(ELC). If

Seite 6 von 22

M.Kaul: Practical Applications of PGG, 1986

the embedding mechanism offers a way to divide the neighborhood into three different classes, the proof above will work. Obviously graph parsing is much harder than string parsing. Cocke-Younger-Kasami-type and Earley-type parsers, which are commonly used in syntactic pattern recognition [Fu 82], cannot be extended to the full class of contextfree gg´s. Therefore other types of parsers have been examined whether they can be transferred to graph parsing: LL(k)-, LR(k)- and precedence. But without introducing unnatural orderings into graphs, as e.g. in [Lud 81], only the last approach has been successful [Fra 78], [Ka 86] in gaining low-order polynomials for parsing complexity.

3.

Derivation Sequences

Let D = (Gi-1 ÷ Gi | 1 ≤ i ≤ n), n ∈ ‚, si = (pi,ài,ìi,èi), be a derivation sequence. si precedes sj if àj ‰ ìi , 1 ≤ i,j ≤ n. The reflexive and transitive closure of this relation is denoted ≤D . si , sj are incomparable if neither si ≤D sj nor sj ≤D si . ≤D is a partial order, called derivation order of D. The Hasse-diagram is a tree, called derivation tree of D. Let sD(v) Ù si if v ∈ Vì˙ , 1 ≤ i ≤ n . The derivation order imposes an ordering of the nodes in Gn as follows: v Θ w ⇔ sD(v) ΘD sD(w) , v,w ∈ VG~ adjacent, Θ ∈ {=,}. Instead of =, we write Ï,Ì,Ó. If sD(v), sD(w) are incomparable we write v Ô w. Ï,Ì,Ó,Ô are the precedence relations between nodes. The precedence relations between labels RΘ , Θ ∈ {Ï,Ì,Ó,Ô} is the set of all labG(v,w) s.t. there is a derivable graph G, v,w ∈ VG , v Θ w.

E.g.

the

precedence

relations

of

GG1

are

RÏ = {(a,{x,y},∅,S),(S,∅,{x,y},a),(a,{x,y},∅,a),(a,∅,{x,y},a)}, RÌ = {(a,{x},∅,S)} ∪ { (a,ξ,∅,a) | ξ ∈ {{x}, {y}}}, RÓ = {(S,∅,{x},a)} ∪ { (a,∅,ξ,a) | ξ ∈ {{x}, {y}}}, RÔ = ∅. If the precedence relations between labels are pairwise disjoint, the precedence relations between nodes can easily be inferred from them. A precedence conflict is a label tuple t , that occurs in more than one precedence relation. Let n ∈ ‚, D = (Gi-1 ÷ Gi | 1 ≤ i ≤ n ), F = (Hi-1 t˙ Hi | 1 ≤ i ≤ n ) be two derivation sequences of the same length n, si = (pi,ài,ìi,èi), pi = (Li,Ri,Ii,Oi) , ti = (qi,ài´,ìi´,èi´), 1 ≤ i ≤ n. Let α = (ai : VR˙ → VR˙ | 1 ≤ i ≤ n) be a family of automorphisms ai on the rhs´s Ri and β be a permutation on {1,...,n} with pi=qβ(i) and si ≤D sj ⇔ tβ(i) ≤F tβ(j) , 1 ≤ i,j ≤ n. D,F are called symmetrically equivalent under (α,β) if àj ‰ ìi ⇒ ai(èi(àj)) = è´β(i)( à´β(j)), 1 ≤ i,j ≤ n. (α,β) is called symmetric derivation permutation. Note that derivation steps as well as symmetric nodes with rhs´s may be interchanged. A gg GG is unambiguous if any two derivation sequences of GG yielding isomorphic

M.Kaul: Practical Applications of PGG, 1986

Seite 7 von 22

graphs are symmetrically equivalent. GG is confluent if for all derivable graphs G1,G2,G3 and incomparable derivation specifications s1,s2 with G1 s1 G2 , G1 s2 G3 there is a derivable graph G4 s.t. G2 s2 G4 and G3 s1 G4 are derivation steps. Let p = (L,R,I,O) be a production, v ∈ VR , Iv Ù {(w,A,x,x´)∈I | w = v} , Ov Ù {(w,A,x,x´) ∈ O | w = v}. p is symmetric if Iv = Ia(v) , Ov=Oa(v) for all automorphisms a : VR→ VR , v ∈ VR. GG is symmetric if all productions are. If GG is both symmetric and confluent, then all symmetric equivalent derivation sequences yield isomorphic results. GG is uniquely invertible if every derivation step can be inverted uniquely only by inspection of the substituted subgraph and its direct neighbourhood. A nonterminal B is called reflexive if B can be derived from B in at least one step.

4.

Precedence Graph Grammars

A gg that is confluent, symmetric, uniquely invertible, has no reflexive nonterminals and no precedence conflicts, is called precedence graph grammar, short pgg. Main properties of pgg´s are: Pgg´s are neighbourhood preserving and unambiguous. The last feature is especially important for attributed graph grammars, which facilitates semantic evaluation of the attributes in the derivation tree. In [Sch 87] attributed graph grammars, attribute evaluators, their implementation and practical applications are discussed in depth. The parser module implemented there is exactly our precedence graph parser. For any contextfree gg, it can easily be decided, whether it is a pgg. In particular precedence relations can efficiently be computed by taking into account subgraphs H of derivable graphs with |VH| ≤ 2. The same technique applies to the test of confluence. On the other hand it is undecidable, whether an equivalent pgg can be found. The membership of a graph G in L(GG) is decidable in O(|VG|2) time and O(|VG|+|EG|) space. If one of both properties, (1.) unique invertibility and (2.) no precedence conflicts, is omitted, the membership problem becomes NP-complete. The proof is by reduction from Hamiltonian paths. There is a trade-off between unique invertibility and precedence conflicts. E.g. consider Lstar = {Sk[a,x] | k ≥ 1 }, where Sk[a,x], called k-star with node label a and edge label x , as shown below, consists of a node vk adjacent to k-1 nodes v1,…,vk-1, which are pairwise nonadjacent. The following gg´s generate Lstar : GG2 is a uniquely invertible gg , GG3 is a gg without precedence conflicts. But there is no pgg for k-stars.

Seite 8 von 22

M.Kaul: Practical Applications of PGG, 1986

GG 2 a

S

S x

x

x

S

a

a

x

v

x

1

a

a

x

GG 3 a

y x

S x

a y

S

x

y

a

x

a

a

a

S x

v x

a

a x

v

k

k-1 S [a,x]

k

In some sense, precedence conflicts can always be eliminated: Let ∑E◊ Ù ∑E×{Ï,Ì,Ó,Ô} is the extended edge label alphabet. ◊ maps every graph G over ∑E◊ to a graph G◊ over ∑E as follows: VG◊ = VG , LG◊ = LG , EG◊ = { (v,x,w) | v,w ∈ VG , x ∈ ∑E , (v,(x,µ),w) ∈ EG , µ ∈ {Ï,Ì,Ó,Ô} }. Then for every gg GG a gg GG◊ without precedence conflicts can effectively be constructed, s.t. [L(GG◊)]◊ = L(GG). GG◊ is the conflictfree normal form of GG. But this construction is at the expense of unique invertibility, which is often lost during the construction of GG◊. Some results are known about the generative power of pgg´s. If a graph language L contains too many nonisomorphic symmetric graphs of certain types, (e.g. k-stars Sk[a,x] as shown above, or symmetric k-chains Kk[a,x], or symmetric k-cycles Ck[a,x] as shown below), then there is no pgg GG with L = L(GG).

k-chain K [a,x] k x a v1

a x

v2

...

k-cycle C [a,x] k

x a v k-1

a x

vk

a v

1 v

v x

2

x k

a

x

a

x

...

v

3

a x

Introducing auxiliary edges that destroy symmetry often helps to construct a pgg. E.g. the graph Fk[a,x,y] of L(GG1) may be considered as a k-star Sk[a,x], to which auxiliary y-edges vi → vi+1 ,

M.Kaul: Practical Applications of PGG, 1986

Seite 9 von 22

1 ≤ i ≤ k-1 , have been added. GG2 has precedence conflicts and therefore the precedence graph parser described in the next chapter fails to parse graphs of L(GG2). Obviously, the membership problem of GG2 has O(n) time complexity, where n is the number of nodes of the input graph. In order to avoid parser construction for every single gg by hand once again, another technique called chaining is proposed here: An appropriate preprocessor transforms the input into a graph without symmetry, by introducing chains, as demonstrated in our example GG1. Of course, this technique can always be applied, but on the other hand the time complexity of the preprocessor may be dreadful: Chaining the graph language L0 from chapter 2 is NP-complete. Nevertheless, in practice constructing preprocessors is much more easier than building whole graph parsers each time from scratch. Furthermore there is an information theoretic argument, that limits the number of nonisomorphic graphs with n nodes within L(GG) to 2c*n, c some constant depending on GG, if GG is a pgg. The reasons are that (1.) the length of derivation sequences yielding graphs with n nodes is O(n); and (2.) pgg´s are confluent. In the case of a graph language L with more than 2c*n graphs, again preprocessing, called compression , is proposed: If there are a lot of graphs within L with the same semantic concerning the intended application, the preprocessor maps all equivalent graphs to their normal form. E.g. if L is the language of all acyclic directed graphs and in the application only the transitive closure is concerned, the normal form is the subset of all acyclic tournaments. Then the preprocessor simply computes the transitive closure of the input graph. Of course, compression is not applicable, if no appropriate equivalence relation is available.

5.

Precedence Graph Parser

The membership problem for gg´s as introduced in chapter 3 is PSPACE-complete. So the parsing problem is at least as hard. A parser that constructs the parse tree efficiently, has obiously to be provided with more information about the syntactical structure of the input graph. The precedence relations describe parts of the parse tree indirectly and thus supply such helpful information. A parse tree is a hierarchical decomposition of the input graph, that may be described by putting the graph into hierarchically nested boxes according to the following rules: (1.) Nodes of equal precedence are derived from the same node and therefore lie within the same box. (2.) Along ascending precedence no box is leaved, but at least one box entered. (3.) Along Ô at least one box is left, and then at least one box is entered, as shown below:

Seite 10 von 22

... ...

Ô

...

Ì

...

Ï

...

...

M.Kaul: Practical Applications of PGG, 1986

Although there are infinitely many ways to put a graph into nested boxes according to these rules, there is only one parse tree T for G up to symmetric equivalence. (Recall Unambiguity.) Two observations show that T can be constructed efficiently: Let n ∈ ‚, D = (Gi-1 ÷ Gi | 1 ≤ i ≤ n), be a derivation sequence of length n, si = (pi,ài,ìi,èi), 1 ≤ i ≤ n. A full subgraph H ‰ Gn is a handle w.r.t. D if there is some ìi . Of course, when parsing Gn bottom-up, a handle must be reduced first. On the other hand let G be some derivable graph. A full subgraph H ‰ G is called a precedence handle in G if labG(v,w) ∈ RÏ for all adjacent v,w ∈ VH and labG(v´,w´) ∉ RÏ ∪ RÓ for all adjacent nodes v´ from RG(H), w´ ∈ VH. In syntactically correct graphs a precedence handle is always a handle and vice versa. The second observation is that if the parser proceeds along ascending precedence, a precedence handle has to be found after at most |VG| steps, G being the input graph. Precedence parsers are well known as shift-reduce parsers [AhUl 72], that store intermediate results into a stack. String parsers store symbols, graph parsers store node addresses into the stack. Precedence graph parsing is performed as follows: (1.) Start at an arbitrary node. (2.) Go along equal or ascending precedence as long as possible. Shift traversed nodes into the stack. (3.) If no further ascending is possible, a precedence handle is reached, which is isomorphic to a rhs of a production. Reduce the precedence handle to the lhs using the production in the opposite direction, in particular the inverse embedding rules, and proceed with (2.). Of course all precedence handles available at the same time can be reduced in parallel (recall confluence), which may reduce parsing time to O(log n), if the derivation tree is balanced. A graph parser that does not visit any node more than two times is described in the following: First we denote an instantaneous description, short ID, by (G,K,Ψ), where G is the instantaneous graph, K an ordered list of nodes in G , Ψ a set of derivation specifications constructed so far. The initial ID is (G0,K0,∅), where G0 is the input graph, K0 some node of G0. Let (G,K,Ψ) be some ID, K = , k ≥ 1. Let j be the minimum index 1 ≤ j ≤ k, s.t. there is some path in G

M.Kaul: Practical Applications of PGG, 1986

Seite 11 von 22

from vj to vk along equal precedence. Then TOP(G,K) Ù G|{vj,…,vk}. There are two types of moves, called shift and reduce: (G,K,Ψ) S (G,Kw, Ψ) is a shift if (i) w ∈ VG does not occur in K already; (ii) labG(v,w) ∈ RÏ ∪ RÌ for some v in TOP(G,K). (G,K1K2,Ψ) R (G´,K1w,Ψ∪{s}) is a reduce if (i) w is a node not used in G or Ψ already; (ii) s = (p,à,ì,è) , G´¯G is a derivation step, s ∉ Ψ, K2 = Vì , G|K2 = TOP(G,K) is a precedence handle, G´|{w} = à, K1K2 denotes the concatenation of both sequences K1 and K2. The number of moves is O(|VG|) , G the input graph. But there are gg´s s.t. a reduce may need O(|VG|) steps as well, e.g. our example GG1. But there is an equivalent pgg GG1´ s.t. every reduce can be performed by O(1) steps only. The idea is to generate vn+1 first, then the chain v1,…,vn within the graph Gn. It is still an open problem whether always such a "better" gg can be found. Comparing precedence graph parsers with other approaches such as LL(k), LR(k), it turns out to be superior concerning the following advantages: (1.) No starting node has to be distinguished. There is no fixed linear order in which the graph has to be traversed. All traversals, even in parallel, yield the same parse tree up to equivalence, because pgg´s are unambiguous. (2.) In [Ka 86] an abstract concept of precedence relations is introduced, that is general enough to describe every bottomup parser as a precedence graph parser. Furthermore the schema for computing precedence relations is independent of the special type of embedding rules, and is therefore applicable to a wide range of ggclasses. (3.) Attributing grammars [Knu 68] is a well known technique to incorporate additional semantic information into a grammar, which may be applied to pgg´s as well. [Fu 82] applies precedence string grammars as well as attributed string grammars to pattern recognition. In [Sch 85] algorithms for attribute evaluation on attributed pgg´s are given. There exactly the pgg-class given in this paper is used.(4.) Introducing application conditions, an interface to knowledge based production systems [Nag 82] can easily be introduced. Pattern descriptions that could be expressed in logical formulas more comfortably, can be incorporated into the precedence parser easily. (5.) Precedence string grammars have been subject to a wide range of publications since 1966. Several techniques are already known for precedence string grammars and can be transferred to pgg´s easily. E.g. [Has 74], [Bab 79] give efficient parsers for context-sensitive precedence languages, e.g. {anbncndn | n ≥ 1 } and { wcw | w ∈ {a,b}* }. [Fu 82] describes stochastic precedence parsers. The availability of this wide range of techniques often helps to adapt precedence parsers to special applications.

M.Kaul: Practical Applications of PGG, 1986

6.

Seite 12 von 22

Applications

An extensive bibliography about applications of gg´s can be found in [EhNaRo 83]. By graph parsing the underlying hierarchical structure of a graph is revealed, thereby providing additional help for accelerating algorithms on graphs. E.g. for testing isomorphism of two graphs up to now no polynomial upper bound for the time complexity is known. But if the derivation trees of both graphs are available, the isomorphism test becomes sovable in linear time (cf. M.Schnitzler: "The isomorphism problem is polynomial solvable for certain graph languages"; in [EhNaRo 83],p.372). Applications of pgg´s, in particular, are discussed in [Ka 86] in detail. E.g. pgg´s are given for context-sensitive languages, e.g. anbncn, trees with arbitrary degree, k-nary trees (k fixed), seriesparallel networks, acyclic tournaments, complete graphs, complete bipartite graphs, maximal outerplanar graphs, well-structured flow graphs, program graphs, recursive datastructures, petri-nets, especially for a control-flow model of Prolog. Due to the lack of space, here we can present only two examples: First, in traditional programming languages as e.g. Pascal, recursive data structures are implemented by pointers, offering complete freedom in building pointer structures, which is at least as harmful as Goto-programming. E.g. no traditional Pascal system prevents building doubly linked lists or completely different pointer structures from records declared as binary trees: type tree = ↑ record left, right : tree; info: infotype; end;

Our approach is to specify recursive datastructures by precedence graph grammars. Here we give a pgg GGTree generating exactly binary trees ( see next page ). Checking consistency is easily and efficiently done by precedence graph parsing. The graph parser is table-driven. The table consists mainly of a precedence table and the graph productions. It is generated from the specification automatically. Using error-handling, not only consistency is checked by the precedence graph parser, but also errors are identified, reported and repaired, if wanted.

Seite 13 von 22

M.Kaul: Practical Applications of PGG, 1986

GGTree

info right

info left Tree

info right

left Tree

info left

info

right

left

right info

Tree

Tree

Our second example is analysis of electrical networks. The following pgg GGSPN generates exactly series-parallel networks of resistors:

M.Kaul: Practical Applications of PGG, 1986

Seite 14 von 22

In GGSPN the start symbol is Z. =,-,~ are the edge labels. GGSPN consists of 4+k productions, k ≥ 2. S generates series of networks, P generates parallel networks. k is the maximim fan and exactly the number of S´-nodes in the rhs of p4+k . Of course the fan is bounded here, which might be desirable for some applications. If not, see [Ka 86] for a pgg generating all series-parallel networks with unbounded fan. Error handling and correction is interesting in both applications presented here. Furthermore, it is essential for pattern recognition and is discussed in the next chapter.

7.

Error Handling

Error handling consists of error detection and error correction. Of course, only local errors can be detected without parsing the whole graph completely. Error correction modifies the graph in a minimal way s.t. a shift/reduce can be performed again. Let (G,K,Ψ) be some ID. A vertex w in G is called a shift error if w can be shifted onto the stack, but (i) there are vertices v,v´ in TOP(G,K) s.t. labG(v,w) ∈ RÏ and labG(v´,w) ∈ RÌ ; or (ii) there is a vertex v" ∈ K\VTOP(G,K) s.t. labG(v",w) ∉ RÌ . TOP(G,K) is a reduce error if neither a shift nor a reduce can be applied to (G,K,Ψ). Clearly, TOP(G,K) is a precedence handle, that matches with no rhs, or there is some embedding edge between TOP(G,K) and the host graph with illegal precedence. Both shift and reduce error can be detected by regarding the local context only. In order to discuss error correction, first minimal modification of graphs has to be specified. For this purpose the difference of two graphs G,G´, short ∆(G,G´) is introduced as follows: ∆(G,G´) is

Seite 15 von 22

M.Kaul: Practical Applications of PGG, 1986

∆1(G,G´) Ù {(1,v,A) | v ∈ VG\VG´ , A = LG(v) }, ∆2(G,G´) Ù {(2,v,A,B) | v ∈ VG\VG´ , A = LG(v), B = LG´(v), A ≠ B }, ∆3(G,G´) Ù {(3,v,A) | v ∈ VG´\VG , A = LG´(v) }, ∆4(G,G´) Ù {(4,v,w,X,Y) | v,w ∈ VG∪VG´ , X = { x | (v,x,w) ∈ EG\EG´}, Y = { y | (v,y,w) ∈ EG´\EG} }. An element from ∆j(G,G´), 1 ≤ j ≤ 3, is the union of

∆j(G,G´), 1 ≤ j ≤ 4, where

an elementary vertex transformation, in particular a (1) vertex deletion, (2) vertex relabelling, (3) vertex insertion. An element from ∆4(G,G´) is an elemenatry edge transformation, called edge relabelling. LABj[d], d ∈ ∆j(G,G´), 1 ≤ j ≤ 4 denotes the tuple of those components of d, that are labels, e.g. LAB4[(4,v,w,X,Y)] = (X,Y). Let COSTj assign costs to elementary transformations from ∆j(G,G´), dependent on labels only, that is COST1,3 : ∑V → ℜ+ ∪ {∞}, COST2 : ∑V × ∑V → ℜ+ ∪ {∞}, COST4 : 2∑E × 2∑E → ℜ+ ∪ {∞} are mappings, ℜ+ denotes the set of positive real numbers. The error distance between

G,G´ is

δ(G,G´) Ù ∑d ∈ ∆˙(G,G´)

COSTj[LABj[d]] , 1 ≤ j ≤ 4. In the following example ( see next page ) we have ∆1(G,G´) = {(1,3,A)}, ∆2(G,G´) = ∆3(G,G´) = ∅, ∆4(G,G´) = {(4,1,2,{y},{x})}. δ should be a metric, that is δ(G,G´) = 0 iff G = G´ , δ(G,G´) = δ(G´,G), δ(G,G´) ≤ δ(G,G") + δ(G",G´). In particular COST1[A] = COST3[A], COST2[A,B] = COST2[B,A], COST4[X,Y] = COST4[Y,X], A,B ∈ ∑V , X,Y ∈ 2∑E . 3

y

a

b

x

G

2

1

a

x

b

2

x a

1



Furthermore δ should be compatible with the pgg. This means e.g. infinite loops of vertex relabellings and reduce moves have to be avoided by a proper choice of COST2 , that is, the relation ;RC Ù {(A,B) ∈ ∑N × ∑N | B ⇒ A or ∀ C ∈ ∑N : COST2[A,B] ≤ COST2[A,C] } has to be cyclefree: ∀ A ∈ ∑N : (A,A) ∉ (;RC)+, the transitive closure of ;RC. We are now ready for specifying shift and error correction: Let (G,K,Ψ) be an ID, w a shiftable vertex. G´ is a shift correction of G if G´ if G´ is identical to G up to edge labels between w and K, w remains shiftable within G´ , and w is no shift error any more. G´ is a minimal shift correction if for all shift corrections G" δ(G´,G) ≤ δ(G",G). (G´,K´,Ψ) is a reduce correction if (i) G´,K´ are identical to G,K up to TOP(G,K) and its embedding edges, i.e.

M.Kaul: Practical Applications of PGG, 1986

G|(VG\VTOP(G,K)) = G´|(VG´\VTOP(G´,K´)); and (ii) to (G´,K´,Ψ)

Seite 16 von 22

a reduce can be applied;

and(iii) if TOP(G,K) is a single vertex only, then TOP(G´,K´) is a single vertex, too. (The last condition guarantees decreasing size of the parsed graph.) (G´,K´,Ψ) is a minimal reduce correction if for all reduce corrections (G",K",Ψ) δ(G´,G) ≤ δ(G",G). Error correcting moves first detect some error, compute some minimal error correction and finally proceed with an ordinary move. But there are several startegies to do so in the case of shift errors: Strategy A ("Eliminate errors as early as possible"): Traverse all shiftable vertices and apply minimal shift correction to all of them. Shift all traversed vertices onto the stack. Strategy B ("Modify graph in a minimal way"): Search for a shiftable vertex, such that its minimal shift correction is less than those of all other shiftable vertices. Clearly, if shiftable vertices with no shift error exist, then one of them is shifted onto the stack. Startegy C ("Shift at random"): Take any shiftable vertex w. If w is a shift error, apply minimal shift correction. Shift w onto the stack. The choice is application dependent and not anticipated here. In the case of reduce errors, a minimal reduce correction is applied, and finally a reduce performed. This is simply an application of inexact graph matching [ShHa 81], [BuAl 83] to subgraphs of constant size (the constant given by the pgg). That is, the rhs of some production, that is most similar to the instantaneous precedence handle is found by a backtracking method. Embedding edges with illegal precedence are relabelled with minimal costs s.t. TOP(G,K) remains a precedence handle. For both shift and reduce correction the deletion of an edge may be the cheapest modification. In this case the instantaneous graph G may fall apart into several connected components G1, … , Gk , k ≥ 2. The parser continues to process the current connected component Gi in which TOP(G,K) is a subgraph, until Gi is reduced to a single vertex only. then the parser proceeds with the other components Gj , j ≠ i. Switching from one component to another one os called a next move, denoted (G,K,Ψ) & (G,K´,Ψ). Contrary to the exponential complexity of the algorithms given in [ShHa 81], [BuAl 83], we arrive at O(n) time complexity for a single reduce move, n the number of vertices of the input graph. For a single error correcting shift, O(n2) time in the case of strategies A,B and O(n) time in the case of strategy C is needed. Since the number of shift and reduce moves is O(n) , we finally end up with O(n2), O(n3) time complexity resp. for the entire parsing process, which essentially is a divide-andconquer strategy for inexact graph matching. Note that this approach is not intended to yield O(n3) time complexity for a NP-complete problem. Instead we changed the problem specification in a

Seite 17 von 22

M.Kaul: Practical Applications of PGG, 1986

profitable way: The user of the parser has to specify his notion of similarity of graphs in a different way. Instead of specifying weights for graph editing operations only, as in [ShHa 81], [BuAl 83], helpful structural knowledge about the family of graphs is added to the input. The additional information reduces complexity. Let D Ù ((Gi-1,Ki-1,Ψi-1) ;i (Gi,Ki,Ψi) | 1 ≤ i ≤ n ) , n ≥ 0 , be a sequence of error correcting and ordinary moves, that is a shift or reduce. To (Gn,Kn,Ψn) no further move should be applicable. Let δD(G0) be the sum of δ(Gn,S), δ(Gi-1,Gi), for all error correcting moves ;i . δD(G0) is called error distance of G0 w.r.t. D. Of course, D as well as δD(G0) are not uniquely determined by G0 . Syntactically correct graphs, and only those, have error distance 0: Let GG be a pgg, G0 some input graph, δ a metric. There is a sequence of error correcting and ordinary moves D of GG with δD(G0) = 0 iff G0 ∈ S(GG). Fortunately the number of error corrections can be limited by O(n3) in the case of strategies A,B and O(n2) in the case of strategy C. This is especially important, if some error report has to be processed by further processing steps.

8.

Example

A simple example of line drawings describing stairs is presented in this chapter to demonstrate the basic ideas of our method. First, line drawings have to be encoded as node and edge labelled directed graphs, thereby mapping classes of geometrical features to topological representations. The edge direction is obtained from the undirected lines by orientation from left to right, from the bottom to the top. The edge labels describe approximate direction and length of a basic line. Our notation is based on the well-known quarters of the heavens, that is ∑E = {N,E,NE,NW}. Node labels ∑T = {a,b,c,d,e,f,g,h} describe the shape of intersections of lines, that is their number and the angles between them approximately as follows:

a

b

c

d

e

f

g

h

All line drawings describing stairs viewed from the right with an arbitrary number of steps are generated by the following pgg GG4:

Seite 18 von 22

M.Kaul: Practical Applications of PGG, 1986

A

S A E

a

p

a

N

N

N

b

e

N

g

p

p 3

N

b

N

h

NE

NE

1

E

f

2

B NE

e

p

B

E

c

NE

d

f

B

4

A N

N e

NE

g

E

h

NE

f

Precedence relations are as follows: In RÏ all label tuples are included from rhs´s of GG4. In RÌ we have edges between terminals labelled by N or NE with ascending precedence in the direction of the edge. RÓ consists of the same edges as RÌ, but with opposite direction. RÔ = ∅. By GG4 only undistorted stairs are described, such as a stair with one step, see G1 below. Now let D2 as shown below be a distorted line drawing of a stair with two steps. First we assume D2 to be encoded as the graph G2, shown below. Note that there are several intersections of lines in D2 , for which no appropriate vertex label of GG4 exist. At these places the most similar vertex labels are used instead. Let G2 be the input of our error correcting precedence graph parser.

Seite 19 von 22

M.Kaul: Practical Applications of PGG, 1986

E

c

d

NE

NE e

f

E

f NW

N E

E

g NE

b

G1

D2

h NE

E

e

NE

NW

N

a

d

NE

e

NE

E

E

c

f

G2

First the parser chooses an arbitrary node to start with, e.g. the c-node at the top. The d-node at the top has equal precedence, and there is no adjacent node with higher precedence. So a precedence handle is found, that is isomorphic to the rhs of p3. But p3 has no embedding relation for the adjacent h-node. Therefore a reduce error occurs and a reduce correction is applied, which simply deletes the annoying edge. Next a reduce can be performed: E

c NE R

C

B

NE

e

NE

f

E

g

E

NE f

f

E

NW

NW

h

g

NE E

NE

e

R NW

NW

e

d

E

NE e

h NE

E

f

Next we arrive at a precedence handle isomorphic to the rhs of p2. There are no embedding relation for NW-edges, but for N-edges, which are most similar in this case:

Seite 20 von 22

M.Kaul: Practical Applications of PGG, 1986

B NE R

C

e

N

f

E

N

g

R

E

NE

NE E

e

h

N h

E

NE

N g

e

A

NE

f

NE E

f

B R

NE

NE

e

f E

R

A

Finally we end up with a single node, which is not the start graph S, but subgraph of the rhs of p1 , that has S as its lhs. The easiest way to end up with the start graph is to insert the missing nodes of the rhs of p1 . A R

N

C

N

a E

b

R

S

Collecting all derivation specifications involved in reduce moves, we get a derivation sequence D. The Hasse-diagram of D is the parse tree of the corrected input. The result of D is the syntactically correct stair with two steps, that is most similar to the input.

9.

Conclusion

Compiler construction has much gained from parser generators like YACC under UNIXTM. Not only pattern recognition research lacks of general theories, that enable us to build such general tools for other applications as well, cf. [Rf 82]. In the area of dynamic data structures, general tools to check consistency formally and efficiently, are urgently needed likewise. Our contribution is an error

M.Kaul: Practical Applications of PGG, 1986

Seite 21 von 22

correcting precedence graph parser, which can be adapted to a wide range of applications easily. In pattern recognition, inexact graph matching, which is NP-complete in general, becomes solvable in O(n3) time, using our method. The technique is, to incorporate application dependent knowledge systematically: The structural knowledge about the pattern class is described by the pgg, the statistical knowledge is given by the weights for the editing operations. (It should be emphasized, that no polynomial time solution to a NP-complete problem is attempted, but that the problem specification is changed in such a way, that the problem becomes tractable.) The precedence approach to efficient graph parsing turns out to be superior in performance, unambiguity and its wide range of applications. The precedence graph parser has been implemented and tested successfully on a Siemens 7.531 under BS2000, VAX 11/750 under VMS and on a SUN3Workstation under UNIX. The programming language has been Pascal. The method, its theory, applications and implementations are described in depth in the author´s dissertation [Ka 86].

10.

References

[AaRoEh 86] [AhUl 72] [Bab 79] [BuAl 83] [ClEhRo 79] [EhNaRo 83] [Fra 78] [Fu 82]

Aalbersberg, I.J./G.Rozenberg/A.Ehrenfeucht: On the membership problem for regular DNLC grammars; Discr. Appl. Math. 13, (1986) 79-85 A.V.Aho/J.D.Ullman: The Theory of Parsing, Translation, and Compiling; I,II, Prentice-Hall, Englewood Cliffs, NJ (1972) J.P.Babinov: Class of generalized context-sensitive prcedence languages; Progr.Comput. Software 5 (1979) 117-126 H.Bunke/G.Allermann: Inexact Graph Matching for Structural Pattern Recognition; Pat.Rec.Let. 1 (1983) 245-253 V.Claus/H.Ehrig/G.Rozenberg: Graph-Grammars and Their Application to Computer Science and Biology; 1st Int. Workshop, LNCS 73, Springer (1979) H.Ehrig/M.Nagl/G.Rozenberg(Eds.): Graph-Grammars and Their Application to Computer Science, 2nd Int. Workshop, LNCS 153, Springer (1983) R.Franck: A Class of Linearly Parsable Graph Grammars, Acta Inform. 10(1978)175-201 K.S.Fu: Syntactic Pattern Recognition; Prentice-Hall, Englewood Cliffs, NJ (1982)

M.Kaul: Practical Applications of PGG, 1986

[GaJo 79] [Har 69] [Har 78] [Has 74] [JaRo 80] [Ka 86] [Knu 68] [LeNa 84] [Lud 81] [Nag 79] [Nag 82] [Rf 82] [Sch 87] [ShHa 81]

Seite 22 von 22

M.R.Garey/D.S.Johnson: Computers and Intractability; A Guide to the Theory of NP-Completeness; Freeman, San Francisco(1979) F.Harary: Graph Theory; Addison-Wesley Publ. Comp., Reading Mass. (1969) M.Harrison: Introduction to Formal Language Theory; Addison-Wesley Publ. Comp., Reading Mass. (1978) R.Haskell: Symmetrical precedence relations on general phrase structure grammars; Comp. Journ. 17 (1974) 234-241 D.Janssens/G.Rozenberg: On the structure of Node Label Contolled Graph Languages; Inform.Sci. 20 (1980) 191-216 M.Kaul: Syntaxanalyse von Graphen bei Präzedenz-GraphGrammatiken; Techn. Report MIP-8610, Uni. Passau, West-Germany D.E.Knuth: Semantic of Context-free Languages; Math. Syst. Theo. (1968) C.Lewerentz/M.Nagl: A Formal Specification Language for Software Systems Defined by Graph Grammars; in U.Pape (Ed.):Proc. WG´84, Workshop on Graphtheor. Conc. in Computer Science, June 13-15, Berlin (1984) H.Ludwigs: Properties of Ordered Graph Grammars; in: H.Noltemeier(Ed.): Graphtheoretic Concepts in Comp. Science; LNCS 100, Springer (1981) 70-79 M.Nagl: Graph-Grammatiken - Theorie, Implementierung, Anwendung; Vieweg, Braunschweig (1979) M.Nagao: Control Strategies in Pattern Analysis; Proc. Pat. Rec. Vol. I, 6th Int. Conf., Munich 1982 (1982) 996-1006 A.Rosenfeld: Image Analysis: Progress, Problems, and Prospects; Proc. Pat. Rec.Vol. I, 6th Int. Conf., Munich 1982 (1982) 7-15 A.Schütte: Spezifikation und Generierung von Übersetzern für Graph-Sprachen durch attributierte Graph-Grammatiken; Dissertation, EXpress Edition ( Reihe Informatik ), Berlin 1987. L.G.Shapiro/R.M.Haralick: Structural Descriptions and Inexact Matching;IEEE Trans. Pat. Ana. PAMI-3, No. 5 (1981)