javaalgorithmnodesa-stareuclidean-distance

Is it logical for Manhattan and Euclidean heuristics to check the same nodes?


So I have created an app where I have to find the shortest route from startPos to endPos using the A* algorithm. I have approached this problem with the 2 most common heuristics:

1. Manhattan Distance

2. Euclidean Distance

Not only both of them give me the same path but they both explore the same nodes. Does it make sense?

My functions for calculating the distances:

public static int manhattanDistance(int row1, int col1, int row2, int col2) {
    return Math.abs(row1 - row2) + Math.abs(col1 - col2);
} 
public static int euclideanDistance(int row1, int col1, int row2, int col2) {
    int dx = col2 - col1;
    int dy = row2 - row1;
    return (int) Math.sqrt(dx * dx + dy * dy);
}

An example is when I have the grid:

S0000001000
11001000010
00000000000
00100000001
0000010100E

with starting position S = (0.0) and ending position E = (4,10)

After executing the algorithm I get the results:

Calculating result using Manhattan Distance.
Nodes explored (33): (0, 0) (0, 1) (0, 2) (0, 3) (1, 2) (1, 3) (2, 2) (2, 3) (0, 4) (3, 3) (0, 5) (4, 3) (1, 5) (3, 4) (1, 6) (3, 5) (2, 6) (1, 7) (2, 7) (1, 8) (3, 6) (3, 7) (2, 8) (3, 8) (2, 9) (4, 8) (2, 10) (4, 9) (0, 8) (2, 1) (0, 9) (3, 1) (4, 1)
Path: (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (1, 5) (1, 6) (2, 6) (2, 7) (3, 7) (3, 8) (4, 8) (4, 9) (4, 10)

Calculating result using Euclidean Distance.
Nodes explored (33): (0, 0) (0, 1) (0, 2) (0, 3) (1, 2) (1, 3) (2, 2) (2, 3) (0, 4) (3, 3) (0, 5) (4, 3) (1, 5) (3, 4) (1, 6) (3, 5) (2, 6) (1, 7) (2, 7) (1, 8) (3, 6) (3, 7) (2, 8) (3, 8) (2, 9) (4, 8) (2, 10) (4, 9) (0, 8) (2, 1) (0, 9) (3, 1) (4, 1)
Path: (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (1, 5) (1, 6) (2, 6) (2, 7) (3, 7) (3, 8) (4, 8) (4, 9) (4, 10)

As we can see not only the path is the same but the nodes are the same explored in the same order. Should it be like this? Does it make sense to be like this? Is it always going to be like this for all scenarios?


Solution

  • In the example grid, the shortest paths have a length that is equal to the Manhattan distance between start and finish, and this is even true for most cells in your grid: the shortest path from those cells to the finish has the Manhattan distance from that cell to the finish. That means that with the Manhattan heuristic function, competing alternatives will often have the same total cost (i.e. 14) and whether or not the priority queue delivers the nodes in the same order as for the other, Euclidean distance heuristic, depends on how such ties are broken.

    However, the sequence of explored nodes you have given for the Euclidean distance heuristic cannot be right:

    For instance, cells (0,5) and (3,3) don't appear in the expected order.

    Here are the relevant cost values for these two cells:

    cell steps made from (0,0) Euclidean to (4,10) total cost (priority)
    (0,5) 5 6.403 -> 6 5 + 6 = 11
    (3,3) 6 7.071 -> 7 6 + 7 = 13

    So clearly, (0, 5) should be pulled from the priority queue before (3, 3), yet in your sequence you have (3, 3) coming before (0, 5). This means something is wrong in your algorithm.