I am trying to use this algorithm to print out the shortest path from a path matrix P.
My implementation:
testmatrix=np.matrix('0,7,7,0,7,7,0,0,7;0,0,7,0,7,9,0,1,0;9,0,0,0,7,0,2,9,6;9,8,8,0,8,8,8,0,8;9,3,0,0,0,3,3,9,6;9,9,9,0,9,0,9,9,0;9,5,5,0,0,5,0,9,6;9,7,7,0,7,7,0,0,7;0,7,7,0,7,0,1,1,0')
def path(q,r):
if (testmatrix[q,r] !=0):
path(q,testmatrix[q,r])
print("v "+str(testmatrix[q,r]))
path(testmatrix[q,r],r)
When I call it to print shortest path for v7 to v8:
path(7,8)
I get an error which prints out:
v 7
v 7
v 7
.
.
.
v 7
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-41-72a35ae59934> in <module>()
----> 1 path(7,8)
<ipython-input-40-7b3409c28cd1> in path(q, r)
3 path(q,testmatrix[q,r])
4 print("v "+str(testmatrix[q,r]))
----> 5 path(testmatrix[q,r],r)
6
... last 1 frames repeated, from the frame below ...
<ipython-input-40-7b3409c28cd1> in path(q, r)
3 path(q,testmatrix[q,r])
4 print("v "+str(testmatrix[q,r]))
----> 5 path(testmatrix[q,r],r)
6
RuntimeError: maximum recursion depth exceeded while calling a Python object
I don't know if I hit some recursive limit in Python or if (most likely) something is wrong with my implementation. But honestly I can't figure out what I did incorrectly. If someone could offer me some guidance on figuring this one out I would really appreciate it.