Possible Duplicate:
[python]: path between two nodes
Can anyone point me to some resources on how to do this? I’m using networkx as my python library.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is based on Alex Martelli’s answer, but it should work. It depends on the expression
source_node.childrenyielding an iterable that will iterate over all the children ofsource_node. It also relies on there being a working way for the==operator to compare two nodes to see if they are the same. Usingismay be a better choice. Apparently, in the library you’re using, the syntax for getting an iterable over all the children isgraph[source_node], so you will need to adjust the code accordingly.My main concern is that this does a depth first search, it will waste effort when there are several paths from the source to a node that’s a grandchild, great grandchild, etc all of source, but not necessarily a parent of sink. If it memoized the answer for a given source and sink node it would be possible to avoid the extra effort.
Here is an example of how that would work:
This also allows you to save the memoization dictionary between invocations so if you need to compute the answer for multiple source and sink nodes you can avoid a lot of extra effort.