[问题] cluster 的写法

楼主: KuangFu (光复熊)   2016-05-15 13:18:17
这是作业 一直卡住好烦啊 拜托大家给点意见QQ
题目:
Your program (Clustering.java) must acquire the input file name from the
command line (args[0]) and then open it. The first line of the input file
specifies the number of points (N), followed by the 2-dimensional coordinates
of the N points.
The clustering procedures are described as follows:
Step 0: Treat each point as a cluster;
Step 1: Find the nearest pair of clusters (a, b) among the remaining N clusters
Step 2: Create a new cluster, c, of which the coordinates are the centroid of
all the points it contains after merging the clusters a and b;
Step 3: Delete the two old clusters: a and b;
Step 4: N = N - 1;
Step 5: Re-calculate the distance of the new cluster, c, to other remaining
clusters;
Step 6: Go to Step 1 unless N = 3;
Step 7: For each point in each cluster, find the nearest point in different
cluster. e.g cluster A has 2 points a1, a2. cluster B has 2 points b1, b2,
cluster C has 2 points c1, c2. compare the distance (a1, b1),
(a1, b2), (a1, c1), (a1, c2), (a2, b1), (a2, b2), (a2, c1), (a2, c2),
(b1, c1) and (b1, c2). print the smallest distance.
简易的翻译一下
就是题目会给N个点,要找出其中有最短距离的两点,把那两点以两点的重心取代
这样就会变成N-1个点,一直做下去,直到N=3,并输出那三个点
以下是程式码
import static java.awt.geom.Point2D.distance;
import java.util.*;
class CV implements Comparable<CV> {
public Point2D[] p1 = new Point2D[1];
public Point2D[] p2 = new Point2D[1];
public double cc;
public CV(Point2D p1, Point2D p2, double cc) {
this.p1[0] = p1;
this.p2[0] = p2;
this.cc = cc;
}
public Point2D[] getp1() {
return this.p1;
}
public Point2D[] getp2() {
return this.p2;
}
public int compareTo(CV that) {
if (this.cc > that.cc) {
return 1;
}
if (this.cc < that.cc) {
return -1;
}
return 0;
}
}
public class Clustering {
/**
* @param args the command line arguments
*/
public static Point2D[] shortist(Point2D[] oh) {
MinPQ<CV> distence = new MinPQ();
MinPQ f = new MinPQ();
for (int i = 0; i < oh.length; i++) {
for (int j = i + 1; j < oh.length; j++) {
double dd = oh[i].distanceTo(oh[j]);
CV a = new CV(oh[i], oh[j], dd);
distence.insert(a);
}
}
Point2D[] output = new Point2D[oh.length - 1];
CV result = distence.delMin();
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius(.02);
result.p1[0].drawTo(result.p2[0]);
double new_x = (result.p1[0].x() + result.p2[0].x()) / 2;
double new_y = (result.p1[0].y() + result.p2[0].y()) / 2;
output[0] = new Point2D(new_x, new_y);
int output_count = 1;
for (int i = 0; i < oh.length; i++) {
if ((oh[i].equals(result.p1[0])) | (oh[i].equals(result.p2[0]))) {
} else {
output[output_count] = oh[i];
output_count++;
}
}
int r = (int) (Math.random() * 255 + 1);
int g = (int) (Math.random() * 255 + 1);
int b = (int) (Math.random() * 255 + 1);
System.out.println(r);
StdDraw.setPenColor(r, g, b);
StdDraw.setPenRadius(0.05);
for (int i = 0; i < output.length; i++) {
System.out.println(output[i]);
output[i].draw();
}
return output;
}
public static void main(String[] args) {
In in = new In(args[0]);
int N = Integer.valueOf(in.readLine());
Point2D[] cluster = new Point2D[N];
int N_3 = N;
int input_count = 0;
String line;
MinPQ show1 = new MinPQ();
while ((line = in.readLine()) != null) {
cluster[input_count] = new Point2D(Double.valueOf(line.split(" ")[0]), Double.valueOf(line.split(" ")[1]));
show1.insert(cluster[input_count]);
input_count++;
}
while (N_3 > 3) {
cluster = shortist(cluster);
N_3
作者: galois (BBS)   2016-05-16 07:51:00
测资给一下好吗
作者: steven11329 (清新柳橙)   2016-05-16 08:49:00
你要不要先看还没有跟其它cluster连结的时候图长什么样子?感觉好像有漏资料。
作者: Souseasou3 (Almighty)   2016-05-21 16:33:00
原来作业可以来这问 这根本伸手牌
作者: arethusa99 (威力)   2016-05-23 01:17:00
不负责任猜测 题目意思是要考虑权重的假设C 是融合A+B之后 那他的权重是2 D是1C跟D做融合后 新生的E点不应该在正中间应该是要考虑权重 稍微偏向C才对这件事情可以从图中右上角观察到 第二小的点你的在中间 他的在偏向两个点的位置然后另一个端倪在Step 2很特地跟你说all the points 感觉上是cluster内可能不只拥有一个point不然直接跟你说a,b中心就好

Links booklink

Contact Us: admin [ a t ] ucptt.com