Skip to content

Instantly share code, notes, and snippets.

@johnmay
Created November 23, 2012 18:50
Show Gist options
  • Select an option

  • Save johnmay/4136821 to your computer and use it in GitHub Desktop.

Select an option

Save johnmay/4136821 to your computer and use it in GitHub Desktop.
AdjacencyList
public class AdjacencyList implements Graph {
private final int[][] vertexes;
private final IAtom[] atoms;
private final int n;
public AdjacencyList(final IAtomContainer container) {
this.n = container.getAtomCount();
this.vertexes = new int[n][16];
this.edges = new Edge[n][16];
this.atoms = new IAtom[n];
// keep the neighbour count to trim at the end
int[] neighbours = new int[n];
for (IBond bond : container.bonds()) {
IAtom a1 = bond.getAtom(0);
IAtom a2 = bond.getAtom(1);
int i = container.getAtomNumber(a1);
int j = container.getAtomNumber(a2);
int in = neighbours[i];
int jn = neighbours[j];
vertexes[i][in] = j;
vertexes[j][jn] = i;
neighbours[i]++;
neighbours[j]++;
}
// trim the adjacency list to size
for (int i = 0; i < n; i++) {
this.atoms[i] = container.getAtom(i);
vertexes[i] = Arrays.copyOf(vertexes[i], neighbours[i]);
}
}
public int[] neighbours(int i) {
return vertexes[i];
}
public IAtom getVertexValue(int i){
return atoms[i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment