这是一个创建于 4318 天前的主题,其中的信息可能已经有所发展或是发生改变。
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;
}
}
====================
主要需要实现上面的两个功能.
有些部分比如 x.touched = true; 这个需要调用到第二个class vertex 不知道怎么在Python中(一个文件,没有class)表示