public int distance (Vertex x, Vertex y) { int returnVal = 0; Queue que = new LinkedList(); x.touched = true; que.add(x); while (!que.isEmpty()) { returnVal++; Vertex n = (Vertex)que.remove(); Vertex child = null; while ((child = getUnvisited(n))!=null) { if (child.value==y.value) return returnVal; child.touched=true; que.add(child); } } return returnVal; }
public int diameter () { int maxDistance = 0; int tempDistance = 0; Vertex one; Vertex two; for (int x = 1; x < numNodes; x++) { one = (Vertex)vert.get(x); for (int y = 0; y < x; y++) { two = (Vertex)vert.get(y); tempDistance = distance(one, two); if (tempDistance > maxDistance) maxDistance = tempDistance; refreshTouched(); } } return maxDistance; }
==============
Vertex class
public class Vertex { public boolean touched; public int value; public Vertex (int value) { touched = false; this.value = value; } }