I am confused on how to initialize my Pointer (edge) class when implementing my Dijkstra's algorithm. The Node class holds an ArrayList of Pointer's called neighbors, which represent the 4 neighbors on any side of the Node. My Pointer class takes the target Node (where it's pointing) as an argument in the constructor. They are all added to a 36x25 2-dimmentional array of Nodes.
As of now I am using a setNeighbors() method ran through each node constructed on a 36x25 grid once all have been constructed, which searches through every possible Node as much as 4 times depending on its relevance in the grid (a corner has 2 neighbors), and breaks once the neighbor is found by comparing (x,y) coordinates.
This initialization process takes entirely too long for my purpose so I wanted to know if anyone can show me a way that would do this initialization if a more efficient manner.
I have a class Node:
import java.util.ArrayList;
public class Node implements Comparable<Node>
{
public int x;
public int y;
public ArrayList<Pointer> neighbors = new ArrayList<Pointer>();
public double minDistance = Double.POSITIVE_INFINITY;
public Node previous;
public Node(int xPos, int yPos)
{
x = xPos;
y = yPos;
}
public int compareTo(Node other)
{
return Double.compare(minDistance, other.minDistance);
}
}
And a class Pointer:
public class Pointer
{
public final Node target;
public final double weight = 1;
public Pointer(Node targ)
{
target = targ;
}
}