caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: fa.caml@googlegroups.com
Cc: caml-list@inria.fr, Jean-Christophe.Filliatre@lri.fr
Subject: Re: [Caml-list] Priority queues
Date: Sat, 2 Jul 2011 16:54:27 -0400 (EDT)	[thread overview]
Message-ID: <alpine.DEB.2.02.1107021642370.25683@sergyar> (raw)
In-Reply-To: <0bb28c99-9ffd-4b5d-b073-8aff7af00493@glegroupsg2000goo.googlegroups.com>



On Fri, 1 Jul 2011, Radu Grigore wrote:

> On Friday, July 1, 2011 11:33:11 AM UTC+1, Andrew wrote:
>>> - or your priority queue does not provide such an operation, and you
>>> simply add another entry for y in the priority queue, with a different
>>> key. It means you have now several entries for y in the priority queue.
>>> The better will be extracted first; the others will be ignored when they
>>> are extracted later. Complexity is now O(E log(V)).
>>
>> Just an extra question though: How come it's not O(E log (E))?
>> You could end up pushing as much as one new element in
>> your heap per edge, couldn't you?
>
> If you use a Set of (distance, vertex) pairs together with min_elt then 
> you can simulate decrease-key using remove followed by add.

You could also keep a map of vertices to distances, so you can update the 
distance of a vertex that is not the minimum element without knowing what 
it's previous distance was.  Something like:

module PQ(Key: Map.Ordered)(Data: Map.Ordered) = struct
 	module X = struct
       		type t = Key.t * Data.t;;
 	        let compare (k1, d1) (k2, d2) =
 			let c = Key.compare k1 k2 in
 			if (c != 0) then
 				c
 			else
 				Data.compare d1 d2
 	end
 	module Y = Set.make(X)
 	module Z = Map.make(Data)

 	type t = Y.t * (Key.t Z.t)

 	let empty : t = Y.empty, Z.empty

 	let add (s, m) k d =
 		try
 			let k' = Z.find d m in
 			let s = Y.remove (k', d) s in
 			let s = Y.add (k, d) s in
 			let m = Z.add d k m in
 			(s, m)
 		with
 		| Not_found ->
 			let s = Y.add (k, s) s in
 			let m = Z.add d k m in
 			(s, m)

 	let head (s, _) -> snd (Y.min_elt s)

end;;

All the other operations should be obvious.

Brian


>
> -- 
> 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
>
>

  reply	other threads:[~2011-07-02 20:54 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <fa.zXwbS6BNVmuh5Yg3lR+NAiHb7b8@ifi.uio.no>
2011-07-01 22:37 ` Radu Grigore
2011-07-02 20:54   ` Brian Hurt [this message]
2011-06-30 11:30 Andrew
2011-06-30 11:40 ` Török Edwin
2011-06-30 11:56   ` Andrew
2011-06-30 12:13     ` Wojciech Meyer
2011-06-30 12:34       ` Andrew
2011-06-30 12:43         ` Wojciech Meyer
2011-06-30 17:29           ` Christophe Raffalli
2011-06-30 17:45             ` Christophe Raffalli
2011-06-30 12:28     ` Guillaume Yziquel
2011-06-30 12:33 ` Jean-Christophe Filliâtre
2011-06-30 13:19   ` Michael Ekstrand
2011-06-30 14:07     ` Alexandre Pilkiewicz
2011-06-30 14:20       ` Michael Ekstrand
2011-06-30 14:22       ` David Rajchenbach-Teller
2011-06-30 14:29         ` Wojciech Meyer
2011-06-30 17:11           ` Andrew
2011-06-30 22:51             ` Wojciech Meyer
2011-07-01  5:06               ` Andrew
2011-06-30 16:06         ` Jean-Christophe Filliâtre
2011-07-01 10:32           ` Andrew
2011-07-01 10:51             ` Frédéric van der Plancke

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=alpine.DEB.2.02.1107021642370.25683@sergyar \
    --to=bhurt@spnz.org \
    --cc=Jean-Christophe.Filliatre@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=fa.caml@googlegroups.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).