Closest distance between two points(disjoint set)

后端 未结 4 1682
梦毁少年i
梦毁少年i 2021-01-31 21:11

\"enter

This problem is a kind of closest pair between two disjoint set. Upperside picture

4条回答
  •  花落未央
    2021-01-31 21:41

    I worked on a similar problem where I had to find a nearest member to identify if a member belong to a cluster within a cluster. I was trying to identify clusters within clusters. Here is the code, This might help you get start.

    /**
     * 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 unsorted = new HashMap();
        TreeMap 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(unsorted);
            this.setDistanceThreshold(avgDistanceInCluster());
            foundNeighbor = sorted.firstEntry().getValue();
            return foundNeighbor;
        } else {
            return 0.0;
        }
    } 
    
    
    /**
     * Method will check if a point belongs to a cluster based on the dynamic 
     * threshold.
     */
    public void isBelongToCluster() {
    
    
            for (int i=0; i < tempList.size(); i++) {
    
                double aPointInCluster = tempList.get(i);
    
                cluster.add(aPointInCluster);
                double newNeighbor = nearestNeighbor(aPointInCluster);
                if ( newNeighbor != 0.0) {
                    cluster.add(newNeighbor);
                    if (i + 1 > tempList.size() && (visited[i] != true)) {
                        isBelongToCluster();
                    }
                }
    
            }
    
        for (int i=0; i < cluster.size(); i++) {
            if (cluster.get(i) != 0.0)
                System.out.println("whats in the cluster -> " + cluster.get(i)); 
        } 
    }
    

提交回复
热议问题