stl heap question: restoring heap validinty after changing 1 element

V

viki

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
 
E

Erik Wikström

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

Arash Partow

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
 
E

Erik Wikström

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

Arash Partow

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
 
I

Ivan Novick

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

Erik Wikström

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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top