Index edges for multi graph simple paths#3358
Conversation
|
Hmmm... This PR points out that we use node-paths. That is, we store and report paths as lists of nodes rather than lists of edges. There is a bijection for simple graphs between node-paths and edge-paths. But for MultiGraphs, node-paths do not uniquely specify edge-paths. I think mixing the two output structures will lead to a convoluted API. Sometimes a function will return a list of nodes and other times a list of edges. That's going to be hard for users to debug and for us to maintain. Does anyone have any comments on paths specified as lists for edges? How hard would it be to make a new function that returns simple paths as edge-paths, that is as lists of edges instead of lists of nodes. For non-multigraphs it could use the current functions and then convert to edge-paths. For multigraphs it would return what you've put into this PR. |
That might be the best solution. I think it should not take much effort, since for non multi graphs one can use G = nx.complete_graph(4)
paths = nx.all_simple_paths(G, source=0, target=3)
for path in map(nx.utils.pairwise, paths):
print(list(path))
[(0, 1), (1, 2), (2, 3)]
[(0, 1), (1, 3)]
[(0, 2), (2, 1), (1, 3)]
[(0, 2), (2, 3)]
[(0, 3)]I can create a function called # Non multi graph
G = nx.complete_graph(4)
paths_edges = nx.all_simple_paths_edges(G, source=0, target=3)
for path_edges in paths_edges:
print(list(path_edges))
[(0, 1), (1, 2), (2, 3)]
[(0, 1), (1, 3)]
[(0, 2), (2, 1), (1, 3)]
[(0, 2), (2, 3)]
[(0, 3)]
# multi graph
mg = nx.MultiGraph()
mg.add_edge(1,2,key='k0')
mg.add_edge(1,2,key='k1')
mg.add_edge(2,3,key='k0')
paths_edges = nx.all_simple_paths_edges(mg, source=1, target=3)
print(list(paths_edges))
[[(1,2,'k0'),(2,3,'k0')], [(1,2,'k1'),(2,3,'k0')]]Do you agree? |
|
Yes -- that looks good to me. Let's think about name (you get to choose): :) |
|
If latter on there will be a data-structure for a path as a list of edges, maybe it is better choosing I'll start writing the Problem: My concern is that the naming can lead to confusions, since one can think that |
|
I saw the PR on trails. Looks like it has some testing errors on travis (but apparently not on appveyor). Let's see if we can make it clear: And just so I check what I think I know:
I suppose we could create something like: |
|
Perfect! I definitely agree on that. :D |
|
Hi, Last commit includes Both pull requests, this and the mentioned above, just have Travis error in Python 3.5 in the docstring. The problem is related to the order in which the docstring code examples present the output, vs. the expected order. Honestly I don't think it's an error, because even python 3.5 obtains a correct output. Do you think it's ready to accept the pull requests #3393 #3358? |
|
Same comment as on the other PR. |
|
Done! Including Now it's ready to be safely merged. |
|
Great -- Thanks for this. Some things to change:
|
|
This PR may be helpful in constructing a solution to shortest paths in |
…lti-simple-paths
|
Hello @MartinPJorge! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2020-07-07 16:02:37 UTC |
* Index edges for multi graph simple paths * Change prints to pyhton 3 * Change list.popitem() to list.pop() * Change another .popitems() to .pop() * Treat target as set * Update test for new multi graph all_simple_paths * Correct doc to match output * Change countall to count * Create all_simple_edge_paths * Correct docstring example * Regression simple paths multigraph tests * Sort docstring all_simple_edge_paths * Sort docstring mg all_simple_edge_paths * add tests and pep8 and doc changes Co-authored-by: Dan Schult <dschult@colgate.edu>
* Index edges for multi graph simple paths * Change prints to pyhton 3 * Change list.popitem() to list.pop() * Change another .popitems() to .pop() * Treat target as set * Update test for new multi graph all_simple_paths * Correct doc to match output * Change countall to count * Create all_simple_edge_paths * Correct docstring example * Regression simple paths multigraph tests * Sort docstring all_simple_edge_paths * Sort docstring mg all_simple_edge_paths * add tests and pep8 and doc changes Co-authored-by: Dan Schult <dschult@colgate.edu>
Modifies
_all_simple_paths_multigraph()to return the traversed edges rather than the nodes.The returned edges contain their associated keys, so user can distinguish across edges connecting same pair of nodes.
If we have following
MultiGraphwith these nodes and edges (with their keys):(1,2,'k0') ________ / \ 1 ___________ 2 ___________ 3 (1,2,'k1') (2,3,'k0')The modified
_all_simple_paths_multigraph()now returns:rather than