Skip to content

DirectNbcPlan

from tensorsig.offset_coded_signatures.graph_queries import DirectNbcPlan

One exact ordered actual-graph NBC family and its reference arrows.

trees

trees() -> Iterator[OccurrenceTree]

Yield every NBC spanning tree once, with LOW before HIGH.

Source code in src/tensorsig/offset_coded_signatures/_graph/direct_nbc.py
def trees(self) -> Iterator[OccurrenceTree]:
    """Yield every NBC spanning tree once, with LOW before HIGH."""
    edges = self.edge_order
    stack: list[tuple[int, tuple[OccurrenceEdge, ...], _Partition]] = [
        (0, (), tuple(range(self.degree)))
    ]
    while stack:
        index, selected, partition = stack.pop()
        if index == len(edges):
            yield frozenset(selected)
            continue

        left, right = edges[index]
        suffix = edges[index + 1 :]
        left_block = partition[left]
        right_block = partition[right]
        if not any(
            (
                partition[higher_left] == left_block
                and partition[higher_right] == right_block
            )
            or (
                partition[higher_left] == right_block
                and partition[higher_right] == left_block
            )
            for higher_left, higher_right in suffix
        ):
            stack.append(
                (
                    index + 1,
                    (*selected, (left, right)),
                    _merge_blocks(partition, left, right),
                )
            )
        if _suffix_is_connected(partition, suffix):
            stack.append((index + 1, selected, partition))

replay

replay(target: Iterable[Sequence[int]]) -> SignedTreeExpansion

Express one actual spanning-tree moment in the NBC family exactly.

Source code in src/tensorsig/offset_coded_signatures/_graph/direct_nbc.py
def replay(self, target: Iterable[Sequence[int]]) -> SignedTreeExpansion:
    """Express one actual spanning-tree moment in the NBC family exactly."""
    supplied = tuple(
        _normalize_edge(edge, self.degree, name="target") for edge in target
    )
    tree = frozenset(supplied)
    if len(supplied) != len(tree):
        raise ValueError("target edges must not contain duplicates.")
    if not tree <= frozenset(self.edge_order):
        raise ValueError("target must use only actual occurrence edges.")
    _prepare_tree(tree, self.degree)

    ranks = {edge: rank for rank, edge in enumerate(self.edge_order)}
    arrow_signs = {
        edge: 1 if arrow == edge else -1
        for edge, arrow in zip(
            self.edge_order,
            self.reference_arrows,
            strict=True,
        )
    }

    def tree_key(value: OccurrenceTree) -> tuple[int, ...]:
        return tuple(sorted(ranks[edge] for edge in value))

    rewrites: dict[
        OccurrenceTree,
        tuple[tuple[OccurrenceTree, int], ...],
    ] = {}
    pending = [tree]
    while pending:
        value = pending.pop()
        if value in rewrites:
            continue
        reduction: tuple[OccurrenceEdge, tuple[OccurrenceEdge, ...]] | None = None
        for higher in reversed(self.edge_order):
            if higher in value:
                continue
            path = _tree_path(value, *higher, self.degree)
            if all(ranks[edge] < ranks[higher] for edge in path):
                reduction = higher, path
                break
        if reduction is None:
            rewrites[value] = ()
            continue

        higher, path = reduction
        unicyclic = value | {higher}
        deltas = {}
        for relation_tree, coefficient in _signed_unicyclic_relation(
            unicyclic,
            self.degree,
        ):
            omitted = next(iter(unicyclic - relation_tree))
            deltas[omitted] = coefficient * arrow_signs[omitted]

        children = tuple(
            (
                frozenset((value - {removed}) | {higher}),
                -deltas[higher] * deltas[removed],
            )
            for removed in path
        )
        rewrites[value] = children
        pending.extend(child for child, _ in children)

    expansions: dict[OccurrenceTree, SignedTreeExpansion] = {}
    for value in sorted(
        rewrites,
        key=lambda item: sum(ranks[edge] for edge in item),
        reverse=True,
    ):
        children = rewrites[value]
        if not children:
            expansions[value] = ((value, 1),)
            continue
        totals: dict[OccurrenceTree, int] = {}
        for child, local_coefficient in children:
            for terminal, coefficient in expansions[child]:
                totals[terminal] = (
                    totals.get(terminal, 0) + local_coefficient * coefficient
                )
        expansions[value] = tuple(
            (terminal, coefficient)
            for terminal, coefficient in sorted(
                totals.items(), key=lambda item: tree_key(item[0])
            )
            if coefficient
        )
    return expansions[tree]

reference_covers

reference_covers(tree: OccurrenceTree) -> tuple[DirectedOccurrenceEdge, ...]

Orient a generated or replayed tree by the stored reference arrows.

Source code in src/tensorsig/offset_coded_signatures/_graph/direct_nbc.py
def reference_covers(
    self, tree: OccurrenceTree
) -> tuple[DirectedOccurrenceEdge, ...]:
    """Orient a generated or replayed tree by the stored reference arrows."""
    prepared = _prepare_tree(tree, self.degree)
    tree = frozenset(tuple(sorted(edge)) for edge in prepared[0])
    if not tree <= frozenset(self.edge_order):
        raise ValueError("tree must use only actual occurrence edges.")
    return self._reference_covers(tree)