evenfurther / pathfinding

Pathfinding library for rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible to quickly compute "all nodes within a distance of D" from an initial node?

paulkernfeld opened this issue · comments

Given an undirected graph G (obviously fine to think of this as directed), I'd like to have a quick way to answer the question: given a starting node N and a distance D, what are all the nodes that are less than or equal to D from node N?

Context: I'm working on a tiny city sim game on a grid. The rule is that the building you may build on node N is constrained by the pre-existing buildings that are "connected" to node N, i.e. the buildings that are within distance D of node N. I'm currently pre-computing a set of the nodes that are connected to any given node, but my algorithm is so slow that it takes seconds on even a 6x6 graph 😞! The reason that this isn't just "check the Manhattan distance" is because there are some buildings that you can travel between for zero cost.

To precompute the distances between each pair of vertices in your graph, you might want to use either Floyd-Warshall or repeated Dijkstra, depending on the number of vertices and edges.

And to answer your initial question (find all nodes within a given distance of another node) you might want to use a modified Dijkstra algorithm, look at run_dijkstra for example in dijkstra.rs.

Thanks! I had never heard of Floyd-Warshall and it looks super-relevant! I just realized that I'll probably have to implement a custom algorithm because my C type only implements PartialOrd, not Ord. I suspect that this is too weird a use case to justify adding to this library. 🤪