By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,648 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

stl heap question: restoring heap validinty after changing 1 element

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 Wikstrm <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 Wikstrm
Jun 27 '08 #5

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.