Skip to content

Commit

Permalink
add Path type
Browse files Browse the repository at this point in the history
  • Loading branch information
ImogenBits committed Jan 8, 2024
1 parent e97a2fb commit d145fea
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions algobattle/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
SupportsLt,
SupportsMod,
)
from itertools import pairwise

from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler
from pydantic.json_schema import JsonSchemaValue
Expand Down Expand Up @@ -63,6 +64,7 @@
"DirectedGraph",
"UndirectedGraph",
"Edge",
"Path",
"EdgeLen",
"EdgeWeights",
"VertexWeights",
Expand Down Expand Up @@ -420,6 +422,17 @@ def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHa
"""Type for edges, encoded as indices into `instance.edges`."""


def path_in_graph(path: list[Vertex], edge_set: set[tuple[Vertex, Vertex]]):
"""Checks that a path actually exists in the graph."""

for edge in pairwise(path):
if edge not in edge_set:
raise ValueError(f"The edge {edge} does not exist in the graph.")


Path = Annotated[list[Vertex], AttributeReferenceValidator(path_in_graph, InstanceRef.edge_set)]


class DirectedGraph(InstanceModel):
"""Base instance class for problems on directed graphs."""

Expand Down

0 comments on commit d145fea

Please sign in to comment.