-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_mst_data.py
141 lines (115 loc) · 4.12 KB
/
collect_mst_data.py
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
from mst_lib import *
import networkx as nx
import numpy as np
def lattice_hexagon_points(radius):
points = set([(0, 0)])
current_rad = radius
while current_rad > 0:
# Diagonal sides
a, b = current_rad, current_rad
while b >= 0:
points.add((a, b))
points.add((a, -b))
points.add((-a, b))
points.add((-a, -b))
a += 1
b -= 1
# Top and bottom
a, b = current_rad, current_rad
while a >= 0:
points.add((a, b))
points.add((a, -b))
points.add((-a, b))
points.add((-a, -b))
a -= 2
current_rad -= 1
return points
def make_hex_grid(radius):
G = nx.Graph()
vertices = lattice_hexagon_points(radius)
for v in vertices:
G.add_node(v)
for v in vertices:
a, b = v
possible_neighbors = [
(a + 1, b + 1),
(a + 1, b - 1),
(a - 1, b + 1),
(a - 1, b - 1),
(a + 2, b),
(a - 2, b),
]
for n in possible_neighbors:
if n in vertices:
G.add_edge(v, n)
return G
if __name__ == "__main__":
import json
import jsonlines
print("Computing probabilities for hexagonal grid of radius 1")
radius = 1
G = make_hex_grid(radius)
n_spanning_trees = int(np.linalg.det(nx.laplacian_matrix(G).toarray()[:-1, :-1]))
with jsonlines.open("./enumerations/hex_grid_radius_1.jsonl", "w") as writer:
for T in tqdm(nx.SpanningTreeIterator(G), total=n_spanning_trees):
diameter = nx.diameter(T)
probability = compute_mst_prob(G, T)
writer.write(
{
"diameter": diameter,
"probability": str(probability),
"tree_edges": list(T.edges),
}
)
for i in range(2, 4):
print(f"Computing probabilities for {i}x{i} grid")
with jsonlines.open(f"./enumerations/mst_{i}x{i}_grid.jsonl", "w") as writer:
G = nx.grid_2d_graph(i, i)
n_spanning_trees = int(
np.linalg.det(nx.laplacian_matrix(G).toarray()[:-1, :-1])
)
for T in tqdm(nx.SpanningTreeIterator(G), total=n_spanning_trees):
diameter = nx.diameter(T)
probability = compute_mst_prob(G, T)
writer.write(
{
"diameter": diameter,
"probability": str(probability),
"tree_edges": list(T.edges),
}
)
for i in range(2, 5):
if i == 3:
continue
with jsonlines.open(f"./enumerations/mst_{3}x{i}_grid.jsonl", "w") as writer:
G = nx.grid_2d_graph(3, i)
n_spanning_trees = int(
np.linalg.det(nx.laplacian_matrix(G).toarray()[:-1, :-1])
)
for T in tqdm(nx.SpanningTreeIterator(G), total=n_spanning_trees):
diameter = nx.diameter(T)
probability = compute_mst_prob(G, T)
writer.write(
{
"diameter": diameter,
"probability": str(probability),
"tree_edges": list(T.edges),
}
)
for i in range(2, 8):
print(f"Computing probabilities for complete graph with {i} nodes")
with jsonlines.open(f"./enumerations/mst_complete_{i}.jsonl", "w") as writer:
G = nx.complete_graph(i)
n_spanning_trees = int(
np.linalg.det(nx.laplacian_matrix(G).toarray()[:-1, :-1])
)
for T in tqdm(nx.SpanningTreeIterator(G), total=n_spanning_trees):
diameter = nx.diameter(T)
probability = compute_mst_prob(G, T)
writer.write(
{
"diameter": diameter,
"probability": str(probability),
"tree_edges": list(T.edges),
}
)