Copyright | (c) Andrey Mokhov 2016-2018 |
---|---|
License | MIT (see the file LICENSE) |
Maintainer | [email protected] |
Stability | unstable |
Safe Haskell | None |
Language | Haskell2010 |
Algebra.Graph.AdjacencyIntMap.Internal
Contents
Description
This module exposes the implementation of adjacency maps. The API is unstable and unsafe, and is exposed only for documentation. You should use the non-internal module Algebra.Graph.AdjacencyIntMap instead.
Synopsis
- newtype AdjacencyIntMap = AM {}
- empty :: AdjacencyIntMap
- vertex :: Int -> AdjacencyIntMap
- overlay :: AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap
- connect :: AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap
- fromAdjacencyIntSets :: [(Int, IntSet)] -> AdjacencyIntMap
- consistent :: AdjacencyIntMap -> Bool
Adjacency map implementation
newtype AdjacencyIntMap Source #
The AdjacencyIntMap
data type represents a graph by a map of vertices to
their adjacency sets. 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))
The Show
instance is defined using basic graph construction primitives:
show (empty :: AdjacencyIntMap Int) == "empty" show (1 :: AdjacencyIntMap Int) == "vertex 1" show (1 + 2 :: AdjacencyIntMap Int) == "vertices [1,2]" show (1 * 2 :: AdjacencyIntMap Int) == "edge 1 2" show (1 * 2 * 3 :: AdjacencyIntMap Int) == "edges [(1,2),(1,3),(2,3)]" show (1 * 2 + 3 :: AdjacencyIntMap Int) == "overlay (vertex 3) (edge 1 2)"
The Eq
instance satisfies all axioms of algebraic graphs:
overlay
is commutative and associative:x + y == y + x x + (y + z) == (x + y) + z
connect
is associative and hasempty
as the identity:x * empty == x empty * x == x x * (y * z) == (x * y) * z
connect
distributes overoverlay
:x * (y + z) == x * y + x * z (x + y) * z == x * z + y * z
connect
can be decomposed:x * y * z == x * y + x * z + y * z
The following useful theorems can be proved from the above set of axioms.
overlay
hasempty
as the identity and is idempotent:x + empty == x empty + x == x x + x == x
Absorption and saturation of
connect
:x * y + x + y == x * y x * x * x == x * x
When specifying the time and memory complexity of graph algorithms, n and m will denote the number of vertices and edges in the graph, respectively.
Constructors
AM | |
Fields
|
Instances
empty :: AdjacencyIntMap Source #
Construct the empty graph. Complexity: O(1) time and memory.
isEmpty
empty == TruehasVertex
x empty == FalsevertexCount
empty == 0edgeCount
empty == 0
vertex :: Int -> AdjacencyIntMap Source #
Construct the graph comprising a single isolated vertex. Complexity: O(1) time and memory.
isEmpty
(vertex x) == FalsehasVertex
x (vertex x) == TruevertexCount
(vertex x) == 1edgeCount
(vertex x) == 0
overlay :: AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap Source #
Overlay two graphs. This is a commutative, associative and idempotent
operation with the identity empty
.
Complexity: O((n + m) * log(n)) time and O(n + m) memory.
isEmpty
(overlay x y) ==isEmpty
x &&isEmpty
yhasVertex
z (overlay x y) ==hasVertex
z x ||hasVertex
z yvertexCount
(overlay x y) >=vertexCount
xvertexCount
(overlay x y) <=vertexCount
x +vertexCount
yedgeCount
(overlay x y) >=edgeCount
xedgeCount
(overlay x y) <=edgeCount
x +edgeCount
yvertexCount
(overlay 1 2) == 2edgeCount
(overlay 1 2) == 0
connect :: AdjacencyIntMap -> AdjacencyIntMap -> AdjacencyIntMap Source #
Connect two graphs. This is an associative operation with the identity
empty
, which distributes over overlay
and obeys the decomposition axiom.
Complexity: O((n + m) * log(n)) time and O(n + m) memory. 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 x y) ==isEmpty
x &&isEmpty
yhasVertex
z (connect x y) ==hasVertex
z x ||hasVertex
z yvertexCount
(connect x y) >=vertexCount
xvertexCount
(connect x y) <=vertexCount
x +vertexCount
yedgeCount
(connect x y) >=edgeCount
xedgeCount
(connect x y) >=edgeCount
yedgeCount
(connect x y) >=vertexCount
x *vertexCount
yedgeCount
(connect x y) <=vertexCount
x *vertexCount
y +edgeCount
x +edgeCount
yvertexCount
(connect 1 2) == 2edgeCount
(connect 1 2) == 1
fromAdjacencyIntSets :: [(Int, IntSet)] -> AdjacencyIntMap Source #
Construct a graph from a list of adjacency sets. Complexity: O((n + m) * log(n)) time and O(n + m) memory.
fromAdjacencyIntSets [] ==empty
fromAdjacencyIntSets [(x, IntSet.empty
)] ==vertex
x fromAdjacencyIntSets [(x, IntSet.singleton
y)] ==edge
x y fromAdjacencyIntSets . map (fmap IntSet.fromList
) .adjacencyList
== idoverlay
(fromAdjacencyIntSets xs) (fromAdjacencyIntSets ys) == fromAdjacencyIntSets (xs ++ ys)
consistent :: AdjacencyIntMap -> Bool Source #
Check if the internal graph representation is consistent, i.e. that all edges refer to existing vertices. It should be impossible to create an inconsistent adjacency map, and we use this function in testing. Note: this function is for internal use only.
consistentempty
== True consistent (vertex
x) == True consistent (overlay
x y) == True consistent (connect
x y) == True consistent (edge
x y) == True consistent (edges
xs) == True consistent (stars
xs) == True