Split dijkstra.py into three files: node, edge, graph

This commit is contained in:
Toby Lane 2024-06-30 19:09:47 +02:00
parent 509c7fec8a
commit 9870a643dd
3 changed files with 12 additions and 36 deletions

5
src/graph/edge.py Normal file
View File

@ -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

View File

@ -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]

4
src/graph/node.py Normal file
View File

@ -0,0 +1,4 @@
class Node:
def __init__(self, x: int, y: int):
self.x: int = x
self.y: int = y