Repository navigation
Conversation
|
Andrey, This implementation looks good to me, and the tests that you wrote are nice. The only thing to do for the unweighted case is to update the docstrings of Regarding the weighted case, do you think that it is worth to also support restrictions? We could use the same approach, but I'm not sure that it is as useful as in the unweighted case because you always can manipulate the weights to impose restrictions. @hagberg wrote the current implementation of |
|
Test results for commit 0d7f141 merged into master (192d2e7)
|
|
2012/8/31 Jordi Torrents [email protected]
Thanks. I think support for the weighted case should be also implemented, and Also I think that all shortest_path procedures should support When everyone agrees on the interface I will also push my Best wishes, |
|
This all looks intriguing and useful. Have you done any speed tests for the case when restrictions are not needed? (Some other algorithms call shortest_path repeatedly and so want it to not be slow.) Can this be done for ALL the algorithms of network using a subclass of Graph/DiGraph rather than a change to each algorithm? (The subclass could hold the list of nodes and/or edges that get restricted before returning edges/neighbors/etc.) Nice work! On Aug 31, 2012, at 9:59 AM, aparamon wrote:
|
|
2012/8/31 Dan Schult [email protected]
My change was designed to not slow down the common case of no
I think it's not better compared to just modifying graph in-place Best wishes, |
I agree. These arguments make sense. I'm +1 for this approach. How many of the other shortest_path routines would be straightforward to update? |
|
2012/8/31 aparamon [email protected]
Good points. I agree. We should support restrictions for all shortest
That's great. Thank you very much for your work on this! |
|
2012/8/31 Dan Schult [email protected]
They are all more straightforward to modify than to add doc Best wishes, |
|
I've just committed Yen algorithm implementation (I'm a bit confused that git placed the commit into this pull request but I guess it's Ok). Please review my implementation. It may contain mistakes as Yen's wording is not very clear, imho. In particular I'm missing the explicit mentioning of the problem of duplicate items in the list B. I've solved it rather efficiently by using powerful data structures but maybe there shouldn't be any duplicate items at all? Also please comment on naming. Originally I wanted to use more generic name, but all_simple_paths is already taken, and having simple_paths along with all_simple_paths basically returning the same stuff would be too annoying. |
|
Andrey, The implementation of Yan's looks good to my (on a first sight and without reading the paper nor testing it), it will be a great addition to NetworkX. But we have to use one pull request for each feature. We just migrated our development workflow from trac to github last july, so we are still adjusting (i'm pretty new to git myself) and still have to write proper documentation on the development workflow for NetworkX. We are trying to follow the same approach used in other scipy python projects. I think that the best documentation available is from numpy: http://docs.scipy.org/doc/numpy/dev/gitwash/development_setup.html The main idea is: one new branch for each feature, send a pull request from that branch against NetworkX master, and keep the master branch of your fork up to date fetching from NetworkX master (using the alias Also, notice that with the merge of Chris' enhancement of shortest paths (#744) you changes from commit 0d7f141 do not merge clearly with master anymore. At this point maybe the easiest option is to close this pull request, and then make two new ones following numpy development workflow. What do you think? Sorry for the trouble. |
|
2012/9/4 Jordi Torrents [email protected]
I see the following message in the pull request thread: I thought it means "your change was committed upstream". Apparently it I'll try to study git a little, but it would take time. I absolutely Best wishes, |
|
Thanks Andrey for your patches. We're still adjusting to the workflow after switching to Github (and don't have it documented yet). |
|
2012/9/4, aparamon [email protected]:
That comment was made by a script (adapted from @ipython) for testing
Thank you very much for your code. I'll work on arranging/completing Salut! |
|
Jordi, I can take a look at preparing these patches today. It looks like it should be simple, so I should finish it. Also, the script that tests github pull requests did not seem to run each time a pull request was updated. So while helpful for the initial pull request, we'd need additional feedback anytime the request is updated or else we risk a false sense of security. |
|
It seems there is no way to make pull requests dependent on other pull requests. Does anyone have ideas on how this should be managed, or how other projects have done it? In this situation, the pull request for K-shortest paths is based off the restricted shortest paths pull request. Also, what has been decided about implementing this for the weighted case and other similar shortest path functions? Couldn't almost every NetworkX algorithm be adapted to look over a subset of the graph's nodes or edges? I suppose it is a matter of what people are likely to use. As for now, only one of the four possible ways to call nx.shortest_path will make use of the ignore_nodes and ignore_edges arguments. This is not ideal in my mind, and we should aim to support all call cases. Every time I look at the shortest_path code, I can't help but wonder if we can find a way to make it easier to extend---there really are so many places to touch to keep things consistent. Is there a huge difference in speed between the shortest_path and shortest_path_lengths functions? |
|
Hi Chris, 2012/9/9 chebee7i [email protected]
Great! yes, please feel free to work on this.
The script has to be run manually. You are right that it may give a Salut! |
|
2012/9/9 chebee7i [email protected]
I think that there is no way to link pull requests like that. A
Andrey suggested to support restrictions for all shortest paths Dan suggested to create a subclass that filters nodes and edges to |
|
I think that the rationale behind You are right that shortest path functions are difficult to extend. I think it would be very useful if we can refactor them to make extending them simpler. |
|
Ok, I clearly haven't been able to find time to do this. I'll still try to tackle this, but if someone wants to do it now, feel free. |
|
Just wanted to add that I have also found need for shortest path algorithms with path restrictions. Basically I have a road atlas with nodes ad road intersections and streets as the edges. One-way streets are handles using a Digraph. However there are other driver restrictions, e.g. sometimes you cannot make a left turn. Given 3 nodes [1,2,3], it is feasible to traverse from node 1 to 2 and also form 2-3, but it is not possible to traverse the sequence 1-2-3. But it is feasible to travel the sequence 3-2-1. IT should also be possible to travel 1-2-x-3 for any other node x. I modified the a* algorithm to test if the current head of the explored path is in a set of restricted sub-paths. A single parameter gets passed to a*, the function that will return True or False if the current path is valid or not. The complexity lies in this function.Most of my restricted sequences are only 3 nodes, so a quick check to see if the latest 3 nodes in order exist in a set of restricted sub-paths (tuples). Some of my restricted paths are longer so there is some iteration that is not implemented very fast but these restrictions are rare. I don't know how far the above discussion and implementation has gone towards providing such functionality but I wanted to let the developers know that there is demand for having shortest-path algorithms with restricted sub-paths. |
|
I'm not sure if this is relevant for you, but you can view a road atlas with each street segment being a node. Then intersections act as edges. If there is no left turn than an edge doesn't connect those two nodes. This might relieve the issue of restricted sub-paths. But there may be other issues with this approach. I haven't used it myself. On Oct 23, 2012, at 12:04 PM, timstirling wrote:
|
|
That is an interesting idea that I hadn't thought of. My concern would be how many of the algorithms work, e.g. I use edge attributes to encode the distance between nodes allowing a* algorithms and variants. My change to the a* was really tiny, the only complexity I had was checking is the end of a sequence of nodes exists in the set of restricted paths, which is mostly easy except for restricted paths with many edges, even then if is maybe 6 lines of code. I just wanted to let the developers know that there are common instances when you want to restrict shortest paths. Tim On Oct 23, 2012, at 9:34 AM, Dan Schult wrote:
|
|
Hello Tim! I think the standard solution to your problem is to represent each Anyway, thanks for your voting for the restrictions feature. Hope it's Hope it helps, |
|
I'd like to add code for shortest paths with restrictions, but I don't like the idea of putting node/edge filtering code into many different algorithms. It will make those algorithms harder to use, test, and understand. What is the best alternative to that? Clearly you can modify the graph before calling the existing functions. Are there clean and efficient ways to engineer a subclass or context class to do this? |
|
2013/1/18 Aric Hagberg [email protected]
Context class seems easy to do, robust and clean to me. However this won't Best wishes, |
|
#1011 (just to hook up related Issues/PRs) |
|
I also thought there was more about views than I can find. But I'll try to From a speed perspective, the idea is that views could be faster than From a low memory perspective, the idea is that views might be slimmer than On Fri, Apr 11, 2014 at 1:05 PM, chebee7i [email protected] wrote:
|
|
A graph view does not necessarily have to create a dict for each call to G[n]. It can/should instead return an O(1)-space lightweight filtering proxy. This in particular makes sense for algorithms like Dijkstra's, which examines each edge only once. The same argument goes for materializing a graph view into a graph. Filling up countless dicts of dicts is just not in any way time- and memory-friendly compared enumerating items in existing dicts. For a case where graph copying kills performance, see #1106. |
|
On Thu, Apr 17, 2014 at 1:43 AM, ysitu [email protected] wrote:
Creating a view that has Gview[n] return an iterator while the graph itself |
Maybe we can use this approach for start playing with how it might look like a NetworkX API that returns iterators for all Graph methods. Also we will be doing steps for solving the problem of being able to filter efficiently nodes and edges, a feature that will be useful in a wide range of situations. |
|
In light of #793, I just reread this discussion. This definitely needs to be looked at again wrt 2.0. |
This function is based on Yen's algorithm for finding k shortest paths. It generates all simple paths between source and target starting from the shortest one. See networkx#762 and networkx#793 for prior work on this and discussion. This implementation is based on Andrey's work on networkx#762. It generalizes his approach there to the weighted case, as in Greg's implementation posted on networkx#793. Regarding the discussion on networkx#762 about how to do the filtering of nodes and edges efficiently, this PR adds private modifications of the functions `bidirectional_shortest_path` and `bidirectional_dijkstra` (that accept containers with nodes and edges to exclude during the SP search) for its exclusive use. This still needs work, especially on documentation and tests. I'm pushing it so we can have the discussion with the code available. One thing to discuss is whether or not to include an one liner function to get the k shortest paths. Could be something like this: ```python from itertools import islice def k_shortest_paths(G, source, target, k, weight=None): return list(islice(nx.shortest_simple_paths(G, source, target, weight=weight), k)) ``` I think that, given that `all_simple_paths` is way faster in computing all paths, the principal use of `shortest_simple_paths` will be to get the k best/shortest paths. So it will be a more user friendly interface if we add it. However, we could also add this function as example in the `shortest_simple_paths` docstring. I'm ok either way. Thoughts?
This function is based on Yen's algorithm for finding k shortest paths. It generates all simple paths between source and target starting from the shortest one. See networkx#762 and networkx#793 for prior work on this and discussion. This implementation is based on Andrey's work on networkx#762. It generalizes his approach there to the weighted case, as in Greg's implementation posted on networkx#793. Regarding the discussion on networkx#762 about how to do the filtering of nodes and edges efficiently, this PR adds private modifications of the functions `bidirectional_shortest_path` and `bidirectional_dijkstra` (that accept containers with nodes and edges to exclude during the SP search) for its exclusive use. This still needs work, especially on documentation and tests. I'm pushing it so we can have the discussion with the code available. One thing to discuss is whether or not to include an one liner function to get the k shortest paths. Could be something like this: ```python from itertools import islice def k_shortest_paths(G, source, target, k, weight=None): return list(islice(nx.shortest_simple_paths(G, source, target, weight=weight), k)) ``` I think that, given that `all_simple_paths` is way faster in computing all paths, the principal use of `shortest_simple_paths` will be to get the k best/shortest paths. So it will be a more user friendly interface if we add it. However, we could also add this function as example in the `shortest_simple_paths` docstring. I'm ok either way. Thoughts?
|
There is now an alternate way of ignoring edges (or nodes) due to pull request #1690. For example, to ignore all edges with the color "red": You can also effectively ignore edges this way, by ignoring all edges incident to the node you wish to ignore: This was also requested in issue #1946. |
Many interesting algorithms e.g. Yen's k-shortest simple paths, White's node-independent paths require a procedure for finding shortest path with restrictions:
It is proposed to introduce optional ignore_nodes and ignore_edges parameters for all shortest path procedures.
Here is the initial version (only supporting shortest_path, unweighted case).