Generic Adjacency List Graph implementation
- by DmainEvent
I am trying to come up with a decent Adjacency List graph implementation so I can start tooling around with all kinds of graph problems and algorithms like traveling salesman and other problems... But I can't seem to come up with a decent implementation. This is probably because I am trying to dust the cobwebs off my data structures class.
But what I have so far... and this is implemented in Java... is basically an edgeNode class that has a generic type and a weight-in the event the graph is indeed weighted.
public class edgeNode<E> {
private E y;
private int weight;
//... getters and setters as well as constructors...
}
I have a graph class that has a list of edges a value for the number of Vertices and and an int value for edges as well as a boolean value for whether or not it is directed. The brings up my first question, if the graph is indeed directed, shouldn't I have a value in my edgeNode class? Or would I just need to add another vertices to my LinkedList? That would imply that a directed graph is 2X as big as an undirected graph wouldn't it?
public class graph {
private List<edgeNode<?>> edges;
private int nVertices;
private int nEdges;
private boolean directed;
//... getters and setters as well as constructors...
}
Finally does anybody have a standard way of initializing there graph? I was thinking of reading in a pipe-delimited file but that is so 1997.
public graph GenereateGraph(boolean directed, String file){
List<edgeNode<?>> edges;
graph g;
try{
int count = 0;
String line;
FileReader input = new FileReader("C:\\Users\\derekww\\Documents\\JavaEE Projects\\graphFile");
BufferedReader bufRead = new BufferedReader(input);
line = bufRead.readLine();
count++;
edges = new ArrayList<edgeNode<?>>();
while(line != null){
line = bufRead.readLine();
Object edgeInfo = line.split("|")[0];
int weight = Integer.parseInt(line.split("|")[1]);
edgeNode<String> e = new edgeNode<String>((String)
edges.add(e);
}
return g;
}
catch(Exception e){
return null;
}
}
I guess when I am adding edges if boolean is true I would be adding a second edge. So far, this all depends on the file I write. So if I wrote a file with the following Vertices and weights...
Buffalo | 18 br Pittsburgh | 20 br New York | 15 br D.C | 45 br
I would obviously load them into my list of edges, but how can I represent one vertices connected to the other... so on... I would need the opposite vertices? Say I was representing Highways connected to each city weighted and un-directed (each edge is bi-directional with weights in some fictional distance unit)... Would my implementation be the best way to do that?
I found this tutorial online Graph Tutorial that has a connector object. This appears to me be a collection of vertices pointing to each other. So you would have A and B each with there weights and so on, and you would add this to a list and this list of connectors to your graph... That strikes me as somewhat cumbersome and a little dismissive of the adjacency list concept? Am I wrong and that is a novel solution?
This is all inspired by steve skiena's Algorithm Design Manual. Which I have to say is pretty good so far. Thanks for any help you can provide.