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 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
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
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
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
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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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*...
|
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...
|
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...
|
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...
|
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...
| |
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.
...
|
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);
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |