473,503 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stl heap question: restoring heap validinty after changing 1 element

I have vector 'vec' of integers which was made into valid heap using
std::make_heap().

Then we change value of a single element vec[k], like: vec[k] =
new_value;

Now this makes vec not a valid heap.

Heaps have simple algorithm that restores "heap validity" in O(log
N)after one element was changed, knowing index of changed element.

But I cannot find this function in stl.

Which stl function restores my heap vaidity in O(log N) after single
element change, given known index k of changed element ?

Thanks
vikimun
Jun 27 '08 #1
6 3871
On 2008-06-24 21:14, viki wrote:
I have vector 'vec' of integers which was made into valid heap using
std::make_heap().

Then we change value of a single element vec[k], like: vec[k] =
new_value;

Now this makes vec not a valid heap.

Heaps have simple algorithm that restores "heap validity" in O(log
N)after one element was changed, knowing index of changed element.

But I cannot find this function in stl.

Which stl function restores my heap vaidity in O(log N) after single
element change, given known index k of changed element ?
None that I can find, but it might be possible that make_heap takes no
more than O(log N) if the range is almost heap (you'll have to research
this yourself). If you really need such functionality you have to
implement it yourself, given that the algorithms are well-known it
should not be too hard.

--
Erik Wikström
Jun 27 '08 #2
I believe std::push_heap is what you're looking for:

http://www.sgi.com/tech/stl/push_heap.html

Resulting in a three step process:

vec.erase(vec.begin() + k);
vec.push_back(new_value);
std::push_heap(vec.begin(),vec.end());

Arash Partow
__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net
On Jun 25, 5:14 am, viki <viki...@gmail.comwrote:
I have vector 'vec' of integers which was made into valid heap using
std::make_heap().

Then we change value of a single element vec[k], like: vec[k] =
new_value;

Now this makes vec not a valid heap.

Heaps have simple algorithm that restores "heap validity" in O(log
N)after one element was changed, knowing index of changed element.

But I cannot find this function in stl.

Which stl function restores my heap vaidity in O(log N) after single
element change, given known index k of changed element ?

Thanks
vikimun
Jun 27 '08 #3
On 2008-06-25 04:32, Arash Partow wrote:
On Jun 25, 5:14 am, viki <viki...@gmail.comwrote:
>I have vector 'vec' of integers which was made into valid heap using
std::make_heap().

Then we change value of a single element vec[k], like: vec[k] =
new_value;

Now this makes vec not a valid heap.

Heaps have simple algorithm that restores "heap validity" in O(log
N)after one element was changed, knowing index of changed element.

But I cannot find this function in stl.

Which stl function restores my heap vaidity in O(log N) after single
element change, given known index k of changed element ?
Please do not top-post, and do not quota signatures.
I believe std::push_heap is what you're looking for:

http://www.sgi.com/tech/stl/push_heap.html

Resulting in a three step process:

vec.erase(vec.begin() + k);
vec.push_back(new_value);
std::push_heap(vec.begin(),vec.end());
Is vec guaranteed to still be a valid heap if you erase an arbitrary
element in the middle of it? If not you can not use this method since it
assumes that all elements but the last are a heap.

--
Erik Wikström
Jun 27 '08 #4
Erik you could be right, I believe there is no guarantee. I revise my
suggestion, it now involves re-pushing all the elements after the
erased element. This would be of O(logn) complexity.

typeof(v)::iterator it = v.erase(v.begin() + k);
while(v.end() != it) { std::push_heap(v.begin(),it++); }
v.push_back(new_value);
std::push_heap(v.begin(),v.end());
::assert(std::is_heap(v.begin(),v.end()));
Arash Partow
__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net

On Jun 26, 1:59 am, Erik Wikström <Erik-wikst...@telia.comwrote:
>
Is vec guaranteed to still be a valid heap if you erase an arbitrary
element in the middle of it? If not you can not use this method since it
assumes that all elements but the last are a heap.

--
Erik Wikström
Jun 27 '08 #5
On Jun 24, 12:14*pm, viki <viki...@gmail.comwrote:
I have vector 'vec' of integers which was made into valid heap using
std::make_heap().

Then we change value of *a single element vec[k], like: *vec[k] =
new_value;

Now this makes vec not a valid heap.

Heaps have simple algorithm that restores "heap validity" in O(log
N)after one element was changed, knowing index of changed element.

But I cannot find this function in stl.

Which stl *function restores my heap vaidity in O(log N) after single
element change, given known index k of changed element ?

Thanks
vikimun
If you are trying to use a std::vector to be a heap, could you use the
STL priority queue? I thought the priority queue class was
essentially a heap.

Ivan Novick
http://www.mycppquiz.com/
Jun 27 '08 #6
On 2008-06-26 23:31, Ivan Novick wrote:
On Jun 24, 12:14 pm, viki <viki...@gmail.comwrote:
>I have vector 'vec' of integers which was made into valid heap using
std::make_heap().

Then we change value of a single element vec[k], like: vec[k] =
new_value;

Now this makes vec not a valid heap.

Heaps have simple algorithm that restores "heap validity" in O(log
N)after one element was changed, knowing index of changed element.

But I cannot find this function in stl.

Which stl function restores my heap vaidity in O(log N) after single
element change, given known index k of changed element ?

Thanks
vikimun

If you are trying to use a std::vector to be a heap, could you use the
STL priority queue? I thought the priority queue class was
essentially a heap.
The problem with the priority_queue is that it does not allow random
access of elements, it provides only push(), pop(), and top().

--
Erik Wikström
Jun 28 '08 #7

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

Similar topics

2
7507
by: mordac | last post by:
Hello, I was wondering if I could get some opinions on how best to handle printing in a max heap data structure. Right now my heap struct looks as thus: typedef struct heapStruct { int*...
5
10622
by: Ook | last post by:
I'm not sure this is technically a c++ question, maybe there is a better ng to ask, but I'll start here since I'm coding this in c++. What exactly is a max heap, and more specifically, how do you...
5
8012
by: Mike Nolan | last post by:
I have a 600K row table on my production system (running 7.3.3) that I dump and load on my development system (7.4.1) every night using cron jobs. I would like to be able to restore the table...
3
2217
by: datapro01 | last post by:
I am a DB2 DBA that has been asked to become familiar enough with SQL Server in order to become actively involved in its installation, implementation, and to review database backup/recovery...
7
1532
by: 7stud | last post by:
My book says that in a heap, a value at position i will be smaller than the values at positions 2*i and 2*i + 1. To test that, I ran this program: ---------- from heapq import * from random...
0
13276
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
9
8054
by: sophia.agnes | last post by:
Hi all, Is there a simple algorithm to implement in heapsort??? this is the program i found in my college D.S notebook #include<stdio.h> #include<stdlib.h> void heapsort (int, int);
3
1213
by: Giampaolo Rodola' | last post by:
Hi, I wanted to know if does exist a safe way to, given a heap, move an arbitrary element to the first position of the heap. Something like: --- Giampaolo...
16
7685
by: Jon Harrop | last post by:
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...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
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...
0
7458
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...
0
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
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...

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.