1

I am trying to use this algorithm to print out the shortest path from a path matrix P.

Pseudo for Shortest Path Print

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.

feynmanium
  • 159
  • 2
  • 13

1 Answers1

0

You weights of 0 are conflicting with python's 0 index since your indices are from 1 to 9 not 0 to 8. You can fix this by subtracting 1 from your matrix and considering -1 as weight 0 and switch to python indexing world.

matrix([[-1,  6,  6, -1,  6,  6, -1, -1,  6],
        [-1, -1,  6, -1,  6,  8, -1,  0, -1],
        [ 8, -1, -1, -1,  6, -1,  1,  8,  5],
        [ 8,  7,  7, -1,  7,  7,  7, -1,  7],
        [ 8,  2, -1, -1, -1,  2,  2,  8,  5],
        [ 8,  8,  8, -1,  8, -1,  8,  8, -1],
        [ 8,  4,  4, -1, -1,  4, -1,  8,  5],
        [ 8,  6,  6, -1,  6,  6, -1, -1,  6],
        [-1,  6,  6, -1,  6, -1,  0,  0, -1]])

Now the indices starts from 0 to 8. and if an element is -1 it means it's the shortest path.

code:

import numpy as np
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')
testmatrix = testmatrix -1
def path(q,r):
    if (testmatrix[q,r] !=-1):
        path(q,testmatrix[q,r])
        print("v "+str(testmatrix[q,r]))
        path(testmatrix[q,r],r)

path(6,7)

result :

v 4
v 2
v 5
v 8
v 0

or if you really persist on using 1-9 indices, you can put the path into a newpath and do the job there:

import numpy as np
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 new_path (q1,r1,testmatrix):
    def path(q,r):
        if (testmatrix[q,r] !=-1):
            path(q,testmatrix[q,r])
            print("v "+str(testmatrix[q,r]+1))
            path(testmatrix[q,r],r)
    testmatrix = testmatrix-1
    q = q1-1
    r = r1-1
    path(q,r)


new_path(7,8,testmatrix)

result:

v 5
v 3
v 6
v 9
v 1
Kennet Celeste
  • 4,593
  • 3
  • 25
  • 34