algebraic-graphs

Alga is a library for algebraic construction and manipulation of graphs in Haskell. See this paper for the motivation behind the library, the underlying theory and implementation details. The top-level module Algebra.Graph defines the main data type for algebraic graphs Graph, as well as associated algorithms. For type-safe representation and manipulation of non-empty algebraic graphs, see Algebra.Graph.NonEmpty. Furthermore, algebraic graphs with edge labels are implemented in Algebra.Graph.Labelled. The library also provides conventional graph data structures, such as Algebra.Graph.AdjacencyMap along with its various flavours: adjacency maps specialised to graphs with vertices of type Int (Algebra.Graph.AdjacencyIntMap), non-empty adjacency maps (Algebra.Graph.NonEmpty.AdjacencyMap), adjacency maps for undirected bipartite graphs (Algebra.Graph.Bipartite.AdjacencyMap), adjacency maps with edge labels (Algebra.Graph.Labelled.AdjacencyMap), acyclic adjacency maps (Algebra.Graph.Acyclic.AdjacencyMap), A large part of the API of algebraic graphs and adjacency maps is available through the Foldable-like type class Algebra.Graph.ToGraph. The type classes defined in Algebra.Graph.Class and Algebra.Graph.HigherKinded.Class can be used for polymorphic construction and manipulation of graphs. This is an experimental library and the API is expected to remain unstable until version 1.0.0. Please consider contributing to the on-going discussions on the library API.


Keywords
algorithms, data-structures, graphs, library, mit, Propose Tags , Alga, this paper, Algebra.Graph, Graph, Algebra.Graph.NonEmpty, Algebra.Graph.Labelled, Algebra.Graph.AdjacencyMap, Algebra.Graph.AdjacencyIntMap, Algebra.Graph.NonEmpty.AdjacencyMap, Algebra.Graph.Bipartite.AdjacencyMap, Algebra.Graph.Labelled.AdjacencyMap, Algebra.Graph.Acyclic.AdjacencyMap, Algebra.Graph.ToGraph, Algebra.Graph.Class, Algebra.Graph.HigherKinded.Class, discussions on the library API, Skip to Readme, , Index, Quick Jump, Algebra.Graph.AdjacencyIntMap.Algorithm, Algebra.Graph.AdjacencyMap.Algorithm, Algebra.Graph.Bipartite.AdjacencyMap.Algorithm, Algebra.Graph.Example.Todo, Algebra.Graph.Export, Algebra.Graph.Export.Dot, Algebra.Graph.Internal, Algebra.Graph.Label, Algebra.Graph.Labelled.Example.Automaton, Algebra.Graph.Labelled.Example.Network, Algebra.Graph.Relation, Algebra.Graph.Relation.Preorder, Algebra.Graph.Relation.Reflexive, Algebra.Graph.Relation.Symmetric, Algebra.Graph.Relation.Transitive, Algebra.Graph.Undirected, Data.Graph.Typed, algebraic-graphs-0.7.tar.gz, browse, Package description, package maintainers, edit package information , this Haskell Symposium paper, talk, Haskell eXchange talk, tutorial, semiring, Haskell eXchange 2018 talk, here, https://blogs.ncl.ac.uk/andreymokhov/an-algebra-of-graphs/, https://blogs.ncl.ac.uk/andreymokhov/graphs-a-la-carte/, https://blogs.ncl.ac.uk/andreymokhov/graphs-in-disguise/, https://blogs.ncl.ac.uk/andreymokhov/old-graphs-from-new-types/, Agda, F#, Scala, TypeScript, algebra, haskell
License
MIT
Install
cabal install algebraic-graphs-0.7

Documentation

Algebraic graphs

Hackage version Build status

Alga is a library for algebraic construction and manipulation of graphs in Haskell. See this Haskell Symposium paper and the corresponding talk for the motivation behind the library, the underlying theory and implementation details. There is also a Haskell eXchange talk, and a tutorial by Alexandre Moine.

Main idea

Consider the following data type, which is defined in the top-level module Algebra.Graph of the library:

data Graph a = Empty | Vertex a | Overlay (Graph a) (Graph a) | Connect (Graph a) (Graph a)

We can give the following semantics to the constructors in terms of the pair (V, E) of graph vertices and edges:

  • Empty constructs the empty graph (∅, ∅).
  • Vertex x constructs a graph containing a single vertex, i.e. ({x}, ∅).
  • Overlay x y overlays graphs (Vx, Ex) and (Vy, Ey) constructing (Vx ∪ Vy, Ex ∪ Ey).
  • Connect x y connects graphs (Vx, Ex) and (Vy, Ey) constructing (Vx ∪ Vy, Ex ∪ Ey ∪ Vx × Vy).

Alternatively, we can give an algebraic semantics to the above graph construction primitives by defining the following type class and specifying a set of laws for its instances (see module Algebra.Graph.Class):

class Graph g where
    type Vertex g
    empty   :: g
    vertex  :: Vertex g -> g
    overlay :: g -> g -> g
    connect :: g -> g -> g

The laws of the type class are remarkably similar to those of a semiring, so we use + and * as convenient shortcuts for overlay and connect, respectively:

  • (+, empty) is an idempotent commutative monoid.
  • (*, empty) is a monoid.
  • * distributes over +, that is: x * (y + z) == x * y + x * z and (x + y) * z == x * z + y * z.
  • * can be decomposed: x * y * z == x * y + x * z + y * z.

This algebraic structure corresponds to unlabelled directed graphs: every expression represents a graph, and every graph can be represented by an expression. Other types of graphs (e.g. undirected) can be obtained by modifying the above set of laws. Algebraic graphs provide a convenient, safe and powerful interface for working with graphs in Haskell, and allow the application of equational reasoning for proving the correctness of graph algorithms.

To represent non-empty graphs, we can drop the Empty constructor -- see module Algebra.Graph.NonEmpty.

To represent edge-labelled graphs, we can switch to the following data type, as explained in my Haskell eXchange 2018 talk:

data Graph e a = Empty
               | Vertex a
               | Connect e (Graph e a) (Graph e a)

Here e is the type of edge labels. If e is a monoid (<+>, zero) then graph overlay can be recovered as Connect zero, and <+> corresponds to parallel composition of edge labels.

How fast is the library?

Alga can handle graphs comprising millions of vertices and billions of edges in a matter of seconds, which is fast enough for many applications. We believe there is a lot of potential for improving the performance of the library, and this is one of our top priorities. If you come across a performance issue when using the library, please let us know.

Some preliminary benchmarks can be found here.

Blog posts

The development of the library has been documented in the series of blog posts:

Algebraic graphs in other languages

Algebraic graphs were implemented in a few other languages, including Agda, F#, Scala and TypeScript.