Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/dsa/various.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from collections import deque


class Graph:
Expand Down Expand Up @@ -96,21 +97,25 @@ def find_shortest_path(self, start: str, end: str) -> list[str]:
if start not in self.graph or end not in self.graph:
return []

queue = [[start]]
queue = deque([start])
visited = set([start])
parent = {start: None}

while queue:
path = queue.pop(0)
node = path[-1]
node = queue.popleft()

if node == end:
return path
# Reconstruct path backwards
path = []
while node is not None:
path.append(node)
node = parent[node]
return path[::-1]

for neighbor in self.graph.get(node, []):
if neighbor not in visited:
visited.add(neighbor)
new_path = list(path)
new_path.append(neighbor)
queue.append(new_path)
parent[neighbor] = node
queue.append(neighbor)

return [] # No path found