Note that there are some explanatory texts on larger screens.

plurals
  1. POfind nearest neighbor in recursive dfs fashion
    primarykey
    data
    text
    <p>I am trying to find the nearest neighbor in recursive depth-first-fashion. There are many elements involve prior to getting to this point, to keep it simple I have only included the section that I am currently having trouble. </p> <p>My idea is to find nearest neighbor to a given point based on some threshold distance, I decided to separate the process int to two methods. One to really find if the nearest neighbor exists and other method to just call that function over and over recursively.</p> <p>I have ArrayList with double values that I treat as points... if return 0.0, means I did't find the nearest neighbor (Wondering if I really use Object, might do it once I cleanup the logic).</p> <pre><code>/** * 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&lt;Double, Double&gt; unsorted = new HashMap&lt;Double, Double&gt;(); TreeMap&lt;Double, Double&gt; sorted = null; double foundNeighbor = 0.0; for (int i = 0; i &lt; bigCluster.length; i++) { if (bigCluster[i] != 0.0 &amp;&amp; bigCluster[i] != currentPoint) { double shortestDistance = Math.abs(currentPoint - bigCluster[i]); if (shortestDistance &lt;= this.getDistanceThreshold()) unsorted.put(shortestDistance, bigCluster[i]); } } if (!unsorted.isEmpty()) { sorted = new TreeMap&lt;Double, Double&gt;(unsorted); this.setDistanceThreshold(avgDistanceInCluster()); foundNeighbor = sorted.firstEntry().getValue(); return foundNeighbor; } else { return 0.0; } } </code></pre> <p>And here is my method, that I planned on calling the above in a recursive dfs fashion. </p> <pre><code>/** * 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 &lt; 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() &amp;&amp; (isVisited[i].visited != true)) { isBelongToCluster(newNeighbor); } } } for (int i = 0; i &lt; cluster.size(); i++) { if (cluster.get(i) != 0.0) System.out.println("whats in the cluster -&gt; " + cluster.get(i)); } } </code></pre> <p>What I am struggling is with the recursive depth first search. In Addition to that my visitor implementation doesn't look right. </p> <p>This is how I tried to handle the visitor</p> <pre><code>public class VisitedPoint { public double point; public boolean visited; public VisitedPoint(double pointToCheck) { point = pointToCheck; visited = false; } } </code></pre> <p>then I would create a VisitedPoint object, <code>private VisitedPoint[] isVisited;</code> but when I used it in my isBelongTo() method I get a null pointer exception.</p> <p>Thanks in advance for any help. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload