Search code examples
pythonpython-3.xpath-finding

Algorithm to find the path (train path with interchanges)


So this is my simplified code in python, I'm trying to use recursion to find which stations do the user need to go if they want to go from station1 to station12 (as an example) but I can't figure it out at all, please help me guys, i'm stuck with this for so long, thank you so much!

x = ["station1","station2","station3","station4"]
y = ["station5","station6","station7","station8"]
z = ["station9", "station10"]
e = ["station11", "station12"]
array = x, y, z, e
interchange = [ ["station4", "station5"] , ["station8", "station9"], ["station10, station[11] ]
cur = "station1"
des = "station12"

So the codes below are the algorithm i'm trying to find, but it's not working at all.

def find(cur, des):
    check = 0
    for each in array:
        if cur in each and des in each:
            check = 1
            ind = each.index(cur)
            ind2 = each.index(des)
            for i in range(ind, ind2+1):
                print("-->", end = " ")
                print(each[i], end = " ")
            print("\n")
    for each in array:
        if cur in each and check == 0:
            for station in interchange:
                if station in each and cur != station:
                    ind = each.index(cur)
                    ind2 = each.index(station)
                    for i in range(ind, ind2+1):
                        print("-->", end = " ")
                        print(each[i], end = " ")
                    print("\n")
                    find(station, des)

This is the result I'm trying to get: station1-->station2-->station3-->station4-->station5-->station6-->station7-->station8-->station9-->station10-->station11-->station12

EDITS: Answer of Canh:

def find(cur, des):
    check = 0
    for each in array:
        if cur in each and des in each:
            check = 1
            ind = each.index(cur)
            ind2 = each.index(des)
            for i in range(ind, ind2+1):
                print("-->", end = " ")
                print(each[i], end = " ")
            print("\n")
    for each in array:
        if cur in each and check == 0:
            for station1, station2 in interchange:
                if station1 in each and cur != station1:
                    ind = each.index(cur)
                    ind2 = each.index(station1)
                    for i in range(ind, ind2+1):
                        print("-->", end = " ")
                        print(each[i], end = " ")
                    find(station2, des)

Sorry but I still have another question, how do i need to modify the code so that it will find ALL the possible routes (note: train can go forward and backward) if the condition is:

x = ["station1","station2","station3","station4"]
y = ["station8","station7","station6","station5"]
z = ["station9", "station10"]
e = ["station11", "station12"]

interchange = [ ["station1", "station9"], ["station5", "station4"] , ["station9", "station8"], ["station10", "station11" ],  ["station2", "station9" ] ]

cur = "station12"
des = "station1"

Some of the example of the routes should be:

station12 --> station11 --> station10 --> station9 --> station1

station12 --> station11 --> station10 --> station9 --> station2 --> station1

station12 --> station11 --> station10 --> station8 --> station7 --> station6 --> station5 --> station4 --> station3 --> station2 --> station1

and if cur and des are swapped:

cur = "station1"
des = "station12"

then the possible routes would just be the opposite of the previous one.


Solution

  • Your interchange variable is a list of a list. In the statement for station in interchange, station is a list, thus the condition station in each would never be True.

    I think it should be like this

    for station1, station2 in interchange:
        if station1 in each and cur != station1:
            ind = each.index(cur)
            ind2 = each.index(station1)
            for i in range(ind, ind2+1):
                print("-->", end = " ")
                print(each[i], end = " ")
            print("\n")
            find(station2, des)