Repository navigation
Shortest path with only a target specified. - #744
Conversation
|
How about just making operators.unary.reverse() call the graph method reverse()? (We do that with subgraph). The reverse operation can be pretty "low level" depending on the implementation and I think it is wise not to be swapping around the graph class data outside of the class methods. For the additions to the shortest path (target only case) I'm fairly ambivalent. It is simple code but makes the documentation for usage much more complicated. |
|
For the target-only addition to shortest_path, we are really just adding a fourth case to a function that already has 3 different output values. Given that it is completing the set, I didn't see how this would be that much more complicated. I wonder if the overall style of the changes is what made it seem more complicated. My goal was to make the existing docstring more clearly delineate the various return types and their formats. Below is a simpler attempt...perhaps better? If not, I'll yield on this feature as it could easily be achieved through a separate function. |
|
Now on the reverse() method. I definitely expected this to be controversial. There are two approaches we are considering:
Presently, NetworkX takes the first approach, though a nx.reverse() function does not yet exist. My proposal is that we switch to the second approach. Let me elaborate a bit more on the DFA example I provided earlier, as I think it does a good job showing the difficulties the first approach runs into. I believe the reverse() method to be significantly different from other methods such as add_node(), out_degree(), subgraph(), etc. The reason has to do with how subclasses are likely to override various methods. Most derived classes will only need to make cosmetic changes to add_node, out_degree, subgraph...if any at all. However, derived classes might need to make incompatible changes to reverse(). This is because the meaning of "reverse" is really discipline dependent, whereas the interpretation for, say, add_node() is uniform across disciplines. When an incompatible change is made, all functions in NetworkX which relied on the original behavior will cease to work correctly. The advantage of implementing "reverse" as a function is that the definition is always available to NetworkX functions and methods, even if a subclass makes an incompatible change to the reverse() method. The example that makes this very clear is a class for deterministic finite automata. One way to implement this is to derive from nx.MultiDiGraph. The set of all DFAs is not closed under the reversal operation. In general, you get an NFA. So the DFA class must override the NetworkX-provided reverse() method. Otherwise, you end up with a non-deterministic DFA (which makes no sense at all). Two possible overrides are:
In both overrides, there is no guarantee that the final object is compatible in the same way as the original reverse() method. The topology and even the number of states is completely different in the second option. The result is that NetworkX functions will stop working if they make a call to G.reverse(). But if they made a call to nx.reverse() which contained the real implementation of the reverse operation, then this particular subclass could continue to make use of NetworkX functions. Now again, this risk is present with any overridden method. The question is whether it is more likely with something like reverse(), and if so, should NetworkX do anything to make it less of a problem? There probably is no simple answer. If a change is not made, then downstream libraries will need to reimplement NetworkX functions so that they call, say, MultiDiGraph.reverse() instead of G.reverse(). One could also implement DFAs via the has-a MultiDiGraph architecture (though this seems quite unnatural). Of all the methods provided by the base classes, I think reverse() is probably the only one where this issue will come up...perhaps also for subgraph(), but less likely. |
|
Test results for commit cab2ce8 merged into master
Not available for testing: |
|
Name Issue: As for the namespace problem where subclasses could be tempted to use the name "reverse", we could rename the method "reverse_edges", as it more precisely describes what happens. Any change in API is difficult for backward compatibility, but can be managed. Maybe we can start a list of name changes for version 2.0. For the moment it is probably easier to keep it as "reverse" until 2.0. Extra functionality: |
|
Whether we make the change for a matrix representation of a graph in some universal nx.reverse() function that does type-checking or whether we make the change in a method seems mostly book-keeping to me. But I do agree that this is just a namespace hack for something that should be a method. I'm also -1 on a name change before 2.0. Ok...I will revert the changes which gave preference to nx.reverse() over G.reverse(). Then, I'll use the docstring in the comment, while keeping the new target-only specification. We can see what people think after that. |
|
I'm going to try to rebase from master and then squash some of these commits to give a cleaner history. Not sure how this will affect the pull request though. |
|
Had to force push after a rebase -i, but it looks like that worked. Comments are appreciated. |
|
Looks good. Maybe add a line in the docs for this new function at https://raw.github.com/networkx/networkx/master/doc/source/reference/algorithms.operators.rst |
|
Ok, this should be good to merge. I've rebased against the current master. |
|
Is there anything holding this up? I'd like to get it merged it before other changes to shortest_path are brought in, such #762. I've purposely held off on merging it myself...but if we're okay with pull requesters merging their own requests, I'll do so. |
Shortest path with only a target specified.
|
I don't think there is anything holding this up except my inexperience with github. I know I can just hit a button to merge the pull request, but am not comfortable doing so without doing the merge locally and playing with it a little. But, I am working on getting the github workflow into my brain. So, I think rather than having Aric and Jordi be in charge of all the pull requests it would be better for you to merge this request yourself. I have looked through all the code/diffs and it all makes sense and seems in good order. So a second pair of eyes have looked at it. On Sep 3, 2012, at 12:30 AM, chebee7i wrote:
|
|
Thanks for looking at it @dschult. I merged the PR yesterday. |
Presently, calling
returns an exception even though it is clear what the user would like. Namely, a dictionary keyed by sources with values representing paths from the source to the specified target:
This pull request implements target-only calls to nx.shortest_path(). As part of it, I added a reverse() operator to NetworkX, and now, algorithms use that operator rather than using the reverse() method on the classes. My guess is that this particular change will be the most controversial. Some comments:
Comments are appreciated.