[問題] cluster 的寫法

看板java作者 (光復熊)時間9年前 (2016/05/15 13:18), 編輯推噓2(2012)
留言14則, 4人參與, 最新討論串1/1
這是作業 一直卡住好煩啊 拜託大家給點意見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--; } } } 可是我算出來的就是怪怪的 http://imgur.com/xf3CexM
這是正確的圖 http://imgur.com/QMJ5WIi
這是我的圖 有點像可是又不對QQ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.94.192 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1463289499.A.D57.html

05/16 07:51, , 1F
測資給一下好嗎
05/16 07:51, 1F

05/16 08:49, , 2F
你要不要先看還沒有跟其它cluster連結的時候圖長什
05/16 08:49, 2F

05/16 08:49, , 3F
麼樣子?
05/16 08:49, 3F

05/16 08:49, , 4F
感覺好像有漏資料。
05/16 08:49, 4F

05/21 16:33, , 5F
原來作業可以來這問 這根本伸手牌
05/21 16:33, 5F

05/23 01:17, , 6F
不負責任猜測 題目意思是要考慮權重的
05/23 01:17, 6F

05/23 01:18, , 7F
假設C 是融合A+B之後 那他的權重是2 D是1
05/23 01:18, 7F

05/23 01:18, , 8F
C跟D做融合後 新生的E點不應該在正中間
05/23 01:18, 8F

05/23 01:19, , 9F
應該是要考慮權重 稍微偏向C才對
05/23 01:19, 9F

05/23 01:19, , 10F
這件事情可以從圖中右上角觀察到 第二小的點
05/23 01:19, 10F

05/23 01:19, , 11F
你的在中間 他的在偏向兩個點的位置
05/23 01:19, 11F

05/23 01:20, , 12F
然後另一個端倪在Step 2
05/23 01:20, 12F

05/23 01:20, , 13F
很特地跟你說all the points 感覺上是cluster內
05/23 01:20, 13F

05/23 01:20, , 14F
可能不只擁有一個point不然直接跟你說a,b中心就好
05/23 01:20, 14F
文章代碼(AID): #1NE0QRrN (java)
文章代碼(AID): #1NE0QRrN (java)