HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

ModuleAgda-2.7.0.1Haskell2010

Agda.Utils.Graph.AdjacencyMap.Unidirectional

Directed graphs (can of course simulate undirected graphs).

Represented as adjacency maps in direction from source to target.

Each source node maps to an adjacency map of outgoing edges, which is a map from target nodes to edges.

Listed time complexities are for the worst case (and possibly amortised), with n standing for the number of nodes in the graph and e standing for the number of edges. Comparisons, predicates etc. are assumed to take constant time (unless otherwise stated).

  • 5 types
  • 61 values
  • PackageAgda-2.7.0.1
  • Exports66
  • LanguageHaskell2010
  • LicenceMIT
  • SourceUnidirectional.hs

Graphs and edges

3 declarations
newtypenewtype Graph n e
#

Graph n e is a type of directed graphs with nodes in n and edges in e.

At most one edge is allowed between any two nodes. Multigraphs can be simulated by letting the edge type e be a collection type.

The graphs are represented as adjacency maps (adjacency lists, but using finite maps instead of arrays and lists). This makes it possible to compute a node's outgoing edges in logarithmic time (O(log n)). However, computing the incoming edges may be more expensive.

Note that neither the number of nodes nor the number of edges may exceed maxBound :: Int.

Constructors

Instances9SetToInfty, Functor, Eq, Show, Pretty, PrettyTCM, …
  • (Ord r, Ord f) => SetToInfty f (ConGraph r f)Defined in Agda-2.7.0.1 · Agda.TypeChecking.SizedTypes.WarshallSolver
  • Functor (Graph n)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • (Eq n, Eq e) => Eq (Graph n e)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • (Ord n, Show n, Show e) => Show (Graph n e)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • (Ord n, Pretty n, Pretty e) => Pretty (Graph n e)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • (PrettyTCM n, PrettyTCMWithNode e) => PrettyTCM (Graph n e)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Pretty
  • (Monoid a, CombineNewOld a, Ord n) => CombineNewOld (Graph n a)Defined in Agda-2.7.0.1 · Agda.Termination.CallGraph
  • (Ord r, Ord f, Negative a) => Negative (Graph r f a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.SizedTypes.WarshallSolver

    A graph is negative if it contains a negative loop (diagonal edge). Makes sense on transitive graphs.

  • (Ord r, Ord f, Negative a) => Negative (Graphs r f a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.SizedTypes.WarshallSolver
datadata Edge n e
#

Edges.

Constructors

Instances12SetToInfty, Functor, Collection, Singleton, Eq, Ord, …
  • Eq f => SetToInfty f (Edge' r f a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.SizedTypes.WarshallSolver
  • Functor (Edge n)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • Collection (Call cinfo) (CallGraph cinfo)Defined in Agda-2.7.0.1 · Agda.Termination.CallGraph
  • Singleton (Call cinfo) (CallGraph cinfo)Defined in Agda-2.7.0.1 · Agda.Termination.CallGraph
  • (Eq n, Eq e) => Eq (Edge n e)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • (Ord n, Ord e) => Ord (Edge n e)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • (Show n, Show e) => Show (Edge n e)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • (Pretty n, Pretty e) => Pretty (Edge n e)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • (Ord r, Ord f, Dioid a) => Dioid (Edge' r f a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.SizedTypes.WarshallSolver
  • (Ord r, Ord f, MeetSemiLattice a) => MeetSemiLattice (Edge' r f a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.SizedTypes.WarshallSolver
  • (Ord r, Ord f, Top a) => Top (Edge' r f a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.SizedTypes.WarshallSolver
  • Negative a => Negative (Edge' r f a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.SizedTypes.WarshallSolver

    An edge is negative if its label is.

Queries

15 declarations
valuelookup :: Ord n => n -> n -> Graph n e -> Maybe e
#

If there is an edge from s to t, then lookup s t g is Just e, where e is the edge's label. O(log n).

valueneighbours :: Ord n => n -> Graph n e -> [(n, e)]
#

neighbours u g consists of all nodes v for which there is an edge from u to v in g, along with the corresponding edge labels. O(log n + |neighbours u g|).

valueneighboursMap :: Ord n => n -> Graph n e -> Map n e
#

neighboursMap u g consists of all nodes v for which there is an edge from u to v in g, along with the corresponding edge labels. O(log n).

valueedgesFrom :: Ord n => Graph n e -> [n] -> [Edge n e]
#

edgesFrom g ns is a list containing all edges originating in the given nodes (i.e., all outgoing edges for the given nodes). If ns does not contain duplicates, then the resulting list does not contain duplicates. O(|ns| log |n| + |edgesFrom g ns|).

valueedgesTo :: Ord n => Graph n e -> [n] -> [Edge n e]
#

edgesTo g ns is a list containing all edges ending in the given nodes (i.e., all incoming edges for the given nodes). If ns does not contain duplicates, then the resulting list does not contain duplicates. O(|ns| n log n).

valuediscrete :: Null e => Graph n e -> Bool
#

Checks whether the graph is discrete (containing no edges other than null edges). O(n + e).

Construction

14 declarations
valuefromNodes :: Ord n => [n] -> Graph n e
#

Constructs a completely disconnected graph containing the given nodes. O(n log n).

valuefromNodeSet :: Ord n => Set n -> Graph n e
#

Constructs a completely disconnected graph containing the given nodes. O(n).

valuefromEdges :: Ord n => [Edge n e] -> Graph n e
#

fromEdges es is a graph containing the edges in es, with the caveat that later edges overwrite earlier edges. O(|es| log n).

valuefromEdgesWith :: Ord n => (e -> e -> e) -> [Edge n e] -> Graph n e
#

fromEdgesWith f es is a graph containing the edges in es. Later edges are combined with earlier edges using the supplied function. O(|es| log n).

valueempty :: Graph n e
#

Empty graph (no nodes, no edges). O(1).

valuesingleton :: Ord n => n -> n -> e -> Graph n e
#

A graph with two nodes and a single connecting edge. O(1).

valueinsert :: Ord n => n -> n -> e -> Graph n e -> Graph n e
#

Inserts an edge into the graph. O(log n).

valueinsertWith
  1. :: Ord n
  2. => e -> e -> e
  3. -> n
  4. -> n
  5. -> e
  6. -> Graph n e
  7. -> Graph n e
#

insertWith f s t new inserts an edge from s to t into the graph. If there is already an edge from s to t with label old, then this edge gets replaced by an edge with label f new old, and otherwise the edge's label is new. O(log n).

valueunionWith :: Ord n => (e -> e -> e) -> Graph n e -> Graph n e -> Graph n e
#

Union. The function is used to combine edge labels for edges that occur in both graphs (labels from the first graph are given as the first argument to the function).

Time complexity: O(n₁ log (n₂n₁ + 1) + e₁ log e₂), where n₁/ is the number of nodes in the graph with the smallest number of nodes and n₂ is the number of nodes in the other graph, and e₁ is the number of edges in the graph with the smallest number of edges and e₂ is the number of edges in the other graph.

Less complicated time complexity: O((n + e) log n (where n and e refer to the resulting graph).

valueunions :: Ord n => [Graph n e] -> Graph n e
#

Union. O((n + e) log n (where n and e refer to the resulting graph).

valueunionsWith :: Ord n => (e -> e -> e) -> [Graph n e] -> Graph n e
#

Union. The function is used to combine edge labels for edges that occur in several graphs. O((n + e) log n (where n and e refer to the resulting graph).

Transformation

16 declarations
valuemapWithEdge :: (Edge n e -> e') -> Graph n e -> Graph n e'
#

A variant of fmap that provides extra information to the function argument. O(n + e).

valuetranspose :: Ord n => Graph n e -> Graph n e
#

The opposite graph (with all edges reversed). O((n + e) log n).

valueremoveNode :: Ord n => n -> Graph n e -> Graph n e
#

removeNode n g removes the node n (and all corresponding edges) from g. O(n + e).

valueremoveNodes :: Ord n => Set n -> Graph n e -> Graph n e
#

removeNodes ns g removes the nodes in ns (and all corresponding edges) from g. O((n + e) log |ns|).

valueremoveEdge :: Ord n => n -> n -> Graph n e -> Graph n e
#

removeEdge s t g removes the edge going from s to t, if any. O(log n).

valuefilterNodes :: Ord n => (n -> Bool) -> Graph n e -> Graph n e
#

The graph filterNodes p g contains exactly those nodes from g that satisfy the predicate p. Edges to or from nodes that are removed are also removed. O(n + e).

valuefilterNodesKeepingEdges
  1. :: (Ord n, SemiRing e)
  2. => n -> Bool
  3. -> Graph n e
  4. -> Graph n e
#

Removes the nodes that do not satisfy the predicate from the graph, but keeps the edges: if there is a path in the original graph between two nodes that are retained, then there is a path between these two nodes also in the resulting graph.

Precondition: The graph must be acyclic.

Worst-case time complexity: O(e n log n) (this has not been verified carefully).

valuerenameNodes :: Ord n2 => (n1 -> n2) -> Graph n1 e -> Graph n2 e
#

Renames the nodes.

Precondition: The renaming function must be injective.

Time complexity: O((n + e) log n).

valuerenameNodesMonotonic
  1. :: (Ord n1, Ord n2)
  2. => n1 -> n2
  3. -> Graph n1 e
  4. -> Graph n2 e
#

Renames the nodes.

Precondition: The renaming function ren must be strictly increasing (if x < y then ren x < ren y).

Time complexity: O(n + e).

datadata WithUniqueInt n
#

WithUniqueInt n consists of pairs of (unique) Ints and values of type n.

Values of this type are compared by comparing the Ints.

Instances5Functor, Eq, Ord, Show, Pretty
  • Functor WithUniqueIntDefined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • Eq (WithUniqueInt n)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • Ord (WithUniqueInt n)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • Show n => Show (WithUniqueInt n)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
  • Pretty n => Pretty (WithUniqueInt n)Defined in Agda-2.7.0.1 · Agda.Utils.Graph.AdjacencyMap.Unidirectional
valuecomposeWith
  1. :: Ord n
  2. => c -> d -> e
  3. -> e -> e -> e
  4. -> Graph n c
  5. -> Graph n d
  6. -> Graph n e
#

composeWith times plus g g' finds all edges s --c_i--> t_i --d_i--> u and constructs the result graph from edge(s,u) = sum_i (c_i times d_i).

Complexity: For each edge s --> t in g we look up all edges starting with t in g'.

Precondition: The two graphs must have exactly the same nodes.

Strongly connected components

8 declarations
valuesccs' :: Ord n => Graph n e -> [SCC n]
#

The graph's strongly connected components, in reverse topological order.

The time complexity is likely O(n + e log n) (but this depends on the, at the time of writing undocumented, time complexity of stronglyConnComp).

valuesccs :: Ord n => Graph n e -> [List1 n]
#

The graph's strongly connected components, in reverse topological order.

The time complexity is likely O(n + e log n) (but this depends on the, at the time of writing undocumented, time complexity of stronglyConnComp).

valuesccDAG'
  1. :: Ord n
  2. => Graph n e
  3. -> [SCC n]

    The graph's strongly connected components.

  4. -> DAG n
#

Constructs a DAG containing the graph's strongly connected components.

valuesccDAG :: Ord n => Graph n e -> DAG n
#

Constructs a DAG containing the graph's strongly connected components.

Reachability

4 declarations
valuereachableFrom :: Ord n => Graph n e -> n -> Map n (Int, [Edge n e])
#

reachableFrom g n is a map containing all nodes reachable from n in g. For each node a simple path to the node is given, along with its length (the number of edges). The paths are as short as possible (in terms of the number of edges).

Precondition: n must be a node in g. The number of nodes in the graph must not be larger than maxBound :: Int.

Amortised time complexity (assuming that comparisons take constant time): O(e log n), if the lists are not inspected. Inspection of a prefix of a list is linear in the length of the prefix.

valuereachableFromSet :: Ord n => Graph n e -> Set n -> Set n
#

reachableFromSet g ns is a set containing all nodes reachable from ns in g.

Precondition: Every node in ns must be a node in g. The number of nodes in the graph must not be larger than maxBound :: Int.

Amortised time complexity (assuming that comparisons take constant time): O((|ns| + e) log n).

valuewalkSatisfying
  1. :: Ord n
  2. => Edge n e -> Bool
  3. -> Edge n e -> Bool
  4. -> Graph n e
  5. -> n
  6. -> n
  7. -> Maybe [Edge n e]
#

walkSatisfying every some g from to determines if there is a walk from from to to in g, in which every edge satisfies the predicate every, and some edge satisfies the predicate some. If there are several such walks, then a shortest one (in terms of the number of edges) is returned.

Precondition: from and to must be nodes in g. The number of nodes in the graph must not be larger than maxBound :: Int.

Amortised time complexity (assuming that comparisons and the predicates take constant time to compute): O(n + e log n).

valuelongestPaths :: Ord n => Graph n e -> Graph n (Int, [[Edge n e]])
#

Constructs a graph g' with the same nodes as the original graph g. In g' there is an edge from n1 to n2 if and only if there is a (possibly empty) simple path from n1 to n2 in g. In that case the edge is labelled with all of the longest (in terms of numbers of edges) simple paths from n1 to n2 in g, as well as the lengths of these paths.

Precondition: The graph must be acyclic. The number of nodes in the graph must not be larger than maxBound :: Int.

Worst-case time complexity (if the paths are not inspected): O(e n log n) (this has not been verified carefully).

The algorithm is based on one found on Wikipedia.

Transitive closure

6 declarations
valuegaussJordanFloydWarshallMcNaughtonYamada
  1. :: (Ord n, Eq e, StarSemiRing e)
  2. => Graph n e
  3. -> (Graph n e, [SCC n])
#

Computes the transitive closure of the graph.

Uses the Gauss-Jordan-Floyd-Warshall-McNaughton-Yamada algorithm (as described by Russell O'Connor in "A Very General Method of Computing Shortest Paths" http://r6.ca/blog/20110808T035622Z.html), implemented using Graph, and with some shortcuts:

  • Zero edge differences are not added to the graph, thus avoiding some zero edges.

  • Strongly connected components are used to avoid computing some zero edges.

The graph's strongly connected components (in reverse topological order) are returned along with the transitive closure.

Computes the transitive closure of the graph.

Uses the Gauss-Jordan-Floyd-Warshall-McNaughton-Yamada algorithm (as described by Russell O'Connor in "A Very General Method of Computing Shortest Paths" http://r6.ca/blog/20110808T035622Z.html), implemented using matrices.

The resulting graph does not contain any zero edges.

This algorithm should be seen as a reference implementation. In practice gaussJordanFloydWarshallMcNaughtonYamada is likely to be more efficient.

valuetransitiveReduction :: Ord n => Graph n e -> Graph n ()
#

The transitive reduction of the graph: a graph with the same reachability relation as the graph, but with as few edges as possible.

Precondition: The graph must be acyclic. The number of nodes in the graph must not be larger than maxBound :: Int.

Worst-case time complexity: O(e n log n) (this has not been verified carefully).

The algorithm is based on one found on Wikipedia.

valuecompleteIter
  1. :: (Eq e, Null e, SemiRing e, Ord n)
  2. => Graph n e
  3. -> [(Graph n e, Graph n e)]
#

Version of complete that produces a list of intermediate results paired to the left with a difference that lead to the new intermediat result.

The last element in the list is the transitive closure, paired with the empty graph.

complete g = snd $ last $ completeIter g