Skip to content

cell_witness

from tensorsig.offset_coded_signatures.graph_queries import cell_witness

Return an actual nonzero occurrence tree, or None after exact rejection.

max_trees limits evaluated candidates; exhaustion raises ResourceLimit. The returned edges are unordered. Their ascending orientation has a nonzero moment; changing edge directions only changes its sign.

backend selects auto, direct_nbc or chordal_nbc. The latter rejects connected nonchordal graphs unless the universal-zero guard already decides the cell. Custom edge order/reference arrows require direct_nbc. Compiled plans are also exposed for independent reuse. Metrics are reset for each query; resource exhaustion never means zero.

Source code in src/tensorsig/offset_coded_signatures/graph_queries.py
def cell_witness(
    composition: Sequence[int],
    occurrence_channels: Sequence[int],
    graph: DependenceGraph,
    *,
    max_trees: int | None = None,
    backend: str = "auto",
    metrics: CellQueryMetrics | None = None,
    edge_order: Iterable[Sequence[int]] | None = None,
    reference_arrows: Iterable[Sequence[int]] | None = None,
) -> frozenset[tuple[int, int]] | None:
    """Return an actual nonzero occurrence tree, or None after exact rejection.

    ``max_trees`` limits evaluated candidates; exhaustion raises ResourceLimit.
    The returned edges are unordered. Their ascending orientation has a
    nonzero moment; changing edge directions only changes its sign.

    ``backend`` selects ``auto``, ``direct_nbc`` or ``chordal_nbc``. The latter
    rejects connected nonchordal graphs unless the universal-zero guard already
    decides the cell. Custom edge order/reference arrows require ``direct_nbc``.
    Compiled plans are also exposed for independent reuse.
    Metrics are reset for each query; resource exhaustion never means zero.
    """
    parts, channels = validate_cell(composition, occurrence_channels, graph)
    if max_trees is not None and (type(max_trees) is not int or max_trees < 1):
        raise ValueError("max_trees must be a positive integer or None.")
    if not isinstance(backend, str) or backend not in {
        "auto",
        "direct_nbc",
        "chordal_nbc",
    }:
        raise ValueError("backend must be auto, direct_nbc or chordal_nbc.")
    if metrics is not None and not isinstance(metrics, CellQueryMetrics):
        raise TypeError("metrics must be a CellQueryMetrics instance or None.")
    if backend != "direct_nbc" and (
        edge_order is not None or reference_arrows is not None
    ):
        raise ValueError(
            "edge_order and reference_arrows require backend='direct_nbc'."
        )
    scalar_backend = tree_moment_backend(parts)
    if metrics is not None:
        metrics.backend = backend
        metrics.candidate_trees = None
        metrics.trees_examined = 0
        metrics.scalar_backend = scalar_backend
    # Validate custom edge data even when a zero/disconnected guard can decide.
    direct = (
        _compile_direct_nbc(
            channels, graph, edge_order=edge_order, reference_arrows=reference_arrows
        )
        if backend == "direct_nbc"
        else None
    )
    if scalar_backend == "zero":
        return None
    if backend == "direct_nbc":
        if direct is None:
            return None
        trees = (direct._reference_covers(tree) for tree in direct.trees())
    else:
        adjacency = occurrence_adjacency(channels, graph)
        if not is_connected(adjacency):
            return None
        if backend == "chordal_nbc":
            peo = perfect_elimination_order(adjacency)
            if peo is None:
                raise ValueError("the occurrence dependence graph must be chordal.")
            plan = _parent_plan(adjacency, peo)
            if metrics is not None:
                metrics.candidate_trees = plan.tree_count
            trees = plan.trees()
        else:
            trees = _trees(adjacency, parts, metrics)
    for index, covers in enumerate(trees):
        if max_trees is not None and index >= max_trees:
            raise ResourceLimit(f"evaluated trees exceed {max_trees}.")
        if metrics is not None:
            metrics.trees_examined += 1
        if tree_moment(parts, covers):
            return frozenset(tuple(sorted(edge)) for edge in covers)
    return None