caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien <Damien.Pous@ens-lyon.fr>
To: caml-list@inria.fr
Subject: [Caml-list] how to calculate a "xor"
Date: Fri, 5 Dec 2003 20:08:29 +0100	[thread overview]
Message-ID: <20031205200829.7e29a2c6.Damien.Pous@ens-lyon.fr> (raw)

Hi algorithmers,

Given two sets A and B, I want to calculate A\B _and_ B\A.
The sets are represented by lists.

without using an order to sort the lists, 
is there something better than the following (O(n^2)) ?

<<
let omem a = 
  let rec aux acc = function
    | [] -> None
    | a'::q when a=a' -> Some (List.rev_append acc q)
    | a'::q -> aux (a'::acc) q
  in aux []

let xor x = 
  let rec aux (a',b') = function
    | la, [] -> List.rev_append a' la, b'
    | [], lb -> a', List.rev_append b' lb
    | a::qa, b::qb when a=b -> aux (a',b') (qa,qb)
    | a::qa, b::qb -> match omem a qb, omem b qa with
	| None,     None     -> aux (a::a', b::b') (qa, qb )
	| Some qb', None     -> aux (   a', b::b') (qa, qb')
	| None,     Some qa' -> aux (a::a',    b') (qa',qb )
	| Some qb', Some qa' -> aux (   a',    b') (qa',qb')
  in aux ([],[]) x
>>

# xor ([4;1;6;2;8],[3;9;5;2;8;1;7]);;
- : int list * int list = ([6; 4], [3; 9; 5; 7])



with an order, is there something better than (O(n*ln n)) :
 * sort the two lists,
 * "merge" them to extract the result
?


what's the complexity of <Set.Make(M).diff>, from the standard library ?


thanks,
damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2003-12-05 19:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-05 19:08 Damien [this message]
2003-12-06  5:08 ` [Caml-list] " Alan Post
2003-12-10 20:37 ` [Caml-list] " Jean-Baptiste Rouquier

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=20031205200829.7e29a2c6.Damien.Pous@ens-lyon.fr \
    --to=damien.pous@ens-lyon.fr \
    --cc=caml-list@inria.fr \
    /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).