caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Fwd: interval trees
@ 2012-02-10  1:07 Francois Berenger
  2012-02-10 18:29 ` Richard W.M. Jones
  2012-02-11 10:54 ` Philippe Veber
  0 siblings, 2 replies; 20+ messages in thread
From: Francois Berenger @ 2012-02-10  1:07 UTC (permalink / raw)
  To: caml-list

-------- Original Message --------
Subject: interval trees
Date: Thu, 09 Feb 2012 17:30:21 +0900
From: Francois Berenger
To: batteries-discuss@lists.forge.ocamlcore.org
CC: biocaml@googlegroups.com

Hello,

I need to use an interval tree.

Biocaml has one, batteries have imap/iset, nice!

However, I have intervals of reals, not integers. :(

I want to build the tree (once), then query it with a real number
(many times) like in: which intervals contain the query real number?

Should I convert my floats to ints (by sorting them then ranking) before
inserting them into some existing interval tree for integers?
I am not so concerned about the pre-processing time.

Should I write from scratch?

Thanks for any suggestion,
F.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-10  1:07 [Caml-list] Fwd: interval trees Francois Berenger
@ 2012-02-10 18:29 ` Richard W.M. Jones
  2012-02-11 17:38   ` Goswin von Brederlow
  2012-02-11 10:54 ` Philippe Veber
  1 sibling, 1 reply; 20+ messages in thread
From: Richard W.M. Jones @ 2012-02-10 18:29 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list


On Fri, Feb 10, 2012 at 10:07:05AM +0900, Francois Berenger wrote:
> -------- Original Message --------
> Subject: interval trees
> Date: Thu, 09 Feb 2012 17:30:21 +0900
> From: Francois Berenger
> To: batteries-discuss@lists.forge.ocamlcore.org
> CC: biocaml@googlegroups.com
> 
> Hello,
> 
> I need to use an interval tree.
> 
> Biocaml has one, batteries have imap/iset, nice!
> 
> However, I have intervals of reals, not integers. :(
> 
> I want to build the tree (once), then query it with a real number
> (many times) like in: which intervals contain the query real number?
> 
> Should I convert my floats to ints (by sorting them then ranking) before
> inserting them into some existing interval tree for integers?
> I am not so concerned about the pre-processing time.
> 
> Should I write from scratch?

I wrote a segment tree (integers, not floats), which is similar.  It
wasn't very hard.  The code is here if it helps:

http://git.annexia.org/?p=virt-mem.git;a=blob;f=lib/virt_mem_mmap.ml;hb=HEAD

Rich.

-- 
Richard Jones
Red Hat

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-10  1:07 [Caml-list] Fwd: interval trees Francois Berenger
  2012-02-10 18:29 ` Richard W.M. Jones
@ 2012-02-11 10:54 ` Philippe Veber
  2012-02-11 17:33   ` Goswin von Brederlow
  1 sibling, 1 reply; 20+ messages in thread
From: Philippe Veber @ 2012-02-11 10:54 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1612 bytes --]

Hi François,

The Biocaml_intervalTree module is merely a specialization of Set in the
standard library. It should be fairly easy to functorize it over a type
with a total order relation. I think you might even sed -e 's/int/float/g'
the current implementation (with a couple of additional and minor
modifications).

ph.

2012/2/10 Francois Berenger <berenger@riken.jp>

> -------- Original Message --------
> Subject: interval trees
> Date: Thu, 09 Feb 2012 17:30:21 +0900
> From: Francois Berenger
> To: batteries-discuss@lists.forge.**ocamlcore.org<batteries-discuss@lists.forge.ocamlcore.org>
> CC: biocaml@googlegroups.com
>
> Hello,
>
> I need to use an interval tree.
>
> Biocaml has one, batteries have imap/iset, nice!
>
> However, I have intervals of reals, not integers. :(
>
> I want to build the tree (once), then query it with a real number
> (many times) like in: which intervals contain the query real number?
>
> Should I convert my floats to ints (by sorting them then ranking) before
> inserting them into some existing interval tree for integers?
> I am not so concerned about the pre-processing time.
>
> Should I write from scratch?
>
> Thanks for any suggestion,
> F.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/**wws/info/caml-list<https://sympa-roc.inria.fr/wws/info/caml-list>
> Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners<http://groups.yahoo.com/group/ocaml_beginners>
> Bug reports: http://caml.inria.fr/bin/caml-**bugs<http://caml.inria.fr/bin/caml-bugs>
>
>

[-- Attachment #2: Type: text/html, Size: 2244 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-11 10:54 ` Philippe Veber
@ 2012-02-11 17:33   ` Goswin von Brederlow
  0 siblings, 0 replies; 20+ messages in thread
From: Goswin von Brederlow @ 2012-02-11 17:33 UTC (permalink / raw)
  To: Philippe Veber; +Cc: Francois Berenger, caml-list

Philippe Veber <philippe.veber@gmail.com> writes:

> Hi François,
>
> The Biocaml_intervalTree module is merely a specialization of Set in the
> standard library. It should be fairly easy to functorize it over a type with a
> total order relation. I think you might even sed -e 's/int/float/g' the current
> implementation (with a couple of additional and minor modifications).
>
> ph.

Even better would be to polymorphize or functorize the code and send the
patch upstream.

MfG
        Goswin

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-10 18:29 ` Richard W.M. Jones
@ 2012-02-11 17:38   ` Goswin von Brederlow
  2012-02-11 17:49     ` Eliot Handelman
  2012-02-11 20:49     ` Edgar Friendly
  0 siblings, 2 replies; 20+ messages in thread
From: Goswin von Brederlow @ 2012-02-11 17:38 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: Francois Berenger, caml-list

"Richard W.M. Jones" <rich@annexia.org> writes:

> On Fri, Feb 10, 2012 at 10:07:05AM +0900, Francois Berenger wrote:
>> -------- Original Message --------
>> Subject: interval trees
>> Date: Thu, 09 Feb 2012 17:30:21 +0900
>> From: Francois Berenger
>> To: batteries-discuss@lists.forge.ocamlcore.org
>> CC: biocaml@googlegroups.com
>> 
>> Hello,
>> 
>> I need to use an interval tree.
>> 
>> Biocaml has one, batteries have imap/iset, nice!
>> 
>> However, I have intervals of reals, not integers. :(
>> 
>> I want to build the tree (once), then query it with a real number
>> (many times) like in: which intervals contain the query real number?
>> 
>> Should I convert my floats to ints (by sorting them then ranking) before
>> inserting them into some existing interval tree for integers?
>> I am not so concerned about the pre-processing time.
>> 
>> Should I write from scratch?
>
> I wrote a segment tree (integers, not floats), which is similar.  It
> wasn't very hard.  The code is here if it helps:
>
> http://git.annexia.org/?p=virt-mem.git;a=blob;f=lib/virt_mem_mmap.ml;hb=HEAD
>
> Rich.

Anyone have something like this but for non-overlapping intervals and
allowing interval insertion and removal with merging and spliting of the
internaly used intervals?

MfG
        Goswin

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-11 17:38   ` Goswin von Brederlow
@ 2012-02-11 17:49     ` Eliot Handelman
  2012-02-13  9:13       ` Sebastien Ferre
  2012-02-11 20:49     ` Edgar Friendly
  1 sibling, 1 reply; 20+ messages in thread
From: Eliot Handelman @ 2012-02-11 17:49 UTC (permalink / raw)
  To: caml-list

On 11/02/2012 12:38 PM, Goswin von Brederlow wrote:
>
> Anyone have something like this but for non-overlapping intervals and
> allowing interval insertion and removal with merging and spliting of the
> internaly used intervals?

Cis  from Sébastien Ferré?

http://www.irisa.fr/LIS/ferre/software.en.html

-- eliot

> MfG
>          Goswin
>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-11 17:38   ` Goswin von Brederlow
  2012-02-11 17:49     ` Eliot Handelman
@ 2012-02-11 20:49     ` Edgar Friendly
  2012-02-11 23:54       ` Philippe Veber
  1 sibling, 1 reply; 20+ messages in thread
From: Edgar Friendly @ 2012-02-11 20:49 UTC (permalink / raw)
  To: caml-list

On 02/11/2012 12:38 PM, Goswin von Brederlow wrote:
>> On Fri, Feb 10, 2012 at 10:07:05AM +0900, Francois Berenger wrote:
>>> I need to use an interval tree.
>>>
>>> Biocaml has one, batteries have imap/iset, nice!
>>>
> Anyone have something like this but for non-overlapping intervals and
> allowing interval insertion and removal with merging and spliting of the
> internaly used intervals?

Yes, IMap / ISet (borrowed from camomile and improved) do this.  I 
assume biocaml's is the same.

E.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-11 20:49     ` Edgar Friendly
@ 2012-02-11 23:54       ` Philippe Veber
  0 siblings, 0 replies; 20+ messages in thread
From: Philippe Veber @ 2012-02-11 23:54 UTC (permalink / raw)
  To: Edgar Friendly; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1310 bytes --]

2012/2/11 Edgar Friendly <thelema314@gmail.com>

> On 02/11/2012 12:38 PM, Goswin von Brederlow wrote:
>
>> On Fri, Feb 10, 2012 at 10:07:05AM +0900, Francois Berenger wrote:
>>>
>>>> I need to use an interval tree.
>>>>
>>>> Biocaml has one, batteries have imap/iset, nice!
>>>>
>>>>  Anyone have something like this but for non-overlapping intervals and
>> allowing interval insertion and removal with merging and spliting of the
>> internaly used intervals?
>>
>
> Yes, IMap / ISet (borrowed from camomile and improved) do this.  I assume
> biocaml's is the same.

Actually no, biocaml_intervalTree keeps the inserted intervals untouched,
it is in fact pretty similar to an interval multimap, with some specialized
operations. In cases when we want to describe a set of integers (vs a set
of intervals), we use ISet from Batteries. With these two structures we can
describe an interesting range of genome annotations.



>
>
> E.
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/**wws/info/caml-list<https://sympa-roc.inria.fr/wws/info/caml-list>
> Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners<http://groups.yahoo.com/group/ocaml_beginners>
> Bug reports: http://caml.inria.fr/bin/caml-**bugs<http://caml.inria.fr/bin/caml-bugs>
>
>

[-- Attachment #2: Type: text/html, Size: 2391 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-11 17:49     ` Eliot Handelman
@ 2012-02-13  9:13       ` Sebastien Ferre
  2012-02-15  1:28         ` Francois Berenger
  0 siblings, 1 reply; 20+ messages in thread
From: Sebastien Ferre @ 2012-02-13  9:13 UTC (permalink / raw)
  To: caml-list



On 02/11/2012 06:49 PM, Eliot Handelman wrote:
> On 11/02/2012 12:38 PM, Goswin von Brederlow wrote:
>>
>> Anyone have something like this but for non-overlapping intervals and
>> allowing interval insertion and removal with merging and spliting of the
>> internaly used intervals?
>
> Cis from Sébastien Ferré?
>
> http://www.irisa.fr/LIS/ferre/software.en.html

The Cis library (Cis for Compact Integer Sets) is
designed for representing sets of integers, but it
could easily be adapted to the insertion and
removal of intervals since it already handles
the merging and spliting og intervals.

Sébastien

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-13  9:13       ` Sebastien Ferre
@ 2012-02-15  1:28         ` Francois Berenger
  2012-02-15 15:21           ` Goswin von Brederlow
  0 siblings, 1 reply; 20+ messages in thread
From: Francois Berenger @ 2012-02-15  1:28 UTC (permalink / raw)
  To: caml-list; +Cc: biocaml

Hello,

I did a naive implementation of interval trees for float intervals.

It is available here:
https://github.com/HappyCrow/interval-tree

I wonder if it is possible to construct the trees in a tail recursive 
fashion. Maybe I knew how to do this when I was still at university.

Regards,
Francois.

On 02/13/2012 06:13 PM, Sebastien Ferre wrote:
>
>
> On 02/11/2012 06:49 PM, Eliot Handelman wrote:
>> On 11/02/2012 12:38 PM, Goswin von Brederlow wrote:
>>>
>>> Anyone have something like this but for non-overlapping intervals and
>>> allowing interval insertion and removal with merging and spliting of the
>>> internaly used intervals?
>>
>> Cis from Sébastien Ferré?
>>
>> http://www.irisa.fr/LIS/ferre/software.en.html
>
> The Cis library (Cis for Compact Integer Sets) is
> designed for representing sets of integers, but it
> could easily be adapted to the insertion and
> removal of intervals since it already handles
> the merging and spliting og intervals.
>
> Sébastien


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-15  1:28         ` Francois Berenger
@ 2012-02-15 15:21           ` Goswin von Brederlow
  2012-02-15 17:22             ` Edgar Friendly
  2012-02-16  2:32             ` Francois Berenger
  0 siblings, 2 replies; 20+ messages in thread
From: Goswin von Brederlow @ 2012-02-15 15:21 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list, biocaml

Francois Berenger <berenger@riken.jp> writes:

> Hello,
>
> I did a naive implementation of interval trees for float intervals.
>
> It is available here:
> https://github.com/HappyCrow/interval-tree
>
> I wonder if it is possible to construct the trees in a tail recursive
> fashion. Maybe I knew how to do this when I was still at university.
>
> Regards,
> Francois.

| Node of
   (* x_mid left_list right_list left_tree right_tree *)
      float * interval list * interval list * interval_tree * interval_tree

Why interval list? You only need a single interval in leafes and none in
other nodes (although it can help to store min and max in each node).

You are also missing insert and remove operations, which is the actually
hard part in this. Inserting an interval might require merging the
rightmost interval left of the root and the leftmost interval right of
the root. So you would have 2 removals and one insertion of a combined
interval, which complicates balancing the whole thing efficiently.

That is the part I'm struggling with.

MfG
        Goswin

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-15 15:21           ` Goswin von Brederlow
@ 2012-02-15 17:22             ` Edgar Friendly
  2012-02-16  2:48               ` Francois Berenger
  2012-02-16  2:32             ` Francois Berenger
  1 sibling, 1 reply; 20+ messages in thread
From: Edgar Friendly @ 2012-02-15 17:22 UTC (permalink / raw)
  To: Goswin von Brederlow; +Cc: Francois Berenger, caml-list, biocaml

[-- Attachment #1: Type: text/plain, Size: 2077 bytes --]

I struggled with this too, but if you read the wikipedia page
http://en.wikipedia.org/wiki/Interval_tree, he's implemented a centered
interval tree.  Yes, there's a lot of complications needed to insert and
remove, but what he's done works for static interval trees.

His lookup function is `int -> interval list`, not `int -> bool`, so he
must keep all the intervals that overlap the center point so he can return
them.  It's useful to have them sorted by left endpoint as well as right
endpoint.  I might have used arrays for them instead of lists so that
binary search is doable, but if they're small, it doesn't matter much.

E.

On Wed, Feb 15, 2012 at 10:21 AM, Goswin von Brederlow <goswin-v-b@web.de>wrote:

> Francois Berenger <berenger@riken.jp> writes:
>
> > Hello,
> >
> > I did a naive implementation of interval trees for float intervals.
> >
> > It is available here:
> > https://github.com/HappyCrow/interval-tree
> >
> > I wonder if it is possible to construct the trees in a tail recursive
> > fashion. Maybe I knew how to do this when I was still at university.
> >
> > Regards,
> > Francois.
>
> | Node of
>   (* x_mid left_list right_list left_tree right_tree *)
>      float * interval list * interval list * interval_tree * interval_tree
>
> Why interval list? You only need a single interval in leafes and none in
> other nodes (although it can help to store min and max in each node).
>
> You are also missing insert and remove operations, which is the actually
> hard part in this. Inserting an interval might require merging the
> rightmost interval left of the root and the leftmost interval right of
> the root. So you would have 2 removals and one insertion of a combined
> interval, which complicates balancing the whole thing efficiently.
>
> That is the part I'm struggling with.
>
> MfG
>         Goswin
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>

[-- Attachment #2: Type: text/html, Size: 3054 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-15 15:21           ` Goswin von Brederlow
  2012-02-15 17:22             ` Edgar Friendly
@ 2012-02-16  2:32             ` Francois Berenger
  2012-02-16  2:42               ` Francois Berenger
  1 sibling, 1 reply; 20+ messages in thread
From: Francois Berenger @ 2012-02-16  2:32 UTC (permalink / raw)
  To: caml-list

On 02/16/2012 12:21 AM, Goswin von Brederlow wrote:
> Francois Berenger<berenger@riken.jp>  writes:
>
>> Hello,
>>
>> I did a naive implementation of interval trees for float intervals.
>>
>> It is available here:
>> https://github.com/HappyCrow/interval-tree
>>
>> I wonder if it is possible to construct the trees in a tail recursive
>> fashion. Maybe I knew how to do this when I was still at university.
>>
>> Regards,
>> Francois.
>
> | Node of
>     (* x_mid left_list right_list left_tree right_tree *)
>        float * interval list * interval list * interval_tree * interval_tree
>
> Why interval list? You only need a single interval in leafes and none in
> other nodes (although it can help to store min and max in each node).

Not in my case, there is a payload associated with each interval in my 
application so I need to keep track of the individual intervals.

> You are also missing insert and remove operations,

I don't miss anything: I don't need these operations in my application.

 > which is the actually
> hard part in this. Inserting an interval might require merging the
> rightmost interval left of the root and the leftmost interval right of
> the root. So you would have 2 removals and one insertion of a combined
> interval, which complicates balancing the whole thing efficiently.

I hope you have a good book on this.
By the way, the one I refer to in my code is quite nice.
At first I thought it was too theoretical for me, but in fact
they give algorithms in recursive form so the book can become pretty handy.

> That is the part I'm struggling with.

You might be intersted into having a look at
the Computational Geometry Algorithms Library: http://www.cgal.org/
It's open source and it's also an INRIA product,
which makes two good points. ;)

You might want to bind your code to this library (they introduced
some framework recently, SWIG if I remember well, so that it should be 
easy to do wrappers for any language).
It's heavily templated C++ code, good luck if you read their code.

They have a lot of crazily useful data structures (interval skip lists, 
k-d trees, segment trees, range trees, AABB trees), and their
code is impossible to crash when using some specific math kernels.

Regards,
F.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-16  2:32             ` Francois Berenger
@ 2012-02-16  2:42               ` Francois Berenger
  2012-02-16  8:34                 ` Goswin von Brederlow
  2012-02-16 10:21                 ` Gabriel Scherer
  0 siblings, 2 replies; 20+ messages in thread
From: Francois Berenger @ 2012-02-16  2:42 UTC (permalink / raw)
  To: caml-list

Hello,

Anyone can translate this into being tail recursive
if it's possible:

let rec interval_tree intervals =
   match intervals with
       [] -> Empty
     | _ ->
         let x_mid = median intervals in
         let left, mid, right = partition intervals x_mid in
         let left_list = L.sort leftmost_bound_first mid in
         let right_list = L.sort rightmost_bound_first mid in
         Node (x_mid,
               left_list, right_list,
               interval_tree left, interval_tree right)

I'm afraid my program could crash on a very long list of intervals.

Thanks a lot,
F.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-15 17:22             ` Edgar Friendly
@ 2012-02-16  2:48               ` Francois Berenger
  0 siblings, 0 replies; 20+ messages in thread
From: Francois Berenger @ 2012-02-16  2:48 UTC (permalink / raw)
  To: caml-list

On 02/16/2012 02:22 AM, Edgar Friendly wrote:
> I struggled with this too, but if you read the wikipedia page
> http://en.wikipedia.org/wiki/Interval_tree, he's implemented a centered
> interval tree.  Yes, there's a lot of complications needed to insert and
> remove, but what he's done works for static interval trees.
>
> His lookup function is `int -> interval list`

Precisely, it is:
type: interval_tree -> float -> interval list

 > , not `int -> bool`, so he
> must keep all the intervals that overlap the center point so he can
> return them.  It's useful to have them sorted by left endpoint as well
> as right endpoint.  I might have used arrays for them instead of lists
> so that binary search is doable,

That would be a nice optimization for queries indeed.

At the moment, I'm more concerned about the program blowing up
during tree construction.

Regards,
F.

 > but if they're small, it doesn't matter
> much.
>
> E.
>
> On Wed, Feb 15, 2012 at 10:21 AM, Goswin von Brederlow
> <goswin-v-b@web.de <mailto:goswin-v-b@web.de>> wrote:
>
>     Francois Berenger <berenger@riken.jp <mailto:berenger@riken.jp>> writes:
>
>      > Hello,
>      >
>      > I did a naive implementation of interval trees for float intervals.
>      >
>      > It is available here:
>      > https://github.com/HappyCrow/interval-tree
>      >
>      > I wonder if it is possible to construct the trees in a tail recursive
>      > fashion. Maybe I knew how to do this when I was still at university.
>      >
>      > Regards,
>      > Francois.
>
>     | Node of
>        (* x_mid left_list right_list left_tree right_tree *)
>           float * interval list * interval list * interval_tree *
>     interval_tree
>
>     Why interval list? You only need a single interval in leafes and none in
>     other nodes (although it can help to store min and max in each node).
>
>     You are also missing insert and remove operations, which is the actually
>     hard part in this. Inserting an interval might require merging the
>     rightmost interval left of the root and the leftmost interval right of
>     the root. So you would have 2 removals and one insertion of a combined
>     interval, which complicates balancing the whole thing efficiently.
>
>     That is the part I'm struggling with.
>
>     MfG
>             Goswin
>
>     --
>     Caml-list mailing list.  Subscription management and archives:
>     https://sympa-roc.inria.fr/wws/info/caml-list
>     Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>     Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-16  2:42               ` Francois Berenger
@ 2012-02-16  8:34                 ` Goswin von Brederlow
  2012-02-16 12:20                   ` John Carr
  2012-02-16 10:21                 ` Gabriel Scherer
  1 sibling, 1 reply; 20+ messages in thread
From: Goswin von Brederlow @ 2012-02-16  8:34 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list

Francois Berenger <berenger@riken.jp> writes:

> Hello,
>
> Anyone can translate this into being tail recursive
> if it's possible:
>
> let rec interval_tree intervals =
>   match intervals with
>       [] -> Empty
>     | _ ->
>         let x_mid = median intervals in
>         let left, mid, right = partition intervals x_mid in
>         let left_list = L.sort leftmost_bound_first mid in
>         let right_list = L.sort rightmost_bound_first mid in
>         Node (x_mid,
>               left_list, right_list,
>               interval_tree left, interval_tree right)
>
> I'm afraid my program could crash on a very long list of intervals.
>
> Thanks a lot,
> F.

Each recursion halves the lists, right? That means you need a very very
very long list to exceed the recursion depth.

MfG
        Goswin

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-16  2:42               ` Francois Berenger
  2012-02-16  8:34                 ` Goswin von Brederlow
@ 2012-02-16 10:21                 ` Gabriel Scherer
  2012-02-17  0:59                   ` Francois Berenger
  1 sibling, 1 reply; 20+ messages in thread
From: Gabriel Scherer @ 2012-02-16 10:21 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list

I can't resist giving the usual tail-recursive CPS-transformed version
(untested):

let interval_tree intervals =
  let rec interval_tree intervals k =
   match intervals with
     | [] -> k Empty
     | _ ->
         let x_mid = median intervals in
         let left, mid, right = partition intervals x_mid in
         let left_list = L.sort leftmost_bound_first mid in
         let right_list = L.sort rightmost_bound_first mid in
         interval_tree left (fun left_tree ->
           interval_tree right (fun right_tree ->
             k (Node(x_mid, left_list, right_list, left_tree, right_tree))))
  in interval_tree intervals (fun t -> t)

But see Goswin's remark: if non-tailrec makes your stack grow in
log(n) only, there is no point in jumping through hoops to get a
tail-recursive version.

On Thu, Feb 16, 2012 at 3:42 AM, Francois Berenger <berenger@riken.jp> wrote:
> Hello,
>
> Anyone can translate this into being tail recursive
> if it's possible:
>
> let rec interval_tree intervals =
>  match intervals with
>      [] -> Empty
>    | _ ->
>        let x_mid = median intervals in
>        let left, mid, right = partition intervals x_mid in
>        let left_list = L.sort leftmost_bound_first mid in
>        let right_list = L.sort rightmost_bound_first mid in
>        Node (x_mid,
>              left_list, right_list,
>              interval_tree left, interval_tree right)
>
> I'm afraid my program could crash on a very long list of intervals.
>
> Thanks a lot,
>
> F.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-16  8:34                 ` Goswin von Brederlow
@ 2012-02-16 12:20                   ` John Carr
  0 siblings, 0 replies; 20+ messages in thread
From: John Carr @ 2012-02-16 12:20 UTC (permalink / raw)
  To: Goswin von Brederlow; +Cc: Francois Berenger, caml-list


Goswin von Brederlow <goswin-v-b@web.de> wrote:

> Francois Berenger <berenger@riken.jp> writes:
> >
> > let rec interval_tree intervals =
> >   match intervals with
> >       [] -> Empty
> >     | _ ->
> >         let x_mid = median intervals in
> >         let left, mid, right = partition intervals x_mid in
> >         let left_list = L.sort leftmost_bound_first mid in
> >         let right_list = L.sort rightmost_bound_first mid in
> >         Node (x_mid,
> >               left_list, right_list,
> >               interval_tree left, interval_tree right)
> >
> > I'm afraid my program could crash on a very long list of intervals.
> 
> Each recursion halves the lists, right? That means you need a very very
> very long list to exceed the recursion depth.

How good is median selection?  If it works like quicksort, approximating
the median, typical stack depth is logarithmic and worst case stack
depth is linear.  If median selection is precise, creating a balanced
tree, I wouldn't worry about stack depth.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-16 10:21                 ` Gabriel Scherer
@ 2012-02-17  0:59                   ` Francois Berenger
  2012-02-17  8:11                     ` Christophe Raffalli
  0 siblings, 1 reply; 20+ messages in thread
From: Francois Berenger @ 2012-02-17  0:59 UTC (permalink / raw)
  To: caml-list

On 02/16/2012 07:21 PM, Gabriel Scherer wrote:
> I can't resist giving the usual tail-recursive CPS-transformed version
> (untested):

Thanks! That's the technique I was looking for
(Continuation Passing Style), as I may have to use
this on some other algorithms in the future.

> let interval_tree intervals =
>    let rec interval_tree intervals k =
>     match intervals with
>       | [] ->  k Empty
>       | _ ->
>           let x_mid = median intervals in
>           let left, mid, right = partition intervals x_mid in
>           let left_list = L.sort leftmost_bound_first mid in
>           let right_list = L.sort rightmost_bound_first mid in
>           interval_tree left (fun left_tree ->
>             interval_tree right (fun right_tree ->
>               k (Node(x_mid, left_list, right_list, left_tree, right_tree))))
>    in interval_tree intervals (fun t ->  t)
>
> But see Goswin's remark: if non-tailrec makes your stack grow in
> log(n) only, there is no point in jumping through hoops to get a
> tail-recursive version.
>
> On Thu, Feb 16, 2012 at 3:42 AM, Francois Berenger<berenger@riken.jp>  wrote:
>> Hello,
>>
>> Anyone can translate this into being tail recursive
>> if it's possible:
>>
>> let rec interval_tree intervals =
>>   match intervals with
>>       [] ->  Empty
>>     | _ ->
>>         let x_mid = median intervals in
>>         let left, mid, right = partition intervals x_mid in
>>         let left_list = L.sort leftmost_bound_first mid in
>>         let right_list = L.sort rightmost_bound_first mid in
>>         Node (x_mid,
>>               left_list, right_list,
>>               interval_tree left, interval_tree right)
>>
>> I'm afraid my program could crash on a very long list of intervals.
>>
>> Thanks a lot,
>>
>> F.
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa-roc.inria.fr/wws/info/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [Caml-list] Fwd: interval trees
  2012-02-17  0:59                   ` Francois Berenger
@ 2012-02-17  8:11                     ` Christophe Raffalli
  0 siblings, 0 replies; 20+ messages in thread
From: Christophe Raffalli @ 2012-02-17  8:11 UTC (permalink / raw)
  To: caml-list

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Hello,

> 
> Thanks! That's the technique I was looking for
> (Continuation Passing Style), as I may have to use
> this on some other algorithms in the future.
> 
>> let interval_tree intervals =
>>    let rec interval_tree intervals k =
>>     match intervals with
>>       | [] ->  k Empty
>>       | _ ->
>>           let x_mid = median intervals in
>>           let left, mid, right = partition intervals x_mid in
>>           let left_list = L.sort leftmost_bound_first mid in
>>           let right_list = L.sort rightmost_bound_first mid in
>>           interval_tree left (fun left_tree ->
>>             interval_tree right (fun right_tree ->
>>               k (Node(x_mid, left_list, right_list, left_tree,
>> right_tree))))
>>    in interval_tree intervals (fun t ->  t)

There is still one non tail-rec call, and this will basically rebuild
the stack in the heap, with more GC work. So unless you know your trees
are non balanced and left branch are deeper, this code is probably worst.

In the old days (I think now OCaml is clever), It could save some stack
space (and this could retain some pointer), to do the last call by
another function :

let rec interval_tree intervals =
   match intervals with
       [] ->  Empty
     | _ ->
         let x_mid = median intervals in
         let left, mid, right = partition intervals x_mid in
         let left_list = L.sort leftmost_bound_first mid in
         let right_list = L.sort rightmost_bound_first mid in
         last_call x_mid  left_list right_list left right

and last_call x_mid  left_list right_list left right =
         Node (x_mid,
               left_list, right_list,
               interval_tree left, interval_tree right)

Here it makes sure mid and intervals are not stored in the stack frame
... But it may be done anyway now, I am not sure.

Someone knowns the current rules to know what's retain on the stack in
today OCaml ?

Cheers,
Christophe

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk8+C8MACgkQi9jr/RgYAS4nHwCfYX9mY8nHAkz65QQXDerPwfHd
tvgAoIjvVxlDONYRjwpSZS0/5LhhCo44
=OnkD
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2012-02-17  8:13 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-10  1:07 [Caml-list] Fwd: interval trees Francois Berenger
2012-02-10 18:29 ` Richard W.M. Jones
2012-02-11 17:38   ` Goswin von Brederlow
2012-02-11 17:49     ` Eliot Handelman
2012-02-13  9:13       ` Sebastien Ferre
2012-02-15  1:28         ` Francois Berenger
2012-02-15 15:21           ` Goswin von Brederlow
2012-02-15 17:22             ` Edgar Friendly
2012-02-16  2:48               ` Francois Berenger
2012-02-16  2:32             ` Francois Berenger
2012-02-16  2:42               ` Francois Berenger
2012-02-16  8:34                 ` Goswin von Brederlow
2012-02-16 12:20                   ` John Carr
2012-02-16 10:21                 ` Gabriel Scherer
2012-02-17  0:59                   ` Francois Berenger
2012-02-17  8:11                     ` Christophe Raffalli
2012-02-11 20:49     ` Edgar Friendly
2012-02-11 23:54       ` Philippe Veber
2012-02-11 10:54 ` Philippe Veber
2012-02-11 17:33   ` Goswin von Brederlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).