algorithm - search in array with specific properties -
i have 2d array in values monotonic. how find (x,y) |f(x,y) – v1| < t in best way.
searching value in matrix sorted rows , columns classic problem. initialize row
maximum value , column
minimum value. examine matrix entry @ row, column
. if it's less target value, increase column
one. if it's greater target value, decrease row
one. number of matrix entries inspected @ #rows + #columns - 1
.
if search continued after target value found decreasing row
1 (respectively, increasing column
one), obtain byproduct a determination of elements less than (respectively, less or equal to) the target value. perform search algorithm 2|v| times different target values (less or equal v_i - t
, less v_i + t
) solve problem o(|v|n) matrix elements inspected.
various optimizations possible. obvious cache matrix values. is, instead of stepping one, use unbounded binary search determine appropriate increment/decrement. it's unclear in prospect whether higher worst-case constant factor overcome savings resulting large changes in neighboring entries.
example using mooing duck's instance:
to elements in range (48, 52), elements less or equal 48 , (separately) elements less 52. looking elements less or equal 48: go right (>) if current element less or equal 48. go down (v) if current element greater 48 50 60 70 80 90 100 v 40 > 51 60 70 80 90 v 30 50 52 55 70 80 v 30 40 > 45 > 46 > 51 52 v 30 40 42 45 50 51 v 30 40 41 44 49 50 v done (access out of bounds) elements less or equal 48 in submatrix containing examined element found less or equal 48 , elements left , below.
Comments
Post a Comment