dijkstra: add simple dijkstra
This commit is contained in:
33
dijsktra.py
33
dijsktra.py
@@ -4,16 +4,45 @@ This module holds the logic for the solving
|
||||
|
||||
|
||||
class Dijkstra():
|
||||
def __init__(self, graph: dict):
|
||||
def __init__(self, graph: dict, connection: tuple):
|
||||
"""
|
||||
table: matrix with vertex as key, price and prev vertex as value
|
||||
OL: open list, to keep track of finished vertecis
|
||||
"""
|
||||
self.graph = graph
|
||||
self.table = {}
|
||||
self.OL = []
|
||||
for vertex in graph:
|
||||
self.table.update({vertex: {"distance": 9999999, "prev": None}})
|
||||
self.table[connection[0]]["distance"] = 0
|
||||
|
||||
def algorithm(self):
|
||||
print("Performing Dijkstra on the following graph...")
|
||||
for vertex in self.graph:
|
||||
print(f"{vertex}: {self.graph[vertex]}")
|
||||
|
||||
for v1 in self.graph.keys():
|
||||
neighbors = self.graph[v1]
|
||||
for n in neighbors:
|
||||
key = [elem for elem in n.keys()][0] # name of neighbor node
|
||||
# NOTE: change time or co2 here
|
||||
edge = n[key]["time"]
|
||||
dist = self.table[v1]["distance"] + edge
|
||||
if dist < self.table[key]["distance"]:
|
||||
self.table[key]["distance"] = dist
|
||||
self.table[key]["prev"] = v1
|
||||
|
||||
print("self.table:", self.table)
|
||||
|
||||
def print_result(self, connection: tuple):
|
||||
sequence = []
|
||||
(start, dest) = connection
|
||||
# start at destination
|
||||
sequence.append(dest)
|
||||
|
||||
while start not in sequence:
|
||||
# previous jump
|
||||
prev = self.table[sequence[-1]]["prev"]
|
||||
sequence.append(prev)
|
||||
|
||||
sequence.reverse()
|
||||
print(sequence)
|
||||
|
||||
2
file.txt
2
file.txt
@@ -10,4 +10,4 @@ FlightSchedule:
|
||||
mun_flugh; ber_flugh; 0; True
|
||||
|
||||
FindBestConnections:
|
||||
csu_zen; muc
|
||||
csu_zen; reichstag
|
||||
|
||||
5
graph.py
5
graph.py
@@ -50,9 +50,8 @@ def calc_time(
|
||||
if distance <= 25:
|
||||
return distance / TransportMethod.OEPNV.value.speed
|
||||
else:
|
||||
speed = 25 / TransportMethod.OEPNV.value.speed
|
||||
# NOTE: should we subtract here?
|
||||
return speed + ((distance - 25) / TransportMethod.ICE.value.speed)
|
||||
return (distance / TransportMethod.ICE.value.speed)
|
||||
case TransportKind.FLYING:
|
||||
assert(flights != None)
|
||||
assert(frm != None)
|
||||
@@ -73,6 +72,8 @@ def create_graph(dataset: DataSet) -> dict:
|
||||
locations = dataset.locations
|
||||
graph: dict = {}
|
||||
|
||||
print("Creating graph...")
|
||||
|
||||
# add nodes with no edges
|
||||
for start in locations:
|
||||
graph.update({start: []})
|
||||
|
||||
9
main.py
9
main.py
@@ -12,12 +12,15 @@ INPUTFILE = "file.txt"
|
||||
|
||||
|
||||
def main():
|
||||
# parse inputfile
|
||||
dataset: dict = parse(INPUTFILE)
|
||||
# print(dataset)
|
||||
# create graph
|
||||
# TODO: train only on same continent
|
||||
graph: dict = create_graph(dataset)
|
||||
# print(graph)
|
||||
dijkstra = Dijkstra(graph)
|
||||
# solve
|
||||
dijkstra = Dijkstra(graph, dataset.connection)
|
||||
dijkstra.algorithm()
|
||||
dijkstra.print_result(dataset.connection)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user