Algorithm to find two points furthest away from each other

前端 未结 9 1321
闹比i
闹比i 2020-12-13 06:23

Im looking for an algorithm to be used in a racing game Im making. The map/level/track is randomly generated so I need to find two locations, start and goal, that makes use

相关标签:
9条回答
  • 2020-12-13 07:20

    Follow up to the question about Floyd-Warshall or the simple algorithm of Hosam Aly:

    I created a test program which can use both methods. Those are the files:

    • maze creator
    • find longest distance

    In all test cases Floyd-Warshall was by a great magnitude slower, probably this is because of the very limited amount of edges that help this algorithm to achieve this.

    These were the times, each time the field was quadruplet and 3 out of 10 fields were an obstacle.

    Size         Hosam Aly      Floyd-Warshall
    (10x10)      0m0.002s       0m0.007s     
    (20x20)      0m0.009s       0m0.307s
    (40x40)      0m0.166s       0m22.052s
    (80x80)      0m2.753s       -
    (160x160)    0m48.028s      -
    

    The time of Hosam Aly seems to be quadratic, therefore I'd recommend using that algorithm. Also the memory consumption by Floyd-Warshall is n2, clearly more than needed. If you have any idea why Floyd-Warshall is so slow, please leave a comment or edit this post.

    PS: I haven't written C or C++ in a long time, I hope I haven't made too many mistakes.

    0 讨论(0)
  • 2020-12-13 07:21

    I deleted my original post recommending the Floyd-Warshall algorithm. :(

    gs did a realistic benchmark and guess what, F-W is substantially slower than Hosam Aly's "flood fill" algorithm for typical map sizes! So even though F-W is a cool algorithm and much faster than Dijkstra's for dense graphs, I can't recommend it anymore for the OP's problem, which involves very sparse graphs (each vertex has only 4 edges).

    For the record:

    • An efficient implementation of Dijkstra's algorithm takes O(Elog V) time for a graph with E edges and V vertices.
    • Hosam Aly's "flood fill" is a breadth first search, which is O(V). This can be thought of as a special case of Dijkstra's algorithm in which no vertex can have its distance estimate revised.
    • The Floyd-Warshall algorithm takes O(V^3) time, is very easy to code, and is still the fastest for dense graphs (those graphs where vertices are typically connected to many other vertices). But it's not the right choice for the OP's task, which involves very sparse graphs.
    0 讨论(0)
  • 2020-12-13 07:24

    Ok, "Hosam's algorithm" is a breadth first search with a preselection on the nodes. Dijkstra's algorithm should NOT be applied here, because your edges don't have weights.

    The difference is crucial, because if the weights of the edges vary, you need to keep a lot of options (alternate routes) open and check them with every step. This makes the algorithm more complex. With the breadth first search, you simply explore all edges once in a way that garantuees that you find the shortest path to each node. i.e. by exploring the edges in the order you find them.

    So basically the difference is Dijkstra's has to 'backtrack' and look at edges it has explored before to make sure it is following the shortest route, while the breadth first search always knows it is following the shortest route.

    Also, in a maze the points on the outer border are not guaranteed to be part of the longest route. For instance, if you have a maze in the shape of a giant spiral, but with the outer end going back to the middle, you could have two points one at the heart of the spiral and the other in the end of the spiral, both in the middle!

    So, a good way to do this is to use a breadth first search from every point, but remove the starting point after a search (you already know all the routes to and from it). Complexity of breadth first is O(n), where n = |V|+|E|. We do this once for every node in V, so it becomes O(n^2).

    0 讨论(0)
提交回复
热议问题