再帰的な深さ優先の方法で最近傍を見つけようとしています。この点に到達する前に多くの要素が関係していますが、簡単にするために、現在問題が発生しているセクションのみを含めました。
私の考えは、あるしきい値距離に基づいて特定のポイントに最も近いものを見つけることです。プロセスを 2 つの方法に分けることにしました。最近隣が存在するかどうかを実際に確認する方法と、その関数を再帰的に何度も呼び出す方法です。
ポイントとして扱う double 値を持つ ArrayList があります... 0.0 を返す場合は、最も近い隣人を見つけられなかったことを意味します (実際にオブジェクトを使用しているかどうか疑問に思っている場合は、ロジックをクリーンアップすると実行される可能性があります)。
/**
* Find the nearest neighbor based on the distance threshold.
* TODO:
* @param currentPoint current point in the memory.
* @param threshold dynamic distance threshold.
* @return return the neighbor.
*/
private double nearestNeighbor(double currentPoint) {
HashMap<Double, Double> unsorted = new HashMap<Double, Double>();
TreeMap<Double, Double> sorted = null;
double foundNeighbor = 0.0;
for (int i = 0; i < bigCluster.length; i++) {
if (bigCluster[i] != 0.0 && bigCluster[i] != currentPoint) {
double shortestDistance = Math.abs(currentPoint - bigCluster[i]);
if (shortestDistance <= this.getDistanceThreshold())
unsorted.put(shortestDistance, bigCluster[i]);
}
}
if (!unsorted.isEmpty()) {
sorted = new TreeMap<Double, Double>(unsorted);
this.setDistanceThreshold(avgDistanceInCluster());
foundNeighbor = sorted.firstEntry().getValue();
return foundNeighbor;
} else {
return 0.0;
}
}
そして、これが私の方法で、上記を再帰的な dfs の方法で呼び出すことを計画していました。
/**
* Method will check if a point belongs to a cluster based on the dynamic
* threshold.
*/
public void isBelongToCluster(double point) {
for (int i = 0; i < tempList.size(); i++) {
double aPointInCluster = point;
cluster.add(aPointInCluster);
isVisited[i].visited = true;
double newNeighbor = nearestNeighbor(aPointInCluster);
if (newNeighbor != 0.0) {
cluster.add(newNeighbor);
if (i + 1 != tempList.size() && (isVisited[i].visited != true)) {
isBelongToCluster(newNeighbor);
}
}
}
for (int i = 0; i < cluster.size(); i++) {
if (cluster.get(i) != 0.0)
System.out.println("whats in the cluster -> " + cluster.get(i));
}
}
私が苦労しているのは、再帰的な深さ優先検索です。それに加えて、ビジターの実装が正しくないようです。
これが私が訪問者を処理しようとした方法です
public class VisitedPoint {
public double point;
public boolean visited;
public VisitedPoint(double pointToCheck) {
point = pointToCheck;
visited = false;
}
}
次に VisitedPoint オブジェクトを作成しますprivate VisitedPoint[] isVisited;
が、それを isBelongTo() メソッドで使用すると、null ポインター例外が発生します。
助けてくれてありがとう。