initial commit

This commit is contained in:
2026-03-02 20:55:36 +09:00
commit f224644b49
86 changed files with 1065250 additions and 0 deletions
@@ -0,0 +1,36 @@
import numpy as np
def generate_graph(NODES=10, DENSITY=0.1, DISTRIBUTION=('lognormal', (100, 1)), SEED=42):
rng = np.random.default_rng(SEED)
# Calculate variables
max_edges = (NODES - 1) * NODES
edges = int(round(DENSITY * max_edges))
# Make edges
sel = rng.choice(max_edges, size=edges, replace=False)
# Make distance array
dist_name, dist_params = DISTRIBUTION
if dist_name == "lognormal":
mean, sigma = dist_params
distances = rng.lognormal(mean=mean, sigma=sigma, size=edges)
elif dist_name == "uniform":
mean = dist_params[0]
distances = rng.uniform(0.0, 2.0 * mean, size=edges)
elif dist_name == "exponential":
mean = dist_params[0]
distances = rng.exponential(scale=mean, size=edges)
else:
raise ValueError("Unknown DISTRIBUTION")
# Make adj
adj = [[] for _ in range(NODES)]
for idx, dist in zip(sel, distances):
u = idx // (NODES-1)
r = idx % (NODES-1)
v = r if r < u else r + 1
adj[u].append((v, dist))
return adj
@@ -0,0 +1,44 @@
import numpy as np
def outdegree_generate_graph(NODES, DENSITY, DISTRIBUTION, SEED=42):
rng = np.random.default_rng(SEED)
max_edges = (NODES - 1) * NODES
edges = int(round(DENSITY * max_edges))
base = edges // NODES
rem = edges % NODES
adj = [[] for _ in range(NODES)]
dist_name, dist_params = DISTRIBUTION
if dist_name == "lognormal":
mean, sigma = dist_params
dist_func = lambda size: rng.lognormal(mean=mean, sigma=sigma, size=size)
elif dist_name == "uniform":
mean = dist_params[0]
dist_func = lambda size: rng.uniform(0.0, 2.0 * mean, size=size)
elif dist_name == "exponential":
mean = dist_params[0]
dist_func = lambda size: rng.exponential(scale=mean, size=size)
else:
raise ValueError("Unknown DISTRIBUTION")
for u in range(NODES):
d_u = base + (1 if u < rem else 0)
if d_u == 0:
continue
targets = rng.choice(NODES-1, size=d_u, replace=False)
targets = np.where(targets < u, targets, targets + 1)
weights = dist_func(d_u)
weights = np.rint(weights).astype(np.int64)
weights = np.maximum(weights, 1)
for v, w in zip(targets, weights):
adj[u].append((v, float(w)))
return adj