From 0f972ba6a2519edf94f74ab2a0f5b3771812e057 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 3 Oct 2013 21:53:45 +0200 Subject: [PATCH] reverse collatz graph; also export Heap --- lazyGraph.ml | 12 +++++++++++- lazyGraph.mli | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lazyGraph.ml b/lazyGraph.ml index 32ada490..2eefe514 100644 --- a/lazyGraph.ml +++ b/lazyGraph.ml @@ -348,7 +348,7 @@ let a_star graph let dist, v' = Heap.pop h in (* data for this vertex *) let cell = nodes.map_get v' in - if (not cell.cf_explored) || ignore v' then begin + if not (cell.cf_explored || ignore v') then begin (* 'explore' the node *) on_explore v'; cell.cf_explored <- true; @@ -573,6 +573,16 @@ let collatz_graph = else Node (i, i, Sequence.singleton ((), i * 3 + 1)) in make force +let collatz_graph_bis = + let force i = + let l = + [ true, if i mod 2 = 0 then i/2 else i*3+1 + ; false, i * 2 ] @ + if i mod 3 = 1 then [false, (i-1)/3] else [] + in + Node (i, i, Sequence.of_list l) + in make force + let heap_graph = let force i = Node (i, i, Sequence.of_list [(), 2*i; (), 2*i+1]) diff --git a/lazyGraph.mli b/lazyGraph.mli index 10fc104f..e5be1036 100644 --- a/lazyGraph.mli +++ b/lazyGraph.mli @@ -129,6 +129,14 @@ val bfs : ('id, 'v, 'e) t -> 'id -> ('id * 'v * int) Sequence.t val dfs : ('id, 'v, 'e) t -> 'id -> ('id * 'v * int) Sequence.t (** Lazy traversal in depth first *) +module Heap : sig + type 'a t + val empty : cmp:('a -> 'a -> int) -> 'a t + val is_empty : _ t -> bool + val insert : 'a t -> 'a -> unit + val pop : 'a t -> 'a +end + val a_star : ('id, 'v, 'e) t -> ?on_explore:('id -> unit) -> ?ignore:('id -> bool) -> @@ -229,6 +237,12 @@ end val divisors_graph : (int, int, unit) t val collatz_graph : (int, int, unit) t + (** If [n] is even, [n] points to [n/2], otherwise to [3n+1] *) + +val collatz_graph_bis : (int, int, bool) t + (** Same as {! collatz_graph}, but also with reverse edges (n -> n*2, + and n -> (n-1)/3 if n mod 3 = 1. Direct edges annotated with [true], + reverse edges with [false] *) val heap_graph : (int, int, unit) t (** maps an integer i to 2*i and 2*i+1 *)