Open the context
Choose the project folder and name the files or behavior involved.
Group numbered nodes by undirected connections with a disjoint-set structure. Check isolated nodes, repeated edges and an empty graph, rejecting invalid endpoints before joining any component.
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.
def components(count, edges):
if type(count) is not int or not 0 <= count <= 10000:
raise ValueError("node count")
pairs = list(edges)
for pair in pairs:
if len(pair) != 2 or any(type(v) is not int or not 0 <= v < count for v in pair):
raise ValueError("edge endpoint")
parent = list(range(count))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for a, b in pairs:
parent[find(b)] = find(a)
groups = {}
for node in range(count): groups.setdefault(find(node), []).append(node)
return sorted(groups.values())The 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.