UUnionPebble
Product

Product features · Disjoint component grouping

01

Compare the edge fixture with the returned groups and account for isolated vertices before connecting component labels to downstream code.

01

Connection

Base URL, credentials and a reachable server.

02

Selection

A model available through the configured endpoint.

03

Capabilities

Context limits and tool support are determined by the model and server.

Disjoint component grouping

● ● ●UnionPebble●
A concrete starting point · Disjoint component grouping ×
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())
✳Group numbered nodes by undirected connections with a disjoint-set structure.
✓ ChangesReview before applying ↗
Illustrative workspace
AI coding workspace

Your next change starts here.

Get the Windows app ↗