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.
python python-3.x path-finding
add a comment |
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.
python python-3.x path-finding
add a comment |
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.
python python-3.x path-finding
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.
python python-3.x path-finding
python python-3.x path-finding
edited Nov 16 '18 at 7:20
Key
asked Nov 15 '18 at 15:39
KeyKey
263
263
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
Ty so much! really appreciate it, however i still have another question and I already edited my question, can you please look at it again?
– Key
Nov 16 '18 at 7:21
I suggest you learn a little bit about graph, i.e how to present your data in a graph and the algorithm to find a path between 2 vertices in a graph (Depth First Search - DFS). That will solve your problem
– Canh
Nov 16 '18 at 12:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322926%2falgorithm-to-find-the-path-train-path-with-interchanges%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
Ty so much! really appreciate it, however i still have another question and I already edited my question, can you please look at it again?
– Key
Nov 16 '18 at 7:21
I suggest you learn a little bit about graph, i.e how to present your data in a graph and the algorithm to find a path between 2 vertices in a graph (Depth First Search - DFS). That will solve your problem
– Canh
Nov 16 '18 at 12:58
add a comment |
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)
Ty so much! really appreciate it, however i still have another question and I already edited my question, can you please look at it again?
– Key
Nov 16 '18 at 7:21
I suggest you learn a little bit about graph, i.e how to present your data in a graph and the algorithm to find a path between 2 vertices in a graph (Depth First Search - DFS). That will solve your problem
– Canh
Nov 16 '18 at 12:58
add a comment |
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)
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)
answered Nov 15 '18 at 16:29
CanhCanh
533418
533418
Ty so much! really appreciate it, however i still have another question and I already edited my question, can you please look at it again?
– Key
Nov 16 '18 at 7:21
I suggest you learn a little bit about graph, i.e how to present your data in a graph and the algorithm to find a path between 2 vertices in a graph (Depth First Search - DFS). That will solve your problem
– Canh
Nov 16 '18 at 12:58
add a comment |
Ty so much! really appreciate it, however i still have another question and I already edited my question, can you please look at it again?
– Key
Nov 16 '18 at 7:21
I suggest you learn a little bit about graph, i.e how to present your data in a graph and the algorithm to find a path between 2 vertices in a graph (Depth First Search - DFS). That will solve your problem
– Canh
Nov 16 '18 at 12:58
Ty so much! really appreciate it, however i still have another question and I already edited my question, can you please look at it again?
– Key
Nov 16 '18 at 7:21
Ty so much! really appreciate it, however i still have another question and I already edited my question, can you please look at it again?
– Key
Nov 16 '18 at 7:21
I suggest you learn a little bit about graph, i.e how to present your data in a graph and the algorithm to find a path between 2 vertices in a graph (Depth First Search - DFS). That will solve your problem
– Canh
Nov 16 '18 at 12:58
I suggest you learn a little bit about graph, i.e how to present your data in a graph and the algorithm to find a path between 2 vertices in a graph (Depth First Search - DFS). That will solve your problem
– Canh
Nov 16 '18 at 12:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322926%2falgorithm-to-find-the-path-train-path-with-interchanges%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown