現在
%matplotlib inline
import matplotlib,pyplot as plt
import networkx as nx
import numpy as np
でダイクストラ法を写経しておりますが、教科書通り入力すると
KeyError Traceback (most recent call last)
<ipython-input-18-db35ddf27575> in <module>()
19 T = set({})
20 for v in nodes:
---> 21 sp = nx.dijkstra_path(G,s,v)
22 T=T.union({tuple(x) for x in np.array([sp[:-1],sp[1:]]).T})
23 T = list(T)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py in dijkstra_path(G, source, target, weight)
158 """
159 (length, path) = single_source_dijkstra(G, source, target=target,
--> 160 weight=weight)
161 return path
162
~\AppData\Local\Continuum\anaconda3\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py in single_source_dijkstra(G, source, target, cutoff, weight)
450 """
451 return multi_source_dijkstra(G, {source}, cutoff=cutoff, target=target,
--> 452 weight=weight)
453
454
~\AppData\Local\Continuum\anaconda3\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py in multi_source_dijkstra(G, sources, target, cutoff, weight)
704 paths = {source: [source] for source in sources} # dictionary of paths
705 dist = _dijkstra_multisource(G, sources, weight, paths=paths,
--> 706 cutoff=cutoff, target=target)
707 if target is None:
708 return (dist, paths)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\networkx\algorithms\shortest_paths\weighted.py in _dijkstra_multisource(G, sources, weight, pred, paths, cutoff, target)
792 if v == target:
793 break
--> 794 for u, e in G_succ[v].items():
795 cost = weight(v, u, e)
796 if cost is None:
KeyError: 'v1'
となってしまいます。
Qiitaでは辞書に登録されていないためのエラーとの記載があり
対処法を試してみましたがどれも解決には至りませんでした。
どなたか対処法を教えて頂けますでしょうか。
宜しくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー