creating graphs in Java with ArrayLists

11,484

To be able to access the ArrayList from accross methods within the same class, you would need to promote that local variable to a global variable (field), like below:

public class GraphNode {
    /*Global Variables*/
    boolean visited;
    List<GraphNode> nodeNeighbours;
    /*Global Variables*/

    public GraphNode() {
        this.nodeNeighbours = new ArrayList<GraphNode>();  // store neighbors
        this.visited = false;

    }

    public void addNeighbor(GraphNode target) {
        this.nodeNeighbours.add(target);    //This will add target to the neighbours of this given node.
    }
}

EDIT:
Shortest path algorithms (as far as memory serves) have a fixed starting point, meaning that the root will always be the same. The same can be said to their weight, which usually does not change.

However, as different paths are explored, the distance to the root node is most likely to change. With that in mind, you could re write your class as follows:

public class GraphNode {
    /*Global Variables*/
    protected double weight;
    protected double distanceFromRoot;
    protected List<GraphNode> neighbours;
    protected boolean visited;
    /*Global Variables*/

    public GraphNode(double weight, double distanceFromRoot) {
        this.weight = weight;
        this.distanceFromRoot = distanceFromRoot;
        this.neighbours = new ArrayList<GraphNode>();
        this.visited = false;
    }

    public void setDistanceFromRoot(double distanceFromRoot) {
         this.distanceFromRoot = distanceFromRoot;
    }

    public void setVisited(boolean visited) {
         this.visited = visited;
    }

    public double getWeight() {
        return this.weight;
    }

    public double getDistanceFromRoot() {
        return this.distanceFromRoot;
    }

    public List<GraphNode> getNeighbours() {
        return this.neighbours;
    }   

    public void addNeighbour(GraphNode neighbour) {
        this.neighbours.add(neighbour)
    }
}

This might be a little bit more extensive than what you started with. The main change in your code is that this code promoted encapsulation. Encapsulation is one of the core fundamentals of OOP in which you essentially deny direct access to your global variables. The access is offered through appropriate set and get method which you can use to define how and when does someone external to your program modifies the internal state of your program.

Share:
11,484
Liondancer
Author by

Liondancer

Just trying to get better at programming! Any helpful tips are much appreciated! =D

Updated on June 14, 2022

Comments

  • Liondancer
    Liondancer almost 2 years

    I'm new to Java and I wanted to practice by creating a graph. I am having trouble creating the graph because I'm not sure how to do so correctly. I am lacking the logic and being new in Java makes things quite difficult. I was wondering if anyone could help me or guide me in the right path! Thanks!

    Basically I am trying to create something like this as a graph. A node will contain an ArrayList for it's neighbors.

        A,B,C,D
    A = 0,1,3,0
    B = 1,0,0,2
    C = 1,0,0,3   //shorter path to A (cycle)
    D = 0,0,3,0
    

    The nodes are connected to each other with or without weights (if I change numbers to 1)

    Here is what I have so far (it is incomplete):

    public class GraphNode {
        boolean visited;
    
    
        public GraphNode() {
            List<GraphNode> node = new ArrayList<GraphNode>();  // store neighbors
            this.visited = false;
    
        }
    
        public void addNeighbor(GraphNode root, GraphNode target, int distance) {
            root.add(target);     // cannot use "add" to ArrayList
    
        }
    }