Created
January 1, 2012 10:03
-
-
Save scepion1d/1546887 to your computer and use it in GitHub Desktop.
Realization of two clusterization algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package clusterizer; | |
import java.util.ArrayList; | |
public abstract class Clusterizer { | |
protected class Pair { | |
public int x, y; // ID's of clusters | |
public Pair(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
protected Point_double getCenterOfMass(ArrayList<Point_double> cluster) { | |
Point_double result = new Point_double(0.0, 0.0); | |
for (Point_double aCluster : cluster) { | |
result.x += aCluster.x; | |
result.y += aCluster.y; | |
} | |
result.x /= cluster.size(); | |
result.y /= cluster.size(); | |
return result; | |
} | |
protected double getDistance(Point_double A, Point_double B) { | |
return Math.sqrt(Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2)); | |
} | |
protected abstract Pair getMinimum(ArrayList<ArrayList<Point_double>> clusters); | |
protected abstract ArrayList<ArrayList<Point_double>> split(ArrayList<ArrayList<Point_double>> clusters); | |
protected boolean containMinimalClusters(ArrayList<ArrayList<Point_double>> clusters) { | |
for (ArrayList<Point_double> cluster : clusters) | |
if (cluster.size() == 1) | |
return true; | |
return false; | |
} | |
protected void printClusters(ArrayList<ArrayList<Point_double>> clusters) { | |
for (int i = 0, clustersSize = clusters.size(); i < clustersSize; i++) { | |
ArrayList<Point_double> cluster = clusters.get(i); | |
System.out.print(i + ":["); | |
for (Point_double point : cluster) { | |
System.out.print(point.x + "," + point.y + "; "); | |
} | |
System.out.print("]; "); | |
} | |
System.out.println("\n"); | |
} | |
public abstract ArrayList<ArrayList<Point_double>> process(ArrayList<ArrayList<Point_double>> clusters); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package clusterizer; | |
import java.util.ArrayList; | |
public class DependentClusterizer extends Clusterizer { | |
private Pair checkMinimum(Pair min, ArrayList<ArrayList<Point_double>> clusters) { | |
double distance = getDistance( | |
getCenterOfMass(clusters.get(min.x)), | |
getCenterOfMass(clusters.get(min.y)) | |
); | |
if (distance > Config.MAX_DISTANCE) | |
return null; | |
return min; | |
} | |
protected Pair getMinimum(ArrayList<ArrayList<Point_double>> clusters) { | |
Pair min = new Pair(0, 1); | |
double minDistance = getDistance(getCenterOfMass(clusters.get(0)), getCenterOfMass(clusters.get(1))); | |
for (int i = 0; i < clusters.size() - 1; i++) { | |
for (int j = i + 1; j < clusters.size(); j++) { | |
double distance = getDistance( | |
getCenterOfMass(clusters.get(i)), | |
getCenterOfMass(clusters.get(j)) | |
); | |
if (distance < minDistance) { | |
minDistance = distance; | |
min = new Pair(i, j); | |
} | |
} | |
} | |
return checkMinimum(min, clusters); | |
} | |
protected ArrayList<ArrayList<Point_double>> split(ArrayList<ArrayList<Point_double>> clusters) { | |
ArrayList<ArrayList<Point_double>> result = new ArrayList<ArrayList<Point_double>>(); | |
for (int i = 0; i < Config.COLOR.length; i++) { | |
if (i == clusters.size()) | |
break; | |
result.add(clusters.get(i)); | |
} | |
for (int i = Config.COLOR.length; i < clusters.size(); i++) { | |
ArrayList<Point_double> cluster = clusters.get(i); | |
for (Point_double point : cluster) { | |
ArrayList<Point_double> tmp = new ArrayList<Point_double>(); | |
tmp.add(point); | |
result.add(tmp); | |
} | |
} | |
return result; | |
} | |
public ArrayList<ArrayList<Point_double>> process(ArrayList<ArrayList<Point_double>> clusters) { | |
System.out.println("<-------------" + clusters.size() + "------------->"); | |
ArrayList<ArrayList<Point_double>> result = clusters; | |
printClusters(result); | |
if (clusters.size() > 1) { | |
Pair min = getMinimum(result); | |
System.out.print("Min: "); | |
if (min == null) | |
System.out.println("null"); | |
else { | |
System.out.print( "[" + min.x + "," + min.y + "]; "); | |
if (!result.get(min.x).isEmpty() && !result.get(min.y).isEmpty()) { | |
for (Point_double point : result.get(min.y)) | |
result.get(min.x).add(point); | |
} | |
System.out.println(); | |
result.remove(min.y); | |
split(result); | |
printClusters(result); | |
} | |
} | |
System.out.println("<--------------------------->\n\n"); | |
return result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package clusterizer; | |
import java.util.ArrayList; | |
public class IndependentClusterizer extends Clusterizer { | |
protected Pair getMinimum(ArrayList<ArrayList<Point_double>> clusters) { | |
Pair min =null; | |
double minDistance = 0; | |
for (int i = 0; i < clusters.size() - 1; i++) { | |
for (int j = i + 1; j < clusters.size(); j++) { | |
if ((clusters.get(i).size() == 1) || (clusters.get(j).size() == 1)) { | |
if (min == null) { | |
min = new Pair(i, j); | |
minDistance = getDistance( | |
getCenterOfMass(clusters.get(i)), | |
getCenterOfMass(clusters.get(j)) | |
); | |
} else { | |
double distance = getDistance( | |
getCenterOfMass(clusters.get(i)), | |
getCenterOfMass(clusters.get(j)) | |
); | |
if (distance < minDistance) { | |
min = new Pair(i, j); | |
minDistance = distance; | |
} | |
} | |
} | |
} | |
} | |
return min; | |
} | |
protected ArrayList<ArrayList<Point_double>> split(ArrayList<ArrayList<Point_double>> clusters) { | |
ArrayList<ArrayList<Point_double>> result = new ArrayList<ArrayList<Point_double>>(); | |
for (ArrayList<Point_double> cluster : clusters) { | |
for (Point_double point : cluster) { | |
ArrayList<Point_double> tmp = new ArrayList<Point_double>(); | |
tmp.add(point); | |
result.add(tmp); | |
} | |
} | |
return result; | |
} | |
public ArrayList<ArrayList<Point_double>> process(ArrayList<ArrayList<Point_double>> clusters) { | |
ArrayList<ArrayList<Point_double>> result = split(clusters); | |
if (clusters.size() > 1) { | |
System.out.println("<--------" + result.size() + "-------->"); | |
printClusters(result); | |
while (containMinimalClusters(result)) { | |
Pair min = getMinimum(result); | |
for (Point_double point : result.get(min.y)) | |
result.get(min.x).add(point); | |
result.remove(min.y); | |
System.out.println("Merge: " + min.x + " & " + min.y + ";"); | |
printClusters(result); | |
} | |
System.out.println("<------------------->\n\n"); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment