Adjacency list representation of a graph, mapping each vertex to its list of successors.
Modulererebase-1.21.2Haskell2010
Data.Graph
- 8 types
- 21 values
- Packagererebase-1.21.2
- Exports30
- LanguageHaskell2010
- LicenceMIT
- SourceGraph.hs
Non-empty, possibly infinite, multi-way trees; also known as rose trees.
Instances51Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
Monad TreeDefined in containers-0.7 · Data.TreeFunctor TreeDefined in containers-0.7 · Data.TreeMonadFix TreeDefined in containers-0.7 · Data.TreeApplicative TreeDefined in containers-0.7 · Data.TreeFoldable TreeDefined in containers-0.7 · Data.TreeFolds in preorder
Traversable TreeDefined in containers-0.7 · Data.TreeMonadZip TreeDefined in containers-0.7 · Data.TreeFoldable1 TreeDefined in containers-0.7 · Data.TreeFolds in preorder
Eq1 TreeDefined in containers-0.7 · Data.TreeOrd1 TreeDefined in containers-0.7 · Data.TreeRead1 TreeDefined in containers-0.7 · Data.TreeShow1 TreeDefined in containers-0.7 · Data.TreeHashable1 TreeDefined in hashable-1.4.7.0 · Data.Hashable.ClassComonad TreeDefined in comonad-5.0.9 · Control.ComonadComonadApply TreeDefined in comonad-5.0.9 · Control.ComonadApply TreeDefined in semigroupoids-6.0.1 · Data.Functor.Bind.ClassBind TreeDefined in semigroupoids-6.0.1 · Data.Functor.Bind.ClassExtend TreeDefined in semigroupoids-6.0.1 · Data.Functor.ExtendTraversable1 TreeDefined in semigroupoids-6.0.1 · Data.Semigroup.Traversable.ClassInvariant TreeDefined in invariant-0.6.4 · Data.Functor.Invariantfrom the
containerspackageAdjustable TreeDefined in keys-3.12.3 · Data.KeyFoldableWithKey TreeDefined in keys-3.12.3 · Data.KeyFoldableWithKey1 TreeDefined in keys-3.12.3 · Data.KeyIndexable TreeDefined in keys-3.12.3 · Data.KeyKeyed TreeDefined in keys-3.12.3 · Data.KeyLookup TreeDefined in keys-3.12.3 · Data.KeyTraversableWithKey TreeDefined in keys-3.12.3 · Data.KeyTraversableWithKey1 TreeDefined in keys-3.12.3 · Data.KeyZip TreeDefined in keys-3.12.3 · Data.KeyZipWithKey TreeDefined in keys-3.12.3 · Data.KeyCopointed TreeDefined in pointed-5.0.4 · Data.CopointedPointed TreeDefined in pointed-5.0.4 · Data.PointedGeneric1 TreeDefined in containers-0.7 · Data.TreeComonadCofree [] TreeDefined in free-5.2 · Control.Comonad.Cofree.ClassLift a => Lift (Tree a)Defined in containers-0.7 · Data.TreeEq a => Eq (Tree a)Defined in containers-0.7 · Data.TreeData a => Data (Tree a)Defined in containers-0.7 · Data.TreeOrd a => Ord (Tree a)Defined in containers-0.7 · Data.TreeRead a => Read (Tree a)Defined in containers-0.7 · Data.TreeShow a => Show (Tree a)Defined in containers-0.7 · Data.TreeGeneric (Tree a)Defined in containers-0.7 · Data.TreeNFData a => NFData (Tree a)Defined in containers-0.7 · Data.TreeBinary e => Binary (Tree e)Defined in binary-0.8.9.3 · Data.Binary.ClassHashable v => Hashable (Tree v)Defined in hashable-1.4.7.0 · Data.Hashable.ClassDefault a => Default (Tree a)Defined in data-default-0.8.0.1 · Data.Default.InternalFoldableWithIndex [Int] TreeDefined in indexed-traversable-0.1.4 · WithIndexFunctorWithIndex [Int] TreeDefined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex [Int] TreeDefined in indexed-traversable-0.1.4 · WithIndextype Rep (Tree a) = D1 ('MetaDataDefined in containers-0.7 · Data.Tree"Tree"
"Data.Tree"
"containers-0.7-1cc3"
'False) (C1 ('MetaCons"Node"
'PrefixI 'True) (S1 ('MetaSel ('Just"rootLabel"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Just"subForest"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [Tree a])))type Rep1 Tree = D1 ('MetaDataDefined in containers-0.7 · Data.Tree"Tree"
"Data.Tree"
"containers-0.7-1cc3"
'False) (C1 ('MetaCons"Node"
'PrefixI 'True) (S1 ('MetaSel ('Just"rootLabel"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1 :*: S1 ('MetaSel ('Just"subForest"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) ([] :.: Rec1 Tree)))type Key Tree = Seq IntDefined in keys-3.12.3 · Data.Key
The bounds of an Array.
O(V+E). Returns True if the second vertex reachable from the first.
Examples
path (buildG (0,0) []) 0 0 == Truepath (buildG (0,2) [(0,1), (1,2)]) 0 2 == Truepath (buildG (0,2) [(0,1), (1,2)]) 2 0 == FalseThis type synonym exists primarily for historical reasons.
O(V+E). The biconnected components of a graph.
An undirected graph is biconnected if the deletion of any vertex
leaves it connected.
The input graph is expected to be undirected, i.e. for every edge in the graph the reverse edge is also in the graph. If the graph is not undirected the output is arbitrary.
O(V+E). Build a graph from a list of edges.
Warning: This function will cause a runtime exception if a vertex in the edge
list is not within the given Bounds.
Examples
buildG (0,-1) [] == array (0,-1) []
buildG (0,2) [(0,1), (1,2)] == array (0,1) [(0,[1]),(1,[2])]
buildG (0,2) [(0,1), (0,2), (1,2)] == array (0,2) [(0,[2,1]),(1,[2]),(2,[])]O(V+E). The connected components of a graph.
Two vertices are connected if there is a path between them, traversing
edges in either direction.
O(V+E). A spanning forest of the graph, obtained from a depth-first
search of the graph starting from each vertex in an unspecified order.
O(V+E). A spanning forest of the part of the graph reachable from the
listed vertices, obtained from a depth-first search of the graph starting at
each of the listed vertices in order.
O(V+E). Returns the list of edges in the graph.
Examples
edges (buildG (0,-1) []) == []edges (buildG (0,2) [(0,1),(1,2)]) == [(0,1),(1,2)]The vertices of a strongly connected component.
The vertices of a list of strongly connected components.
O((V+E) \log V). Build a graph from a list of nodes uniquely identified
by keys, with a list of keys of nodes this node should have edges to.
This function takes an adjacency list representing a graph with vertices of
type key labeled by values of type node and produces a Graph-based
representation of that list. The Graph result represents the shape of the
graph, and the functions describe a) how to retrieve the label and adjacent
vertices of a given vertex, and b) how to retrieve a vertex given a key.
(graph, nodeFromVertex, vertexFromKey) = graphFromEdges edgeListgraph :: Graphis the raw, array based adjacency list for the graph.nodeFromVertex :: Vertex -> (node, key, [key])returns the node associated with the given 0-basedIntvertex; see warning below. This runs inO(1)time.vertexFromKey :: key -> Maybe Vertexreturns theIntvertex for the key if it exists in the graph,Nothingotherwise. This runs inO(\log V)time.
To safely use this API you must either extract the list of vertices directly
from the graph or first call vertexFromKey k to check if a vertex
corresponds to the key k. Once it is known that a vertex exists you can use
nodeFromVertex to access the labelled node and adjacent vertices. See below
for examples.
Note: The out-list may contain keys that don't correspond to nodes of the graph; they are ignored.
Warning: The nodeFromVertex function will cause a runtime exception if the
given Vertex does not exist.
Examples
An empty graph.
(graph, nodeFromVertex, vertexFromKey) = graphFromEdges []
graph = array (0,-1) []A graph where the out-list references unspecified nodes ('c'), these are
ignored.
(graph, _, _) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c'])]
array (0,1) [(0,[1]),(1,[])]A graph with 3 vertices: ("a") -> ("b") -> ("c")
(graph, nodeFromVertex, vertexFromKey) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c']), ("c", 'c', [])]
graph == array (0,2) [(0,[1]),(1,[2]),(2,[])]
nodeFromVertex 0 == ("a",'a',"b")
vertexFromKey 'a' == Just 0Get the label for a given key.
let getNodePart (n, _, _) = n
(graph, nodeFromVertex, vertexFromKey) = graphFromEdges [("a", 'a', ['b']), ("b", 'b', ['c']), ("c", 'c', [])]
getNodePart . nodeFromVertex <$> vertexFromKey 'a' == Just "A"O((V+E) \log V). Identical to graphFromEdges, except that the return
value does not include the function which maps keys to vertices. This
version of graphFromEdges is for backwards compatibility.
O(V+E). A table of the count of edges into each node.
Examples
indegree (buildG (0,-1) []) == array (0,-1) []indegree (buildG (0,2) [(0,1), (1,2)]) == array (0,2) [(0,0),(1,1),(2,1)]O(V+E). A table of the count of edges from each node.
Examples
outdegree (buildG (0,-1) []) == array (0,-1) []outdegree (buildG (0,2) [(0,1), (1,2)]) == array (0,2) [(0,1),(1,1),(2,0)]O(V+E). Returns the list of vertices reachable from a given vertex.
Examples
reachable (buildG (0,0) []) 0 == [0]reachable (buildG (0,2) [(0,1), (1,2)]) 0 == [0,1,2]O(V+E). The strongly connected components of a graph, in reverse
topological order.
Examples
scc (buildG (0,3) [(3,1),(1,2),(2,0),(0,1)])
== [Node {rootLabel = 0, subForest = [Node {rootLabel = 1, subForest = [Node {rootLabel = 2, subForest = []}]}]}
,Node {rootLabel = 3, subForest = []}]stronglyConnComp O((V+E) \log V). The strongly connected components of a directed graph,
reverse topologically sorted.
Examples
stronglyConnComp [("a",0,[1]),("b",1,[2,3]),("c",2,[1]),("d",3,[3])]
== [CyclicSCC ["d"],CyclicSCC ["b","c"],AcyclicSCC "a"]stronglyConnCompR O((V+E) \log V). The strongly connected components of a directed graph,
reverse topologically sorted. The function is the same as
stronglyConnComp, except that all the information about each node retained.
This interface is used when you expect to apply SCC to
(some of) the result of SCC, so you don't want to lose the
dependency information.
Examples
stronglyConnCompR [("a",0,[1]),("b",1,[2,3]),("c",2,[1]),("d",3,[3])]
== [CyclicSCC [("d",3,[3])],CyclicSCC [("b",1,[2,3]),("c",2,[1])],AcyclicSCC ("a",0,[1])]O(V+E). A topological sort of the graph.
The order is partially specified by the condition that a vertex i
precedes j whenever j is reachable from i but not vice versa.
Note: A topological sort exists only when there are no cycles in the graph. If the graph has cycles, the output of this function will not be a topological sort. In such a case consider using scc.
O(V+E). The graph obtained by reversing all edges.
Examples
transposeG (buildG (0,2) [(0,1), (1,2)]) == array (0,2) [(0,[]),(1,[0]),(2,[1])]O(V). Returns the list of vertices in the graph.
Examples
vertices (buildG (0,-1) []) == []vertices (buildG (0,2) [(0,1),(1,2)]) == [0,1,2]An edge from the first vertex to the second.
Strongly connected component.
Constructors
AcyclicSCC vertexA single vertex that is not in any cycle.
NECyclicSCC !(NonEmpty vertex)A maximal set of mutually reachable vertices.
Instances17Functor, Foldable, Traversable, Foldable1, Eq1, Read1, …
Functor SCCDefined in containers-0.7 · Data.GraphFoldable SCCDefined in containers-0.7 · Data.GraphTraversable SCCDefined in containers-0.7 · Data.GraphFoldable1 SCCDefined in containers-0.7 · Data.GraphEq1 SCCDefined in containers-0.7 · Data.GraphRead1 SCCDefined in containers-0.7 · Data.GraphShow1 SCCDefined in containers-0.7 · Data.GraphGeneric1 SCCDefined in containers-0.7 · Data.GraphLift vertex => Lift (SCC vertex)Defined in containers-0.7 · Data.GraphEq vertex => Eq (SCC vertex)Defined in containers-0.7 · Data.GraphData vertex => Data (SCC vertex)Defined in containers-0.7 · Data.GraphRead vertex => Read (SCC vertex)Defined in containers-0.7 · Data.GraphShow vertex => Show (SCC vertex)Defined in containers-0.7 · Data.GraphGeneric (SCC vertex)Defined in containers-0.7 · Data.GraphNFData a => NFData (SCC a)Defined in containers-0.7 · Data.Graphtype Rep (SCC vertex) = D1 ('MetaDataDefined in containers-0.7 · Data.Graph"SCC"
"Data.Graph"
"containers-0.7-1cc3"
'False) (C1 ('MetaCons"AcyclicSCC"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 vertex)) :+: C1 ('MetaCons"NECyclicSCC"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 (NonEmpty vertex))))type Rep1 SCC = D1 ('MetaDataDefined in containers-0.7 · Data.Graph"SCC"
"Data.Graph"
"containers-0.7-1cc3"
'False) (C1 ('MetaCons"AcyclicSCC"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1) :+: C1 ('MetaCons"NECyclicSCC"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec1 NonEmpty)))
Partial pattern synonym for backward compatibility with containers < 0.7.
Table indexed by a contiguous set of vertices.
Note: This is included for backwards compatibility.
Abstract representation of vertices.