Non-empty algebraic graphs, which are constructed using three primitives: vertex, overlay and connect. See module Algebra.Graph for algebraic graphs that can be empty.
We define a Num instance as a convenient notation for working with graphs:
0 == vertex 0
1 + 2 == overlay (vertex 1) (vertex 2)
1 * 2 == connect (vertex 1) (vertex 2)
1 + 2 * 3 == overlay (vertex 1) (connect (vertex 2) (vertex 3))
1 * (2 + 3) == connect (vertex 1) (overlay (vertex 2) (vertex 3))
Note: the signum method of the type class Num cannot be implemented and
will throw an error. Furthermore, the Num instance does not satisfy several
"customary laws" of Num, which dictate that fromInteger 0 and
fromInteger 1 should act as additive and multiplicative identities, and
negate as additive inverse. Nevertheless, overloading fromInteger, + and
* is very convenient when working with algebraic graphs; we hope that in
future Haskell's Prelude will provide a more fine-grained class hierarchy for
algebraic structures, which we would be able to utilise without violating any
laws.
The Eq instance satisfies the following laws of non-empty algebraic graphs.
overlay is commutative, associative and idempotent:
x + y == y + x x + (y + z) == (x + y) + z x + x == xconnect is associative:
x * (y * z) == (x * y) * zconnect distributes over overlay:
x * (y + z) == x * y + x * z (x + y) * z == x * z + y * zconnect can be decomposed:
x * y * z == x * y + x * z + y * zconnect satisfies absorption and saturation:
x * y + x + y == x * y x * x * x == x * x
When specifying the time and memory complexity of graph algorithms, n will
denote the number of vertices in the graph, m will denote the number of
edges in the graph, and s will denote the size of the corresponding Graph
expression, defined as the number of vertex leaves (note that n <= s). If
g is a Graph, the corresponding n, m and s can be computed as follows:
n == vertexCount g
m == edgeCount g
s == size gConverting a Graph to the corresponding AdjacencyMap takes O(s + m * log(m)) time and O(s + m) memory. This is also the complexity of the graph equality test, because it is currently implemented by converting graph expressions to canonical representations based on adjacency maps.
The total order Ord on graphs is defined using size-lexicographic comparison:
Compare the number of vertices. In case of a tie, continue.
Compare the sets of vertices. In case of a tie, continue.
Compare the number of edges. In case of a tie, continue.
Compare the sets of edges.
Here are a few examples:
vertex 1 < vertex 2
vertex 3 < edge 1 2
vertex 1 < edge 1 1
edge 1 1 < edge 1 2
edge 1 2 < edge 1 1 + edge 2 2
edge 1 2 < edge 1 3Note that the resulting order refines the isSubgraphOf relation and is compatible with overlay and connect operations:
isSubgraphOf x y ==> x <= yx <= x + y
x + y <= x * yInstances12Monad, Functor, Applicative, Eq, Num, Ord, …
Monad GraphDefined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyFunctor GraphDefined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyApplicative GraphDefined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyOrd a => Eq (Graph a)Defined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyNum a => Num (Graph a)Defined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyNote: this does not satisfy the usual ring laws; see Graph for more details.
Ord a => Ord (Graph a)Defined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyShow a => Show (Graph a)Defined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyIsString a => IsString (Graph a)Defined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptySemigroup (Graph a)Defined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyDefined via overlay.
NFData a => NFData (Graph a)Defined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptyToGraph (Graph a)Defined in algebraic-graphs-0.8 · Algebra.Graph.NonEmptytype ToVertex (Graph a) = aDefined in algebraic-graphs-0.8 · Algebra.Graph.NonEmpty