Copyright | Copyright © 2018-2019 Kadena LLC. |
---|---|
License | MIT |
Maintainer | Lars Kuhtz <lars@kadena.io> |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Directed graphs in adjacency set representation. The implementation is based on Data.HashMap.Strict and Data.HashSet from the unordered-containers package
Undirected graphs are represented as symmetric, irreflexive directed graphs.
Synopsis
- data DiGraph a
- type DiEdge a = (a, a)
- adjacencySets :: DiGraph a -> HashMap a (HashSet a)
- vertices :: DiGraph a -> HashSet a
- edges :: Eq a => Hashable a => DiGraph a -> HashSet (DiEdge a)
- adjacents :: Eq a => Hashable a => a -> DiGraph a -> HashSet a
- incidents :: Eq a => Hashable a => a -> DiGraph a -> [(a, a)]
- insertEdge :: Eq a => Hashable a => DiEdge a -> DiGraph a -> DiGraph a
- fromEdges :: Eq a => Hashable a => Foldable f => f (a, a) -> DiGraph a
- insertVertex :: Eq a => Hashable a => a -> DiGraph a -> DiGraph a
- mapVertices :: Eq b => Hashable b => (a -> b) -> DiGraph a -> DiGraph b
- union :: Eq a => Hashable a => DiGraph a -> DiGraph a -> DiGraph a
- transpose :: Eq a => Hashable a => DiGraph a -> DiGraph a
- symmetric :: Eq a => Hashable a => DiGraph a -> DiGraph a
- fromList :: Eq a => Hashable a => [(a, [a])] -> DiGraph a
- unsafeFromList :: Eq a => Hashable a => [(a, [a])] -> DiGraph a
- isDiGraph :: Eq a => Hashable a => DiGraph a -> Bool
- isAdjacent :: Eq a => Hashable a => a -> a -> DiGraph a -> Bool
- isRegular :: DiGraph a -> Bool
- isSymmetric :: Hashable a => Eq a => DiGraph a -> Bool
- isIrreflexive :: Eq a => Hashable a => DiGraph a -> Bool
- isEdge :: Eq a => Hashable a => DiEdge a -> DiGraph a -> Bool
- isVertex :: Eq a => Hashable a => a -> DiGraph a -> Bool
- order :: DiGraph a -> Natural
- size :: Eq a => Hashable a => DiGraph a -> Natural
- diSize :: Eq a => Hashable a => DiGraph a -> Natural
- symSize :: Eq a => Hashable a => DiGraph a -> Natural
- outDegree :: Eq a => Hashable a => DiGraph a -> a -> Natural
- inDegree :: Eq a => Hashable a => DiGraph a -> a -> Natural
- maxOutDegree :: Eq a => Hashable a => DiGraph a -> Natural
- maxInDegree :: Eq a => Hashable a => DiGraph a -> Natural
- minOutDegree :: Eq a => Hashable a => DiGraph a -> Natural
- minInDegree :: Eq a => Hashable a => DiGraph a -> Natural
- data ShortestPathCache a
- shortestPathCache :: Eq a => Hashable a => DiGraph a -> ShortestPathCache a
- shortestPath :: Eq a => Hashable a => a -> a -> DiGraph a -> Maybe [a]
- shortestPath_ :: Eq a => Hashable a => a -> a -> ShortestPathCache a -> Maybe [a]
- distance :: Eq a => Hashable a => a -> a -> DiGraph a -> Maybe Natural
- distance_ :: Eq a => Hashable a => a -> a -> ShortestPathCache a -> Maybe Natural
- diameter :: Eq a => Hashable a => DiGraph a -> Maybe Natural
- diameter_ :: ShortestPathCache a -> Maybe Natural
- emptyGraph :: Natural -> DiGraph Int
- singleton :: DiGraph Int
- clique :: Natural -> DiGraph Int
- pair :: DiGraph Int
- triangle :: DiGraph Int
- cycle :: Natural -> DiGraph Int
- diCycle :: Natural -> DiGraph Int
- line :: Natural -> DiGraph Int
- diLine :: Natural -> DiGraph Int
- petersonGraph :: DiGraph Int
- twentyChainGraph :: DiGraph Int
- hoffmanSingleton :: DiGraph Int
Documentation
Adjacency set representation of directed graphs.
It is assumed that each target of an edge is also explicitly a vertex in the graph.
It is not generally required that graphs are irreflexive, but all concrete graphs that are defined in this module are irreflexive.
Undirected graphs are represented as symmetric directed graphs.
Instances
Eq a => Eq (DiGraph a) Source # | |
Ord a => Ord (DiGraph a) Source # | |
Defined in Data.DiGraph | |
Show a => Show (DiGraph a) Source # | |
Generic (DiGraph a) Source # | |
(Hashable a, Eq a) => Semigroup (DiGraph a) Source # | |
(Hashable a, Eq a) => Monoid (DiGraph a) Source # | |
NFData a => NFData (DiGraph a) Source # | |
Defined in Data.DiGraph | |
Hashable a => Hashable (DiGraph a) Source # | |
Defined in Data.DiGraph | |
type Rep (DiGraph a) Source # | |
Defined in Data.DiGraph |
adjacents :: Eq a => Hashable a => a -> DiGraph a -> HashSet a Source #
The set of adjacent pairs of a graph.
incidents :: Eq a => Hashable a => a -> DiGraph a -> [(a, a)] Source #
The set of incident edges of a graph.
Construction and Modification of Graphs
insertEdge :: Eq a => Hashable a => DiEdge a -> DiGraph a -> DiGraph a Source #
Insert an edge. Returns the graph unmodified if the edge is already in the graph. Non-existing vertices are added.
fromEdges :: Eq a => Hashable a => Foldable f => f (a, a) -> DiGraph a Source #
Construct a graph from a foldable structure of edges.
insertVertex :: Eq a => Hashable a => a -> DiGraph a -> DiGraph a Source #
Insert a vertex. Returns the graph unmodified if the vertex is already in the graph.
mapVertices :: Eq b => Hashable b => (a -> b) -> DiGraph a -> DiGraph b Source #
Map a function over all vertices of a graph.
transpose :: Eq a => Hashable a => DiGraph a -> DiGraph a Source #
Transpose a graph, i.e. reverse all edges of the graph.
fromList :: Eq a => Hashable a => [(a, [a])] -> DiGraph a Source #
Construct a graph from adjacency lists.
unsafeFromList :: Eq a => Hashable a => [(a, [a])] -> DiGraph a Source #
Unsafely construct a graph from adjacency lists.
This function assumes that the input includes a adjacency list of each vertex
that appears in a adjacency list of another vertex. Generally, fromList
should be preferred.
Predicates
isDiGraph :: Eq a => Hashable a => DiGraph a -> Bool Source #
A predicate that asserts that every target of an edge is also a vertex in the graph. Any graph that is constructed without using unsafe methods is guaranteed to satisfy this predicate.
isAdjacent :: Eq a => Hashable a => a -> a -> DiGraph a -> Bool Source #
Return whether two vertices are adjacent in a graph.
isRegular :: DiGraph a -> Bool Source #
Return whether a graph is regular, i.e. whether all vertices have the same out-degree. Note that the latter implies that all vertices also have the same in-degree.
isSymmetric :: Hashable a => Eq a => DiGraph a -> Bool Source #
Return whether a graph is symmetric, i.e. whether for each edge \((a,b)\) there is also the edge \((b,a)\) in the graph.
isIrreflexive :: Eq a => Hashable a => DiGraph a -> Bool Source #
Return whether a graph is irreflexive. A graph is irreflexive if for each edge \((a,b)\) it holds that \(a \neq b\), i.e there are no self-loops in the graph.
isEdge :: Eq a => Hashable a => DiEdge a -> DiGraph a -> Bool Source #
Return whether an edge is contained in a graph.
isVertex :: Eq a => Hashable a => a -> DiGraph a -> Bool Source #
Return whether a vertex is contained in a graph.
Properties
size :: Eq a => Hashable a => DiGraph a -> Natural Source #
Directed Size. This the number of edges of the graph.
diSize :: Eq a => Hashable a => DiGraph a -> Natural Source #
Directed Size. This the number of edges of the graph.
symSize :: Eq a => Hashable a => DiGraph a -> Natural Source #
Undirected Size of a graph. This is the number of edges of the symmetric closure of the graph.
outDegree :: Eq a => Hashable a => DiGraph a -> a -> Natural Source #
The number of outgoing edges of vertex in a graph.
inDegree :: Eq a => Hashable a => DiGraph a -> a -> Natural Source #
The number of incoming edges of vertex in a graph.
maxOutDegree :: Eq a => Hashable a => DiGraph a -> Natural Source #
The maximum out-degree of the vertices of a graph.
maxInDegree :: Eq a => Hashable a => DiGraph a -> Natural Source #
The maximum in-degree of the vertices of a graph.
minOutDegree :: Eq a => Hashable a => DiGraph a -> Natural Source #
The minimum out-degree of the vertices of a graph.
minInDegree :: Eq a => Hashable a => DiGraph a -> Natural Source #
The minimum in-degree of the vertices of a graph.
Distances, Shortest Paths, and Diameter
data ShortestPathCache a Source #
The shortest path matrix of a graph.
The shortest path matrix of a graph can be used to efficiently query the distance and shortest path between any two vertices of the graph. It can also be used to efficiently compute the diameter of the graph.
Computing the shortest path matrix is expensive for larger graphs. The matrix is computed using the Floyd-Warshall algorithm. The space and time complexity is quadratic in the order of the graph. For sparse graphs there are more efficient algorithms for computing distances and shortest paths between the nodes of the graph.
Instances
shortestPathCache :: Eq a => Hashable a => DiGraph a -> ShortestPathCache a Source #
Compute the shortest path matrix for a graph. The result can be used to efficiently query the distance and shortest path between any two vertices of the graph. It can also be used to efficiently compute the diameter of the graph.
shortestPath :: Eq a => Hashable a => a -> a -> DiGraph a -> Maybe [a] Source #
Compute the shortest path between two vertices of a graph.
| This is expensive for larger graphs. If more than one path is needed one
should use shortestPathCache
to cache the result of the search and use
shortestPath_
to query paths from the cache.
The algorithm is optimized for dense graphs. For large sparse graphs a more efficient algorithm should be used.
shortestPath_ :: Eq a => Hashable a => a -> a -> ShortestPathCache a -> Maybe [a] Source #
Compute the shortest path between two vertices from the shortest path matrix of a graph.
The algorithm is optimized for dense graphs. For large sparse graphs a more efficient algorithm should be used.
distance :: Eq a => Hashable a => a -> a -> DiGraph a -> Maybe Natural Source #
Compute the distance between two vertices of a graph.
| This is expensive for larger graphs. If more than one distance is needed
one should use shortestPathCache
to cache the result of the search and use
distance_
to query paths from the cache.
The algorithm is optimized for dense graphs. For large sparse graphs a more efficient algorithm should be used.
distance_ :: Eq a => Hashable a => a -> a -> ShortestPathCache a -> Maybe Natural Source #
Compute the distance between two vertices from the shortest path matrix of a graph.
The algorithm is optimized for dense graphs. For large sparse graphs a more efficient algorithm should be used.
diameter :: Eq a => Hashable a => DiGraph a -> Maybe Natural Source #
Compute the Diameter of a graph, i.e. the maximum length of a shortest path between two vertices in the graph.
This is expensive to compute for larger graphs. If also the shortest paths or
distances are needed, one should use shortestPathCache
to cache the result
of the search and use the diameter_
, shortestPath_
, and distance_
to
query the respective results from the cache.
The algorithm is optimized for dense graphs. For large sparse graphs a more efficient algorithm should be used.
diameter_ :: ShortestPathCache a -> Maybe Natural Source #
Compute the Diameter of a graph from a shortest path matrix. The diameter of a graph is the maximum length of a shortest path between two vertices in the graph.
Graphs
petersonGraph :: DiGraph Int Source #
The Peterson graph.
twentyChainGraph :: DiGraph Int Source #
The "twenty chain" graph.
hoffmanSingleton :: DiGraph Int Source #
Hoffman-Singleton Graph.
The Hoffman-Singleton graph is a 7-regular graph with 50 vertices and 175 edges. It's the largest graph of max-degree 7 and diameter 2. Cf. [https:/en.wikipedia.orgwiki/Hoffman–Singleton_graph]()