Skip to content

Commit

Permalink
refactor: simplify shortest_path code
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines authored and joanise committed Sep 12, 2024
1 parent d4febd3 commit cd61c42
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions g2p/mappings/langs/network_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ def shortest_path(self, u: T, v: T) -> List[T]:
visited: Dict[T, Union[T, None]] = {
u: None
} # dict of {node: predecessor on shortest path from u}
queue: deque[T] = deque()
while True:
queue: deque[T] = deque([u])
while queue:
u = queue.popleft()
if u == v:
rev_path: List[T] = []
nextu: Union[T, None] = u
Expand All @@ -146,9 +147,7 @@ def shortest_path(self, u: T, v: T) -> List[T]:
if neighbour not in visited:
visited[neighbour] = u
queue.append(neighbour)
if len(queue) == 0:
raise ValueError(f"No path from {u} to {v}")
u = queue.popleft()
raise ValueError(f"No path from {u} to {v}")


NodeDict = TypedDict("NodeDict", {"id": Any})
Expand Down

0 comments on commit cd61c42

Please sign in to comment.