package containers
Install
Dune Dependency
Authors
Maintainers
Sources
md5=d84e09c5d0abc501aa17cd502e31a038
sha512=8b832f4ada6035e80d81be0cfb7bdffb695ec67d465ed6097a144019e2b8a8f909095e78019c3da2d8181cc3cd730cd48f7519e87d3162442562103b7f36aabb
doc/containers.data/CCGraph/index.html
Module CCGraph
Source
Simple Graph Interface
A collections of algorithms on (mostly read-only) graph structures. The user provides her own graph structure as a ('v, 'e) CCGraph.t
, where 'v
is the type of vertices and 'e
the type of edges (for instance, 'e = ('v * 'v)
is perfectly fine in many cases).
Such a ('v, 'e) CCGraph.t
structure is a record containing three functions: two relate edges to their origin and destination, and one maps vertices to their outgoing edges. This abstract notion of graph makes it possible to run the algorithms on any user-specific type that happens to have a graph structure.
Many graph algorithms here take an iterator of vertices as input. The helper module Iter
contains basic functions for that, as does the iter
library on opam. If the user only has a single vertex (e.g., for a topological sort from a given vertex), they can use Iter.return x
to build a iter of one element.
status: unstable
Iter Helpers
A sequence of items of type 'a
, possibly infinite
Raised when a sequence meant to be used once is used several times.
Interfaces for graphs
This interface is designed for oriented graphs with labels on edges
Directed graph with vertices of type 'v
and edges labeled with e'
Make a graph by providing the children function.
Tags
Mutable tags from values of type 'v
to tags of type bool
Table
Mutable table with keys 'k
and values 'a
Bags of vertices
Bag of elements of type 'a
mk_heap ~leq
makes a priority queue where leq x y = true
means that x
is smaller than y
and should be prioritary.
Traversals
Cycles
is_dag ~graph vs
returns true
if the subset of graph
reachable from vs
is acyclic.
Topological Sort
val topo_sort :
eq:('v -> 'v -> bool) ->
?rev:bool ->
tbl:'v set ->
graph:('v, 'e) t ->
'v iter ->
'v list
topo_sort ~graph seq
returns a list of vertices l
where each element of l
is reachable from seq
. The list is sorted in a way such that if v -> v'
in the graph, then v
comes before v'
in the list (i.e. has a smaller index). Basically v -> v'
means that v
is smaller than v'
. See wikipedia.
val topo_sort_tag :
eq:('v -> 'v -> bool) ->
?rev:bool ->
tags:'v tag_set ->
graph:('v, 'e) t ->
'v iter ->
'v list
Same as topo_sort
but uses an explicit tag set.
Lazy Spanning Tree
spanning_tree ~graph v
computes a lazy spanning tree that has v
as a root. The table tbl
is used for the memoization part.
Strongly Connected Components
Strongly connected components reachable from the given vertices. Each component is a list of vertices that are all mutually reachable in the graph. The components are explored in a topological order (if C1 and C2 are components, and C1 points to C2, then C2 will be yielded before C1). Uses Tarjan's algorithm.
Pretty printing in the DOT (graphviz) format
Example (print divisors from 42
):
let open CCGraph in
let open Dot in
with_out "/tmp/truc.dot"
(fun out ->
pp ~attrs_v:(fun i -> [`Label (string_of_int i)]) ~graph:divisors_graph out 42
)
Mutable Graph
Make a new mutable graph from a Hashtbl. Edges are labelled with type 'a
.
Immutable Graph
A classic implementation of a graph structure on totally ordered vertices, with unlabelled edges. The graph allows to add and remove edges and vertices, and to iterate on edges and vertices.
Misc
of_list l
makes a graph from a list of pairs of vertices. Each pair (a,b)
is an edge from a
to b
.
of_hashtbl tbl
makes a graph from a hashtable that maps vertices to lists of children.
of_fun f
makes a graph out of a function that maps a vertex to the list of its children. The function is assumed to be deterministic.