HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulealgebraic-graphs-0.8Haskell2010

Algebra.Graph.Labelled

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.

This module provides a minimal and experimental implementation of algebraic graphs with edge labels. The API will be expanded in the next release.

  • 5 types
  • 35 values

Algebraic data type for edge-labelled graphs

11 declarations
datadata Graph e a
#

Edge-labelled graphs, where the type variable e stands for edge labels. For example, Graph Bool a is isomorphic to unlabelled graphs defined in the top-level module Algebra.Graph.Graph, where False and True denote the lack of and the existence of an unlabelled edge, respectively.

Constructors

Instances16Bifunctor, Functor, Eq, Num, Ord, Show, …
value(-<) :: a -> e -> (a, e)
#

The left-hand part of a convenient ternary-ish operator x-<e>-y for creating labelled edges.

x -<e>- y == edge e x y
value(>-) :: (a, e) -> a -> Graph e a
#

The right-hand part of a convenient ternary-ish operator x-<e>-y for creating labelled edges.

x -<e>- y == edge e x y
valueoverlay :: Monoid e => Graph e a -> Graph e a -> Graph e a
#

Overlay two graphs. An alias for Connect zero. Complexity: O(1) time and memory, O(s1 + s2) size.

isEmpty     (overlay x y) == isEmpty   x   && isEmpty   y
hasVertex z (overlay x y) == hasVertex z x || hasVertex z y
vertexCount (overlay x y) >= vertexCount x
vertexCount (overlay x y) <= vertexCount x + vertexCount y
edgeCount   (overlay x y) >= edgeCount x
edgeCount   (overlay x y) <= edgeCount x   + edgeCount y
vertexCount (overlay 1 2) == 2
edgeCount   (overlay 1 2) == 0

Note: overlay composes edges in parallel using the operator <+> with zero acting as the identity:

edgeLabel x y $ overlay (edge e x y) (edge zero x y) == e
edgeLabel x y $ overlay (edge e x y) (edge f    x y) == e <+> f

Furthermore, when applied to transitive graphs, overlay composes edges in sequence using the operator <.> with one acting as the identity:

edgeLabel x z $ transitiveClosure (overlay (edge e x y) (edge one y z)) == e
edgeLabel x z $ transitiveClosure (overlay (edge e x y) (edge f   y z)) == e <.> f
valueconnect :: e -> Graph e a -> Graph e a -> Graph e a
#

Connect two graphs with edges labelled by a given label. An alias for Connect. Complexity: O(1) time and memory, O(s1 + s2) size. Note that the number of edges in the resulting graph is quadratic with respect to the number of vertices of the arguments: m = O(m1 + m2 + n1 * n2).

isEmpty     (connect e x y) == isEmpty   x   && isEmpty   y
hasVertex z (connect e x y) == hasVertex z x || hasVertex z y
vertexCount (connect e x y) >= vertexCount x
vertexCount (connect e x y) <= vertexCount x + vertexCount y
edgeCount   (connect e x y) <= vertexCount x * vertexCount y + edgeCount x + edgeCount y
vertexCount (connect e 1 2) == 2
edgeCount   (connect e 1 2) == if e == zero then 0 else 1
valueedges :: Monoid e => [(e, a, a)] -> Graph e a
#

Construct the graph from a list of labelled edges. Complexity: O(L) time, memory and size, where L is the length of the given list.

edges []        == empty
edges [(e,x,y)] == edge e x y
edges           == overlays . map (\(e, x, y) -> edge e x y)
valueoverlays :: Monoid e => [Graph e a] -> Graph e a
#

Overlay a given list of graphs. Complexity: O(L) time and memory, and O(S) size, where L is the length of the given list, and S is the sum of sizes of the graphs in the list.

overlays []        == empty
overlays [x]       == x
overlays [x,y]     == overlay x y
overlays           == foldr overlay empty
isEmpty . overlays == all isEmpty

Graph folding

2 declarations
valuefoldg :: b -> (a -> b) -> (e -> b -> b -> b) -> Graph e a -> b
#

Generalised Graph folding: recursively collapse a Graph by applying the provided functions to the leaves and internal nodes of the expression. The order of arguments is: empty, vertex and connect. Complexity: O(s) applications of the given functions. As an example, the complexity of size is O(s), since const and + have constant costs.

foldg empty     vertex        connect             == id
foldg empty     vertex        (fmap flip connect) == transpose
foldg 1         (const 1)     (const (+))         == size
foldg True      (const False) (const (&&))        == isEmpty
foldg False     (== x)        (const (||))        == hasVertex x
foldg Set.empty Set.singleton (const Set.union)   == vertexSet
valuebuildg :: (forall r. r -> (a -> r) -> (e -> r -> r -> r) -> r) -> Graph e a
#

Build a graph given an interpretation of the three graph construction primitives empty, vertex and connect, in this order. See examples for further clarification.

buildg f                                               == f empty vertex connect
buildg (\e _ _ -> e)                                   == empty
buildg (\_ v _ -> v x)                                 == vertex x
buildg (\e v c -> c l (foldg e v c x) (foldg e v c y)) == connect l x y
buildg (\e v c -> foldr (c zero) e (map v xs))         == vertices xs
buildg (\e v c -> foldg e v (flip . c) g)              == transpose g
foldg e v c (buildg f)                                 == f e v c

Relations on graphs

1 declaration
valueisSubgraphOf :: (Eq e, Monoid e, Ord a) => Graph e a -> Graph e a -> Bool
#

The isSubgraphOf function takes two graphs and returns True if the first graph is a subgraph of the second. Complexity: O(s + m * log(m)) time. Note that the number of edges m of a graph can be quadratic with respect to the expression size s.

isSubgraphOf empty         x             ==  True
isSubgraphOf (vertex x)    empty         ==  False
isSubgraphOf x             (overlay x y) ==  True
isSubgraphOf (overlay x y) (connect x y) ==  True
isSubgraphOf x y                         ==> x <= y

Graph properties

9 declarations
valuesize :: Graph e a -> Int
#

The size of a graph, i.e. the number of leaves of the expression including empty leaves. Complexity: O(s) time.

size empty         == 1
size (vertex x)    == 1
size (overlay x y) == size x + size y
size (connect x y) == size x + size y
size x             >= 1
size x             >= vertexCount x
valueedgeLabel :: (Eq a, Monoid e) => a -> a -> Graph e a -> e
#

Extract the label of a specified edge from a graph.

valuevertexList :: Ord a => Graph e a -> [a]
#

The sorted list of vertices of a given graph. Complexity: O(s * log(n)) time and O(n) memory.

vertexList empty      == []
vertexList (vertex x) == [x]
vertexList . vertices == nub . sort
valueedgeList :: (Eq e, Monoid e, Ord a) => Graph e a -> [(e, a, a)]
#

The list of edges of a graph, sorted lexicographically with respect to pairs of connected vertices (i.e. edge-labels are ignored when sorting). Complexity: O(n + m) time and O(m) memory.

edgeList empty        == []
edgeList (vertex x)   == []
edgeList (edge e x y) == if e == zero then [] else [(e,x,y)]

Graph transformation

8 declarations
valueremoveVertex :: Eq a => a -> Graph e a -> Graph e a
#

Remove a vertex from a given graph. Complexity: O(s) time, memory and size.

removeVertex x (vertex x)       == empty
removeVertex 1 (vertex 2)       == vertex 2
removeVertex x (edge e x x)     == empty
removeVertex 1 (edge e 1 2)     == vertex 2
removeVertex x . removeVertex x == removeVertex x
valueremoveEdge :: (Eq a, Eq e, Monoid e) => a -> a -> Graph e a -> Graph e a
#

Remove an edge from a given graph. Complexity: O(s) time, memory and size.

removeEdge x y (edge e x y)     == vertices [x,y]
removeEdge x y . removeEdge x y == removeEdge x y
removeEdge x y . removeVertex x == removeVertex x
removeEdge 1 1 (1 * 1 * 2 * 2)  == 1 * 2 * 2
removeEdge 1 2 (1 * 1 * 2 * 2)  == 1 * 1 + 2 * 2
valuereplaceVertex :: Eq a => a -> a -> Graph e a -> Graph e a
#

The function replaceVertex x y replaces vertex x with vertex y in a given Graph. If y already exists, x and y will be merged. Complexity: O(s) time, memory and size.

replaceVertex x x            == id
replaceVertex x y (vertex x) == vertex y
replaceVertex x y            == fmap (\v -> if v == x then y else v)
valuereplaceEdge
  1. :: (Eq e, Monoid e, Ord a)
  2. => e
  3. -> a
  4. -> a
  5. -> Graph e a
  6. -> Graph e a
#

Replace an edge from a given graph. If it doesn't exist, it will be created. Complexity: O(log(n)) time.

replaceEdge e x y z                 == overlay (removeEdge x y z) (edge e x y)
replaceEdge e x y (edge f x y)      == edge e x y
edgeLabel x y (replaceEdge e x y z) == e
valueemap :: (e -> f) -> Graph e a -> Graph f a
#

Transform a graph by applying a function to each of its edge labels. Complexity: O(s) time, memory and size.

The function h is required to be a homomorphism on the underlying type of labels e. At the very least it must preserve zero and <+>:

h zero      == zero
h x <+> h y == h (x <+> y)

If e is also a semiring, then h must also preserve the multiplicative structure:

h one       == one
h x <.> h y == h (x <.> y)

If the above requirements hold, then the implementation provides the following guarantees.

emap h empty           == empty
emap h (vertex x)      == vertex x
emap h (edge e x y)    == edge (h e) x y
emap h (overlay x y)   == overlay (emap h x) (emap h y)
emap h (connect e x y) == connect (h e) (emap h x) (emap h y)
emap id                == id
emap g . emap h        == emap (g . h)
valueinduce :: (a -> Bool) -> Graph e a -> Graph e a
#

Construct the induced subgraph of a given graph by removing the vertices that do not satisfy a given predicate. Complexity: O(s) time, memory and size, assuming that the predicate takes constant time.

induce (const True ) x      == x
induce (const False) x      == empty
induce (/= x)               == removeVertex x
induce p . induce q         == induce (\x -> p x && q x)
isSubgraphOf (induce p x) x == True

Relational operations

4 declarations
valueclosure :: (Eq e, Ord a, StarSemiring e) => Graph e a -> Graph e a
#

Compute the reflexive and transitive closure of a graph over the underlying star semiring using the Warshall-Floyd-Kleene algorithm.

closure empty         == empty
closure (vertex x)    == edge one x x
closure (edge e x x)  == edge one x x
closure (edge e x y)  == edges [(one,x,x), (e,x,y), (one,y,y)]
closure               == reflexiveClosure . transitiveClosure
closure               == transitiveClosure . reflexiveClosure
closure . closure     == closure
postSet x (closure y) == Set.fromList (reachable y x)
valuereflexiveClosure :: (Ord a, Semiring e) => Graph e a -> Graph e a
#

Compute the reflexive closure of a graph over the underlying semiring by adding a self-loop of weight one to every vertex. Complexity: O(n * log(n)) time.

reflexiveClosure empty              == empty
reflexiveClosure (vertex x)         == edge one x x
reflexiveClosure (edge e x x)       == edge one x x
reflexiveClosure (edge e x y)       == edges [(one,x,x), (e,x,y), (one,y,y)]
reflexiveClosure . reflexiveClosure == reflexiveClosure
valuesymmetricClosure :: Monoid e => Graph e a -> Graph e a
#

Compute the symmetric closure of a graph by overlaying it with its own transpose. Complexity: O((n + m) * log(n)) time.

symmetricClosure empty              == empty
symmetricClosure (vertex x)         == vertex x
symmetricClosure (edge e x y)       == edges [(e,x,y), (e,y,x)]
symmetricClosure x                  == overlay x (transpose x)
symmetricClosure . symmetricClosure == symmetricClosure
valuetransitiveClosure :: (Eq e, Ord a, StarSemiring e) => Graph e a -> Graph e a
#

Compute the transitive closure of a graph over the underlying star semiring using a modified version of the Warshall-Floyd-Kleene algorithm, which omits the reflexivity step.

transitiveClosure empty               == empty
transitiveClosure (vertex x)          == vertex x
transitiveClosure (edge e x y)        == edge e x y
transitiveClosure . transitiveClosure == transitiveClosure

Types of edge-labelled graphs

3 declarations
typetype Network e a = Graph (Distance e) a
#

A network is a graph whose edges are labelled with distances.

Context

2 declarations
datadata Context e a
#

The Context of a subgraph comprises its inputs and outputs, i.e. all the vertices that are connected to the subgraph's vertices (along with the corresponding edge labels). Note that inputs and outputs can belong to the subgraph itself. In general, there are no guarantees on the order of vertices in inputs and outputs; furthermore, there may be repetitions.

Constructors

Instances2Eq, Show
  • (Eq e, Eq a) => Eq (Context e a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Labelled
  • (Show e, Show a) => Show (Context e a)Defined in algebraic-graphs-0.8 · Algebra.Graph.Labelled
valuecontext
  1. :: (Eq e, Monoid e)
  2. => a -> Bool
  3. -> Graph e a
  4. -> Maybe (Context e a)
#

Extract the Context of a subgraph specified by a given predicate. Returns Nothing if the specified subgraph is empty.

context (const False) x                   == Nothing
context (== 1)        (edge e 1 2)        == if e == zero then Just (Context [] []) else Just (Context [     ] [(e,2)])
context (== 2)        (edge e 1 2)        == if e == zero then Just (Context [] []) else Just (Context [(e,1)] [     ])
context (const True ) (edge e 1 2)        == if e == zero then Just (Context [] []) else Just (Context [(e,1)] [(e,2)])
context (== 4)        (3 * 1 * 4 * 1 * 5) == Just (Context [(one,3), (one,1)] [(one,1), (one,5)])