From 9870a643ddbe40077d90eea5bea3e7bf16e1e634 Mon Sep 17 00:00:00 2001 From: Toby Lane Date: Sun, 30 Jun 2024 19:09:47 +0200 Subject: [PATCH] Split dijkstra.py into three files: node, edge, graph --- src/graph/edge.py | 5 ++++ utils/dijkstra.py => src/graph/graph.py | 39 ++----------------------- src/graph/node.py | 4 +++ 3 files changed, 12 insertions(+), 36 deletions(-) create mode 100644 src/graph/edge.py rename utils/dijkstra.py => src/graph/graph.py (71%) create mode 100644 src/graph/node.py diff --git a/src/graph/edge.py b/src/graph/edge.py new file mode 100644 index 0000000..839100d --- /dev/null +++ b/src/graph/edge.py @@ -0,0 +1,5 @@ +class Edge: + def __init__(self, start: int, end: int, length: float): + self.length: float = length + self.start: int = start + self.end: int = end \ No newline at end of file diff --git a/utils/dijkstra.py b/src/graph/graph.py similarity index 71% rename from utils/dijkstra.py rename to src/graph/graph.py index 8f8ba8d..57921fc 100644 --- a/utils/dijkstra.py +++ b/src/graph/graph.py @@ -1,19 +1,8 @@ from math import inf from typing import Iterator, Optional - -class Node: - def __init__(self, x: int, y: int): - self.x: int = x - self.y: int = y - - -class Edge: - def __init__(self, start: int, end: int, length: float): - self.length: float = length - self.start: int = start - self.end: int = end - +from src.graph.node import Node +from src.graph.edge import Edge class Graph: def __init__(self): @@ -63,26 +52,4 @@ class Graph: node_sequences[end] = node_sequences[start].copy() node_sequences[end].append(end) - return node_sequences[target_index] - - -def main() -> None: - graph = Graph() - - graph.add_node(1, 2) - graph.add_node(4, 7) - graph.add_node(3, 1) - graph.add_node(-2, 0) - graph.add_node(0, 0) - - graph.add_edge(0, 1, 1) - graph.add_edge(1, 2, 2) - graph.add_edge(2, 3, 3) - graph.add_edge(3, 0, 1) - graph.add_edge(1, 3, 3) - - print(graph.dijkstra(0, 3)) - - -if __name__ == "__main__": - main() + return node_sequences[target_index] \ No newline at end of file diff --git a/src/graph/node.py b/src/graph/node.py new file mode 100644 index 0000000..b37a03d --- /dev/null +++ b/src/graph/node.py @@ -0,0 +1,4 @@ +class Node: + def __init__(self, x: int, y: int): + self.x: int = x + self.y: int = y \ No newline at end of file