···1122+ - [Classic]: new functions [cycle] and [grid]
23 - [Eulerian]: Eulerian paths (new module)
34 currently limited to undirected graphs
45 - [Components]: strong articulation points (see functors [Connectivity]
+27
src/classic.ml
···19192020module type S = sig
2121 type graph
2222+ type vertex
2223 val divisors : int -> graph
2324 val de_bruijn : int -> graph
2425 val vertex_only : int -> graph
2526 val full : ?self:bool -> int -> graph
2727+ val cycle : int -> graph * vertex array
2828+ val grid : n:int -> m:int -> graph * vertex array array
2629end
27302831module Generic(B : Builder.INT) = struct
29323033 type graph = B.G.t
3434+3535+ type vertex = B.G.V.t
31363237 let divisors n =
3338 if n < 2 then invalid_arg "divisors";
···7782 (fun g j -> if self || i <> j then B.add_edge g v.(i) v.(j) else g)
7883 g)
7984 (fold_for 1 n (fun g i -> B.add_vertex g v.(i)) (B.empty ()))
8585+8686+ let cycle n =
8787+ if n < 0 then invalid_arg "cycle";
8888+ let v = Array.init n (fun i -> B.G.V.create i) in
8989+ let g = Array.fold_left B.add_vertex (B.empty ()) v in
9090+ let rec loop g i =
9191+ if i = n then g
9292+ else let g = B.add_edge g v.(i) v.((i+1) mod n) in loop g (i+1) in
9393+ loop g 0, v
9494+9595+ let grid ~n ~m =
9696+ if n < 0 || m < 0 then invalid_arg "grid";
9797+ let create i j = B.G.V.create (m * i + j) in
9898+ let v = Array.init n (fun i -> Array.init m (fun j -> create i j)) in
9999+ let g = Array.fold_left (Array.fold_left B.add_vertex) (B.empty ()) v in
100100+ let rec loop g i j =
101101+ if i = n then g
102102+ else if j = m then loop g (i+1) 0
103103+ else let g = if j < m-1 then B.add_edge g v.(i).(j) v.(i).(j+1) else g in
104104+ let g = if i < n-1 then B.add_edge g v.(i).(j) v.(i+1).(j) else g in
105105+ loop g i (j+1) in
106106+ loop g 0 0, v
8010781108end
82109
+18-4
src/classic.mli
···1515(* *)
1616(**************************************************************************)
17171818-(* $Id: classic.mli,v 1.12 2005-02-25 13:54:33 signoles Exp $ *)
1919-2018(** Some classic graphs *)
21192220module type S = sig
23212422 type graph
2323+2424+ type vertex
25252626 val divisors : int -> graph
2727 (** [divisors n] builds the graph of divisors.
···4545 The optional argument [self] indicates if loop edges should be added
4646 (default value is [true]). *)
47474848+ val cycle : int -> graph * vertex array
4949+ (** [cycle n] builds a graph that is a cycle with [n] vertices.
5050+ Vertices are labelled with [0,1,...,n-1] and there is an edge from
5151+ vertex [i] to vertex [(i+1) mod n].
5252+ Vertices are also returned in an array for convenience. *)
5353+5454+ val grid : n:int -> m:int -> graph * vertex array array
5555+ (** [grid n m] builds a grid graph with [n*m] vertices, with edges
5656+ from vertex [(i,j)] to vertices [(i+1,j)] and [(i,j+1)] (and no
5757+ wrapping around). Vertex [(i,j)] is labelled with [i*m+j].
5858+ Vertices are also returned in a [n*m] matrix for convenience. *)
5959+4860end
49615050-module P (G : Sig.P with type V.label = int) : S with type graph = G.t
6262+module P (G : Sig.P with type V.label = int) :
6363+ S with type graph = G.t and type vertex = G.V.t
5164(** Classic Persistent Graphs *)
52655353-module I (G : Sig.I with type V.label = int) : S with type graph = G.t
6666+module I (G : Sig.I with type V.label = int) :
6767+ S with type graph = G.t and type vertex = G.V.t
5468(** Classic Imperative Graphs *)
+12
src/sig_pack.mli
···344344 (** [full n] builds a graph with [n] vertices and all possible edges.
345345 The optional argument [self] indicates if loop edges should be added
346346 (default value is [true]). *)
347347+348348+ val cycle : int -> t * V.t array
349349+ (** [cycle n] builds a graph that is a cycle with [n] vertices.
350350+ Vertices are labelled with 0,1,...,n-1 and there is an edge from
351351+ vertex [i] to vertex [(i+1) mod n].
352352+ Vertices are also returned in an array for convenience. *)
353353+354354+ val grid : n:int -> m:int -> t * V.t array array
355355+ (** [grid n m] builds a grid graph with [n*m] vertices, with edges
356356+ from vertex [(i,j)] to vertices [(i+1,j)] and [(i,j+1)] (and no
357357+ wrapping around). Vertex [(i,j)] is labelled with [i*m+j].
358358+ Vertices are also returned in a [n*m] matrix for convenience. *)
347359 end
348360349361 (** Random graphs *)
+6
tests/test_eulerian.ml
···8888 assert c;
8989 assert (List.length p = (n+1)*(2*n+1))
9090 done
9191+9292+(* let () =
9393+ * let g, _ = Classic.grid ~n:2 ~m:3 in
9494+ * assert (exists_path g);
9595+ * assert (not (exists_cycle g)) *)
9696+