Repository navigation
Optimizing Dijkstra's path construction for all targets case - #8218
Conversation
Peiffap
left a comment
There was a problem hiding this comment.
Interesting! How common are these adversarial/worst-case graphs in practice?
| for v in dist: | ||
| pred_v = pred_dict.get(v) | ||
| if pred_v is not None: | ||
| paths[v] = paths[pred_v[0]] + [v] |
There was a problem hiding this comment.
Untested, but would this be slower/faster?
| for v in dist: | |
| pred_v = pred_dict.get(v) | |
| if pred_v is not None: | |
| paths[v] = paths[pred_v[0]] + [v] | |
| for v in dist.keys() & pred_dict.keys(): | |
| paths[v] = paths[pred_dict[v][0]] + [v] |
There was a problem hiding this comment.
The problem is that & returns a set, which does not follow insertion order. Recursion depends on the insertion order of the dist dictionary.
I forgot to add a comment clarifying the use of the trick!
There was a problem hiding this comment.
@fei0319 iirc you gave me that idea for the implementation trick. Feel free to send the comment suggestion so you show up as co-author 🙂
There was a problem hiding this comment.
You can add the co-author text to any commit message. The format is a little tricky to get right (so github parses it correctly and gives credit), so suggestions are a good way too.
For this case, what about:
for v in dist:
if v not in pred_dict:
continue
paths[v] = paths[pred_dict[v][0]] + [v]Or can we rely on the fact that v is in dist, so it is also in pred_dict. Then the last line is all that's needed in the loop?
There was a problem hiding this comment.
The sources are not in pred dict (do we have a bug with 0 weights here?). I think the more robust approach would be to skip the sources here. I can skip the first len(sources) elements in dist.
I should probably avoid calling len in case any user is passing an iterable without len
There was a problem hiding this comment.
You could use something like number_of_sources = len(seen) before the while loop.
I think the zero-weight case needs to be updated on line 875 from pred[u] to pred_dict[u].
Probably good to add a test that triggers that as well, but could be a separate PR.
There was a problem hiding this comment.
Good idea, Dan! seen works perfectly because it drops duplicates. I sent a new revision with the number_of_sources skip using itertools.islice. I also unified the use of pred and pred_dict.
I don't think there was a bug with pred and pred_dict because if pred is not None, then pred is pred_dict. But code is simpler if we always access pred_dict when updating the dict. However, it is important that we keep the checks if pred is not None because they mean that caller wants all predecesors.
Really hard to answer, it really depends on the use case! For a graph to benefit from this optimization it needs to have the shortest paths that are not the shortest in terms of hops. Computer networks can have this property due to routing policies, for example the direct link might have very little bandwidth so its metric is artificially increased to divert traffic from it. For the graphs where that is not the case, we pay some extra overhead coming from having to re-iterate the distances dictionary. This seems to be in the order of microseconds. The main advantage I see on the change is that it reduces algo complexity. Right now running time increases cubically as we increase n. This change makes it quadratical. It is particularly noticeable as we increase n (I did some experiments with n=10_000 and it was 4x faster but I need to double check that). |
Co-authored-by: Gilles Peiffer <[email protected]>
|
Where does the cubic complexity come from, and how did we avoid it? |
|
The current complexity comes from creating a new path on each relaxation. On this worst-case graph, each node is relaxed More especifically, each node This happens for each node (My numbers probably have some +-1 bugs, but it shouldn't change complexity results 😄) This is clearer when adding a log line on each relaxation: ...
paths_dict[u] = paths_dict[v] + [u]
print(f"Best path to {u} is now {paths_dict[u]}")
...Then if we run: >>> def dijkstra_relaxation_worst_case(n):
for i in range(n):
for j in range(i + 1, n):
G.add_edge(i, j, weight=2 * (j - 1 - i) + 1)
return G
>>> G = dijkstra_relaxation_worst_case(5)
>>> nx.shortest_path(G, 0, weight='weight')
Best path to 1 is now [0, 1]
Best path to 2 is now [0, 2]
Best path to 3 is now [0, 3]
Best path to 4 is now [0, 4]
Best path to 2 is now [0, 1, 2]
Best path to 3 is now [0, 1, 3]
Best path to 4 is now [0, 1, 4]
Best path to 3 is now [0, 1, 2, 3]
Best path to 4 is now [0, 1, 2, 4]
Best path to 4 is now [0, 1, 2, 3, 4]We can see there how we are computing and then dropping paths for each node. Node 1 constructs one path, node 2 constructs 2 and so on. How did we avoid it? By updating predecesors on each relaxation (which is O(1)) and constructing the path only once (at the end). With the current implementation, each path is constructed once per destination based on the path of the predecesor. This means that we pay |
Co-authored-by: Fei Pan <[email protected]>
|
Implementation is pretty compact at this point. We can try moving the path construction after visiting each v (when we set dist). This would make algo work with only one pass through vertices. |
|
If we try to move path construction inside the bigger loop, we'd need more to track the sources, check if paths is None and target is None in the big loop. Not clear its simpler -- but if you think it might be go ahead. |
|
I can leave it for another PR. I'd only do it if faster. Code-wise I prefer current version! |
|
@dschult what do you think of the current state? Are you good with implementation and timings? |
dschult
left a comment
There was a problem hiding this comment.
This looks good to me.
It delays computing paths until the search is complete to avoid intermediate path construction. It uses a pred_dict dict to hols the information for the delayed path construction. If pred is requested and paths is requested, then pred_dict uses the provided pred dict to store the predecessors -- conveniently providing the info needed to get paths and the pred dict itself.
Thanks @amcandio
Same approach of #8023, now for all target case, result from benchmark of #8059.
We can see noticeable improvements for adversarial graphs at the expense of extra overhead on small and "simple" graphs.
Results: