我的版本。在其中一个用例中,这对我来说效果很好。
public class DirectedWeightedGraph<E> {
// Map having Vertex as key and List of Edges as Value.
Map<Vertex<E>, List<Edge<E>>> adj = new HashMap<>();
public static class Vertex<E> {
E value;
public Vertex(E value) {
this.value = value;
}
}
public static class Edge<E> {
E from;
E to;
double weight;
public Edge(E from, E to, double weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
}
public void addVertex(E value) {
Vertex<E> v = new Vertex<E>(value);
List<Edge<E>> edges = new ArrayList<>();
this.adj.put(v, edges);
}
public void addEdge(E from, E to, double weight) {
List<Edge<E>> fromEdges = this.getEdges(from);
List<Edge<E>> toEdges = this.getEdges(from);
// Add source vertex and then add edge
if(fromEdges == null) {
this.addVertex(from);
}
if(toEdges == null) {
this.addVertex(to);
}
fromEdges.add(new Edge<E>(from, to, weight));
}
}
Example:
DirectedWeightedGraph <Integer> graph = new DirectedWeightedGraph<>();
graph.addEdge(1, 2, 10.0);
graph.addEdge(2,3,15.0);