Linear-Time Graph Algorithms in GP 2

Linear-Time Graph Algorithms in GP 2

Linear-Time Graph Algorithms in GP 2 Graham Campbell Department of Computer Science, University of York, United Kingdom https://gjcampbell.co.uk/ [email protected] Brian Courtehoute Department of Computer Science, University of York, United Kingdom https://www.cs.york.ac.uk/people/brianc [email protected] Detlef Plump Department of Computer Science, University of York, United Kingdom https://www-users.cs.york.ac.uk/det/ [email protected] Abstract GP 2 is an experimental programming language based on graph transformation rules which aims to facilitate program analysis and verification. However, implementing graph algorithms efficiently in a rule-based language is challenging because graph pattern matching is expensive. GP 2 mitigates this problem by providing rooted rules which, under mild conditions, can be matched in constant time. In this paper, we present linear-time GP 2 programs for three problems: tree recognition, binary directed acyclic graph (DAG) recognition, and topological sorting. In each case, we show the correctness of the program, prove its linear time complexity, and also give empirical evidence for the linear run time. For DAG recognition and topological sorting, the linear behaviour is achieved by implementing depth-first search strategies based on an encoding of stacks in graphs. 2012 ACM Subject Classification Theory of computation → Design and analysis of algorithms Keywords and phrases Graph transformation, rooted graph programs, GP 2, linear-time algorithms, depth-first search, topological sorting Digital Object Identifier 10.4230/LIPIcs.CALCO.2019.16 1 Introduction Rule-based graph transformation was established as a research field in the 1970s and has since then been the subject of countless articles. While many of these contributions have a theoretical nature (see the monograph [8] for a recent overview), there has also been work on languages and tools for executing and analysing graph transformation systems. Languages based on graph transformation rules include AGG [18], GReAT [1], GROOVE [10], GrGen.Net [13], Henshin [3] and PORGY [9]. This paper focuses on GP 2 [14], an experimental graph programming language which aims to facilitate formal reasoning on programs. The language has a simple formal semantics and is computationally complete in that every computable function on graphs can be programmed [15]. Research on graph programs has provided, for example, a Hoare-calculus for program verification [16, 17] and a static analysis for confluence checking [12]. A challenge for the design and implementation of graph transformation languages is to narrow the performance gap between imperative and rule-based graph programming. The bottleneck for achieving fast graph transformation is the cost of graph matching. In general, matching the left-hand graph L of a rule within a host graph G requires time size(G)size(L) (which is polynomial since L is fixed). As a consequence, linear-time imperative graph algorithms may be slowed down to polynomial time when they are recast as rule-based graph programs. © Graham Campbell, Brian Courtehoute, and Detlef Plump; licensed under Creative Commons License CC-BY 8th Conference on Algebra and Coalgebra in Computer Science (CALCO 2019). Editors: Markus Roggenbach and Ana Sokolova; Article No. 16; pp. 16:1–16:23 Leibniz International Proceedings in Informatics Schloss Dagstuhl – Leibniz-Zentrum für Informatik, Dagstuhl Publishing, Germany 16:2 Linear-Time Graph Algorithms in GP 2 To mitigate this problem, GP 2 supports rooted graph transformation which was first proposed by Dörr [7]. The idea is to distinguish certain nodes as roots and to match roots in rules with roots in host graphs. Then only the neighbourhood of host graph roots needs to be searched for matches, allowing, under mild conditions, to match rules in constant time. In [5], fast rules were identified as a class of rooted rules that can be applied in constant time if host graphs have a bounded node degree and contain a bounded number of roots. The condition of a bounded number of host graph roots can be satisfied by requiring unrooted input graphs and using in loops only rules that do not increase the number of roots. This simply means that no such rule must have more roots in its right-hand side than in its left-hand side. (A refined condition considers the “root balance” of all rules in a loop body simultaneously.) The condition that host graphs must have a bounded node degree depends on the application domain of a program. For example, traffic networks or digital circuits can be considered as graphs of bounded degree. The first linear-time graph problem implemented by a GP 2 program with fast rules was 2-colouring. In [6] it is shown that this program colours connected graphs of bounded degree in linear time. The compiled program even matches the speed of Sedgewick’s textbook C program [19] on grid graphs of up to 100,000 nodes. In this paper, we continue to provide evidence that rooted graph programs can rival the time complexity of graph algorithms (on bounded-degree graphs) in conventional programming languages. We present three new case studies: recognition of trees, recognition of binary DAGs, and topological sorting of acyclic graphs. Each of these problems is solvable in linear time with algorithms in imperative languages. For each problem, we present a GP 2 program with fast rules, show its correctness, and prove its linear time complexity on graphs of bounded node degree. We also give empirical evidence for the linear run time by presenting benchmark results for graphs of up to 100,000 nodes in various graph classes. For DAG recognition and topological sorting, the linear behaviour is achieved by implementing depth-first search strategies based on an encoding of stacks in host graphs. It is worth noting that rooted rules per se are not a blueprint for imitating algorithms in imperative languages. This is because GP 2 intentionally does not provide access to the graph data structure of its implementation. As a consequence, for example, currently there seems to be no way of traversing arbitrary disconnected graphs with GP 2 in linear time. 2 The Graph Programming Language GP 2 This section briefly introduces GP 2, a non-deterministic language based on graph-transfor- mation rules, first defined in [14]. Up-to-date versions of the syntax and semantics of GP 2 can be found in [4]. The language is implemented by a compiler generating C code [6]. 2.1 Graphs, Rules and Programs GP 2 programs transform input graphs into output graphs, where graphs are directed and may contain parallel edges and loops. Both nodes and edges are labelled with lists consisting of integers and character strings. This includes the special case of items labelled with the empty list which may be considered as “unlabelled”. The principal programming construct in GP 2 consist of conditional graph transformation rules labelled with expressions. For example, the rule i0_push in Figure 6 has two formal parameters of type list, a left-hand graph and a right-hand graph which are specified graphically, and a textual condition starting with the keyword where. G. Campbell, B. Courtehoute, and D. Plump 16:3 The small numbers attached to nodes are identifiers, all other text in the graphs consist of labels. Parameters are typed but in this paper we only need the most general type list which represents lists with arbitrary values. Besides carrying expressions, nodes and edges can be marked red, green or blue. In addition, nodes can be marked grey and edges can be dashed. For example, rule i0_push in Figure 6 contains red and blue nodes and a blue edge. Marks are convenient, among other things, to record visited items during a graph traversal and to encode auxiliary structures in graphs. The programs in the following sections use marks extensively. Rules operate on host graphs which are labelled with constant values (lists containing integers and character strings). Formally, the application of a rule to a host graph is defined as a two-stage process in which first the rule is instantiated by replacing all variables with values of the same type, and evaluating all expressions. This yields a standard rule (without expressions) in the so-called double-pushout approach with relabelling [11]. In the second stage, the instantiated rule is applied to the host graph by constructing two suitable pushouts. We refer to [4] for details and only give an equivalent operational description of rule application. Applying a rule L ⇒ R to a host graph G works roughly as follows: (1) Replace the variables in L and R with constant values and evaluate the expressions in L and R, to obtain an instantiated rule Lˆ ⇒ Rˆ. (2) Choose a subgraph S of G isomorphic to Lˆ such that the dangling condition and the rule’s application condition are satisfied (see below). (3) Replace S with Rˆ as follows: numbered nodes stay in place (possibly relabelled), edges and unnumbered nodes of Lˆ are deleted, and edges and unnumbered nodes of Rˆ are inserted. In this construction, the dangling condition requires that nodes in S corresponding to unnumbered nodes in Lˆ (which should be deleted) must not be incident with edges outside S. The rule’s application condition is evaluated after variables have been replaced with the corresponding values of Lˆ, and node identifiers of L with the corresponding identifiers of S. For example, the condition indeg(1) = 0 of rule i0_push in Figure 6 requires that node g(1) has no incoming edges, where g(1) is the node in S corresponding to 1. A program consists of declarations of conditional rules and procedures, and exactly one declaration of a main command sequence, which is a distinct procedure named Main.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    23 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us