문제

I have my own objects, say pepperoni. I have a list of edges to an from every pepperoni and a list of pepperonis. I then build a graph using networkx. I'm trying to find the weight of the shortest path from one pepperoni to another. However, I'm getting an error as follows, which traces internal things from networkx as follows:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
  File "pizza.py", line 437, in shortestPath
    cost = nx.shortest_path_length(a, spepp, tpepp, True)
  File "/Library/Python/2.6/site-packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest_paths/generic.py", line 181, in shortest_path_length
    paths=nx.dijkstra_path_length(G,source,target)
  File "/Library/Python/2.6/site-packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest_paths/weighted.py", line 119, in dijkstra_path_length
    (length,path)=single_source_dijkstra(G,source, weight = weight)
  File "/Library/Python/2.6/site-packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest_paths/weighted.py", line 424, in single_source_dijkstra
    edata=iter(G[v].items())
  File "/Library/Python/2.6/site-packages/networkx-1.3-py2.6.egg/networkx/classes/graph.py", line 323, in __getitem__
    return self.adj[n]
KeyError: <pizza.pepperoni object at 0x100ea2810>

Any idea as to what is the error, or what I have to add to my pizza class in order to not get this KeyError?

Edit: I have my edges formatted correctly. I don't know if the objects can be handled as nodes though.

도움이 되었습니까?

해결책

If you have edges and nodes each as a list, then building a graph in networkx is straightforward. Given that your problem occurs in building your graph object, perhaps the best diagnostic is to go through graph construction in networkx step by step:

import networkx as NX
import string
import random

G = NX.Graph()    # initialize the graph

# just generate some synthetic data for the nodes and edges:
my_nodes = [ ch for ch in string.ascii_uppercase ]
my_nodes2 = list(my_nodes)
random.shuffle(my_nodes2)
my_edges = [ t for t in zip(my_nodes, my_nodes2) if not t[0]==t[1] ]

# now add the edges and nodes to the networkx graph object:
G.add_nodes_from(my_nodes)
G.add_edges_from(my_edges)

# look at the graph's properties:
In [87]: len(G.nodes())
Out[87]: 26

In [88]: len(G.edges())
Out[88]: 25

In [89]: G.edges()[:5]
Out[89]: [('A', 'O'), ('A', 'W'), ('C', 'U'), ('C', 'F'), ('B', 'L')]

# likewise, shortest path calculation is straightforward
In [86]: NX.shortest_path(G, source='A', target='D', weighted=False)
Out[86]: ['A', 'W', 'R', 'D']

In my experience, Networkx has an extremely permissive interface, in particular, it will accept a broad range of object types as nodes and edges. A node can be any hashable object except None.

The only thing i can think of that might cause the error you presented in your Q is that perhaps after you crated the graph you directly manipulated the graph object (the dict, *G*), which you shouldn't do--there are plenty of accessor methods.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top