The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #151 from backtracking/kneser-graphs

Classic: new functions kneser and petersen

authored by

Jean-Christophe Filliatre and committed by
GitHub
e3530dc3 9e8c1d7c

+77
+2
CHANGES.md
··· 1 1 2 + - [Classic] new functions [kneser] and [petersen] to build Kneser's 3 + graphs and the Petersen graph 2 4 - [Oper] fixed transitive reduction (#145, reported by sim642) 3 5 and tests for transitive reduction! 4 6 - new example `depend2dot` to turn `make`-like dependencies
+24
src/classic.ml
··· 26 26 val full : ?self:bool -> int -> graph 27 27 val cycle : int -> graph * vertex array 28 28 val grid : n:int -> m:int -> graph * vertex array array 29 + val kneser : n:int -> k:int -> graph 30 + val petersen : unit -> graph 29 31 end 30 32 31 33 module Generic(B : Builder.INT) = struct ··· 104 106 let g = if i < n-1 then B.add_edge g v.(i).(j) v.(i+1).(j) else g in 105 107 loop g i (j+1) in 106 108 loop g 0 0, v 109 + 110 + let kneser ~n ~k = 111 + if n < 0 || k > n then invalid_arg "kneser"; 112 + let vert = Hashtbl.create (1 lsl n) in 113 + let add x = Hashtbl.add vert x (B.G.V.create x) in 114 + let rec visit mask n k = 115 + assert (0 <= k && k <= n); 116 + if k = 0 then add mask else 117 + if k = n then add (mask lor (1 lsl n - 1)) else ( 118 + let n = n - 1 in 119 + visit mask n k ; 120 + visit (mask lor (1 lsl n)) n (k - 1); 121 + ) in 122 + visit 0 n k; 123 + let g = Hashtbl.fold (fun _ v g -> B.add_vertex g v) vert (B.empty ()) in 124 + let g = Hashtbl.fold (fun i vi g -> 125 + Hashtbl.fold (fun j vj g -> 126 + if i land j = 0 then B.add_edge g vi vj else g) vert g) vert g in 127 + g 128 + 129 + let petersen () = 130 + kneser ~n:5 ~k:2 107 131 108 132 end 109 133
+10
src/classic.mli
··· 57 57 wrapping around). Vertex [(i,j)] is labelled with [i*m+j]. 58 58 Vertices are also returned in a [n*m] matrix for convenience. *) 59 59 60 + val kneser : n:int -> k:int -> graph 61 + (** [kneser n k] builds the Kneser graph K(n, k), where vertices 62 + correspond to the k-element subsets of a set of n elements, and 63 + where two vertices are adjacent if and only if the two 64 + corresponding sets are disjoint. *) 65 + 66 + val petersen : unit -> graph 67 + (** [petersen ()] builds the Petersen graph, that is isomorphic 68 + to the Kneser graph K(5,2). It has 10 vertices and 15 edges. *) 69 + 60 70 end 61 71 62 72 module P (G : Sig.P with type V.label = int) :
+10
src/sig_pack.mli
··· 356 356 from vertex [(i,j)] to vertices [(i+1,j)] and [(i,j+1)] (and no 357 357 wrapping around). Vertex [(i,j)] is labelled with [i*m+j]. 358 358 Vertices are also returned in a [n*m] matrix for convenience. *) 359 + 360 + val kneser : n:int -> k:int -> t 361 + (** [kneser n k] builds the Kneser graph K(n, k), where vertices 362 + correspond to the k-element subsets of a set of n elements, and 363 + where two vertices are adjacent if and only if the two 364 + corresponding sets are disjoint. *) 365 + 366 + val petersen : unit -> t 367 + (** [petersen ()] builds the Petersen graph, that is isomorphic 368 + to the Kneser graph K(5,2). It has 10 vertices and 15 edges. *) 359 369 end 360 370 361 371 (** Random graphs *)
+5
tests/dune
··· 9 9 (modules test_bfs)) 10 10 11 11 (test 12 + (name test_classic) 13 + (libraries graph) 14 + (modules test_classic)) 15 + 16 + (test 12 17 (name test_dfs) 13 18 (libraries graph) 14 19 (modules test_dfs))
+26
tests/test_classic.ml
··· 1 + (**************************************************************************) 2 + (* *) 3 + (* Ocamlgraph: a generic graph library for OCaml *) 4 + (* Copyright (C) 2004-2007 *) 5 + (* Sylvain Conchon, Jean-Christophe Filliatre and Julien Signoles *) 6 + (* *) 7 + (* This software is free software; you can redistribute it and/or *) 8 + (* modify it under the terms of the GNU Library General Public *) 9 + (* License version 2, with the special exception on linking *) 10 + (* described in file LICENSE. *) 11 + (* *) 12 + (* This software is distributed in the hope that it will be useful, *) 13 + (* but WITHOUT ANY WARRANTY; without even the implied warranty of *) 14 + (* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *) 15 + (* *) 16 + (**************************************************************************) 17 + 18 + open Graph.Pack.Graph 19 + 20 + let g = Classic.petersen () 21 + let () = assert (nb_vertex g = 10) 22 + let () = assert (nb_edges g = 15) 23 + let () = dot_output g "petersen.dot" 24 + 25 + let g = Classic.kneser ~n:7 ~k:3 26 + let () = dot_output g "k_7_3.dot"