回答編集履歴
2
修正
test
CHANGED
@@ -24,15 +24,17 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
# ノード。
|
27
|
+
# ノード。碁盤目状に配置
|
28
|
+
|
29
|
+
N, D = 10, 0.95 # 碁盤の大きさ, ノードの密度
|
28
30
|
|
29
31
|
random.seed(110)
|
30
32
|
|
31
|
-
pos = list(product(range(
|
33
|
+
pos = list(product(range(N), repeat=2))
|
32
34
|
|
33
35
|
random.shuffle(pos)
|
34
36
|
|
35
|
-
pos = {f'{p[0]}{p[1]}':p for p in pos[:
|
37
|
+
pos = {f'{p[0]}{p[1]}':p for p in pos[:int(len(pos)*D)]}
|
36
38
|
|
37
39
|
|
38
40
|
|
@@ -66,4 +68,10 @@
|
|
66
68
|
|
67
69
|
```
|
68
70
|
|
71
|
+
密度=0.5
|
72
|
+
|
73
|
+
![イメージ説明](1842eaeaf383da268e54c6b94fe434d2.png)
|
74
|
+
|
75
|
+
密度=0.95
|
76
|
+
|
69
|
-
![イメージ説明](
|
77
|
+
![イメージ説明](e4bf3a1d25787b0a6bbe91ea400fc894.png)
|
1
修正
test
CHANGED
@@ -16,17 +16,47 @@
|
|
16
16
|
|
17
17
|
import networkx as nx
|
18
18
|
|
19
|
-
from itertools import combinations
|
19
|
+
from itertools import combinations, product
|
20
|
+
|
21
|
+
import random
|
22
|
+
|
23
|
+
import math
|
20
24
|
|
21
25
|
|
22
26
|
|
27
|
+
# ノード。5x5の碁盤目状の20個
|
28
|
+
|
29
|
+
random.seed(110)
|
30
|
+
|
31
|
+
pos = list(product(range(5), repeat=2))
|
32
|
+
|
33
|
+
random.shuffle(pos)
|
34
|
+
|
23
|
-
pos = {'
|
35
|
+
pos = {f'{p[0]}{p[1]}':p for p in pos[:20]}
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
# エッジ。ノード間の距離が1以下
|
40
|
+
|
41
|
+
nodes = pos.keys()
|
42
|
+
|
43
|
+
edges = []
|
44
|
+
|
45
|
+
for n1, n2 in combinations(nodes, 2):
|
46
|
+
|
47
|
+
p1, p2 = pos[n1], pos[n2]
|
48
|
+
|
49
|
+
if math.sqrt(abs(p1[0]-p2[0]) + abs(p1[1]-p2[1])) <= 1:
|
50
|
+
|
51
|
+
edges.append((n1, n2))
|
52
|
+
|
53
|
+
|
24
54
|
|
25
55
|
G = nx.Graph()
|
26
56
|
|
27
|
-
G.add_nodes_from(
|
57
|
+
G.add_nodes_from(nodes)
|
28
58
|
|
29
|
-
G.add_edges_from(
|
59
|
+
G.add_edges_from(edges)
|
30
60
|
|
31
61
|
|
32
62
|
|
@@ -36,4 +66,4 @@
|
|
36
66
|
|
37
67
|
```
|
38
68
|
|
39
|
-
![イメージ説明](6
|
69
|
+
![イメージ説明](671d66d3c9066bd58f0d5da8e0a22cdb.png)
|