-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathhypercube.py
More file actions
153 lines (126 loc) · 3.72 KB
/
Copy pathhypercube.py
File metadata and controls
153 lines (126 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# /// script
# requires-python = ">=3.14"
# dependencies = [
# "marimo>=0.19.7",
# "wigglystuff==0.5.1",
# ]
# ///
import marimo
__generated_with = "0.23.6"
app = marimo.App(width="medium")
@app.cell
def _():
from pathlib import Path
import sys
import marimo as mo
repo_root = Path(__file__).resolve().parents[1]
if (repo_root / "wigglystuff").exists():
sys.path.insert(0, str(repo_root))
from wigglystuff import GraphWidget
return GraphWidget, mo
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Hypercube $Q_n$
Every vertex is a binary string of length $n$; two vertices are connected
when they differ in exactly one bit. Because every vertex has the same
degree and the graph is perfectly symmetric, the force layout unfurls it:
$Q_3$ snaps into a cube, $Q_4$ projects into a tesseract (cube within a
cube), and so on.
Nodes are colored by popcount (number of 1 bits); edges are colored by
which bit position the endpoints differ in.
""")
return
@app.cell
def _(mo):
dim = mo.ui.slider(start=1, stop=6, step=1, value=3, label="Dimension n")
node_size = mo.ui.slider(start=2, stop=14, step=1, value=10, label="Node size")
mo.hstack([dim, node_size])
return dim, node_size
@app.cell(hide_code=True)
def _():
NODE_PALETTE = [
"#1e40af",
"#3b82f6",
"#06b6d4",
"#10b981",
"#f59e0b",
"#f97316",
"#dc2626",
]
EDGE_PALETTE = [
"#ef4444",
"#f59e0b",
"#10b981",
"#3b82f6",
"#8b5cf6",
"#ec4899",
]
def build_hypercube(n, size_px=10):
size = 1 << n
nodes = []
for i in range(size):
label = format(i, f"0{n}b") if n > 0 else "0"
popcount = bin(i).count("1")
nodes.append(
{
"id": label,
"name": label,
"color": NODE_PALETTE[popcount % len(NODE_PALETTE)],
"size": size_px,
}
)
edges = []
for i in range(size):
label_i = format(i, f"0{n}b") if n > 0 else "0"
for bit in range(n):
j = i ^ (1 << bit)
if i < j:
label_j = format(j, f"0{n}b")
edges.append(
{
"source": label_i,
"target": label_j,
"color": EDGE_PALETTE[bit % len(EDGE_PALETTE)],
"width": 1.5,
}
)
return nodes, edges
return (build_hypercube,)
@app.cell(hide_code=True)
def _(GraphWidget, mo):
hypercube = mo.ui.anywidget(
GraphWidget(
nodes=[],
edges=[],
directed=False,
bounded=False,
width=720,
height=520,
)
)
hypercube
return (hypercube,)
@app.cell
def _(build_hypercube, dim, hypercube, node_size):
_nodes, _edges = build_hypercube(dim.value, size_px=node_size.value)
with hypercube.hold_sync():
hypercube.nodes = _nodes
hypercube.edges = _edges
return
@app.cell
def _(dim, hypercube, mo):
_n = dim.value
_v = 1 << _n
_e = _n * (1 << (_n - 1)) if _n > 0 else 0
mo.vstack(
[
mo.md(f"**n:** `{_n}` **|V|:** `{_v}` **|E|:** `{_e}`"),
mo.md(f"**Hovered node:** `{hypercube.hovered_node}`"),
mo.md(f"**Selected nodes:** `{hypercube.selected_nodes}`"),
mo.md(f"**Selected edges:** `{hypercube.selected_edges}`"),
]
)
return
if __name__ == "__main__":
app.run()