| 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.AdjacencyMap.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.AdjacencyMap instead.
Synopsis
- newtype AdjacencyMap a = AM {
- adjacencyMap :: Map a (Set a)
- empty :: AdjacencyMap a
- vertex :: a -> AdjacencyMap a
- overlay :: Ord a => AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a
- connect :: Ord a => AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a
- fromAdjacencySets :: Ord a => [(a, Set a)] -> AdjacencyMap a
- consistent :: Ord a => AdjacencyMap a -> Bool
Adjacency map implementation
newtype AdjacencyMap a Source #
The AdjacencyMap 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 :: AdjacencyMap Int) == "empty" show (1 :: AdjacencyMap Int) == "vertex 1" show (1 + 2 :: AdjacencyMap Int) == "vertices [1,2]" show (1 * 2 :: AdjacencyMap Int) == "edge 1 2" show (1 * 2 * 3 :: AdjacencyMap Int) == "edges [(1,2),(1,3),(2,3)]" show (1 * 2 + 3 :: AdjacencyMap 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 :: AdjacencyMap a Source #
Construct the empty graph. Complexity: O(1) time and memory.
isEmptyempty == TruehasVertexx empty == FalsevertexCountempty == 0edgeCountempty == 0
vertex :: a -> AdjacencyMap a 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 :: Ord a => AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a 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 :: Ord a => AdjacencyMap a -> AdjacencyMap a -> AdjacencyMap a 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
fromAdjacencySets :: Ord a => [(a, Set a)] -> AdjacencyMap a Source #
Construct a graph from a list of adjacency sets. Complexity: O((n + m) * log(n)) time and O(n + m) memory.
fromAdjacencySets [] ==emptyfromAdjacencySets [(x, Set.empty)] ==vertexx fromAdjacencySets [(x, Set.singletony)] ==edgex y fromAdjacencySets . map (fmap Set.fromList) .adjacencyList== idoverlay(fromAdjacencySets xs) (fromAdjacencySets ys) == fromAdjacencySets (xs ++ ys)
consistent :: Ord a => AdjacencyMap a -> 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