Module Pack.Digraph
Directed imperative graphs with edges and vertices labeled with integer.
Graph structure
module V : sig ... endVertices
type vertex= V.t
module E : sig ... endEdges
type edge= E.t
Graph constructors and destructors
val create : ?size:int -> unit -> tReturn an empty graph. Optionally, a size can be given, which should be on the order of the expected number of vertices that will be in the graph (for hash tables-based implementations). The graph grows as needed, so
sizeis just an initial guess.
val clear : t -> unitRemove all vertices and edges from the given graph.
- since
- ocamlgraph 1.4
val copy : t -> tcopy greturns a copy ofg. Vertices and edges (and eventually marks, see moduleMark) are duplicated.
val add_vertex : t -> V.t -> unitadd_vertex g vadds the vertexvfrom the graphg. Do nothing ifvis already ing.
val remove_vertex : t -> V.t -> unitremove g vremoves the vertexvfrom the graphg(and all the edges going fromving). Do nothing ifvis not ing.
val add_edge : t -> V.t -> V.t -> unitadd_edge g v1 v2adds an edge from the vertexv1to the vertexv2in the graphg. Add alsov1(resp.v2) ingifv1(resp.v2) is not ing. Do nothing if this edge is already ing.
val add_edge_e : t -> E.t -> unitadd_edge_e g eadds the edgeein the graphg. Add alsoE.src e(resp.E.dst e) ingifE.src e(resp.E.dst e) is not ing. Do nothing ifeis already ing.
val remove_edge : t -> V.t -> V.t -> unitremove_edge g v1 v2removes the edge going fromv1tov2from the graphg. Do nothing if this edge is not ing.- raises Invalid_argument
if
v1orv2are not ing.
val remove_edge_e : t -> E.t -> unitremove_edge_e g eremoves the edgeefrom the graphg. Do nothing ifeis not ing.- raises Invalid_argument
if
E.src eorE.dst eare not ing.
module Mark : sig ... endVertices contains integers marks, which can be set or used by some algorithms (see for instance module
Markingbelow)
Size functions
Membership functions
Successors and predecessors of a vertex
val succ : t -> V.t -> V.t listsucc g vreturns the successors ofving.- raises Invalid_argument
if
vis not ing.
val pred : t -> V.t -> V.t listpred g vreturns the predecessors ofving.- raises Invalid_argument
if
vis not ing.
Graph iterators
Vertex iterators
Each iterator iterator f v g iters f to the successors/predecessors of v in the graph g and raises Invalid_argument if v is not in g.
Basic operations
val find_vertex : t -> int -> V.tvertex g ireturns a vertex of labeliing. The behaviour is unspecified ifghas several vertices with labeli. Note: this function is inefficient (linear in the number of vertices); you should better keep the vertices as long as you create them.
val transitive_closure : ?reflexive:bool -> t -> ttransitive_closure ?reflexive greturns the transitive closure ofg(as a new graph). Loops (i.e. edges from a vertex to itself) are added only ifreflexiveistrue(default isfalse).
val add_transitive_closure : ?reflexive:bool -> t -> tadd_transitive_closure ?reflexive greplacesgby its transitive closure. Meaningless for persistent implementations (then acts astransitive_closure).
val transitive_reduction : ?reflexive:bool -> t -> ttransitive_reduction ?reflexive greturns the transitive reduction ofg(as a new graph). Loops (i.e. edges from a vertex to itself) are removed only ifreflexiveistrue(default isfalse).
val replace_by_transitive_reduction : ?reflexive:bool -> t -> treplace_by_transitive_reduction ?reflexive greplacesgby its transitive reduction. Meaningless for persistent implementations (then acts astransitive_reduction).
val mirror : t -> tmirror greturns a new graph which is the mirror image ofg: each edge fromutovhas been replaced by an edge fromvtou. For undirected graphs, it simply returns a copy ofg.
val complement : t -> tcomplement gbuilds a new graph which is the complement ofg: each edge present ingis not present in the resulting graph and vice-versa. Edges of the returned graph are unlabeled.
Traversal
module Dfs : sig ... endDepth-first search
module Bfs : sig ... endBreadth-first search
module Marking : sig ... endGraph traversal with marking
module Coloring : sig ... endColoring
Graph generators
module Classic : sig ... endClassic graphs
module Rand : sig ... endRandom graphs
module Components : sig ... endStrongly connected components
Classical algorithms
val shortest_path : t -> V.t -> V.t -> E.t list * intDijkstra's shortest path algorithm. Weights are the labels.
val bellman_ford : t -> V.t -> E.t listbellman_ford g vfinds a negative cycle fromv, and returns it, or raisesNot_foundif there is no such cycle
module PathCheck : sig ... endPath checking
module Topological : sig ... endTopological order
Input / Output
val dot_output : t -> string -> unitDOT output in a file
val display_with_gv : t -> unitDisplays the given graph using the external tools "dot" and "gv" and returns when gv's window is closed