[問題] 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