mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
reverse collatz graph; also export Heap
This commit is contained in:
parent
d828eca3f6
commit
0f972ba6a2
2 changed files with 25 additions and 1 deletions
12
lazyGraph.ml
12
lazyGraph.ml
|
|
@ -348,7 +348,7 @@ let a_star graph
|
||||||
let dist, v' = Heap.pop h in
|
let dist, v' = Heap.pop h in
|
||||||
(* data for this vertex *)
|
(* data for this vertex *)
|
||||||
let cell = nodes.map_get v' in
|
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 *)
|
(* 'explore' the node *)
|
||||||
on_explore v';
|
on_explore v';
|
||||||
cell.cf_explored <- true;
|
cell.cf_explored <- true;
|
||||||
|
|
@ -573,6 +573,16 @@ let collatz_graph =
|
||||||
else Node (i, i, Sequence.singleton ((), i * 3 + 1))
|
else Node (i, i, Sequence.singleton ((), i * 3 + 1))
|
||||||
in make force
|
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 heap_graph =
|
||||||
let force i =
|
let force i =
|
||||||
Node (i, i, Sequence.of_list [(), 2*i; (), 2*i+1])
|
Node (i, i, Sequence.of_list [(), 2*i; (), 2*i+1])
|
||||||
|
|
|
||||||
|
|
@ -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
|
val dfs : ('id, 'v, 'e) t -> 'id -> ('id * 'v * int) Sequence.t
|
||||||
(** Lazy traversal in depth first *)
|
(** 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 ->
|
val a_star : ('id, 'v, 'e) t ->
|
||||||
?on_explore:('id -> unit) ->
|
?on_explore:('id -> unit) ->
|
||||||
?ignore:('id -> bool) ->
|
?ignore:('id -> bool) ->
|
||||||
|
|
@ -229,6 +237,12 @@ end
|
||||||
val divisors_graph : (int, int, unit) t
|
val divisors_graph : (int, int, unit) t
|
||||||
|
|
||||||
val collatz_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
|
val heap_graph : (int, int, unit) t
|
||||||
(** maps an integer i to 2*i and 2*i+1 *)
|
(** maps an integer i to 2*i and 2*i+1 *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue