Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ImogenBits committed Jan 8, 2024
1 parent 92ee660 commit 1bfba8f
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions algobattle/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHa

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.")
Expand All @@ -447,20 +446,19 @@ def size(self) -> int:
@cached_property
def edge_set(self) -> set[tuple[Vertex, Vertex]]:
"""The set of edges in this graph."""

return set(self.edges)

@cache
def neighbors(self, vertex: Vertex, direction: Literal["all", "outgoing", "incoming"] = "all") -> set[Vertex]:
"""The neighbors of a vertex."""

res = set[Vertex]()
if direction in {"all", "outgoing"}:
res |= set(v for (u, v) in self.edges if u == vertex)
if direction in {"all", "incoming"}:
res |= set(v for (v, u) in self.edges if u == vertex)
return res


class UndirectedGraph(DirectedGraph):
"""Base instance class for problems on undirected graphs."""

Expand All @@ -481,10 +479,9 @@ def validate_instance(self):
@cached_property
def edge_set(self) -> set[tuple[Vertex, Vertex]]:
"""The set of edges in this graph.
Normalized to contain every edge in both directions.
"""

return set(self.edges) | set((v, u) for (u, v) in self.edges)

@cache
Expand Down Expand Up @@ -526,16 +523,14 @@ class EdgeWeights(DirectedGraph, BaseModel, Generic[Weight]):
@cached_property
def edges_with_weights(self) -> Iterator[tuple[tuple[Vertex, Vertex], Weight]]:
"""Iterate over all edges and their weights."""

return zip(self.edges, self.edge_weights)

@cache
def weight(self, edge: Edge | tuple[Vertex, Vertex]) -> Weight:
"""Returns the weight of an edge.
Raises KeyError if the given edge does not exist.
"""

if isinstance(edge, tuple):
try:
edge = self.edges.index(edge)
Expand All @@ -550,6 +545,7 @@ def weight(self, edge: Edge | tuple[Vertex, Vertex]) -> Weight:

return self.edge_weights[edge]


class VertexWeights(DirectedGraph, BaseModel, Generic[Weight]):
"""Mixin for graphs with weighted vertices."""

Expand All @@ -558,7 +554,6 @@ class VertexWeights(DirectedGraph, BaseModel, Generic[Weight]):
@cached_property
def vertices_with_weights(self) -> Iterator[tuple[Vertex, Weight]]:
"""Iterate over all edges and their weights."""

return enumerate(self.vertex_weights)


Expand Down

0 comments on commit 1bfba8f

Please sign in to comment.