This is my code for recursion, for solving a cities problem. Each city has at most 1 train and 1 bus station and some cities have both bus and train station.
So I run my code, it works but Recursion doesnt run I expect.
So far example if current station is "bus"
and there is a train
station in that city, hence, take the first if condition
if currentStation == "Bus" and currentCity in CitiesWithTrain: #check if trainStationExists
This runs but the next one with only "bus" condition, doesnt run anything inside. The if i am referring to is
if currentStation == "Bus"
and I dont return anything before that, just run a recursive call. Why doesnt the next if conditions get triggered ?
So the recursion doesnt keep going. Why doesn't the function run that ? so My output is below, keeps appending to the array but this is supposed to be happening with many running threads, not only one that keeps going (not like depth first search) I want to do it recursive.
from copy import copy
import sys
sys.setrecursionlimit(10000)
visitedCities = []
Cities = {"Istanbul" : 0, "Bursa" : 0, "Eskisehir" : 0, "Agri" : 0, "Temel" : 0, "SomeCity": 0, "Venzan": 0, "Milan": 0}
totalCostsSaved = {
"Istanbul" : copy(Cities),
"Bursa" : copy(Cities),
"Eskisehir" : copy(Cities),
"Agri" : copy(Cities),
"Temel" : copy(Cities),
"SomeCity": copy(Cities),
"Venzan": copy(Cities),
"Milan": copy(Cities)
}
#To find Quickest Itenaries from city, just set these variables Istanbul-Bus instead of istanbul-harem for example
startCity = "Temel" #City C
startStation = "Bus" #Station of City
StationAverageTimeChange = { "Istanbul" : 30, "Eskisehir" : 10, "Temel": 50}
Bus = { "Istanbul-Bursa" : 100, "Bursa-Eskisehir" : 120, "Istanbul-Temel": 400 }
Train = { "Istanbul-Eskisehir" : 180, "Milan-Istanbul" : 80, "Venzan-Istanbul": 230, "SomeCity-Venzan" : 350, "Temel-Eskisehir": 290}
CitiesWithTrain = ["Istanbul", "Eskisehir", "Milan", "Venzan", "SomeCity", "Temel"]
CitiesWithBus = ["Istanbul", "Bursa", "Eskisehir", "Temel"]
def getCostToDestination(currentCity, currentStation, currentCost, TraveledPathsBus, TraveledPathsTrain):
print(currentCity, currentStation, currentCost, TraveledPathsBus, TraveledPathsTrain)
for city in totalCostsSaved.keys():
if currentCity == city:
continue
if currentStation == "Bus" and currentCity in CitiesWithTrain: #check if trainStationExists
for cityPairs in Train.keys():
if currentCity in cityPairs.split("-"):
for cit in cityPairs.split("-"):
if cit != currentCity:
costToNewCity = currentCost + Train[cityPairs] + StationAverageTimeChange[currentCity]
if totalCostsSaved[currentCity][cit] == 0 or costToNewCity < totalCostsSaved[currentCity][cit]:
totalCostsSaved[currentCity][cit] = costToNewCity
if cityPairs not in TraveledPathsTrain:
TraveledPathsTrain.append(cityPairs)
getCostToDestination(cit, "Train", currentCost + costToNewCity, TraveledPathsBus, TraveledPathsTrain)
if currentStation == "Bus":
for cityPairs in Bus.keys():
if currentCity in cityPairs.split("-"):
for cit in cityPairs.split("-"):
if cit != currentCity:
costToNewCity = currentCost + Bus[cityPairs]
if totalCostsSaved[currentCity][cit] == 0 or costToNewCity < totalCostsSaved[currentCity][cit]:
totalCostsSaved[currentCity][cit] = costToNewCity
if cityPairs not in TraveledPathsBus:
TraveledPathsBus.append(cityPairs)
getCostToDestination(cit, "Bus", currentCost + costToNewCity, TraveledPathsBus, TraveledPathsTrain)
if currentStation == "Train" and currentCity in CitiesWithBus: #check if bus
for cityPairs in Bus.keys():
if currentCity in cityPairs.split("-"):
for cit in cityPairs.split("-"):
if cit != currentCity:
costToNewCity = currentCost + Bus[cityPairs] + StationAverageTimeChange[currentCity]
if totalCostsSaved[currentCity][cit] == 0 or costToNewCity < totalCostsSaved[currentCity][cit]:
totalCostsSaved[currentCity][cit] = costToNewCity
if cityPairs not in TraveledPathsBus:
TraveledPathsBus.append(cityPairs)
getCostToDestination(cit, "Bus", currentCost + costToNewCity, TraveledPathsBus, TraveledPathsTrain)
if currentStation == "Train":
for cityPairs in Train.keys():
if currentCity in cityPairs.split("-"):
for cit in cityPairs.split("-"):
if cit != currentCity:
costToNewCity = currentCost + Train[cityPairs]
if totalCostsSaved[currentCity][cit] == 0 or costToNewCity < totalCostsSaved[currentCity][cit]:
totalCostsSaved[currentCity][cit] = costToNewCity
if cityPairs not in TraveledPathsTrain:
TraveledPathsTrain.append(cityPairs)
getCostToDestination(cit, "Train", currentCost + costToNewCity, TraveledPathsBus, TraveledPathsTrain)
getCostToDestination(startCity, startStation, 0, [], [])
print(totalCostsSaved[startCity])
here is the output
Temel Bus 0 [] []
Eskisehir Train 340 [] ['Temel-Eskisehir']
Bursa Bus 810 ['Bursa-Eskisehir'] ['Temel-Eskisehir']
Istanbul Bus 1720 ['Bursa-Eskisehir', 'Istanbul-Bursa'] ['Temel-Eskisehir']
Eskisehir Train 3650 ['Bursa-Eskisehir', 'Istanbul-Bursa'] ['Temel-Eskisehir', 'Istanbul-Eskisehir']
Milan Train 3550 ['Bursa-Eskisehir', 'Istanbul-Bursa'] ['Temel-Eskisehir', 'Istanbul-Eskisehir', 'Milan-Istanbul']
Venzan Train 3700 ['Bursa-Eskisehir', 'Istanbul-Bursa'] ['Temel-Eskisehir', 'Istanbul-Eskisehir', 'Milan-Istanbul', 'Venzan-Istanbul']
SomeCity Train 7750 ['Bursa-Eskisehir', 'Istanbul-Bursa'] ['Temel-Eskisehir', 'Istanbul-Eskisehir', 'Milan-Istanbul', 'Venzan-Istanbul', 'SomeCity-Venzan']
Temel Bus 3840 ['Bursa-Eskisehir', 'Istanbul-Bursa', 'Istanbul-Temel'] ['Temel-Eskisehir', 'Istanbul-Eskisehir', 'Milan-Istanbul', 'Venzan-Istanbul', 'SomeCity-Venzan']
{'Istanbul': 400, 'Bursa': 470, 'Eskisehir': 340, 'Agri': 0, 'Temel': 630, 'SomeCity': 4050, 'Venzan': 1980, 'Milan': 1830}
where it is supposed to be
Temel Bus 0 [] []
Eskisehir Train 340 [] ['Temel-Eskisehir']
Istanbul Bus 400 ['Istanbul-Temel'][]
Etc. etc. and keep it recursive
So there are 4 if conditions if one Meets the condition, the rest doesnt run even if they were true. And I dont return anything