UUnionPebble
Workflows

Coding workflows · Disjoint component grouping

03
Built around your work

A concrete starting point

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.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Disjoint component grouping

A concrete starting point
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.

AI coding workspace

Your next change starts here.

Get the Windows app ↗