質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

891閲覧

KeyErrorの対処法についてご教示頂き度

h-matsudo

総合スコア11

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

NumPy

NumPyはPythonのプログラミング言語の科学的と数学的なコンピューティングに関する拡張モジュールです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/10/19 07:26

現在Jupyter notebookでダイクストラ法を写経しておりますが

python3.x

1%matplotlib inline 2import matplotlib.pyplot as plt 3import networkx as nx 4import numpy as np 5 6weighted_elist = [('PS','Z',80),('PS','A',160),('Z','A',70), 7 ('Z','B',60),('Z','C',190),('A','B',50), 8 ('A','D',80),('B','C',140),('B','D',140), 9 ('C','D',110),('C','PA',70),('D','PA',70)] 10p = {'PS': (0,1),'Z':(1,2),'A':(1,0),'B':(2,1),'C':(3,2), 11 'D':(3,0), 'PA':(4,1)} 12G = nx.Graph() 13G.add_weighted_edges_from(weighted_elist) 14elbs = {(u,v):G[u][v]['weight'] for (u,v) in G.edges()} 15 16s = 'v1' 17nodes = set(G.nodes())-{s} 18T = set({}) 19for v in nodes: 20 sp = nx.dijkstra_path(G,s,v) 21 T=T.union({tuple(x) for x in np.array([sp[:-1],sp[1:]]).T}) 22T = list(T) 23 24nx.draw_networkx(G, pos=p, node_color='lightgrey', 25 node_size=500, width=1) 26nx.draw_networkx_edges(G, pos=p, edgelist=T, width=5) 27nx.draw_networkx_edge_labels(G, pos=p,edge_labels=elbs) 28plot.axis('off') 29plot.show()

でRUNしたところ

KeyError Traceback (most recent call last) <ipython-input-21-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で調べたところ、辞書にはない文字とのことで対処法を試して
みましたが、どれも解決には至りませんでした。

お手数ですがどなたかご教示頂ければ幸いです。
宜しくお願い申し上げます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tiitoi

2018/10/19 07:49 編集

質問は再編集できますよ。なので、https://teratail.com/questions/153029 かこちらのどちらか一方を解決済みにして一本化したほうがよいかと思います。
h-matsudo

2018/10/19 07:54

ご指摘ありがとうございます。一方を解決済みに致しました。
hayataka2049

2018/10/19 07:58

コードの出典はどこですか?
guest

回答1

0

ベストアンサー

nx.dijkstra()はあるネットワーク(コードのGに相当)においてスタートノード(コードのsに相当)からエンドノード(コードのvに相当)の最短経路を求めるものなので、sおよびvの中身はネットワークGのなかに存在する必要があります。しかしながらsには'v1'というノードが与えられています。この'v1'はweighted_elistに存在しないので、計算不能ということでエラーになっています。
したがって、s='ps'やs='z'などのようにネットワークGにあるものに変更すれば、結果が得られます。

投稿2018/10/19 07:59

R.Shigemori

総合スコア3376

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

h-matsudo

2018/10/20 04:41

無事解決致しました。 論理を理解しておらず、初歩的なミスを発見できませんでした。 申し訳ありません。 ご回答頂きありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問