I want to implement some graph algorithms for learning purposes (e.g BFS, DFS, Karger's min-cut algo) and before diving into them I want to make sure I have got the graph representation right (using an adjacency list). I want to keep things minimum to exactly what I need to implement these algorithms on undirected graphs for the time being.
Here's my Vertex
class:
public final class Vertex {
private final int label;
private VertexColor color = VertexColor.WHITE;
private int distance;
private Vertex parent;
public Vertex(int label, int distance, Vertex parent) {
this(label);
this.distance = distance;
this.parent = parent;
}
public Vertex(int label) {
this.label = label;
}
public int getLabel() {
return label;
}
public VertexColor getColor() {
return color;
}
public void setColor(VertexColor color) {
this.color = color;
}
public Vertex getParent() {
return parent;
}
public void setParent(Vertex parent) {
this.parent = parent;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
@Override
public String toString() {
return "Vertex{" +
"label=" + label +
", color=" + color +
", distance=" + distance +
", parent=" + parent +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vertex vertex = (Vertex) o;
if (label != vertex.label) return false;
if (distance != vertex.distance) return false;
if (color != vertex.color) return false;
return !(parent != null ? !parent.equals(vertex.parent) : vertex.parent != null);
}
@Override
public int hashCode() {
int result = label;
result = 31 * result + (color != null ? color.hashCode() : 0);
result = 31 * result + distance;
result = 31 * result + (parent != null ? parent.hashCode() : 0);
return result;
}
}
My VertexColor
enum:
public enum VertexColor {
WHITE, GREY, BLACK
}
My adjacency list implementation:
public class SimpleAdjacencyList {
private Map<Integer, List<Vertex>> adjacencyList = new HashMap<>();
/**
* Initializes a new graph from a file.
* @param file format should be:
* 1 2 4
* 2 3 1
* 3 2 4
* 4 3 1
*/
public SimpleAdjacencyList(String file) {
String line;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
String[] numbers = line.trim().split("(\\s)+");
int key = Integer.parseInt(numbers[0]);
List<Vertex> vertices = new ArrayList<>(numbers.length);
Vertex parent = new Vertex(key);
vertices.add(parent);
for(int i = 1; i < numbers.length; i++){
int label = Integer.parseInt(numbers[i]);
vertices.add(new Vertex(label, 0, null));
}
adjacencyList.put(key, vertices);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public String toString() {
return "SimpleAdjacencyList{" +
"adjacencyList=" + adjacencyList +
'}';
}
public Map<Integer, List<Vertex>> getAdjacencyList() {
return adjacencyList;
}
// TODO: remove this
public static void main(String[] args) {
SimpleAdjacencyList list = new SimpleAdjacencyList("/Users/orestis/Documents/test.txt");
System.out.println(list);
}
}