| 1 |
module type Raw_graph = sig |
| 2 |
(** {1 Types} *) |
| 3 |
|
| 4 |
(** The graph data structure that is modified by the operations below. *) |
| 5 |
type graph |
| 6 |
|
| 7 |
(** The type of vertices of the graph. *) |
| 8 |
type vertex |
| 9 |
|
| 10 |
(** The type of marks (each vertex has an associated mark). *) |
| 11 |
type mark |
| 12 |
|
| 13 |
(** {1 Standard graph operations} *) |
| 14 |
|
| 15 |
(** NB: One must {e not} call [raw_add_edge] and [raw_add_vertex] manually, as |
| 16 |
it would break the internal invariants of the cycle detection algorithm. |
| 17 |
One must use instead the safe wrappers [add_vertex] and |
| 18 |
[add_edge_or_detect_cycle] that are provided as output of the |
| 19 |
[Incremental_cycles.Make] functor. *) |
| 20 |
|
| 21 |
(** [vertex_eq v1 v2] tests whether vertices [v1] and [v2] are equal. *) |
| 22 |
val vertex_eq : vertex -> vertex -> bool |
| 23 |
|
| 24 |
(** [get_outgoing g v] returns the list of successors of [v] in the graph. *) |
| 25 |
val get_outgoing : graph -> vertex -> vertex list |
| 26 |
|
| 27 |
(** [raw_add_edge g v w] inserts a new (directed) arc between vertices [v] and |
| 28 |
[w]. |
| 29 |
|
| 30 |
[v] and [w] must have been previously added to the graph using |
| 31 |
[raw_add_vertex], and the arc [v]->[w] must not already be in the graph. *) |
| 32 |
val raw_add_edge : graph -> vertex -> vertex -> unit |
| 33 |
|
| 34 |
(** [raw_add_vertex g v] inserts a new vertex [v] into the graph. |
| 35 |
|
| 36 |
- The mark of a new vertex must be some "default mark" which is different |
| 37 |
from all marks that can be returned by [new_mark]. |
| 38 |
- The level (returned by [get_level]) of a new vertex must be [1]. |
| 39 |
- The incoming vertices (returned by [get_incoming]) of a new vertex |
| 40 |
must be [\[\]] (the empty list). |
| 41 |
*) |
| 42 |
val raw_add_vertex : graph -> vertex -> unit |
| 43 |
|
| 44 |
|
| 45 |
(** {1 Operations on graph meta-data} *) |
| 46 |
|
| 47 |
(** [new_mark g] generates a fresh mark. |
| 48 |
|
| 49 |
More specifically, this mark must be different from all the marks |
| 50 |
previously returned by [new_mark g] (on the same graph [g]). It must also |
| 51 |
be different from all the marks currently associated to vertices of the |
| 52 |
graph [g]. *) |
| 53 |
val new_mark : graph -> mark |
| 54 |
|
| 55 |
(** [is_marked g v m] tests whether the vertex [v] has mark [m]. |
| 56 |
|
| 57 |
NB: [is_marked g v m] can only hold if [set_mark g v m] has been called |
| 58 |
previously. *) |
| 59 |
val is_marked : graph -> vertex -> mark -> bool |
| 60 |
|
| 61 |
(** [set_mark g v m] sets the mark of vertex [v] to be [m]. *) |
| 62 |
val set_mark : graph -> vertex -> mark -> unit |
| 63 |
|
| 64 |
(** [get_level g v] returns the level of node [v]. |
| 65 |
|
| 66 |
It is either the value previously set by [set_level], or the default value |
| 67 |
for a newly created vertex (i.e. [1], see [raw_add_vertex]). *) |
| 68 |
val get_level : graph -> vertex -> int |
| 69 |
|
| 70 |
(** [set_level g v l] sets the level of node [v] to be [l]. *) |
| 71 |
val set_level : graph -> vertex -> int -> unit |
| 72 |
|
| 73 |
(** [get_incoming g v] returns the list of "incoming" vertices of node [v]. |
| 74 |
|
| 75 |
It corresponds to either the default value for a newly created vertex |
| 76 |
(i.e. the empty list, see [raw_add_vertex]), or the result of previous |
| 77 |
calls to [clear_incoming] and [add_incoming]. *) |
| 78 |
val get_incoming : graph -> vertex -> vertex list |
| 79 |
|
| 80 |
(** [clear_incoming g v] sets the list of "incoming" vertices of [v] to be the |
| 81 |
empty list. *) |
| 82 |
val clear_incoming : graph -> vertex -> unit |
| 83 |
|
| 84 |
(** [add_incoming g v w] adds [w] to the list of "incoming" vertices of [v]. *) |
| 85 |
val add_incoming : graph -> vertex -> vertex -> unit |
| 86 |
|
| 87 |
(** [get_parent g v] returns the "parent" of node [v], as set by [set_parent]. |
| 88 |
|
| 89 |
{e Note: there is no default value for [get_parent]}. It is fine for |
| 90 |
[get_parent g v] to fail if it is called while [set_parent g v w] has not |
| 91 |
be called beforehand. *) |
| 92 |
val get_parent : graph -> vertex -> vertex |
| 93 |
|
| 94 |
(** [set_parent g v w] sets the "parent" of node [v] to be [w]. *) |
| 95 |
val set_parent : graph -> vertex -> vertex -> unit |
| 96 |
|
| 97 |
|
| 98 |
(** {1 Asymptotic complexity} *) |
| 99 |
|
| 100 |
(** All the operations provided by [Raw_graph] must run in constant time. *) |
| 101 |
end |
| 102 |
|
| 103 |
module type S = sig |
| 104 |
(** The [graph] type from [Raw_graph]. |
| 105 |
|
| 106 |
NB: one must always start from an empty [graph], and add vertices and edges |
| 107 |
using the functions [add_edge_or_detect_cycle] and [add_vertex] provided |
| 108 |
below. It is {e not} safe to use these functions on a graph manually |
| 109 |
constructed using the internal operations of [Raw_graph]. *) |
| 110 |
type graph |
| 111 |
|
| 112 |
(** The [vertex] type from [Raw_graph]. *) |
| 113 |
type vertex |
| 114 |
|
| 115 |
(** {1 Operations} *) |
| 116 |
|
| 117 |
(** The result of [add_edge_or_detect_cycle]. *) |
| 118 |
type add_edge_result = |
| 119 |
| EdgeAdded |
| 120 |
| EdgeCreatesCycle of (unit -> vertex list) |
| 121 |
|
| 122 |
(** [add_edge_or_detect_cycle g v w] adds the edge [v]->[w] to the graph [g], |
| 123 |
provided doing so does not make the graph cyclic. |
| 124 |
|
| 125 |
This assumes that [v] and [w] have previously been added to the graph |
| 126 |
using [add_vertex], and that the edge [v]->[w] is not already in the graph. |
| 127 |
|
| 128 |
- If adding the edge does not make the graph cyclic, the function returns |
| 129 |
[EdgeAdded], and updates the graph [g]. |
| 130 |
|
| 131 |
- If adding the edge would make the graph cyclic (i.e. there is currently |
| 132 |
a path from [w] to [v]), the function returns [EdgeCreatesCycle |
| 133 |
compute_cycle]. Then, [compute_cycle ()] can be called to get the list of |
| 134 |
vertices that form a path from [w] to [v] (in linear time). |
| 135 |
|
| 136 |
In the [EdgeCreatesCycle] case, the edge [v]->[w] is not inserted in the |
| 137 |
graph, but the internal invariants of the graph do not hold anymore. It is |
| 138 |
{e not} safe to call again [add_edge_or_detect_cycle] or [add_vertex] on |
| 139 |
the graph. |
| 140 |
*) |
| 141 |
val add_edge_or_detect_cycle : |
| 142 |
graph -> vertex -> vertex -> |
| 143 |
add_edge_result |
| 144 |
|
| 145 |
(** [add_vertex g v] adds the vertex [v] to the graph [g]. *) |
| 146 |
val add_vertex : graph -> vertex -> unit |
| 147 |
|
| 148 |
(** {1 Asymptotic complexity} *) |
| 149 |
|
| 150 |
(** Inserting [n] vertices and [m] edges (using [add_edge_or_detect_cycle] and |
| 151 |
[add_vertex]) has complexity [O(m * min(m^1/2, n^2/3) + n)]. |
| 152 |
|
| 153 |
Roughly speaking, in a sparse enough graph (for which this algorithm is |
| 154 |
optimized), this means that each edge insertion has amortized complexity |
| 155 |
[O(sqrt(m))] (as opposed to [O(m)] for a naive algorithm). |
| 156 |
*) |
| 157 |
end |