层次聚类(java未优化版)

  • G6_374295
    了解作者
  • 9.6KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-06 04:13
    上传日期
暂时直接遍历重复计算了相似度,以后会用迪吉斯特或者哈夫曼树的方式优化
linkage.rar
内容介绍
package com.company; import com.sun.deploy.util.ArrayUtil; import java.util.ArrayList; public class Main { static int[][][] raw={{{107,41}},{{83,41}},{{151,54}},{{148,33}},{{83,24}},{{199,45}},{{93,21}},{{215,45}},{{142,18}},{{333,59}},{{336,168}}}; static ArrayList<int[][]> arrayList=new ArrayList(); public static void main(String[] args) { for (int i=0;i<raw.length;i++){ arrayList.add(raw[i]); } Hcluster(); for(int i=0;i<arrayList.size();i++){ for(int j=0;j<arrayList.get(i).length;j++){ System.out.print(arrayList.get(i)[j][0]); System.out.print(","); System.out.println(arrayList.get(i)[j][1]); } System.out.println(); } } public static int getSimilary(int[][] a,int[][] b){ int dis=0; for (int i=0;i<a.length;i++){ for (int j=0;j<b.length;j++){ if(distance(a[i],b[j]) rel='nofollow' onclick='return false;'>dis) dis=distance(a[i],b[j]); } } return dis; } public static int distance(int[] a,int[] b){ return (a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]); } public static void Hcluster(){ if(arrayList.size()<=3) System.out.println("分类完成"); else { int x=0; int y=0; int mSimilartty=100000; for (int i=0;i<arrayList.size();i++){ for (int j=0;j<arrayList.size();j++){ if(getSimilary(arrayList.get(i),arrayList.get(j))<mSimilartty&&i!=j) { mSimilartty = getSimilary(arrayList.get(i), arrayList.get(j)); x=i; y=j; } } } merge(x,y); Hcluster(); } } public static void merge(int i,int j){ int [][] nList=new int[arrayList.get(i).length+arrayList.get(j).length][]; for (int n=0;n<arrayList.get(i).length;n++){ nList[n]=arrayList.get(i)[n]; } for (int n=0;n<arrayList.get(j).length;n++){ nList[arrayList.get(i).length+n]=arrayList.get(j)[n]; } arrayList.remove(j); arrayList.remove(i); arrayList.add(nList); } }
评论
    相关推荐