views:

90

answers:

2

I have a heightmap (a 2D array of floating point values) and I wish to find the highest point on the map, once I have found this point, I want to change its value, and the values of all nearby points. What's the best datastructure to use for efficient retrieval of the highest point?

Requirements:

  • Find the highest point efficiently
  • Change the value of an arbitrary set of points, this set will always contain the highest current point, and a load of points nearby, the delta will be different for each point.

My current thinking is a priority queue, I can find the highest point in O(1) and I can change a load of values and heapify in O(n log n).

Nb. I've marked this as language-agnostic and Lua, because it is a largely language agnostic question, but I will be implementing the final solution in Lua.

A: 

While you are building your priority queue I'd simply be scanning the array and returning the indices of the highest value found. I can then access any element of the array 'nearby' in O(1).

Or am I missing something ?

High Performance Mark
How does scanning the array take O(1)? :/
Martin
I didn't state that scanning the array is O(1). But, given the indices of an element of an array, the access time is O(1).
High Performance Mark
True, but finding the index requires a scan over the entire array, which is what I'm trying to avoid
Martin
+1  A: 

If memory is not that big of an issue I would store each value in a priority queue as a table so that each table has its data value and references to its closest neighbors. Something like this: { data = number, neighbors = { ... } }.

ponzao
hmm, that sounds like an interesting way of doing things. Writing a priority queue in Lua should be fun :D
Martin
It is ;) I recommend first starting out with a list-based version and then moving on to a binary heap-based. The reason for this is that the list-based is a lot faster to implement and thus you know if it was the right path to take.I recommend overriding the objects' comparison metamethods so that in the priority queue you can compare them with the comparison operators, thus the priority queue can easily be made generic.
ponzao
I already had a min-max-heap implemented in C#, so I ported that over, it's blazingly fast :D
Martin
Cool, I hadn't noticed your message earlier :) Did the solution work out as you wanted?
ponzao