python - How can I iterate networkx edge data with functions that do not return an edge with key? -


using multigraph().

trying find use networkx functions iterate on edges produce (src, dst) edges without key , obtain key inside loop.

eg: networkx.dfs_edges()

iterates on edges depth-first producing (src, dst) tuples.

the function doesn't accept keys=true argument.

therefore inside loop following has different behaviours:

g.get_edge_data(edge)

import networkx nx # works keys g = nx.multigraph() g.add_edge(1,2) edge in g.edges_iter(keys=true):     print g.get_edge_data(*edge) 

{}

# gets dict edge key, function doesn't take `keys=true` arguments. edge in nx.dfs_edges(g):     print g.get_edge_data(*edge) 

{0: {}}

is possible iterate these sort of functions?

in particular example able get_edge_data of specific edge edge given dfs_edges, rather getting dictionaries multi-edges produce between 2 nodes of multi<di>graph

# eg: works. edge in g.edges_iter(keys=true):     print  g.edge[edge[0]][edge[1]][edge[2]] 

{}

# can't similar `dfs_edge` lack key information. edge in nx.dfs_edges(g):     print  g.edge[edge[0]][edge[1]][edge[2]] 

indexerror: tuple index out of range

the depth first search see first edge given node. if there 2 edges between 2 nodes (since it's multigraph), or there multiple paths through different nodes same node, ever return first edge encountered.

thus if have

g=nx.multigraph() g.add_edges_from([(1,2),(1,2),(1,2),(2,3),(3,4),(1,4)]) g.edges() > [(1, 2), (1, 2), (1, 2), (1, 4), (2, 3), (3, 4)] edge in nx.dfs_edges(g,1): #depth first search starting node 1.     print edge > (1, 2) > (2, 3) > (3, 4) 

it not return every single edge of network. note example, (1,4) , 2 of (1,2) edges not returned. if did depth first search starting @ 4:

for edge in nx.dfs_edges(g,4):     print edge > (4, 1) > (1, 2) > (2, 3) 

which misses (3,4) , again 2 of (1,2) edges.

so reason didn't include option key of edge returned because there isn't reason want return key. it's going return 1 of edges.

from question, looks expecting iterate through every single edge rather subset of edges crossed in dfs. if can desired output g i've defined, can try produce function. now, can do

for edge in nx.dfs_edges(g):     edgekeys = g.edge[edge[0]][edge[1]].keys()     print  g.edge[edge[0]][edge[1]][edgekeys[0]] 

which takes first key possible choices.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -