From 489b223db74f468032c1eeb6ca18ee6a4da3bc33 Mon Sep 17 00:00:00 2001 From: yctct Date: Wed, 25 Nov 2026 17:56:21 +0100 Subject: Add files, first commit --- src/graph.py | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/graph.py (limited to 'src/graph.py') diff --git a/src/graph.py b/src/graph.py new file mode 100644 index 0000000..c986c16 --- /dev/null +++ b/src/graph.py @@ -0,0 +1,131 @@ +"""Converts map info into a graph""" + +from models import MapData, Zone, Connection +from typing import List + + +class Graph: + """Represents zone connectivity for a drone map.""" + + def __init__( + self, + drone_map: MapData + ) -> None: + self._zones: dict[str, Zone] = {} + # a list of adjacent zones per zone + self._adjacency: dict[str, List[Zone]] = {} + # a list of connections per zone + self._connections: dict[str, List[Connection]] = {} + self._build(drone_map) + + def _build( + self, + drone_map: MapData + ) -> None: + """Build a bidirectional adjacency list. + + There is no duplicate connection i.e. only a-b and no b-a + so when we append a connection to a zone, we also need to + append the reverse connection to its destination. + """ + all_zones = [ + drone_map.start, + drone_map.end, + *drone_map.zones, + ] + + # create dictionaries' keys + for zone in all_zones: + self._zones[zone.name] = zone + self._adjacency[zone.name] = [] + self._connections[zone.name] = [] + + for connection in drone_map.connections: + src = connection.src + dest = connection.dest + + self._adjacency[src.name].append(dest) + self._adjacency[dest.name].append(src) + + self._connections[src.name].append(connection) + reverse = Connection( + src=dest, + dest=src, + link_capacity=connection.link_capacity + ) + self._connections[dest.name].append(reverse) + + def neighbours( + self, + zone_name: str + ) -> List[Zone]: + """Return zones directly connected to the given zone.""" + if zone_name not in self._adjacency: + raise KeyError(f"Unknown zone: {zone_name}") + + return self._adjacency[zone_name] + + def get_link_capacity( + self, + src: Zone, + dest: Zone + ) -> int: + """Return the max capacity for a link between two zones.""" + if src.name not in self._adjacency: + raise KeyError(f"Unknown zone: {src.name}") + if dest.name not in self._adjacency: + raise KeyError(f"Unknown zone: {dest.name}") + connection = self.get_connection(src, dest) + return connection.link_capacity + + def get_link_occupancy( + self, + src: Zone, + dest: Zone + ) -> int: + """Return the occupancy for a link between two zones.""" + if src.name not in self._adjacency: + raise KeyError(f"Unknown zone: {src.name}") + if dest.name not in self._adjacency: + raise KeyError(f"Unknown zone: {dest.name}") + connection = self.get_connection(src, dest) + return connection.link_occupancy + + def get_zone( + self, + zone_name: str + ) -> Zone: + """Return a zone by name.""" + return self._zones[zone_name] + + def get_start(self) -> Zone: + """Return the end zone""" + zones = self._zones + for zone in zones.values(): + if zone.is_start is True: + return zone + raise KeyError(f"No zone {zone}") + + def get_end(self) -> Zone: + """Return the start zone""" + zones = self._zones + for zone in zones.values(): + if zone.is_end is True: + return zone + raise KeyError(f"No zone {zone}") + + def get_connection( + self, + src: Zone, + dest: Zone + ) -> Connection: + if src.name not in self._adjacency: + raise KeyError(f"Unknown zone: {src.name}") + if dest.name not in self._adjacency: + raise KeyError(f"Unknown zone: {dest.name}") + """Return the connection between two zones""" + connections = self._connections[src.name] + for connection in connections: + if connection.dest.name == dest.name: + return connection + raise KeyError(f"No connection between {src} and {dest}") -- cgit v1.2.3