質問編集履歴
6
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -184,7 +184,7 @@
|
|
184
184
|
|
185
185
|
def find_lowest_cost_node(costs):
|
186
186
|
|
187
|
-
lowest_cost = float("
|
187
|
+
lowest_cost = float("end")
|
188
188
|
|
189
189
|
lowest_cost_node = None
|
190
190
|
|
5
文法の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -184,92 +184,92 @@
|
|
184
184
|
|
185
185
|
def find_lowest_cost_node(costs):
|
186
186
|
|
187
|
+
lowest_cost = float("inf")
|
188
|
+
|
189
|
+
lowest_cost_node = None
|
190
|
+
|
191
|
+
# Go through each node.
|
192
|
+
|
193
|
+
for node in costs:
|
194
|
+
|
195
|
+
cost = costs[node]
|
196
|
+
|
197
|
+
# If it's the lowest cost so far and hasn't been processed yet...
|
198
|
+
|
199
|
+
if cost < lowest_cost and node not in processed:
|
200
|
+
|
201
|
+
# ... set it as the new lowest-cost node.
|
202
|
+
|
203
|
+
lowest_cost = cost
|
204
|
+
|
205
|
+
lowest_cost_node = node
|
206
|
+
|
207
|
+
return lowest_cost_node
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
# Find the lowest-cost node that you haven't processed yet.
|
212
|
+
|
213
|
+
node = find_lowest_cost_node(costs)
|
214
|
+
|
215
|
+
# If you've processed all the nodes, this while loop is done.
|
216
|
+
|
217
|
+
while node is not None:
|
218
|
+
|
219
|
+
cost = costs[node]
|
220
|
+
|
221
|
+
# Go through all the neighbors of this node.
|
222
|
+
|
223
|
+
neighbors = graph[node]
|
224
|
+
|
225
|
+
for n in neighbors.keys():
|
226
|
+
|
227
|
+
new_cost = cost + neighbors[n]
|
228
|
+
|
229
|
+
# If it's cheaper to get to this neighbor by going through this node...
|
230
|
+
|
231
|
+
if costs[n] > new_cost:
|
232
|
+
|
233
|
+
# ... update the cost for this node.
|
234
|
+
|
235
|
+
costs[n] = new_cost
|
236
|
+
|
237
|
+
# This node becomes the new parent for this neighbor.
|
238
|
+
|
239
|
+
parents[n] = node
|
240
|
+
|
241
|
+
# Mark the node as processed.
|
242
|
+
|
243
|
+
processed.append(node)
|
244
|
+
|
245
|
+
# Find the next node to process, and loop.
|
246
|
+
|
247
|
+
node = find_lowest_cost_node(costs)
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
print "Cost from the start to each node:"
|
252
|
+
|
253
|
+
print costs
|
254
|
+
|
255
|
+
```
|
256
|
+
|
257
|
+
### 発生している問題・エラーメッセージ
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
```
|
262
|
+
|
263
|
+
Traceback (most recent call last):
|
264
|
+
|
265
|
+
File "sample.py", line 76, in <module>
|
266
|
+
|
267
|
+
node = find_lowest_cost_node(costs)
|
268
|
+
|
269
|
+
File "sample.py", line 63, in find_lowest_cost_node
|
270
|
+
|
187
271
|
lowest_cost = float("end")
|
188
272
|
|
189
|
-
lowest_cost_node = None
|
190
|
-
|
191
|
-
# Go through each node.
|
192
|
-
|
193
|
-
for node in costs:
|
194
|
-
|
195
|
-
cost = costs[node]
|
196
|
-
|
197
|
-
# If it's the lowest cost so far and hasn't been processed yet...
|
198
|
-
|
199
|
-
if cost < lowest_cost and node not in processed:
|
200
|
-
|
201
|
-
# ... set it as the new lowest-cost node.
|
202
|
-
|
203
|
-
lowest_cost = cost
|
204
|
-
|
205
|
-
lowest_cost_node = node
|
206
|
-
|
207
|
-
return lowest_cost_node
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
# Find the lowest-cost node that you haven't processed yet.
|
212
|
-
|
213
|
-
node = find_lowest_cost_node(costs)
|
214
|
-
|
215
|
-
# If you've processed all the nodes, this while loop is done.
|
216
|
-
|
217
|
-
while node is not None:
|
218
|
-
|
219
|
-
cost = costs[node]
|
220
|
-
|
221
|
-
# Go through all the neighbors of this node.
|
222
|
-
|
223
|
-
neighbors = graph[node]
|
224
|
-
|
225
|
-
for n in neighbors.keys():
|
226
|
-
|
227
|
-
new_cost = cost + neighbors[n]
|
228
|
-
|
229
|
-
# If it's cheaper to get to this neighbor by going through this node...
|
230
|
-
|
231
|
-
if costs[n] > new_cost:
|
232
|
-
|
233
|
-
# ... update the cost for this node.
|
234
|
-
|
235
|
-
costs[n] = new_cost
|
236
|
-
|
237
|
-
# This node becomes the new parent for this neighbor.
|
238
|
-
|
239
|
-
parents[n] = node
|
240
|
-
|
241
|
-
# Mark the node as processed.
|
242
|
-
|
243
|
-
processed.append(node)
|
244
|
-
|
245
|
-
# Find the next node to process, and loop.
|
246
|
-
|
247
|
-
node = find_lowest_cost_node(costs)
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
print "Cost from the start to each node:"
|
252
|
-
|
253
|
-
print costs
|
254
|
-
|
255
|
-
```
|
256
|
-
|
257
|
-
### 発生している問題・エラーメッセージ
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
```
|
262
|
-
|
263
|
-
Traceback (most recent call last):
|
264
|
-
|
265
|
-
File "sample.py", line 76, in <module>
|
266
|
-
|
267
|
-
node = find_lowest_cost_node(costs)
|
268
|
-
|
269
|
-
File "sample.py", line 63, in find_lowest_cost_node
|
270
|
-
|
271
|
-
lowest_cost = float("end")
|
272
|
-
|
273
273
|
ValueError: could not convert string to float: end
|
274
274
|
|
275
275
|
|
4
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -254,6 +254,28 @@
|
|
254
254
|
|
255
255
|
```
|
256
256
|
|
257
|
+
### 発生している問題・エラーメッセージ
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
```
|
262
|
+
|
263
|
+
Traceback (most recent call last):
|
264
|
+
|
265
|
+
File "sample.py", line 76, in <module>
|
266
|
+
|
267
|
+
node = find_lowest_cost_node(costs)
|
268
|
+
|
269
|
+
File "sample.py", line 63, in find_lowest_cost_node
|
270
|
+
|
271
|
+
lowest_cost = float("end")
|
272
|
+
|
273
|
+
ValueError: could not convert string to float: end
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
```
|
278
|
+
|
257
279
|
|
258
280
|
|
259
281
|
### 試したこと
|
3
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -271,5 +271,3 @@
|
|
271
271
|
Ubuntu18.04
|
272
272
|
|
273
273
|
python2.7
|
274
|
-
|
275
|
-
python3.6.7
|
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
現在Pythonでプログラミングを学んでいるのですが幅優先探索で躓いてます。
|
6
6
|
|
7
|
-
下記のようなグラフを作り具体例のような処理を行いたいですが上手く動作させることが出来ません。
|
7
|
+
下記のようなグラフを作り具体例のような処理を行いたいのですが上手く動作させることが出来ません。
|
8
8
|
|
9
9
|
どなたかご教授していただけると助かります。
|
10
10
|
|
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,13 +32,13 @@
|
|
32
32
|
|
33
33
|
8.dの隣接ノードの中で一番重みの小さいhに100移す。
|
34
34
|
|
35
|
-
9.eの隣接ノードで重みが一番小さいEnd nodeに350移す。
|
36
|
-
|
37
|
-
10.fの隣接ノードで重みが一番小さいEnd nodeに150移す。
|
38
|
-
|
39
|
-
|
35
|
+
9.gの隣接ノードの中で一番重みの小さいiに400移す。
|
40
|
-
|
36
|
+
|
41
|
-
1
|
37
|
+
10.hの隣接ノードの中で一番重みの小さいEnd nodeに100移す。
|
38
|
+
|
39
|
+
11.eの隣接ノードで重みが一番小さいEnd nodeに350移す。
|
40
|
+
|
41
|
+
12.fの隣接ノードで重みが一番小さいEnd nodeに150移す。
|
42
42
|
|
43
43
|
13.iの隣接ノードで重みが一番小さいEnd nodeに400移す。
|
44
44
|
|