0

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;
     }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
SpiderShlong
  • 224
  • 1
  • 8

1 Answers1

0

Well you can let the grid represent something like a square grid with a lower and upper bound on both x and y, you could then create a recursive method where you initialize a node at (x,y) and that initialized node will initialize its north/west/south/east neighbors if those nodes x,y positions are still within the grid, else you have hit the wall and you cannot intialize any more in that direction. The problem here is what Pointer does, it seems like a class that complicates things. If the grid is square you can keep

Node north;
Node west
Node south
Node east

fields in your Node class.

If the grid is not square you can still keep a

List<Node> connectedTo
arynaq
  • 6,710
  • 9
  • 44
  • 74
  • Thanks, just made me rethink my code. I'll still use a list of neighboring nodes, but since I removed the weighted movements there is no longer a need for an edge class (Pointer). – SpiderShlong Nov 07 '13 at 04:23