caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Radu Grigore <radugrigore@gmail.com>
To: Jon Harrop <jon@jdh30.plus.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Complexity of Set.union
Date: Fri, 25 Feb 2005 23:50:25 +0200	[thread overview]
Message-ID: <7f8e92aa0502251350202ec368@mail.gmail.com> (raw)
In-Reply-To: <200502251947.46657.jon@jdh30.plus.com>

On Fri, 25 Feb 2005 19:47:45 +0000, Jon Harrop <jon@jdh30.plus.com> wrote:

> I ask this because the STL set_union is probably O(n+N) (inserting an already
> sorted range into a set is apparently linear) which is worse than the O((n+N)
> log(n+N)) which you've suggested for OCaml.

The complexity of set_union is indeed O(n+N), see [0]. It is basically
a merge of sorted _sequences_ [1]. I assume n is the size of the small
set and N is the size of the small set and the heights are h=O(lg n),
H=O(lg N). With this the complexity of Set.union is more like O(n
lg(n+N)), at least when all elements in one set are smaller than the
elements of the other set.

> I see. This could be improved in the unsymmetric case, by adding elements from
> the smaller set to the larger set. But the size of the set isn't stored so
> you'd have to make do with adding elements from the shallower set to the
> deeper set. I've no idea what the complexity of that would be...

That is how it works now. As Xavier said the trickiest part is split.

> > Did you mean "of two equal height sets such that all elements of the
> > first set are smaller than all elements of the second set"?
> 
> Yes, that's what I meant. :-)

In that case the current Set.union simply adds elements repeatedly
from the set with small height to the set with big height.

> > That
> > could indeed run in constant time (just join the two sets with a
> > "Node" constructor), but I doubt the current implementation achieves
> > this because of the repeated splitting.

What splitting? I see none in this case.

> Having said that, wouldn't it take the Set.union function O(log n + log N)
> time to prove that the inputs are non-overlapping, because it would have to
> traverse to the min/max elements of both sets?

I agree. Also, such a check looks ugly to me (for a standard library).

-- 
regards,
 radu
http://rgrig.blogspot.com/

[0] http://library.n0i.net/programming/c/cp-iso/lib-algorithms.html#lib.set.union
[1] http://rgrig.blogspot.com/2004/11/merging-lists.html


  reply	other threads:[~2005-02-25 21:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-24  9:20 Set union Jon Harrop
2005-02-25 10:56 ` [Caml-list] " Radu Grigore
2005-02-25 17:30   ` Jon Harrop
2005-02-25 17:48     ` Xavier Leroy
2005-02-25 19:47       ` [Caml-list] Complexity of Set.union Jon Harrop
2005-02-25 21:50         ` Radu Grigore [this message]
2005-02-25 21:52           ` Radu Grigore
2005-02-25 22:31           ` Radu Grigore
2005-02-25 22:36           ` Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7f8e92aa0502251350202ec368@mail.gmail.com \
    --to=radugrigore@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@jdh30.plus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).