473,804 Members | 2,085 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 3886
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.b egin() + k);
vec.push_back(n ew_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.b egin() + k);
vec.push_back(n ew_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)::iter ator 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::i s_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
7530
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* heapArray; int heapSize; int arraySize; int maxSize; } heapStruct; arraySize holds the current number of elements in the array.
5
10639
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 inert into one? I've looked at faq, several books, and but am not quite understanding that algorithm to insert into max heap.
5
8152
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 under a different table name without first having to play games with the development copy of the table. If I edit the dump file with 'sed' to change the table name, I get 'invalid command \N' errors trying to reload it.
3
2234
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 procedures. SQL Server will be used by a vendor packaged application. The problem is that we are awaiting word on whether it will be SQL Server 2000 or 2005 we will be working with. I have been told that there are considerable differences between the...
7
1553
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 import shuffle data = range(10) shuffle(data) heap =
0
13329
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. Examples of two main problem scenarios are like this: 1) an array containing a first name and another array containing a last name and possibly a third array containing the age of a person; how to sort the
9
8070
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
1231
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 http://code.google.com/p/pyftpdlib/
16
7720
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 using a sorted immutable balanced binary tree set implementation that provides the above operations each in O(log n) but it is extremely
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
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
9177
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...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
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

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.