Skip to content

Commit

Permalink
fix: using bfs algorithm for util.find_shortest_path
Browse files Browse the repository at this point in the history
  • Loading branch information
deanmalmgren committed Feb 26, 2025
1 parent 79f8be0 commit bff497d
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions pint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import re
import tokenize
import types
from collections import deque
from collections.abc import Callable, Generator, Hashable, Iterable, Iterator, Mapping
from fractions import Fraction
from functools import lru_cache, partial
Expand Down Expand Up @@ -366,19 +367,19 @@ def find_shortest_path(
if start == end:
return path

# TODO: raise ValueError when start not in graph
if start not in graph:
return None

shortest = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
fifo = deque()
fifo.append((start, path))
visited = set()
while fifo:
node, path = fifo.popleft()
visited.add(node)
for adjascent_node in graph[node] - visited:
if adjascent_node == end:
return path + [adjascent_node]
else:
fifo.append((adjascent_node, path + [adjascent_node]))

return shortest
return None


def find_connected_nodes(
Expand Down

0 comments on commit bff497d

Please sign in to comment.