Skip to content

🕸️ Graphs, finite fields and discrete dynamical systems in Kotlin

License

Notifications You must be signed in to change notification settings

breandan/galoisenne

Repository files navigation

Kaliningraph

Kotlin 1.4.20 CI

Kaliningraph is a purely functional graph library with a DSL for constructing graphs and visualizing the behavior of graph algorithms.

Installation

Kaliningraph is hosted on JitPack.

Gradle

repositories {
    maven("https://jitpack.io")
}

dependencies {
    implementation("com.github.breandan:kaliningraph:-SNAPSHOT")
}

Maven

<project>
  <repositories>
    <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
    </repository>
  </repositories>

  <dependency>
    <groupId>com.github.breandan</groupId>
    <artifactId>kaliningraph</artifactId>
    <version>0.0.1</version>
  </dependency>
</project>

Jupyter Notebook

First install the Kotlin Jupyter kernel, then run the jupyterInstall task to install the library descriptor:

./gradlew jupyterInstall [-Path=~/.jupyter_kotlin/libraries]

To access notebook support, use the following line magic:

%use kaliningraph

For more information, explore the tutorial.

Graphs, Inductively

What are graphs? A graph is a (possibly empty) set of vertices.

What are vertices? A vertex is a unique label with neighbors (possibly containing itself).

What are neighbors? Neighbors are a graph.

Getting Started

Run the demo via ./gradlew HelloKaliningraph to get started.

Usage

To construct a graph, the graph builder DSL provides an small alphabet:

val graph = Graph { a - b - c - d - e; a - c - e }

This is the same as:

val abcde = Graph { a - b - c - d - e }
val ace = Graph { a - c - e }
val graph = abcde + ace

Equality is supported using the Weisfeiler-Lehman test:

val x = Graph { a - b - c - d - e; a - c - e }
val y = Graph { b - c - d - e - f; b - d - f }
assertEquals(x == y) // true

Visualization

Kaliningraph supports a number of graph visualizations.

Graphviz

Graph visualization is made possible thanks to KraphViz.

val de = Graph { d - e }
val dacbe = Graph { d - a - c - b - e }
val dce = Graph { d - c - e }

val abcd = Graph { a - b - c - d }
val cfde = Graph { c - "a" - f - d - e }

val dg = Graph(dacbe, dce, de) + Graph(abcd, cfde)
dg.show()

Running the above snippet will cause the following figure to be rendered in the browser:

Matrix form

Graph visualization in both DOT and adjacency matrix format is supported.

DOT Graph Matrix
image image_1

It is also possible to visualize the state and transition matrices and step through the graph (./gradlew PrefAttach).

transition_diagram

Computation graph

Computational notebooks prototyping is also supported.

Notebook {
  a = b + c
  f = b - h
}.show()

The above snippet should display something like the following:

Translation

Bidirectional translation to various graph formats, including Graphviz, JGraphT, Tinkerpop and RedisGraph is supported:

val g = Graph { a - b - c - a }
        .toJGraphT().toKaliningraph()
        .toTinkerpop().toKaliningraph()
        .toGraphviz().toKaliningraph()

Code2Vec

Code2Vec generation and visualization is supported. The following demo was generated using message passing on the adjacency matrix, for graphs of varying height. The technique to create the embeddings is described here. We use TSNE to visualize the resulting vectors in 2D, and can clearly distinguish the clusters.

Automata-Based Regex

A regex to NFA compiler is provided. To run the demo, run ./gradlew RegexDemo. You should see something like this:

Research Questions

  • Is subgraph isomorphism feasible using random walks?
    • Treat graph as a sequence and run string convolution
    • Generate lazy random walk and halt after K steps
    • Convolve permuted variants of query in parallel
    • Need some kind of label permutation / edit distance metric
  • How could we implement graph grammars/rewriting?
    • Rewrites as string substitution on the random walk sequence
    • Reconstruct graph from rewritten string using adjacency matrix
    • Is there an algebraic definition for graph grammars?
    • Maybe graph convolution. How to encode rewrites as a kernel?
    • Rectangular matrix multiplication or square with upper bound?
    • May be possible to represent using tensor contraction
    • Need to look into hyperedge replacement grammars
    • How do we identify confluent rewrite systems?
  • What are the advantages and disadvantages of graph rewriting?
    • Graphs as vertices and rewrites as edges in a nested graph?
    • Reduction/canonicalization versus expansion graph grammar
  • What happens if we represent the graph as a symbolic matrix?
    • Could we propagate functions instead of just values?
    • What if matrix elements were symbolic expressions?
    • Should we represent the whole matrix as a big bold symbol?
  • Is there an efficient way to parallelize arithmetic circuits?
    • Translate formula graph to matrix using Miller's evaluator
    • How to distribute the work evenly across sparse matrices
  • What are some good way to visualize random walks?
    • Display states, transitions and graph occupancy side-by-side
  • Is there a connection between linear algebra and λ-calculus?

References

Graph theory

Graph learning

Functional graphs

Graph Rewriting

Unification

Termination checking

Algebra

Circuits

Propagation

Random Walks

Software Engineering

Proof Search

Code Search

Software

Graphs

  • Alga - a library for algebraic construction and manipulation of graphs in Haskell
  • Bifurcan - high-quality JVM implementations of immutable data structures
  • Kraphviz - Graphviz with pure Java
  • JGraLab - a Java graph library implementing TGraphs: typed, attributed, ordered, and directed graphs (paper)
  • GraphBLAS - open effort to define standard building blocks for graph algorithms in the language of linear algebra
  • GraphBLAST - High-Performance Linear Algebra-based Graph Primitives on GPUs

Rewriting

  • Grez - graph transformation termination checker (manual)
  • GP2 - Rule-based graph programming language
  • AGG - development environment for attributed graph transformation systems supporting an algebraic approach to graph transformation (manual)
  • Henshin - an IDE for developing and simulating triple graph grammars (TGGs) (manual)
  • JavaSMT - Unified Java API for SMT solvers

Automata