# Finding Lowest Elevation Path Between Two Points

Let's say I have a matrix of values that represent heights with function $$f(x,y)$$ and I am trying to find the "lowest value path" beween two points. So this would be the reverse of hill climbing, as in optimization, taking gradient and following that etc, but in a way, I guess I need the anti-gradient - not the direction of steepest climb but the non-steepest walk.

I need this for a mapping application, I have elevation values on a grid and I am trying to find a path between those points that require minimum amt of climbing.

I guess I could create a cost function that gives highest values for high elevation + furthest points to destination + non-smoothness of paths, and do optimization on that. I was just wondering if anyone worked with such cost functions before, or there is another calculus trick I have to utilize.

Keywords: flattest route

• Just curious: if you took the largest value, say $M_{\mbox{max}}$ of your matrix and created a new matrix $M'$ from it, where each entry $m_{ij}$ of $M'$ is $M_{\mbox{max}}-m_{ij}$ -- could you use hill climbing on that, or is there something that blocks this attempt? – postmortes Jan 26 '19 at 14:39

This is an instance of a "shortest path" problem. Create a directed graph with vertex set equal to your grid points, and with a directed edge from $$a$$ to $$b$$ if $$a$$ and $$b$$ are adjacent, and give that edge weight $$w(a,b)=\min(0,h(b)-h(a))$$ where $$h(a)$$ is the elevation of $$a$$, and so on. (That is, the vertices are cells in your matrix, where the typical vertex has 4 neighbors: the cell to the North, the one to the South, etc. The edges represent things like "go North from this cell to its neighbor" and the weight is the climb associated with such a move.) Use one of the algorithms described in the link. Warning: some of these algorithms require intricate coding. (This weight function measures only uphill climb, which, as an occasional hiker I know is not necessarily the whole story about weariness of foot.)

This problem can indeed be seen as shortest path problem. Let's say matrix has elevation data, a cells neighbors can be retrieved using Queen's pattern (8 of them), then code is

from pqdict import pqdict
import numpy as np

def get_neighbor_idx(x,y,dims):
res = []
for i in ([0,-1,1]):
for j in ([0,-1,1]):
if i==0 and j==0: continue
if x+i<(dims) and x+i>-1 and y+j<(dims) and y+j>-1:
res.append((x+i,y+j))
return res

def dijkstra(C,s,e):
D = {}
P = {}
Q = pqdict()
Q[s] = 0

while len(Q)>0:
(v,vv) = Q.popitem()
D[v] = vv
neighs = get_neighbor_idx(v,v,C.shape)
for w in neighs:
vwLength = D[v] + np.abs(C[v,v] - C[w,w])
if w in D:
if vwLength < D[v]:
raise ValueError
elif w not in Q or vwLength < Q[w]:
Q[w] = vwLength
P[w] = v

path = []
while 1:
path.append(e)
if e == s: break
e = P[e]
path.reverse()
return path

m = np.array([[999.9, 999.9, 999.9,   0. ],
[999.9, 999.9, 999.9,   0. ],
[999.9, 999.9, 999.9,   0. ],
[  0.,    0.,    0.,    0. ]])

res = dijkstra(m,(3,0),(0,3))

print (res)


This will print

[[999.9 999.9 999.9   0. ]
[999.9 999.9 999.9   0. ]
[999.9 999.9 999.9   0. ]
[  0.    0.    0.    0. ]]
[(3, 0), (3, 1), (3, 2), (2, 3), (1, 3), (0, 3)]


So starting from lower left corner, to the target upper right corner the flattest route was reported. The 999's are 'hills' the 0's are ground.