SSpanSorbus
Workflows

Coding workflows · Undirected bridge edges

03
Built around your work

A concrete starting point

Find bridges in a simple undirected graph with at most 32 vertices. Test disconnected components and cycles, rejecting duplicate edges, self-loops and invalid endpoints without changing the input.

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.

Undirected bridge edges

A concrete starting point
def bridge_edges(n, edges):
    if type(n) is not int or not 0 <= n <= 32:
        raise ValueError("vertex count")
    if type(edges) is not list or len(edges) > 496:
        raise ValueError("edge count")
    graph = [set() for _ in range(n)]
    for edge in edges:
        if type(edge) is not tuple or len(edge) != 2 or any(
            type(x) is not int or not 0 <= x < n for x in edge):
            raise ValueError("edge endpoints")
        a, b = edge
        if a == b or b in graph[a]:
            raise ValueError("simple undirected edges")
        graph[a].add(b); graph[b].add(a)
    timer, found = 0, []
    entered, low = [-1] * n, [0] * n
    def visit(v, parent):
        nonlocal timer
        entered[v] = low[v] = timer; timer += 1
        for u in sorted(graph[v]):
            if u == parent:
                continue
            if entered[u] < 0:
                visit(u, v); low[v] = min(low[v], low[u])
                if low[u] > entered[v]:
                    found.append(tuple(sorted((v, u))))
            else:
                low[v] = min(low[v], entered[u])
    for v in range(n):
        if entered[v] < 0:
            visit(v, -1)
    return sorted(found)

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 ↗