Skip to content

Shortest path algorithms with restrictions - #762

Closed
aparamon wants to merge 4 commits into
networkx:masterfrom
aparamon:master
Closed

aparamon wants to merge 4 commits into
networkx:masterfrom
aparamon:master

Conversation

@aparamon

Copy link
Copy Markdown
Contributor

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:

  • ignore a subset of graph nodes
  • ignore certain graph edges

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).

@jtorrents

Copy link
Copy Markdown
Member

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 shortest_path and friends. This will be very helpful for implementing White and Newman's approximation for node independent paths.

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 bidirectional_shortest_path (in my opinion, one of the nicer pieces of code of NetworkX), what do you think about this Aric?

@jtorrents

Copy link
Copy Markdown
Member

Test results for commit 0d7f141 merged into master (192d2e7)
Platform: linux2

  • python2.6: OK (SKIP=57) Ran 1527 tests (libraries not available: pyyaml, pydot, numpy, matplotlib, ogr, yaml, scipy, pyparsing, pygraphviz)
  • python2.7: OK (SKIP=2) Ran 1709 tests (libraries not available: ogr)
  • python3.2: OK (SKIP=10) Ran 1680 tests (libraries not available: pyparsing, ogr, pygraphviz, matplotlib, pydot)

@aparamon

Copy link
Copy Markdown
Contributor Author

2012/8/31 Jordi Torrents [email protected]

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 shortest_path and friends. This will be very helpful for implementing White and Newman's approximation for node independent paths.

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.

Thanks.

I think support for the weighted case should be also implemented, and
I think it's not very hard to do. It would be very useful because
currently you cannot get "there is no pass" exception via playing with
weights. And you might not want to play with weights because it's side
effect (frozen graphs etc).

Also I think that all shortest_path procedures should support
restrictions. However, the amount of interface changes looks
frightening to do at once. But I don't really see any practical
alternative to passing the options. Copying graph and modifying it
according to restrictions before passing to shortest_path appears to
be just too damn slow :-(

When everyone agrees on the interface I will also push my
implementation of yen_simple_paths. I only need to come up with the
better name probably :-)

Best wishes,
Andrey Paramonov

@dschult

dschult commented Aug 31, 2012

Copy link
Copy Markdown
Member

This all looks intriguing and useful.
Two things I have thought of to look into:

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!
Dan

On Aug 31, 2012, at 9:59 AM, aparamon wrote:

2012/8/31 Jordi Torrents [email protected]

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 shortest_path and friends. This will be very helpful for implementing White and Newman's approximation for node independent paths.

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.

Thanks.

I think support for the weighted case should be also implemented, and
I think it's not very hard to do. It would be very useful because
currently you cannot get "there is no pass" exception via playing with
weights. And you might not want to play with weights because it's side
effect (frozen graphs etc).

Also I think that all shortest_path procedures should support
restrictions. However, the amount of interface changes looks
frightening to do at once. But I don't really see any practical
alternative to passing the options. Copying graph and modifying it
according to restrictions before passing to shortest_path appears to
be just too damn slow :-(

When everyone agrees on the interface I will also push my
implementation of yen_simple_paths. I only need to come up with the
better name probably :-)

Best wishes,
Andrey Paramonov
—
Reply to this email directly or view it on GitHub.

@aparamon

Copy link
Copy Markdown
Contributor Author

2012/8/31 Dan Schult [email protected]

This all looks intriguing and useful.
Two things I have thought of to look into:

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.)

My change was designed to not slow down the common case of no
restrictions. It only 2 checks per call are added which is absolutely
negligible.

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.)

I think it's not better compared to just modifying graph in-place
before calling shortes_path. I could design robust "with" context
class for this purpose. However this implies that I should rely on the
side effects, which I don't like (frozen graphs etc). But if anyone
agrees that teaching every shortest_path procedure to use
restrictions is just mad, I think I could provide alternative
implementation (context class).

Best wishes,
Andrey Paramonov

@dschult

dschult commented Aug 31, 2012

Copy link
Copy Markdown
Member

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.)

My change was designed to not slow down the common case of no
restrictions. It only 2 checks per call are added which is absolutely
negligible.

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.)

I think it's not better compared to just modifying graph in-place
before calling shortes_path. I could design robust "with" context
class for this purpose. However this implies that I should rely on the
side effects, which I don't like (frozen graphs etc). But if anyone
agrees that teaching every shortest_path procedure to use
restrictions is just mad, I think I could provide alternative
implementation (context class).

Best wishes,
Andrey Paramonov

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?
Certainly, the ones that use _bidirectional_pred_succ. Might be others too...
Maybe more important is "how many wouldn't be straightforward to add this feature to?"
:)
Dan

@jtorrents

Copy link
Copy Markdown
Member

2012/8/31 aparamon [email protected]

2012/8/31 Jordi Torrents [email protected]

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 shortest_path and friends. This will be very helpful for implementing White and Newman's approximation for node independent paths.

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.

Thanks.

I think support for the weighted case should be also implemented, and
I think it's not very hard to do. It would be very useful because
currently you cannot get "there is no pass" exception via playing with
weights. And you might not want to play with weights because it's side
effect (frozen graphs etc).

Also I think that all shortest_path procedures should support
restrictions. However, the amount of interface changes looks
frightening to do at once. But I don't really see any practical
alternative to passing the options. Copying graph and modifying it
according to restrictions before passing to shortest_path appears to
be just too damn slow :-(

Good points. I agree. We should support restrictions for all shortest
paths functions. Regarding the interface changes, they are backwards
compatible, so we do not break working code. And, as you said, the
implementation of restrictions doesn't slow down the case without
restrictions. I'm +1 for implementing this.

When everyone agrees on the interface I will also push my
implementation of yen_simple_paths. I only need to come up with the
better name probably :-)

That's great. Thank you very much for your work on this!

@aparamon

Copy link
Copy Markdown
Contributor Author

2012/8/31 Dan Schult [email protected]

How many of the other shortest_path routines would be straightforward to update?
Certainly, the ones that use _bidirectional_pred_succ. Might be others too...
Maybe more important is "how many wouldn't be straightforward to add this feature to?"
:)

They are all more straightforward to modify than to add doc
strings/write tests/make commit etc ;-)

Best wishes,
Andrey Paramonov

@aparamon

aparamon commented Sep 3, 2012

Copy link
Copy Markdown
Contributor Author

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.

@jtorrents

Copy link
Copy Markdown
Member

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
http://docs.scipy.org/doc/numpy/dev/gitwash/development_workflow.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 upstream).

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.

@aparamon

aparamon commented Sep 4, 2012

Copy link
Copy Markdown
Contributor Author

2012/9/4 Jordi Torrents [email protected]

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 upstream).

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.

I see the following message in the pull request thread:
Test results for commit 0d7f141 merged into master (192d2e7)

I thought it means "your change was committed upstream". Apparently it
is not. What does it mean?

I'll try to study git a little, but it would take time. I absolutely
don't mind if in the meantime Someone(TM) properly rearranges my
commits so that my changes end up in the master branch. (Basically,
I've submitted my patches. You are now free to make use of them to
enhance your great library.)

Best wishes,
Andrey Paramonov

@hagberg

hagberg commented Sep 4, 2012

Copy link
Copy Markdown
Member

Thanks Andrey for your patches. We're still adjusting to the workflow after switching to Github (and don't have it documented yet).

@jtorrents

Copy link
Copy Markdown
Member

2012/9/4, aparamon [email protected]:

2012/9/4 Jordi Torrents [email protected]

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
upstream).

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.

I see the following message in the pull request thread:
Test results for commit 0d7f141 merged into master (192d2e7)
I thought it means "your change was committed upstream". Apparently it
is not. What does it mean?

That comment was made by a script (adapted from @ipython) for testing
github pull requests (see #752 for the code and details). It tests
that the pull request clearly merges with the current master, and that
the test suite passes in different python environments. But it does
not push the merged changes into master. I see now that the message
posted at github can be confusing, I'll change that. As we keep
saying, we are still adjusting to github workflow.

I'll try to study git a little, but it would take time. I absolutely
don't mind if in the meantime Someone(TM) properly rearranges my
commits so that my changes end up in the master branch. (Basically,
I've submitted my patches. You are now free to make use of them to
enhance your great library.)

Thank you very much for your code. I'll work on arranging/completing
your patches in order to be able to merge them with NetworkX master in
the following weeks. I'll leave this pull request open until I manage
to create new ones. If in the meantime Someone(TM) ;) is interested in
working on this, please leave a comment here so we do not duplicate
efforts.

Salut!

@chebee7i

chebee7i commented Sep 9, 2012

Copy link
Copy Markdown
Member

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.

@chebee7i

chebee7i commented Sep 9, 2012

Copy link
Copy Markdown
Member

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?

@jtorrents

Copy link
Copy Markdown
Member

Hi Chris,

2012/9/9 chebee7i [email protected]

Jordi, I can take a look at preparing these patches today. It looks like it should be simple, so I should finish it.

Great! yes, please feel free to work on this.

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.

The script has to be run manually. You are right that it may give a
false sense of security. I guess that the ideal solution would be an
automated script. While we don't have that, we could adopt the
convention of only posting the result of the script if there is some
failure (test_pr.py will post a trace of the nose output in a gist).
Thus we avoid the false sense of security by only posting its result
to signal to the author of the pull request that there is a problem
that needs fixing.

Salut!

@jtorrents

Copy link
Copy Markdown
Member

2012/9/9 chebee7i [email protected]

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.

I think that there is no way to link pull requests like that. A
possible way could be to clone Andrey's NetworkX fork and make patches
(git format-patch) of his changes. Then create a new branches on
your fork (one for each change), apply the patches and fix/complete as
necessary, and then make new pull requests. I'm still adapting to git
and github, so perhaps there is a better (less arduous) way.

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.

Andrey suggested to support restrictions for all shortest paths
functions using the same approach. I think that this is the ideal
solution, but there are a lot of functions (with their docstrings and
tests) to update. Maybe we can do this in several steps.

Dan suggested to create a subclass that filters nodes and edges to
implement this and make it more general. Andrey suggested that this
may not be better than modifying the graph inplace before calling
shortest paths. Maybe we could explore further the subclass option
but I like Andrey's implementation because it is simple and fast.

@jtorrents

Copy link
Copy Markdown
Member

I think that the rationale behind shortest paths and shortest path length is using less memory in the latter.

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.

@chebee7i

Copy link
Copy Markdown
Member

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.

@timstirling

Copy link
Copy Markdown

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.

@dschult

dschult commented Oct 23, 2012

Copy link
Copy Markdown
Member

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

On Oct 23, 2012, at 12:04 PM, timstirling wrote:

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.

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

@timstirling

Copy link
Copy Markdown

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:

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

On Oct 23, 2012, at 12:04 PM, timstirling wrote:

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.

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

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

@aparamon

Copy link
Copy Markdown
Contributor Author

Hello Tim!

I think the standard solution to your problem is to represent each
cross not as a single node, but as 8 nodes (right and left sides of
the road for each side of the cross). Then you gain enormous
flexibility wrt imposing hard (no turn left allowed from north side of
the cross) and soft (right turn has lower weight than straight pass)
restrictions.

Anyway, thanks for your voting for the restrictions feature. Hope it's
taken into account.

Hope it helps,
Andrey Paramonov

@ghost ghost assigned hagberg Dec 29, 2012
@hagberg

hagberg commented Jan 18, 2013

Copy link
Copy Markdown
Member

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?

@aparamon

Copy link
Copy Markdown
Contributor Author

2013/1/18 Aric Hagberg [email protected]

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?

Context class seems easy to do, robust and clean to me. However this won't
work with e.g. frozen graphs. Maybe it's really the way to go, given there
was no advancement in integrating my original code so far...

Best wishes,
Andrey Paramonov

@ysitu

ysitu commented Apr 11, 2014

Copy link
Copy Markdown
Contributor

#1011 (just to hook up related Issues/PRs)

@chebee7i

Copy link
Copy Markdown
Member

@dschult might have more to say on views. I can't remember where that discussion took place. I found #335 but I swear there was more discussion than that.

@dschult

dschult commented Apr 15, 2014

Copy link
Copy Markdown
Member

I also thought there was more about views than I can find. But I'll try to
put down what I can recall of the discussion.

From a speed perspective, the idea is that views could be faster than
constructing the entire restricted graph. But the view has to
construct/return a dict for each call to G[n] and it makes sense to save
that dict in case it has to return it again. If you access G[n] for all n
then you've accessed the entire restricted graph (and might as well have
built the graph instead of using a view). So for speed, the advantage of a
view is that you can slowly create the restricted graph instead of creating
it all on the first call. But if you are planning to traverse the whole
restricted graph you might as well create the restricted graph in the first
place. So I can see using views in this way if you don't expect to need to
traverse the entire restricted graph. It's kind of like a lazy subgraph
that only creates the subgraph as it is needed.

From a low memory perspective, the idea is that views might be slimmer than
creating the restricted graph. So, we in this paradigm we should not save
the neighbor dicts and instead construct them each time a call like G[n] is
used. The slowdown is significant, but I don't have numbers for it. Because
the memory usage of creating the restricted graph is less than the original
graph itself, creating the restricted graph is at worst doubling your
memory needs. So my feeling is that it isn't worth using views to save
memory. A factor of less than two in memory costs is not enough to justify
the much longer access times. Probably better to find a way to split the
graph up somehow--maybe replacing the graph with the restricted graph and
then reconstructing the original graph when done with the restricted one.
Dan

On Fri, Apr 11, 2014 at 1:05 PM, chebee7i [email protected] wrote:

@dschult https://github.com/dschult might have more to say on views. I
can't remember where that discussion took place. I found #335https://github.com/networkx/networkx/issues/335but I swear there was more discussion than that.

Reply to this email directly or view it on GitHubhttps://github.com//pull/762#issuecomment-40226342
.

@ysitu

ysitu commented Apr 17, 2014

Copy link
Copy Markdown
Contributor

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.

@dschult

dschult commented Apr 17, 2014

Copy link
Copy Markdown
Member

On Thu, Apr 17, 2014 at 1:43 AM, ysitu [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.

Agreed... And we talked long ago of making the NetworkX interface more
iterator based in how it returns almost all properties. At that time
version 2.0 was far in the future and we said that shift could come at 2.0.
Well--- now 2.0 is not far away. :) So maybe we need to rethink the
basic interfaces like G[n] to make them iterators. Aric has talked about
moving that direction for a long time.

Creating a view that has Gview[n] return an iterator while the graph itself
returns a dict is certainly possible too.

@jtorrents

Copy link
Copy Markdown
Member

Creating a view that has Gview[n] return an iterator while the graph itself returns a dict is certainly possible too.

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.

@chebee7i

chebee7i commented Apr 6, 2015

Copy link
Copy Markdown
Member

In light of #793, I just reread this discussion. This definitely needs to be looked at again wrt 2.0.

jtorrents added a commit to jtorrents/networkx that referenced this pull request Apr 8, 2015
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?
jtorrents added a commit to jtorrents/networkx that referenced this pull request Apr 17, 2015
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?
@hagberg hagberg mentioned this pull request Dec 29, 2015
@hagberg hagberg modified the milestones: networkx-future, networkx-2.0 Feb 2, 2016
@jfinkels

Copy link
Copy Markdown
Contributor

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":

without_red_edges = lambda u, v, d: 1 if d['color'] != 'red' else None
nx.shortest_path(G, weight=without_red_edges)

You can also effectively ignore edges this way, by ignoring all edges incident to the node you wish to ignore:

evil_nodes = {1, 2, 3}  # or whatever
without_evil_nodes = lambda u, v, d: None if {u, v} & evil_nodes else 1
nx.shortest_path(G, weight=without_evil_nodes)

This was also requested in issue #1946.

@aparamon aparamon closed this Nov 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

9 participants