Search code examples
javaalgorithmsearchgraph-theorya-star

How is the G-cost and F-cost generated in A*


I am looking at this implementation of the A* Algorithm in Java, I was wondering how did they decide on the G-cost and H-cost values, I know the values are generated in the format of: If the neighbor nodes are in the coordinates of X and Y, the distance value is 1. And if the neighbor nodes are diagonal, the distance value is 1.4 or sqrt of 2.

I was wondering, why are the values of G and H cost set to 10e5 primarily? Thank you!

The implementation here is in the format of:

 double G = 10e5, H = 10e5, F = G + H;

Solution

  • The G-cost and the H-cost should be set to the maximum value of nodes that can be presented in a grid where the path would be walked through, for example, if we have a 2D Grid which is created by a 2D Array, 50 rows and 50 columns, that would be equal to 2500. So the H-cost and the G-cost should be set to 2500 because that would be the maximum largest path the nodes can walkthrough, this is how I understood it at least. Hope I am correct!