4

I want to get only the directed path between two nodes in directed G using networkx.

Currently, I am doing this:

G=nx.fast_gnp_random_graph(NUMBER_OF_NODES,PPROB_OF_EDGE,True)
nx.bidirectional_dijkstra(G,u, v) #u and v are some nodes in G

This will return a path even if edges list of G only have following entries

[(u,w),(v,w)]

My goal is to get the directed path only. Is there a way in networkx to do it?

1 Answer 1

2

When you do

G=nx.fast_gnp_random_graph(NUMBER_OF_NODES,PPROB_OF_EDGE,True)

you end up with an undirected graph. Thus the edge (u,w) is an undirected edge --- there is no difference between (u,w) and (w,u). It doesn't make sense to talk about a "directed path".

I believe you want the graph to be directed, and that is what your True argument at the end is. But because of the order that nx.fast_gnp_random_graph takes its optional arguments, this isn't what you're doing. Instead you should say

G = nx.fast_gnp_random_graph(NUMBER_OF_NODES, PROB_OF_EDGE, directed=True)

(and as a note, I think generally capital letters are frowned upon in python code).

1
  • Thanks. This code snippet is a part of a larger code and I am using the capital letters to represent global variable :) Commented Apr 23, 2017 at 2:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.