Datasets:
Download code/low_threshold.py from PureOne/ioxhiru-vq: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/datasets/PureOne/ioxhiru-vq/resolve/main/code/low_threshold.py
- Command line
-
hf download hf://datasets/PureOne/ioxhiru-vq/code/low_threshold.py
-
curl -L -o low_threshold.py https://huggingface.co/datasets/PureOne/ioxhiru-vq/resolve/main/code/low_threshold.py
3.84 kB
| """Count feasible edge signings for rational thresholds below 3/sqrt(2).""" | |
| from collections import deque | |
| from fractions import Fraction | |
| def signed_psd(vertices, edges, threshold, flipped=None, direction=1): | |
| """Test whether threshold*I - direction*A is positive semidefinite.""" | |
| index = {v: i for i, v in enumerate(vertices)} | |
| rows = [{} for _ in vertices] | |
| diagonal = [threshold] * len(vertices) | |
| for u, v in edges: | |
| i, j = index[u], index[v] | |
| entry = Fraction(direction if (u, v) == flipped else -direction) | |
| rows[i][j] = rows[j][i] = entry | |
| active = set(range(len(vertices))) | |
| while active: | |
| i = min(active, key=lambda j: (len(rows[j]), j)) | |
| pivot = diagonal[i] | |
| if pivot < 0 or (pivot == 0 and rows[i]): | |
| return False | |
| neighbors = list(rows[i].items()) | |
| for k, (v, weight) in enumerate(neighbors): | |
| del rows[v][i] | |
| diagonal[v] -= weight * weight / pivot | |
| for u, other in neighbors[k+1:]: | |
| value = rows[v].get(u, 0) - weight * other / pivot | |
| if value: | |
| rows[v][u] = rows[u][v] = value | |
| else: | |
| rows[v].pop(u, None) | |
| rows[u].pop(v, None) | |
| active.remove(i) | |
| return True | |
| def count_below_limit(order, edges, threshold): | |
| """Return an exact count, or None if the structural test is inconclusive.""" | |
| t = Fraction(threshold) | |
| if t < 0 or 2*t*t >= 9: | |
| raise ValueError("Expected a nonnegative rational below 3/sqrt(2).") | |
| neighbors = [set() for _ in range(order)] | |
| for u, v in edges: | |
| if not (0 <= u < order and 0 <= v < order) or u == v or u in neighbors[v]: | |
| raise ValueError("Expected a simple graph.") | |
| neighbors[u].add(v) | |
| neighbors[v].add(u) | |
| radius = 1 | |
| while Fraction(9, 2) * (1 - Fraction(1, 4*2**radius - 3))**2 <= t*t: | |
| radius += 1 | |
| seen = set() | |
| total = 1 | |
| inconclusive = False | |
| for root in range(order): | |
| if root in seen: | |
| continue | |
| component = [root] | |
| seen.add(root) | |
| tree = set() | |
| for v in component: | |
| for w in neighbors[v]: | |
| if w not in seen: | |
| tree.add(tuple(sorted((v, w)))) | |
| component.append(w) | |
| seen.add(w) | |
| local = sorted((v, w) for v in component for w in neighbors[v] if v < w) | |
| chords = [edge for edge in local if edge not in tree] | |
| if len(chords) <= 1: | |
| # The two matrix inequalities can differ on an odd cycle. | |
| feasible_classes = sum( | |
| all(signed_psd(component, local, t, flipped, direction) | |
| for direction in (1, -1)) | |
| for flipped in [None, *chords] | |
| ) | |
| if not feasible_classes: | |
| return 0 | |
| total *= feasible_classes * (1 << (len(component) - 1)) | |
| continue | |
| degree = {v: len(neighbors[v]) for v in component} | |
| leaves = deque(v for v in component if degree[v] <= 1) | |
| core = set(component) | |
| while leaves: | |
| v = leaves.popleft() | |
| if v not in core: | |
| continue | |
| core.remove(v) | |
| for w in neighbors[v] & core: | |
| degree[w] -= 1 | |
| if degree[w] == 1: | |
| leaves.append(w) | |
| v = next(v for v in sorted(core) if degree[v] >= 3) | |
| ball = {v} | |
| fringe = {v} | |
| for _ in range(radius): | |
| fringe = {w for u in fringe for w in neighbors[u] if w in core and w not in ball} | |
| ball.update(fringe) | |
| if sum(len(neighbors[u] & ball) for u in ball) == 2 * (len(ball) - 1): | |
| return 0 | |
| inconclusive = True | |
| return None if inconclusive else total | |