···1122+ - [Classic] new functions [kneser] and [petersen] to build Kneser's
33+ graphs and the Petersen graph
24 - [Oper] fixed transitive reduction (#145, reported by sim642)
35 and tests for transitive reduction!
46 - new example `depend2dot` to turn `make`-like dependencies
+24
src/classic.ml
···2626 val full : ?self:bool -> int -> graph
2727 val cycle : int -> graph * vertex array
2828 val grid : n:int -> m:int -> graph * vertex array array
2929+ val kneser : n:int -> k:int -> graph
3030+ val petersen : unit -> graph
2931end
30323133module Generic(B : Builder.INT) = struct
···104106 let g = if i < n-1 then B.add_edge g v.(i).(j) v.(i+1).(j) else g in
105107 loop g i (j+1) in
106108 loop g 0 0, v
109109+110110+ let kneser ~n ~k =
111111+ if n < 0 || k > n then invalid_arg "kneser";
112112+ let vert = Hashtbl.create (1 lsl n) in
113113+ let add x = Hashtbl.add vert x (B.G.V.create x) in
114114+ let rec visit mask n k =
115115+ assert (0 <= k && k <= n);
116116+ if k = 0 then add mask else
117117+ if k = n then add (mask lor (1 lsl n - 1)) else (
118118+ let n = n - 1 in
119119+ visit mask n k ;
120120+ visit (mask lor (1 lsl n)) n (k - 1);
121121+ ) in
122122+ visit 0 n k;
123123+ let g = Hashtbl.fold (fun _ v g -> B.add_vertex g v) vert (B.empty ()) in
124124+ let g = Hashtbl.fold (fun i vi g ->
125125+ Hashtbl.fold (fun j vj g ->
126126+ if i land j = 0 then B.add_edge g vi vj else g) vert g) vert g in
127127+ g
128128+129129+ let petersen () =
130130+ kneser ~n:5 ~k:2
107131108132end
109133
+10
src/classic.mli
···5757 wrapping around). Vertex [(i,j)] is labelled with [i*m+j].
5858 Vertices are also returned in a [n*m] matrix for convenience. *)
59596060+ val kneser : n:int -> k:int -> graph
6161+ (** [kneser n k] builds the Kneser graph K(n, k), where vertices
6262+ correspond to the k-element subsets of a set of n elements, and
6363+ where two vertices are adjacent if and only if the two
6464+ corresponding sets are disjoint. *)
6565+6666+ val petersen : unit -> graph
6767+ (** [petersen ()] builds the Petersen graph, that is isomorphic
6868+ to the Kneser graph K(5,2). It has 10 vertices and 15 edges. *)
6969+6070end
61716272module P (G : Sig.P with type V.label = int) :
+10
src/sig_pack.mli
···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. *)
359359+360360+ val kneser : n:int -> k:int -> t
361361+ (** [kneser n k] builds the Kneser graph K(n, k), where vertices
362362+ correspond to the k-element subsets of a set of n elements, and
363363+ where two vertices are adjacent if and only if the two
364364+ corresponding sets are disjoint. *)
365365+366366+ val petersen : unit -> t
367367+ (** [petersen ()] builds the Petersen graph, that is isomorphic
368368+ to the Kneser graph K(5,2). It has 10 vertices and 15 edges. *)
359369 end
360370361371 (** Random graphs *)
···11+(**************************************************************************)
22+(* *)
33+(* Ocamlgraph: a generic graph library for OCaml *)
44+(* Copyright (C) 2004-2007 *)
55+(* Sylvain Conchon, Jean-Christophe Filliatre and Julien Signoles *)
66+(* *)
77+(* This software is free software; you can redistribute it and/or *)
88+(* modify it under the terms of the GNU Library General Public *)
99+(* License version 2, with the special exception on linking *)
1010+(* described in file LICENSE. *)
1111+(* *)
1212+(* This software is distributed in the hope that it will be useful, *)
1313+(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
1414+(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)
1515+(* *)
1616+(**************************************************************************)
1717+1818+open Graph.Pack.Graph
1919+2020+let g = Classic.petersen ()
2121+let () = assert (nb_vertex g = 10)
2222+let () = assert (nb_edges g = 15)
2323+let () = dot_output g "petersen.dot"
2424+2525+let g = Classic.kneser ~n:7 ~k:3
2626+let () = dot_output g "k_7_3.dot"