Graph Search: First working version & Shuffle problem

This commit is contained in:
2025-10-10 14:24:50 -04:00
parent ac34fbcb83
commit 6a9630c5ac
4 changed files with 91 additions and 12 deletions

View File

@@ -8,14 +8,36 @@ Honor Code and Acknowledgments:
Comments here on your code and submission.
"""
from collections import deque
from collections import defaultdict, deque
def whatever_first_search(
graph: dict[str, set[str]], start: str, stop: str
) -> list[str]:
def whatever_first_search(graph: dict[str, set[str]], start: str, stop: str):
return []
bag: deque[tuple[str | None, str]] = deque()
bag.append((None, start))
visited: set[str] = set()
parent: dict[str, str | None] = {}
while len(bag) != 0:
parent_node, u = bag.pop()
if u in visited:
continue
visited.add(u)
parent[u] = parent_node
if u == stop:
path: list[str] = []
while u is not None:
path.append(u)
u = parent[u]
path.reverse()
return path
for v in graph[u]:
if v not in visited:
bag.append((u, v))
return None
# All modules for CS 412 must include a main method that allows it
@@ -23,21 +45,18 @@ def whatever_first_search(
def main():
n: int = int(input())
graph: dict[str, set[str]] = {}
graph: dict[str, set[str]] = defaultdict(set)
for _ in range(0, n):
u, v = input().split()
if u not in graph:
graph[u] = set([v])
else:
graph[u].add(v)
graph[u].add(v)
graph[v].add(u)
target = input().split()
path = whatever_first_search(graph, target[0], target[1])
if len(path) == 0:
if path is None:
print("no route possible")
else:
print(" ".join(path))