473,657 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Efficient sorted heap


I need a data structure with the following capabilities:

1. Fast access to the smallest element.

2. Fast insertion of a new element.

3. Fast deletion of an existing element.

I have been using a sorted immutable balanced binary tree set implementation
that provides the above operations each in O(log n) but it is extremely
slow on .NET, around 8x slower than OCaml+Linux.

I do not benefit from the use of an immutable data structure, so what
mutable data structure would a C# programmer use?

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
Oct 17 '08 #1
16 7705
SortedDictionar y<,is O(log n) for retreival, insert and removal; see
"remarks" here:

http://msdn.microsoft.com/en-us/library/f7fta44c.aspx

Note that this only allows one value per unique key. Of course, you
could always craft something grungy looking like ILookup<,>, using a
SortedDictionar y<Foo, List<Bar>(not ideal).

Marc
Oct 17 '08 #2
Hi Marc

Do you know how this differs from Dictionary<T,Vi nternally? I thought
that looking up an index based on a hash would be quicker than sorting the
keys. If you don't know OTTOYH I will spend some time later with reflector
:-)

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 17 '08 #3
Not of the top of my head; I would have to check with either reflector
or the reference source symbol server.

Marc
Oct 17 '08 #4
Jon Harrop wrote:
I need a data structure with the following capabilities:

1. Fast access to the smallest element.

2. Fast insertion of a new element.

3. Fast deletion of an existing element.

I have been using a sorted immutable balanced binary tree set implementation
that provides the above operations each in O(log n) but it is extremely
slow on .NET, around 8x slower than OCaml+Linux.

I do not benefit from the use of an immutable data structure, so what
mutable data structure would a C# programmer use?
How often do you need to get the smallest element compared to how often
you insert and delete elements?

You could create a wrapper that contains a Dictionary<Key, Valuewith
all items, and a short array Key[] with a few of the smallest items,
that you keep sorted. When you insert an item, check if it should be
added to the array of smallest items, and when you delete an item, check
if it's in the array of smallest items.

If the array of smallest items happens to be empty when you try to
access the smallest item (i.e. you have deleted all of the smallest
items), you take a hit as you have to repopulate the array by looking
through the dictionary, otherwise it is an O(1) operation.

--
Göran Andersson
_____
http://www.guffa.com
Oct 17 '08 #5
I can see that the hash could be more expensive for a key that is a long
string, but I wouldn't have thought many objects need such a complicated
hash do that? In my case there are no structs in my apps so mostely my keys
would be

enums
short strings
integers
instance of an object
I'd suspect in this case the Dictionary<T,Vw ould probably be quicker?

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 17 '08 #6
I use a Binary Heap in my application. It is immediate (aka, O(1))
access to the smallest element. It is O(log n) to delete or insert an
element. Perhaps another type of heap (like a Soft Heap) would help
you. See the chart here: http://en.wikipedia.org/wiki/Fibonacci_heap.
I coded a Soft Heap in C# before. Unfortunately it does two
allocations per insert. This was significantly more expensive in
performance than my binary heap that stores everything in an array in
that lots of small allocations are expensive in C#. (The Binary Heap
changes the array size and copies when it runs out of room similar to
List<>).
Oct 17 '08 #7
Göran Andersson wrote:
Jon Harrop wrote:
>I need a data structure with the following capabilities:

1. Fast access to the smallest element.

2. Fast insertion of a new element.

3. Fast deletion of an existing element.

I have been using a sorted immutable balanced binary tree set
implementati on that provides the above operations each in O(log n) but it
is extremely slow on .NET, around 8x slower than OCaml+Linux.

I do not benefit from the use of an immutable data structure, so what
mutable data structure would a C# programmer use?

How often do you need to get the smallest element compared to how often
you insert and delete elements?
1 get smallest to 4 inserts and 2 removes.
You could create a wrapper that contains a Dictionary<Key, Valuewith
all items, and a short array Key[] with a few of the smallest items,
that you keep sorted. When you insert an item, check if it should be
added to the array of smallest items, and when you delete an item, check
if it's in the array of smallest items.

If the array of smallest items happens to be empty when you try to
access the smallest item (i.e. you have deleted all of the smallest
items), you take a hit as you have to repopulate the array by looking
through the dictionary, otherwise it is an O(1) operation.
That is O(n) instead of O(log n) though, so it is definitely slower for
sufficiently large problems.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
Oct 17 '08 #8
Marc Gravell wrote:
SortedDictionar y<,is O(log n) for retreival, insert and removal; see
"remarks" here:

http://msdn.microsoft.com/en-us/library/f7fta44c.aspx

Note that this only allows one value per unique key. Of course, you
could always craft something grungy looking like ILookup<,>, using a
SortedDictionar y<Foo, List<Bar>(not ideal).
SortedDictionar y does not appear to provide the minimum element. Also, its
second type parameter is superfluous in this case, so it will probably
waste space.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
Oct 17 '08 #9
Peter Morris wrote:
Hi Marc

Do you know how this differs from Dictionary<T,Vi nternally?
SortedDictionar y is a balanced binary tree. This is essentially the
implementation I have been using but it is extremely slow on .NET.
I thought that looking up an index based on a hash would be quicker than
sorting the keys.
Only if you know the key you are looking for which I do not: I need the
smallest element. So hashing is obviously worse than useless here.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
Oct 17 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2622
by: Raghavendra Mahuli | last post by:
i need to store a lot of integers(say 10,000) in a datastructure. The constraints are: 1. It should be fast. 2. It should be orderded or sorted. 3.Insterting and deleting should be fast. 4. The no. of elements can change at runtime and hence it should be dynamic. Right now i am using SortedVector , but it is very slow. So, can u pls suggest a suitable Datastructure.
24
3941
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = s2 = s3 = for value in ???(s1, s2, s3):
1
1760
by: Dan H. | last post by:
Hello, I have an application that requires a collection that is sorted by a double field first, and then by a long field. So, lets say I have an class that has 2 fields called time, priority. I want to put a bunch of those classes into a collection and have that collection always stay sorted by time first, then priority. The last object in the list should be the lowest time, highest priority object, etc, for easy picking.
3
1921
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the following layout: 1.0.0.0 1.1.0.0 1.1.1.0 1.1.2.0
11
3602
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
22
7665
by: Curious | last post by:
Hi, I am searching for a data structure that stores key-value pairs in it. This data structure is to hold large amounts of key-value pairs, and so needs to be efficient both in insertion and deletion. Does anybody know of ready data structures that can be freely used under the .Net framwork. Thanks in Advance
1
1894
by: KK | last post by:
Hi, I'm trying to come up with an efficient way to solve the following problem. Let A = { (1, b), (2, d), (3, a), (4, c) ,... } B = { (1, d), (2, a), (3, e), (4, f),... } where a,b,c... belong to set of integers and are UNIQUE. Achieve C = A - B; D = B - A;
74
4798
by: copx | last post by:
In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that properly written C++ outperforms C code. I will just copy his first example here, which is supposed to demonstrate how C++ abstractions do not only make code easier to understand but also make it more efficient: === The simplest specific example I can think of is a program to find the mean and median of a sequence of double precision floating-point numbers read...
0
168
by: sturlamolden | last post by:
On May 25, 8:02 pm, Rodrigo Lazo <rlazo....@gmail.comwrote: Heap is the data structure to use for 'fast (nearly) sorted inserts'. But heapq do not support (as far as I know) deletion of duplicates. But a custom heap class coud do that of course.
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.