| Copyright | (c) Andrey Mokhov 2016-2018 |
|---|---|
| License | MIT (see the file LICENSE) |
| Maintainer | andrey.mokhov@gmail.com |
| 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:
overlayis commutative and associative:x + y == y + x x + (y + z) == (x + y) + z
connectis associative and hasemptyas the identity:x * empty == x empty * x == x x * (y * z) == (x * y) * z
connectdistributes overoverlay:x * (y + z) == x * y + x * z (x + y) * z == x * z + y * z
connectcan be decomposed:x * y * z == x * y + x * z + y * z
The following useful theorems can be proved from the above set of axioms.
overlayhasemptyas the identity and is idempotent:x + empty == x empty + x == x x + x == xAbsorption 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.
isEmptyempty == TruehasVertexx empty == FalsevertexCountempty == 0edgeCountempty == 0
vertex :: Int -> AdjacencyIntMap Source #
Construct the graph comprising a single isolated vertex. Complexity: O(1) time and memory.
isEmpty(vertex x) == FalsehasVertexx (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) ==isEmptyx &&isEmptyyhasVertexz (overlay x y) ==hasVertexz x ||hasVertexz yvertexCount(overlay x y) >=vertexCountxvertexCount(overlay x y) <=vertexCountx +vertexCountyedgeCount(overlay x y) >=edgeCountxedgeCount(overlay x y) <=edgeCountx +edgeCountyvertexCount(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) ==isEmptyx &&isEmptyyhasVertexz (connect x y) ==hasVertexz x ||hasVertexz yvertexCount(connect x y) >=vertexCountxvertexCount(connect x y) <=vertexCountx +vertexCountyedgeCount(connect x y) >=edgeCountxedgeCount(connect x y) >=edgeCountyedgeCount(connect x y) >=vertexCountx *vertexCountyedgeCount(connect x y) <=vertexCountx *vertexCounty +edgeCountx +edgeCountyvertexCount(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 [] ==emptyfromAdjacencyIntSets [(x, IntSet.empty)] ==vertexx fromAdjacencyIntSets [(x, IntSet.singletony)] ==edgex 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 (vertexx) == True consistent (overlayx y) == True consistent (connectx y) == True consistent (edgex y) == True consistent (edgesxs) == True consistent (starsxs) == True