Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^5: Re-orderable keyed access structure?

by Aristotle (Chancellor)
on Aug 15, 2004 at 03:54 UTC ( #383044=note: print w/replies, xml ) Need Help??

in reply to Re^4: Re-orderable keyed access structure?
in thread Re-orderable keyed access structure?

  1. Iterating the heap to find the highest weight and adding one. One recursive operation that will then trigger another. (In Perl not C)

See, you didn't actually understand. Where is the element with the greatest value in a heap? Always at the root. :-)

You also didn't pay attention to my comment that a complete binary tree can be represented trivially using an array. You don't need linked lists, just index math.

Makeshifts last the longest.

  • Comment on Re^5: Re-orderable keyed access structure?

Replies are listed 'Best First'.
Re^6: Re-orderable keyed access structure?
by BrowserUk (Pope) on Aug 15, 2004 at 04:24 UTC

    Sorry, but I'm having trouble picturing how this works? A binary tree in an array. Highest value is always root (element 0?). How do I locate the second highest item? If it's always element 1, then you talking about a sorted array, that resorts itself as you add items to it or change the weight any of the items.

    The array elements contain the weights, where does the payload go? So the array becomes an AoA? Raising an items weight to the top means assigning that item a weight higher than the weight in $array[0][0]--easy enough.

    But then, the process of re-sorting the list is to move the newly highest weighted item to position 0. This is done by comparing this items weight with that of the item above it and if its higher (which it always will be), swap the two items and repeat with the next highest item. Keep repeating until it has found it's way to the top.

    This is a bubble sort. If the previously lowest item in the heap/array gets modified, then it's weight gets set to the highest value +1 (eg. $array[ 0 ][ 0 ] ). The heap algorithm then swaps it with every item in the heap/array, one at a time, until it reaches the top.

    Given that I already know that I am moving the item to the top of the list, the splice operation is vastly more efficient. Once I am going to do that, there is no point in embedding the weight within each element, because it is always, directly related to the elements position in the array. Ie. The elements position is it's weight.

    Back to square one.

    If I have this wrong (and I am pretty sure that I don't) then please illustrate a 5 element weighted heap and the steps required to raise the middle element to top (or bottom).

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

      You could call it a bubble sort in a sorted array (O( n )) I guess. Except you're only looking at log n items. The splice is not more efficient. You need to move the entire array down one position to move a new element at the front. With a heap, you need to swap at most log n elements.

      Google found me a good explanation of heaps. Look at the illustrations. It also explains storage of complete binary trees in an array.

      Please do pick up a book or two on algorithms and data structures; this is stuff anyone who is serious about programming should know.

      Makeshifts last the longest.

        Note however that this is Perl. It is true that one splice is O($#array) in chunks of memory to be moved while a heap moves fewer chunks of memory, O(log($#array)). But it is also true that splice is a single Perl opcode while the heap will be O(log($#array)) in Perl opcodes.

        And I wouldn't be surprised if O(1 opcode)+O($#array moves) isn't quite often a win over O(log($#array) opcodes and moves).

        - tye        

        Yes, you inspect log N item and move one, (steps 1, 2 and 3) below). But then you are not finished. You still need to swap items 1 and 2.

        1) 0 [ 10 ] 2) 0 [ 10 ] 3) 0 [ 11 ] 4) 0 [ 11 ] 1 [ 9 ] 1 [ 9 ] 1 [ 9 ] 1 [ 10 ] 2 [ 8 ] 2 [ 11 ] 2 [ 10 ] 2 [ 9 ] 3 [ 7 ] 3 [ 7 ] 3 [ 7 ] 3 [ 7 ] 4 [ 5 ] 4 [ 5 ] 4 [ 5 ] 4 [ 5 ]

        Now try making that a 7 item array and moving the middle item to the top. Count the number of comparisons and swaps required.

        In the end, you have had to move the middle item to the top and all the intervening items down. Splice does this directly. A heap algorithm does it one at a time.

        Splice does this in O(N). A heap algorithm does it using O(N log N).

        I have several good data structure & algorithm books, a couple of them are almost as old as you. Unlike you apparently, I haven't just read the headlines. I've also implemented many of the algorithms myself and understood the ramifications.

        I was simply waiting for you to catch up with the fact that the use of heaps has no benefit here.

        The likely size of the cache is a few hundred, maybe 1000 elements. More than this and I run out of file handles or memory. splice is way more efficient at moving 1 item in an array of this size than any implementation of a (binary search + swap) * (old_position - new_position) in Perl.

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://383044]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2020-10-30 17:21 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (282 votes). Check out past polls.