diff options
author | haturatu <taro@eyes4you.org> | 2025-04-10 23:23:52 +0900 |
---|---|---|
committer | haturatu <taro@eyes4you.org> | 2025-04-10 23:23:52 +0900 |
commit | 3c5f6d28d374d88048ddbb03e56ae83642d07704 (patch) | |
tree | 0706c0e57a932be798e281a20a51071858950b3e |
first commit
-rw-r--r-- | .python-version | 1 | ||||
-rwxr-xr-x | main.py | 62 | ||||
-rw-r--r-- | requirements.txt | 14 |
3 files changed, 77 insertions, 0 deletions
diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..9919bf8 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.10.13 @@ -0,0 +1,62 @@ +import numpy as np +from collections import deque +import random +import networkx as nx +import matplotlib.pyplot as plt +import ot + +def adj_to_dist(adj_matrix): + n = len(adj_matrix) + distance_matrix = np.full((n, n), np.inf) + + for start in range(n): + # BFS + queue = deque([start]) + distances = [-1] * n + distances[start] = 0 + + while queue: + current = queue.popleft() + for neighbor in range(n): + if adj_matrix[current][neighbor] == 1 and distances[neighbor] == -1: + distances[neighbor] = distances[current] + 1 + queue.append(neighbor) + + for end in range(n): + if distances[end] != -1: + distance_matrix[start][end] = distances[end] + + return distance_matrix.astype(int) + +def plot_graph(adj_matrix): + G = nx.from_numpy_array(adj_matrix) + pos = nx.spring_layout(G, seed=42) + nx.draw(G, pos, with_labels=True, node_color='skyblue', edge_color='gray', + node_size=800, font_size=12, font_weight='bold') + + plt.title("Graph") + plt.savefig("graph.png") + print("Saved: 'graph.png' に保存しました") + +def gen_random_adj_matrix(n, edge_prob=0.3, seed=None): + # n : ノード数 + # edge_prob : 各辺が存在する確率 + # seed : ランダムシード + + if seed is not None: + np.random.seed(seed) + + # 上三角行列にランダムな0/1を生成(対角は0) + upper_tri = np.triu(np.random.rand(n, n) < edge_prob, k=1).astype(int) + + # 対称行列に変換(上三角 + 転置) + adj_matrix = upper_tri + upper_tri.T + + return adj_matrix + +def main(): + adj = gen_random_adj_matrix(6, edge_prob=0.5, seed=42) + plot_graph(adj) + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fda5753 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,14 @@ +contourpy==1.3.1 +cycler==0.12.1 +fonttools==4.57.0 +kiwisolver==1.4.8 +matplotlib==3.10.1 +networkx==3.4.2 +numpy==2.2.4 +packaging==24.2 +pillow==11.1.0 +POT==0.9.5 +pyparsing==3.2.3 +python-dateutil==2.9.0.post0 +scipy==1.15.2 +six==1.17.0 |