···4040 module M = Map.Make(G.V)
4141 module S = Set.Make(G.V)
42424343- let contract prop g =
4343+ let contract' prop g =
4444 (* if the edge is to be removed (property = true):
4545 * make a union of the two union-sets of start and end node;
4646 * put this set in the map for all nodes in this set *)
···7373 (* find all closures *)
7474 let m = G.fold_edges_e f g m in
7575 (* rewrite the node numbers to close the gaps *)
7676- G.fold_edges_e (add m) g G.empty
7676+ G.fold_edges_e (add m) g G.empty, m
7777+7878+ let contract prop g = fst (contract' prop g)
77797880end
7981
+13-1
src/contraction.mli
···4040module Make
4141 (G : G) :
4242sig
4343+ module S : Set.S with type elt = G.vertex
4444+ module M : Map.S with type key = G.vertex
4545+4346 val contract : (G.E.t -> bool) -> G.t -> G.t
4447 (** [contract p g] will perform edge contraction on the graph [g].
4548 The edges for which the property [p] holds/is true will get contracted:
4649 The resulting graph will not have these edges; the start- and end-node
4747- of these edges will get united. *)
5050+ of these edges will get united. The result graph does not include nodes
5151+ with no incoming or outgoing edges. *)
5252+5353+ (** As for {!contract} but additionally returns a mapping that associates
5454+ each node in the original graph to the set of nodes with which it is
5555+ contracted in the result graph. The minimum element of each such set
5656+ is used as the representative of the set in the result graph. Nodes
5757+ with no incoming or outgoing edges are present in the mapping even if
5858+ they are omitted from the result graph. *)
5959+ val contract' : (G.E.t -> bool) -> G.t -> G.t * S.t M.t
4860end
4961
+19
tests/dune
···128128 (modules test_johnson)
129129 (libraries graph))
130130131131+;; Rules for the Contraction test
132132+133133+(rule
134134+ (with-stdout-to
135135+ test_contraction.output
136136+ (run ./test_contraction.exe)))
137137+138138+(rule
139139+ (alias runtest)
140140+ (action
141141+ (progn
142142+ (diff test_contraction.expected test_contraction.output)
143143+ (echo "test_contraction: all tests succeeded.\n"))))
144144+145145+(executable
146146+ (name test_contraction)
147147+ (modules test_contraction)
148148+ (libraries graph))
149149+131150;; Rules for the test_nontrivial_dom test
132151133152(rule