From de34b3acce1a46c871d056b543fcd27d427f5aeb Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Wed, 27 Jul 2022 17:01:59 +0200 Subject: [PATCH] dijkstra: add simple dijkstra --- dijsktra.py | 33 +++++++++++++++++++++++++++++++-- file.txt | 2 +- graph.py | 5 +++-- main.py | 9 ++++++--- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/dijsktra.py b/dijsktra.py index 5f0ae82..b2c2ff6 100644 --- a/dijsktra.py +++ b/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) diff --git a/file.txt b/file.txt index e5351bc..352b210 100644 --- a/file.txt +++ b/file.txt @@ -10,4 +10,4 @@ FlightSchedule: mun_flugh; ber_flugh; 0; True FindBestConnections: -csu_zen; muc +csu_zen; reichstag diff --git a/graph.py b/graph.py index 8e01f0a..ff6c2a8 100644 --- a/graph.py +++ b/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: []}) diff --git a/main.py b/main.py index 27f4cf1..a8205a1 100644 --- a/main.py +++ b/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__":