Open the context
Choose the project folder and name the files or behavior involved.
Calculate shortest directed hop counts from one vertex in a graph of 1 to 1000 vertices. Check cycles, repeated edges and unreachable vertices, rejecting an empty graph or invalid endpoints before traversal.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from collections import deque
def hop_distances(graph, start):
if type(graph) is not list or not 1 <= len(graph) <= 1000:
raise ValueError("graph.size")
n = len(graph)
if type(start) is not int or not 0 <= start < n:
raise ValueError("graph.start")
if any(type(row) is not list or any(type(v) is not int or
not 0 <= v < n for v in row) for row in graph):
raise ValueError("graph.edges")
distances = [None]*n
distances[start] = 0
queue = deque([start])
while queue:
u = queue.popleft()
for v in graph[u]:
if distances[v] is None:
distances[v] = distances[u]+1
queue.append(v)
return distancesThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.