HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

ModuleCabal-syntax-3.12.1.0Haskell2010

Distribution.Compat.Graph

A data type representing directed graphs, backed by Data.Graph. It is strict in the node type.

This is an alternative interface to Data.Graph. In this interface, nodes (identified by the IsNode type class) are associated with a key and record the keys of their neighbors. This interface is more convenient than Graph, which requires vertices to be explicitly handled by integer indexes.

The current implementation has somewhat peculiar performance characteristics. The asymptotics of all map-like operations mirror their counterparts in Data.Map. However, to perform a graph operation, we first must build the Data.Graph representation, an operation that takes O(V + E log V). However, this operation can be amortized across all queries on that particular graph.

Some nodes may be broken, i.e., refer to neighbors which are not stored in the graph. In our graph algorithms, we transparently ignore such edges; however, you can easily query for the broken vertices of a graph using broken (and should, e.g., to ensure that a closure of a graph is well-formed.) It's possible to take a closed subset of a broken graph and get a well-formed graph.

  • 3 types
  • 1 class
  • 26 values

Graph type

2 declarations
datadata Graph a
#

A graph of nodes a. The nodes are expected to have instance of class IsNode.

Instances7Foldable, Eq, Read, Show, NFData, Binary, …
classclass Ord (Key a) => IsNode a where
#

The IsNode class is used for datatypes which represent directed graph nodes. A node of type a is associated with some unique key of type Key a; given a node we can determine its key (nodeKey) and the keys of its neighbors (nodeNeighbors).

Associated types

  • type family Key a

Methods

Instances3IsNode

Query

4 declarations
valuesize :: Graph a -> Int
#

O(1). The number of nodes in the graph.

Construction

4 declarations

Combine

2 declarations

Graph algorithms

11 declarations
valuestronglyConnComp :: Graph a -> [SCC a]
#

Ω(V + E). Compute the strongly connected components of a graph. Requires amortized construction of graph.

datadata SCC vertex
#

Strongly connected component.

Constructors

Instances17Functor, Foldable, Traversable, Foldable1, Eq1, Read1, …
patternpattern CyclicSCC :: [vertex] -> SCC vertex
#

Partial pattern synonym for backward compatibility with containers < 0.7.

valuecycles :: Graph a -> [[a]]
#

Ω(V + E). Compute the cycles of a graph. Requires amortized construction of graph.

valuebroken :: Graph a -> [(a, [Key a])]
#

O(1). Return a list of nodes paired with their broken neighbors (i.e., neighbor keys which are not in the graph). Requires amortized construction of graph.

valueneighbors :: Graph a -> Key a -> Maybe [a]
#

Lookup the immediate neighbors from a key in the graph. Requires amortized construction of graph.

valuerevNeighbors :: Graph a -> Key a -> Maybe [a]
#

Lookup the immediate reverse neighbors from a key in the graph. Requires amortized construction of graph.

valueclosure :: Graph a -> [Key a] -> Maybe [a]
#

Compute the subgraph which is the closure of some set of keys. Returns Nothing if one (or more) keys are not present in the graph. Requires amortized construction of graph.

valuerevClosure :: Graph a -> [Key a] -> Maybe [a]
#

Compute the reverse closure of a graph from some set of keys. Returns Nothing if one (or more) keys are not present in the graph. Requires amortized construction of graph.

valuetopSort :: Graph a -> [a]
#

Topologically sort the nodes of a graph. Requires amortized construction of graph.

valuerevTopSort :: Graph a -> [a]
#

Reverse topologically sort the nodes of a graph. Requires amortized construction of graph.

Conversions

0 declarations

Maps

valuetoMap :: Graph a -> Map (Key a) a
#

O(1). Convert a graph into a map from keys to nodes. The resulting map m is guaranteed to have the property that all ((k,n) -> k == nodeKey n) (Data.Map.toList m).

Lists

valuetoList :: Graph a -> [a]
#

O(V). Convert a graph into a list of nodes.

valuekeys :: Graph a -> [Key a]
#

O(V). Convert a graph into a list of keys.

Sets

Graphs

Node type

2 declarations
datadata Node k a
#

A simple, trivial data type which admits an IsNode instance.

Constructors

  • N a k [k]
Instances5Functor, Eq, Show, IsNode, Key
  • Functor (Node k)Defined in Cabal-syntax-3.12.1.0 · Distribution.Compat.Graph
  • (Eq a, Eq k) => Eq (Node k a)Defined in Cabal-syntax-3.12.1.0 · Distribution.Compat.Graph
  • (Show a, Show k) => Show (Node k a)Defined in Cabal-syntax-3.12.1.0 · Distribution.Compat.Graph
  • Ord k => IsNode (Node k a)Defined in Cabal-syntax-3.12.1.0 · Distribution.Compat.Graph
  • type Key (Node k a) = kDefined in Cabal-syntax-3.12.1.0 · Distribution.Compat.Graph