|
| 1 | +"""Canonical small-order graph6 decoding.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from hashlib import sha256 |
| 6 | +from typing import Literal, Self |
| 7 | + |
| 8 | +from pydantic import Field, StrictInt, StrictStr, model_validator |
| 9 | + |
| 10 | +from jacobian.canonical import canonicalize_json |
| 11 | +from jacobian.contracts.base import ContractModel |
| 12 | +from jacobian.contracts.common import Sha256Digest |
| 13 | + |
| 14 | + |
| 15 | +class Graph6Edge(ContractModel): |
| 16 | + first: StrictInt = Field(ge=0, le=62) |
| 17 | + second: StrictInt = Field(ge=0, le=62) |
| 18 | + |
| 19 | + @model_validator(mode="after") |
| 20 | + def require_canonical_endpoints(self) -> Self: |
| 21 | + if self.first >= self.second: |
| 22 | + raise ValueError("graph6 edge endpoints must be strictly increasing") |
| 23 | + return self |
| 24 | + |
| 25 | + |
| 26 | +class Graph6DecodeValue(ContractModel): |
| 27 | + graph6: StrictStr = Field(min_length=1, max_length=352) |
| 28 | + order: StrictInt = Field(ge=0, le=62) |
| 29 | + edges: tuple[Graph6Edge, ...] = Field(max_length=1891) |
| 30 | + degrees: tuple[StrictInt, ...] = Field(max_length=62) |
| 31 | + graph_digest: Sha256Digest |
| 32 | + format: Literal["GRAPH6_SMALL_ORDER"] = "GRAPH6_SMALL_ORDER" |
| 33 | + bit_order: Literal["COLUMN_MAJOR_UPPER_TRIANGLE"] = ( |
| 34 | + "COLUMN_MAJOR_UPPER_TRIANGLE" |
| 35 | + ) |
| 36 | + |
| 37 | + @model_validator(mode="after") |
| 38 | + def bind_dimensions(self) -> Self: |
| 39 | + if len(self.degrees) != self.order: |
| 40 | + raise ValueError("graph6 degree sequence must match graph order") |
| 41 | + pairs = tuple((edge.first, edge.second) for edge in self.edges) |
| 42 | + if pairs != tuple(sorted(pairs)) or len(pairs) != len(set(pairs)): |
| 43 | + raise ValueError("graph6 edges must be unique and sorted") |
| 44 | + if any(edge.second >= self.order for edge in self.edges): |
| 45 | + raise ValueError("graph6 edge endpoint exceeds graph order") |
| 46 | + expected = [0] * self.order |
| 47 | + for edge in self.edges: |
| 48 | + expected[edge.first] += 1 |
| 49 | + expected[edge.second] += 1 |
| 50 | + if tuple(expected) != self.degrees: |
| 51 | + raise ValueError("graph6 degree sequence does not match edges") |
| 52 | + return self |
| 53 | + |
| 54 | + |
| 55 | +def decode_graph6(encoded: str) -> Graph6DecodeValue: |
| 56 | + value = encoded[10:] if encoded.startswith(">>graph6<<") else encoded |
| 57 | + if not value or value[0] in {":", "&"}: |
| 58 | + raise ValueError("only standard graph6 is supported") |
| 59 | + codes = [ord(character) - 63 for character in value] |
| 60 | + if any(code < 0 or code > 63 for code in codes) or codes[0] == 63: |
| 61 | + raise ValueError("graph6 payload is malformed or uses an extended header") |
| 62 | + order = codes[0] |
| 63 | + bit_count = order * (order - 1) // 2 |
| 64 | + if len(codes) != 1 + (bit_count + 5) // 6: |
| 65 | + raise ValueError("graph6 payload length does not match its order header") |
| 66 | + bits = [ |
| 67 | + (code >> shift) & 1 |
| 68 | + for code in codes[1:] |
| 69 | + for shift in range(5, -1, -1) |
| 70 | + ] |
| 71 | + if any(bits[bit_count:]): |
| 72 | + raise ValueError("unused graph6 padding bits must be zero") |
| 73 | + pairs = [(first, second) for second in range(1, order) for first in range(second)] |
| 74 | + edges = tuple( |
| 75 | + Graph6Edge(first=first, second=second) |
| 76 | + for first, second in sorted( |
| 77 | + pair for pair, bit in zip(pairs, bits, strict=False) if bit |
| 78 | + ) |
| 79 | + ) |
| 80 | + degrees = [0] * order |
| 81 | + for edge in edges: |
| 82 | + degrees[edge.first] += 1 |
| 83 | + degrees[edge.second] += 1 |
| 84 | + digest_payload = { |
| 85 | + "order": order, |
| 86 | + "edges": [[edge.first, edge.second] for edge in edges], |
| 87 | + } |
| 88 | + return Graph6DecodeValue( |
| 89 | + graph6=value, |
| 90 | + order=order, |
| 91 | + edges=edges, |
| 92 | + degrees=tuple(degrees), |
| 93 | + graph_digest="sha256:" + sha256(canonicalize_json(digest_payload)).hexdigest(), |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +__all__ = ["Graph6DecodeValue", "Graph6Edge", "decode_graph6"] |
0 commit comments