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
}
}