# Directed Graph With Weights in Java

I am learning graph theory and I have implemented a Directed Graph with weights in Java. My next objective with this class is to learn more about algorithms like Dijkstra, Bellman-Ford, etc. I have split the implementation into two classes - GraphNode and DirectedGraphWithWeights.

GraphNode.java

public class GraphNode {
private int nodeValue;
private Integer weight = Integer.MAX_VALUE;

GraphNode(int nodeValue) {
this.nodeValue = nodeValue;
}

public int getNodeValue() {
return this.nodeValue;
}

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

public void setWeight(int weight) {
this.weight = weight;
}

@Override
public int hashCode() {
return new Integer(this.nodeValue).hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
GraphNode other = (GraphNode) obj;
if(nodeValue != other.getNodeValue()) return false;
return true;
}
}


DirectedGraphWithWeights.java

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class DirectedGraphWithWeights {

private Set<GraphNode> allNodesSet;

/**
* Constructor
*/
DirectedGraphWithWeights() {
allNodesSet = new HashSet<>();
}

/**
* Returns the number of vertices in the Graph
* @return Returns the number of vertices
*/
public int getNumberOfVertices() {
return this.allNodesSet.size();
}

/**
* Returns if the graph is directed
* @return
*/
public boolean isDirected() {
return true;
}

/**
* Returns the number of edges
* @return Returns the number of edges
*/
public int getNumberOfEdges() {
int numberOfEdges = 0;
numberOfEdges += vertices.size();
}
return numberOfEdges;
}

/**
* Adds a node to the graph. VertexA -> VertexB, adding a node creates an
* edge between VertexA and VertexB with the specified weight
* @param vertexA Vertex A
* @param vertexB Vertex B
* @param weight Weight of the edge
*/
public void addNode(int vertexA, int vertexB, int weight) {
GraphNode vertexANode = new GraphNode(vertexA);
GraphNode vertexBNode = new GraphNode(vertexB);
vertexBNode.setWeight(weight);
}

/**
* Returns all the vertices of the graph
* @return All the vertices of the graph
*/
public Iterable<Integer> getAllVertices() {
Set<Integer> vertices = new HashSet<>();
}
return vertices;
}

/**
* Returns all the adjacent nodes
* @param source Source node
* @return Returns all the adjacent nodes
*/
GraphNode tempNode = new GraphNode(source);
}

public void printAllVertices() {
System.out.print(it.getNodeValue() + " -> ");
System.out.print("value: " + node.getNodeValue() +
"; weight: " + node.getWeight() + " -> ");
}
System.out.println();
}
}
}


I would highly appreciate feedback on my idea of implementation.

• hashCode() use default, why? its tried and tested (unless you have different case) Mar 13, 2020 at 10:02
• Sorry I did not understand. Can you please re-phrase? Mar 13, 2020 at 11:10

Then the method addNode actually adds an edge, so it should be named accordingly.