HeresHere's an implementation that iI dug up from when iI was dealing with graphs. Although itsit's in C#, with a few minor adjustments it could compile in Java.
To make it directed you would have to copy v.Adjacents.Add(new Edge(w, cost)); and reverse the direction, thereby taking up double the space.
I dont
I don't think itsit's any better than what you have though.
class Edge
{
public Vertex Destination { get; set; }
public double Cost { get; set; }
public Edge(Vertex destination, double cost)
{
Destination = destination;
Cost = cost;
}
}
class Vertex
{
public string Name { get; set; }
public List<Edge> Adjacents { get; set; }
public double Distance { get; set; }
public Vertex(string name)
{
Name = name;
Adjacents = new List<Edge>();
Distance = double.MaxValue;
}
}
class Graf
{
private readonly Dictionary<string, Vertex> vertexmap = new Dictionary<string, Vertex>();
public void AddEdge(string source, string dest, double cost)
{
var v = GetVertex(source);
var w = GetVertex(dest);
v.Adjacents.Add(new Edge(w, cost));
}
private Vertex GetVertex(string vertexname)
{
Vertex v;
vertexmap.TryGetValue(vertexname, out v);
if (v == null)
{
v = new Vertex(vertexname);
vertexmap.Add(vertexname, v);
}
return v;
}
}