I'm working on a dynamic programming task of finding a minimum cost path along a directed graph (all possible paths have the same number of weighted nodes).
The approach for solving the problem is a recursive function along with a dynamic programming.
Since this dynamic programming task is encountered in many unrelated problems during the code, the concept of threading could be helpful.
The problem is, that in python, 'threading' won't help much. what are efficient ways of handling such a task in python?
Here's the code:
def rec_fun(pos, path_size, weights, directions):
cost = weights[d][i, j]
if path_size == 0:
key = str(i) + ',' + str(j) + ',' + str(d)
dict.update({key: pix_cost})
return cost
else:
key = str(i) + ',' + str(j) + ',' + str(d)
if key in dict:
return dict[key]
else:
val = cost + min(rec_fun(pos + direction[0], path_size - 1, weights, direction),
rec_fun(pos + direction[1], path_size - 1, weights, direction),
rec_fun(pos + direction[2], path_size - 1, weights, direction))
dict.update({key: val})
return val