質問編集履歴

3

コード変更

2017/09/22 01:19

投稿

kotlinlover
kotlinlover

スコア17

test CHANGED
File without changes
test CHANGED
@@ -113,3 +113,81 @@
113
113
 
114
114
 
115
115
  ```
116
+
117
+
118
+
119
+ ### 追記1
120
+
121
+
122
+
123
+ 選択経路を色を変えて、表現するのは
124
+
125
+ ``networkx``においてナンセンスだと思い直したので変更
126
+
127
+
128
+
129
+ 変更したことで``nx.connected_components(G)``で正しく連結成分を
130
+
131
+ 利用することができます
132
+
133
+
134
+
135
+ 以下のように変更
136
+
137
+ * 経路ではないedgeは表示しない
138
+
139
+
140
+
141
+ ![イメージ説明](4752c5dc587db5b39e60e95a1e3845fe.png)
142
+
143
+
144
+
145
+ ```python
146
+
147
+ import matplotlib.pyplot as plt
148
+
149
+ import networkx as nx
150
+
151
+
152
+
153
+ point_num = 3
154
+
155
+ line_num = point_num - 1
156
+
157
+ edge_num = point_num * line_num * 2
158
+
159
+ selected_edges_index = [0,5,10,11]
160
+
161
+
162
+
163
+ G = nx.grid_graph([point_num, point_num])
164
+
165
+ edges_copy = list(G.edges)
166
+
167
+ pos = dict((n, n) for n in G.nodes())
168
+
169
+ nx.draw_networkx_edge_labels(G, pos,{
170
+
171
+ edge:i for (i,edge) in enumerate(G.edges) #add number of edge
172
+
173
+ })
174
+
175
+ G.remove_edges_from(list(G.edges)) # remove all edges
176
+
177
+
178
+
179
+ for i in selected_edges_index:
180
+
181
+ G.add_edge(edges_copy[i][0],edges_copy[i][1])
182
+
183
+ nx.draw(G,pos,font_size=8)
184
+
185
+
186
+
187
+ #plt.savefig("grid.png")
188
+
189
+ plt.show()
190
+
191
+
192
+
193
+ ```

2

解決後 まとめ

2017/09/22 01:18

投稿

kotlinlover
kotlinlover

スコア17

test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,71 @@
45
45
  ###補足情報(言語/FW/ツール等のバージョンなど)
46
46
 
47
47
  python3.6
48
+
49
+
50
+
51
+ ###解決後 まとめ
52
+
53
+ ``networkx``を用いることで容易に経路の図示ができました
54
+
55
+
56
+
57
+ 回答して頂いたコードを以下のように変更しました
58
+
59
+
60
+
61
+ * 選択された経路の色を変更
62
+
63
+ * 経路の番号の表示
64
+
65
+
66
+
67
+ ![イメージ説明](2a4f8bb4c2b5f2d2736a49450bad80cf.png)]
68
+
69
+
70
+
71
+
72
+
73
+ ```python
74
+
75
+ import matplotlib.pyplot as plt
76
+
77
+ import networkx as nx
78
+
79
+
80
+
81
+ point_num = 3
82
+
83
+ line_num = point_num - 1
84
+
85
+ G = nx.grid_graph([point_num, point_num])
86
+
87
+ pos = dict((n, n) for n in G.nodes())
88
+
89
+
90
+
91
+ edge_num = point_num * line_num * 2
92
+
93
+ colors = ['k' for _ in range(edge_num)] # default color is black (k)
94
+
95
+ selected_edges_index = [0,5,10,11]
96
+
97
+ for index in selected_edges_index:
98
+
99
+ colors[index] = 'r' #change edge color to red
100
+
101
+
102
+
103
+ nx.draw_networkx_edge_labels(G, pos,{
104
+
105
+ edge:i for (i,edge) in enumerate(G.edges) #add number of edge
106
+
107
+ })
108
+
109
+ nx.draw(G,pos,edge_color=colors,font_size=8)
110
+
111
+ plt.savefig("grid.png")
112
+
113
+
114
+
115
+ ```

1

画像

2017/09/21 16:40

投稿

kotlinlover
kotlinlover

スコア17

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  (番号はついてなくてもいいです)
6
6
 
7
-
7
+ ![イメージ説明](366a319f2756bee3c70f467eba341d3e.png)
8
8
 
9
9
 
10
10