CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2007
    Posts
    44

    Efficient data structure for extractMin and extractMax

    Hello Everybody,

    I need to design or use an existing data structure which performs extractMin, extractMax and insert efficiently. What I mean by extractMin (resp. extractMax) is to retrieve the minimum (resp. maximum) element in the data structure. I know that if I use a red-black tree or avl tree or any other balanced binary tree, each of three operations will be O(logn). I am looking for something more efficient.

    Any idea?


    Best,
    -hkullana

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Efficient data structure for extractMin and extractMax

    Quote Originally Posted by hkullana
    I need to design or use an existing data structure which performs extractMin, extractMax and insert efficiently. What I mean by extractMin (resp. extractMax) is to retrieve the minimum (resp. maximum) element in the data structure. I know that if I use a red-black tree or avl tree or any other balanced binary tree, each of three operations will be O(logn). I am looking for something more efficient.
    You can get constant time extractMin and extractMax at the cost of a slightly more expensive insert with a balanced binary tree simply by keeping track of those elements.

    If your stated requirements are really all there is to it, then you can get constant time extractMin, extractMax and insert: use a singly linked list that keeps track of its tail, and also keep track of the minimum and maximum.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Dec 2010
    Posts
    5

    Re: Efficient data structure for extractMin and extractMax

    Using a heap tree structure, you can extract the min in constant time (min heap)
    same for max

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Efficient data structure for extractMin and extractMax

    Quote Originally Posted by mellice
    Using a heap tree structure, you can extract the min in constant time (min heap)
    same for max
    Yeah, but not both, unless you use two heaps that consist of the same elements. Of course, the advantage is that if the elements are removed, you can still efficiently do extractMin and extractMax, but then hkullana did not mention anything about removing elements.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2007
    Posts
    44

    Re: Efficient data structure for extractMin and extractMax

    Thank you for the answers.

    I think I should clarify that what I mean by extractMin is to retrieve and *delete* the minimum element (same for extractMax). Is there still a way for doing these operations in constant time while insert stays at logarithmic time?
    Last edited by hkullana; February 8th, 2011 at 06:52 PM.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Efficient data structure for extractMin and extractMax

    Quote Originally Posted by hkullana
    I think I should clarify that what I mean by extractMin is to retrieve and *delete* the minimum element (same for extractMax). Is there still a way for doing these operations in constant time while insert stays at logarithmic time?
    It occured to me that using two heaps won't work: if you extractMin from the min-heap, removing the corresponding minimum from the max-heap is not going to take constant time in general.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Efficient data structure for extractMin and extractMax

    Quote Originally Posted by hkullana View Post
    I think I should clarify that what I mean by extractMin is to retrieve and *delete* the minimum element (same for extractMax). Is there still a way for doing these operations in constant time while insert stays at logarithmic time?
    I don't think this can be done. If you use a balance binary tree (i.e. AVL), you could keep track of the minimal and maximal elements with pointers, but on deletion you will have to re-balance the binary tree which costs O(log n). I'm not sure if you can beat this using a different type of balance tree (Red-Black?)

    Realistically you might just be better off just using an off-the-shelf balance binary tree library and eating the O(log n) cost of deleting the maximal element. Unless N is REALLY huge or you call it a TON, it would be surprising to find that it was the limiting factor of your program.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Efficient data structure for extractMin and extractMax

    Wikipedia claims Fusion trees achieve O(1) on minimum (just see it, not remove) and O(sqrt(log n)) on insert and extractMin. See: http://en.wikipedia.org/wiki/Priority_queue

    Some lectures notes I found on the topic: http://courses.csail.mit.edu/6.897/s...4/lecture4.pdf
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured