Skip to content

Shortest path with only a target specified. - #744

Merged
hagberg merged 4 commits into
networkx:masterfrom
chebee7i:shortest_path
Sep 3, 2012
Merged

hagberg merged 4 commits into
networkx:masterfrom
chebee7i:shortest_path

Conversation

@chebee7i

Copy link
Copy Markdown
Member

Presently, calling

G = nx.path_graph(5)
nx.shortest_path(G, target=4)

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:

{0: [0, 1, 2, 3, 4], 1: [1, 2, 3, 4], 2: [2, 3, 4], 3: [3, 4], 4: [4]}

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:

  1. Having a reverse() operator is consistent with the other operators that NetworkX provides (union, complement, etc).
  2. There does not exist a union (or complement or ...) method on the graph classes. This actually makes it seem that there should not even be a reverse method, but since it has been there since the beginning, removing it is probably not an option.
  3. Downstream, classes derived from NetworkX classes can reimplement the reverse() method in a manner which fits their use case. As an example, a reverse method on a DFA class might return the minimal DFA which recognizes its reverse language. If algorithms used G.reverse(), then functions like nx.shortest_path() would not work correctly. Although this same argument could be applied to other graph methods, it seems more likely for the reverse method than for others. Reversing, in principle, can mean many things to different domains, whereas add_node, does not vary as much.

Comments are appreciated.

@ghost ghost assigned hagberg Aug 1, 2012
@hagberg

hagberg commented Aug 5, 2012

Copy link
Copy Markdown
Member

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.

@chebee7i

chebee7i commented Aug 6, 2012

Copy link
Copy Markdown
Member Author

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.

    """Compute shortest paths in the graph.

    Parameters
    ----------
    G : NetworkX graph

    source : node, optional
       Starting node for path.
       If not specified, compute shortest paths using all nodes as source nodes.

    target : node, optional
       Ending node for path.
       If not specified, compute shortest paths using all nodes as target nodes.

    weight : None or string, optional (default = None)
       If None, every edge has weight/distance/cost 1.
       If a string, use this edge attribute as the edge weight.
       Any edge attribute not present defaults to 1.

    Returns
    -------
    path: list or dictionary
        All returned paths include both the source and target in the path.

        If the source and target are both specified, return a single list
        of nodes in a shortest path from the source to the target.

        If only the source is specified, return a dictionary keyed by
        targets with a list of nodes in a shortest path from the source
        to one of the targets.

        If only the target is specified, return a dictionary keyed by
        sources with a list of nodes in a shortest path from one of the
        sources to the target.

        If neither the source nor target are specified return a dictionary
        of dictionaries with path[source][target]=[list of nodes in path].

    ...

@chebee7i

chebee7i commented Aug 6, 2012

Copy link
Copy Markdown
Member Author

Now on the reverse() method. I definitely expected this to be controversial. There are two approaches we are considering:

  1. Implement reverse as a method and make nx.reverse() call G.reverse()
  2. Implement reverse as a function and make G.reverse() call nx.reverse()

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:

  1. Make G.reverse() return an NFA
  2. Make G.reverse() return a DFA, the conversion of the NFA that results from reversal

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.

@jtorrents

Copy link
Copy Markdown
Member

Test results for commit cab2ce8 merged into master
Platform: linux2

  • python2.6: OK
  • python2.7: OK
  • python3.2: OK

Not available for testing:

@dschult

dschult commented Aug 8, 2012

Copy link
Copy Markdown
Member

Name Issue:
The proposed nx.reverse() function accesses the data structure directly when copy=False and I think that qualifies it for placement in the class as a method. For example, if we get a matrix representation into a graph class, it may not have a succ or pred attribute and we would want to "transpose" instead. So the nx.reverse would need to be changed. We could add the function though and have it call the class method (similar to subgraph). That's what I would prefer to do.

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:
I like the doc_string proposed in the comment #744 (comment) better than in the pull request. The code is fairly simple and the docstring is almost the same as we have, but more complete in that it handles the remaining input case.

@chebee7i

chebee7i commented Aug 9, 2012

Copy link
Copy Markdown
Member Author

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.

@chebee7i

chebee7i commented Aug 9, 2012

Copy link
Copy Markdown
Member Author

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.

@chebee7i

chebee7i commented Aug 9, 2012

Copy link
Copy Markdown
Member Author

Had to force push after a rebase -i, but it looks like that worked. Comments are appreciated.

@hagberg

hagberg commented Aug 9, 2012

Copy link
Copy Markdown
Member

@chebee7i

Copy link
Copy Markdown
Member Author

Ok, this should be good to merge. I've rebased against the current master.

@chebee7i

chebee7i commented Sep 3, 2012

Copy link
Copy Markdown
Member Author

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.

hagberg added a commit that referenced this pull request Sep 3, 2012
Shortest path with only a target specified.
@hagberg
hagberg merged commit 3ad2f2b into networkx:master Sep 3, 2012
@dschult

dschult commented Sep 4, 2012

Copy link
Copy Markdown
Member

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.
Thanks,
Dan

On Sep 3, 2012, at 12:30 AM, chebee7i wrote:

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.

—
Reply to this email directly or view it on GitHub.

@hagberg

hagberg commented Sep 4, 2012

Copy link
Copy Markdown
Member

Thanks for looking at it @dschult. I merged the PR yesterday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants