Stanford Algorithms学习:Clustering
来源:互联网 发布:货到付款淘宝网商城 编辑:程序博客网 时间:2024/04/30 23:08
第二周的第一个编程作业,是利用贪婪算法来实现一个clustering的问题,和ML里学的unsupervised learning差不多。
Question 1
[number_of_nodes]
[edge 1 node 1] [edge 1 node 2] [edge 1 cost]
[edge 2 node 1] [edge 2 node 2] [edge 2 cost]
...
There is one edge
Your task in this problem is to run the clustering algorithm from lecture on this data set, where the target number
这里的Union-Find和Edge数据结构都是Algorithms fourth edition by Sedgewick里面的:
public class UnionFind {private int[] id;//id[i] = parent of i private int[] sizes;//size of each componentprivate int count;//number of components/** * Creates an empty union-find structure with N nodes * @param N */public UnionFind(int N) {count = N;id = new int[N];sizes = new int[N];for (int i = 0; i < N; i++) {id[i] = i;sizes[i] = 1;}}public int count(){return count;}public boolean connected(int u, int v){return (find(u) == find(v));}/** * Return component identifier for component containing p * @param u * @return */private int find(int u){while (u != id[u]) {u = id[u];}return u;}public void union(int u, int v){int i = find(u);int j= find(v);if (i == j) {return;}if (sizes[i] < sizes[j]) {id[i] = j;sizes[j] += sizes[i];}else {id[j] = i;sizes[i] += sizes[j];}count--;}}
public class Edge implements Comparable<Edge>{private final int u;//the first vertexprivate final int v;//the other vertexprivate final int cost;//edge costpublic Edge(int u, int v, int cost) {this.u = u;this.v = v;this.cost = cost;}public int cost(){return cost;}/** * returns one vertex of the edge */public int either(){return u;}/** * given one vertex, returns the other vertex of that edge * @param vertex * @return */public int other(int vertex) {if (vertex == u) {return v;}else if (vertex == v) {return u;}else {throw new RuntimeException("Inconsistent edge");}}@Overridepublic int compareTo(Edge arg0) {if (this.cost() < arg0.cost()) {return -1;}else if (this.cost() > arg0.cost) {return 1;}else {return 0;}}}
import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.StringTokenizer;public class Clustering {private ArrayList<Edge> edges;//stores all edgesprivate UnionFind uf;private int N;//number of edgesprivate String fileName = "/Users/Zhusong/Documents/Study/AlgorithmsDesignAndAnalysis/Assignments/Ass2/Ass2Prob1/clustering1.txt";public static void main(String[] args) {// TODO Auto-generated method stubClustering cl = new Clustering();cl.run();}private void run(){edges = new ArrayList<Edge>();readGraph();sortEdges();uf = new UnionFind(N);Iterator<Edge> iterator = edges.iterator();while(uf.count() > 4){Edge edge = iterator.next();int u = edge.either();uf.union(u, edge.other(u));}while(true){Edge edge = iterator.next();int u = edge.either();int v = edge.other(u);if (uf.connected(u, v)) {continue;}else {System.out.println(edge.cost());break;}}}/** * read in the text file and form a graph */private void readGraph() {File file = new File(fileName);try {BufferedReader rd = new BufferedReader(new FileReader(file));String line;line = rd.readLine();N = Integer.parseInt(line);while((line = rd.readLine()) != null){StringTokenizer tokenizer = new StringTokenizer(line);int u = Integer.parseInt(tokenizer.nextToken()) - 1;int v = Integer.parseInt(tokenizer.nextToken()) - 1;int cost = Integer.parseInt(tokenizer.nextToken());addEdge(u, v, cost);}rd.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void addEdge(int u, int v, int cost){Edge edge = new Edge(u, v, cost);edges.add(edge);}private void sortEdges(){Collections.sort(edges);}}
- Stanford Algorithms学习:Clustering
- Stanford Algorithms学习:Clustering 2
- Stanford 机器学习-clustering
- Stanford机器学习笔记-9. 聚类(Clustering)
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 2 Assignment: Clustering
- Stanford ML - Clustering 聚类
- Stanford ML - Lecture 9 - Clustering
- Stanford Machine Learning: (7). Clustering
- A Tutorial on Clustering Algorithms
- A Tutorial on Clustering Algorithms
- A Tutorial on Clustering Algorithms
- A Tutorial on Clustering Algorithms
- Stanford 机器学习 Week8 作业:K-means Clustering and Principal Component Analysis
- Data Clustering: Theory, Algorithms, and Applications
- A Tutorial on Clustering Algorithms-聚类小知识
- 层次聚类 Hierarchical Clustering Algorithms
- Stanford ex7: K-means Clustering and Principal Component Analysis
- Clustering Algorithms: From Start To State Of The Art
- PLMN介绍
- windows xp下安装UbuntuKylin双系统详细步骤
- JAVA eclipse(myeclipse)生成帮助文档(html或chm格式)
- 你想说什么就说吧
- uva11069
- Stanford Algorithms学习:Clustering
- Windows环境下Android NDK环境搭建
- java 泛型详解
- LoadRunner 自动登录163邮箱
- Access数据操作报错:操作必须使用一个可用的更新查询
- Loadrunner关联-手动关联
- 制作自己的Cydia发布源
- Flex 页面空白或Error 2032
- 20条开发AIR Native Extension的建议