Repository navigation
Floyd Warshall: Adding support for weight parameter as a callable - #8386
Conversation
tested against older tree version; since older tree version and older fw_pred_dist were giving the same result, testing ensures nothing has been changed in behaviour..
1. regarding weight parameter: copied from other distance func 2. in fw_pred_dist: return value needed more specification, so, taken from tree version
|
Note: earlier in some part of the docstring, there were 3 chars indentation, so I have added 4 chars indentation there along with modifying the content of docstring. |
Generally it's a good idea to leave doc indentation levels as-is, at least in PRs where other things are changing. The reason being that adding a space everywhere makes it look like every single line in the docstring has changed, making review more difficult. If it's possible to back out that change (e.g. revert a specific commit) it would make the review easier! Note also that 3 spaces are fine for docstrings: the 4-spaces indentation from PEP 8 applies only to source code (there are reasons why 3 spaces can look better with rst). |
|
I have reverted the docs update. Will update the docs once after the review. |
| cost = weight(u, v, e_attr) | ||
| if cost is not None: # for hidden edge, weight() returns None | ||
| dist[u][v] = cost | ||
| pred[u][v] = u |
There was a problem hiding this comment.
This doesn't look like the code it is replacing.
Can you explain how this is equivalent?
The old way loops over the edges, while this loops over the neighbors.
BTW, an even cleaner way of doing that is
for u, unbrs in G._adj.items():
for v, e_attr in unbrs.items():So we trade one items lookup for N dict lookups of G_succ[u].
But then I don't understand how the rest is the same. we don't compare cost with previously found costs. But if it is different, why aren't the tests catching this?
There was a problem hiding this comment.
we don't compare
costwith previously found costs
we need to compare cost with previously found cost in case of Multigraphs, but _weight_function already do that internally:
def _weight_function(G, weight):
if callable(weight):
return weight
# If the weight keyword argument is not callable, we assume it is a
# string representing the edge attribute containing the weight of
# the edge.
if G.is_multigraph():
return lambda u, v, d: min(attr.get(weight, 1) for attr in d.values())
return lambda u, v, data: data.get(weight, 1)So we trade one items lookup for N dict lookups of
G_succ[u]
I was thinking like this: since for each node, we are looping over only its neighbours, thus overall it will be O(Number of Edges), (since sum of outdegrees = E)
There was a problem hiding this comment.
Can you explain how this is equivalent?
This part of the code is responsible for assigning weights and pred according to the edge and edge weights.
Thus we need to iterate over all the edges in any method.
This was done in the previous version by just iterating on G.edges(data=True)
In the current version, for each node, we are checking its neighbours (which basically makes a edge). Thus, we are in fact iterating edges but in another way.
Please correct me if I'm wrong
There was a problem hiding this comment.
You are correct -- the G.edges object is actually doing exactly the double loop you use here. I was more confused about looking up the minimum -- and your explanation reminds we that with G.edges we also have loop of the key_dict. Here we handle that via the weight function as you correctly describe.
Thanks!!
There was a problem hiding this comment.
My suggestion of
for u, unbrs in G._adj.items():
for v, e_attr in unbrs.items():doesn't change the complexity of the loops -- it just avoids the lookup cost of G_succ[u].
Your code is correct and works fine -- my experience is that using items() to get G_succ[u] while iterating is faster than actually looking it up. :)
|
Thank you for the review!
TODO: |
|
No need to add a doc_string to the private function |
|
Did we lose the original changes that make this use the weight as a function? I'm not seeing them in the diff... |
You might be talking about this: weight = _weight_function(G, weight)
for u, unbr in G._adj.items():
dist[u][u] = 0
for v, e_attr in unbr.items():
cost = weight(u, v, e_attr)
if cost is not None: # for hidden edge, weight() returns None
dist[u][v] = cost
pred[u][v] = u
return pred, distThese are now moved to |
|
Right -- Thanks... got confused because the doc_string changes are not there anymore. This looks good to me. But is there a problem with the doc_strings? I think it should be the same text that the other shortest paths use for their |
Oh, Sorry for the confusion. I was waiting for the code review to update the docstring. I had reverted doc change after some discussion about 3/4 char indentation
Yes, will add the docstring along with your suggested change about importing Thank you |
|
Now added the docstring, along with other suggestion to import Note that I have respected the earlier indentation rule whatever that was. |
dschult
left a comment
There was a problem hiding this comment.
This looks good! Thanks @gulshan-123 !
(#8425) resolved self positive loop bug
Most of the distance algorithms under shortest path accept
weightto be as either a string or a callable:While implementing
floyd_warshall_tree, we noticed to add this feature to Floyd Warshall algorithms too, which currently takesweightonly as a string.Currently, I have made this available for
floyd_warshall_treeandfloyd_warshall_predecessor_and_distance.There is one another function:
floyd_warshall_numpy. But for it to support callableweight, we need to change the more general functionnx.to_numpy_array, whichfloyd_warshall_numpycalls internally.CC: @amcandio , @dschult
Idea and tests taken from
algorithms/shortest_paths/weighted.py